MediaWiki:Gadget-summary.js

Материал из Викитеки — свободной библиотеки

Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.

  • Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
  • Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
  • Internet Explorer / Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
  • Opera: Нажмите Ctrl+F5.
/* Администраторам: это гаджет, любое изменение его моментально будет видно всем пользователям данного гаджета. */

$(summaryPreview);

function summaryPreview(){

 var select, inputId, isEdit,  maxChars = 255, useMWPreview;

 if (mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit'){
   inputId = 'wpSummary';
   isEdit = true;
   maxChars = 200;
 }else if (mw.config.get('wgCanonicalNamespace') == 'Special' && mw.config.get('wgCanonicalSpecialPageName') == 'Movepage'){
   inputId = 'wpReason';
 }else if (mw.config.get('wgAction') == 'delete'){
   inputId = 'wpReason';
   select = document.getElementById('wpDeleteReasonList');
   $( select ).on( 'change', update );
 }else if (mw.config.get('wgAction') == 'protect'){
   inputId = 'mwProtect-reason';
 }else return;

 var input = document.getElementById(inputId);
 if (!input) return;

 //create counter span
 var cnt = document.createElement('span');
 cnt.id = 'inputCounter';
 cnt.style.marginLeft = '3px';
 input.parentNode.insertBefore(cnt, input.nextSibling);

 //create preview div
 var preview = document.createElement('div');
 preview.id = 'sumPreview';
 preview.title = 'Предварительный просмотр описания';
 with (preview.style){border = '1px solid gray'; width = '100%'; 
  fontStyle = 'italic'; fontSize='90%'; paddingLeft = '2px'}

 //add CSS
 var inputStyle = addCSS('#' + inputId
  + '{' + (window.oversizedSummaryCSS ? oversizedSummaryCSS : 'background:#FFEEEE') + '}');
 inputStyle.disabled = true;

 //attach preview div
 if (isEdit){ 
   var el = document.getElementById('wpSummaryWidget');
   if (!el) return;
   el.parentNode.appendChild(preview);
   //hide standard preview
   el = preview;
   while ((el=el.previousSibling) && (el.tagName != 'DIV'));
   if (el && el.className == 'mw-summary-preview'){
     var t = el.getElementsByTagName('span')[0].innerHTML;
     preview.innerHTML = t.substring(1, t.length-1); //cut ()
     el.style.display = 'none';
     useMWPreview = true;
   }else{
     preview.innerHTML = ' ';
   } 
 }else{
   input.form.appendChild(preview);
 }

 update();
 //assign events
 $( input ).on( 'change keyup mouseup', update );
 /*addHandler(input, 'change', update);
 addHandler(input, 'keyup', update);
 addHandler(input, 'mouseup', update);*/
 useMWPreview = false;

 //functions

 function update(){
  var text = input.value, cutAt, bb;
  if (select && (select.selectedIndex != 0)) text = select.value + ': ' + text;
  var chars = text.length;
  //count bytes
  bytes = 0;
  for (var i=0; i<chars; i++){
    bytes++;
    bb = text.charCodeAt(i);
    if (bb > 127) { 
      bytes++;
      if (bb > 2048) bytes++;
    }
    if (bytes > 255) cutAt = i;
  }
  //show bytes/chars
  cnt.title = 'Used ' + chars + ' chars, ' + bytes + ' bytes';
  cnt.style.opacity = chars/maxChars;
  var left;
  if (255-bytes < maxChars-chars) { left = 255-bytes; cnt.style.fontStyle = 'italic' }
  else { left=maxChars-chars;  cnt.style.fontStyle = 'normal'}
  cnt.innerHTML = left;
  cnt.style.fontColor = (left < 0) ? 'red' : 'black';
  cnt.style.fontWeight = (left < 20)? 'bold' : 'normal';
  inputStyle.disabled = (left >= 0);
  //show preview
  if (useMWPreview) return;
  if (bytes > 255) text = text.substring(0, cutAt);
  text = text.replace(/ +/g, ' ').replace(/</g,'&lt;'); //collapse multiple spaces and make safe
  text = text.replace(/\/\*(.*?)\*\//g, 
    function(str, p){
     p = p.replace(/^ */,'').replace(/ *$/,'');
     return '<span class="autocomment">'
       + (isEdit ? '<a href="/wiki/' + mw.config.get('wgPageName')
       + '#' + encodeURI(p.replace(/ /g,'_')).replace(/%([0-9A-F][0-9A-F])/g, '.$1')
       + '" title="' + mw.config.get('wgPageName').replace(/_/g, ' ') + '">→</a>' + p + ' -' : p )
       + '</span>';
   });
  
  text = text.replace(/\[\[([^\]><}{|]+)\|?([^\]><]*)?\]\]/g,
   function(str,p1,p2){ //[ [ p1 | p2 ] ]
    return '<a href='+mw.config.get('wgServer') + '/wiki/' 
    + encodeURI(p1.replace(/\?/g,'%3F').replace(/&/g,'%26'))
    +' + title=\'' + p1 + '\'>' + (p2?p2:p1) + '</a>';
   });
  preview.innerHTML = text ? text : '&nbsp;';
 }  


 function addCSS(text){ //could use createStyleSheet for IE as well
  var s = document.createElement('style');
  s.setAttribute('type', 'text/css');
  if (s.styleSheet) s.styleSheet.cssText = text; //IE
  else s.appendChild(document.createTextNode(text));
  document.getElementsByTagName('head')[0].appendChild(s);
  return s.sheet || s; //Safari needs sheets, IE the other way
 }

}