Користувач:Js~ukwiki/summary.js

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку

Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.

  • Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
  • Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
  • Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
  • Opera: натисніть Ctrl-F5
$(summaryPreview)
 
function summaryPreview(){
 
 var select, inputId, isEdit,  maxChars = 255, useMWPreview
 
 if (wgAction == 'edit' || wgAction == 'submit'){
   inputId = 'wpSummary'
   isEdit = true
   maxChars = 200
 }else if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Movepage'){
   inputId = 'wpReason'
 }else if (wgAction == 'delete'){
   inputId = 'wpReason'
   select = document.getElementById('wpDeleteReasonList')
   addHandler(select, 'change', update)
 }else if (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('wpMinoredit')
   if (!el) return
   el.parentNode.insertBefore(preview, el)
   //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
 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/'+wgPageName
       + '#' + encodeURI(p.replace(/ /g,'_')).replace(/%([0-9A-F][0-9A-F])/g, '.$1')
       + '" title="' +wgPageName.replace(/_/g, ' ') + '">→</a>' + p + ' -' : p )
     + '</span>'
   })
 
  text = text.replace(/\[\[([^\]><}{|]+)\|?([^\]><]*)?\]\]/g,
   function(str,p1,p2){ //[ [ p1 | p2 ] ]
    return '<a href='+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
 }
 
}