مدیاویکی:Edit.js

From ویکی کد
Revision as of 13:54, 5 October 2019 by imported>Admin (۱ نسخه واردشده)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
//<pre> Javascript included on the edit page. 

/*</pre>
==[[MediaWiki:Edittools]]==
<pre>*/

/*</pre>
===applyCharinserts===
<pre>*/

/* handle <span class="charinsert"> like <charinsert> */
function applyCharinserts()
{
  function patchUpInsertTagsArg(arg)
  {
    return(
      arg.replace(/\x22/g,'&quot;').replace(/\x27/g,"\\'").replace(/\x26nbsp;/g,' '));
  }
 
  function charinsertify(span)
  {
  	// Go through all child nodes of span.
    for ( var node = span.firstChild, oldNode; node; ) {
      if ( node.nodeType === 3 ) {
      	// Text node
	  	var s = node.nodeValue, // text content
	  		addedNew = false;
	    
	    s = s.replace(/\xA0/g, '\x26nbsp;');
	    
	    var strings = s.split(/\s/g);
	    for(var i = 0; i < strings.length; ++i)
	    {
	      if(strings[i] == '')
	        continue;
	      var left, right, index;
	      index = strings[i].indexOf('+');
	      if(index == -1)
	        index = strings[i].length;
	      left = strings[i].substring(0, index);
	      right = strings[i].substring(index + 1);
	      left = patchUpInsertTagsArg(left);
	      right = patchUpInsertTagsArg(right);
	      var textToDisplay = left + right;
	      var a = document.createElement( 'a' );
	      ( function ( left, right ) {
	        a.onclick = function () {
	          mw.toolbar.insertTags( left.replace(/^ */, ''), right, '' );
	          return false;
	        };
	        a.href = '#';
	        a.appendChild( document.createTextNode( textToDisplay ) );
	        if ( i > 0 ) {
	        	span.insertBefore( document.createTextNode( ' ' ), node );
	        }
	        span.insertBefore( a, node );
	        addedNew = true;
	      })( left, right );
	    }
	    oldNode = node;
	    node = node.nextSibling;
	    if ( addedNew ) {
	      span.removeChild( oldNode );
	    }
      } else if ( node.nodeType === 1 ) {
      	// Element node
	    oldNode = node;
	    node = node.nextSibling;
      	charinsertify( oldNode );
      }
    }
  }
 
  var edittools = document.getElementById('editpage-specialchars');
  if(! edittools)
    return;
  var spans = edittools.getElementsByTagName('span');
  if(! spans)
    return;
  for(var i = 0; i < spans.length; ++i)
  {
    if((' ' + spans[i].className + ' ').indexOf(' charinsert ') == -1)
      continue;
    spans[i].className = spans[i].className.replace(/\bcharinsert\b/, '');
    charinsertify( spans[ i ] );
  }
}

/*</pre>

===addCharSubsetMenu===
<pre>*/

/* add menu for selecting subsets of secial characters */
function addCharSubsetMenu() {
  var edittools = document.getElementById('editpage-specialchars');

  if(! edittools)
    return;

  var menu = newNode('select',
                     { id: 'charSubsetControl',
                       style: 'display:inline',
                       onchange: 'chooseCharSubset(selectedIndex)'
                     });

  var pp = edittools.getElementsByTagName('p');
  if(!pp)
    return;
  for(var i = 0; i < pp.length; ++i)
    menu.appendChild(
      newNode('option',
        decodeURIComponent(
          (''+pp[i].id).replace(/^edittools-/, '')
                       .replace(/\.([0-9A-F][0-9A-F])/g, '%$1')
                       .replace(/_/g, '%20')
        )
      )
    );

  edittools.insertBefore(menu, edittools.firstChild);

  /* default subset from cookie */
  var s = parseInt( getCookie('edittoolscharsubset') );
  if ( isNaN(s) ) s = 0;

  /* update dropdown control to value of cookie */
  document.getElementById('charSubsetControl').selectedIndex = s; 

  /* display the subset indicated by the cookie */
  chooseCharSubset( s );
}


/*</pre>

===chooseCharSubsetMenu===
<pre>*/

/* select subsection of special characters */
function chooseCharSubset(s) {
  var l = document.getElementById('editpage-specialchars').getElementsByTagName('p');
  for (var i = 0; i < l.length ; i++) {
    l[i].style.display = i == s ? 'inline' : 'none';
    l[i].style.visibility = i == s ? 'visible' : 'hidden';
  }
  setCookie('edittoolscharsubset', s);
}

jQuery(document).ready(function () {
  if(!window.doNotUseDefaultEditTools) { // [[User:Conrad.Irwin/edittools.js]]
    addCharSubsetMenu();
  }
    applyCharinserts();
});

/*
</pre>
*/