MediaWiki:Base.js

From Ekatra Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Note: After publishing, 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 / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*jshint boss:true, loopfunc:true, scripturl:true*/
/*global mw, $, ws_msg, get_optlist */

/* messages to be moved (FIXME: what this mean ? moved where ?)
	'it':'Il testo proviene da', 
	'la':'Hic textus visibils est in', 
	'es':'Su texto procede de', 
	'ca':'El seu text procedeix de' 
*/


if(!self.ws_messages) self.ws_messages = { };

window.ws_msg = function (name) {
   var m = self.ws_messages[name];
   if(m) return m; else return name;
};

/* FIXME: Remove this around September 11 2014, all use removed August 10 2014 */
/* Note : there is a similar function in wikibits.js, but it does not work for me on page numbers */
mw.log.deprecate( window, 'get_elements_by_classname', function (classname, tagname) {
	if(document.getElementsByClassName) return document.getElementsByClassName(classname);
	var ml = [];
	var l = document.getElementsByTagName(tagname);
	for (var i=0; i< l.length; i++) {
		var a = l[i]; 
		if( a.className.indexOf(classname) !=-1 ) ml[ml.length] = l[i];
	}
	return ml;
}, 'Use jQuery instead.' );


// Copy from MediaWiki:Base.js
window.get_optlist = function () {
	var optlist = document.getElementById("optlist"),displayOptions,
      cl, ulCl;
	if(!optlist) {
		if (mw.config.get('skin')==='vector') {
	displayOptions = document.createElement("nav");
	displayOptions.className = "mw-portlet vector-menu vector-menu-portal portal";
	cl="vector-menu-content";
	ulCl="vector-menu-content-list";
} else {
	 displayOptions = document.createElement("div");
	displayOptions.className = "portlet";
	cl="pBody";
	ulCl="";
}
		displayOptions.innerHTML = '<h3 class="vector-menu-heading">' + ws_msg('optlist') + '<\/h3><div class="'+cl+'"><ul id="optlist" class="'+ ulCl + '"></ul><\/div>';
                var ptb = document.getElementById("p-tb");
                ptb.parentNode.insertBefore(displayOptions,ptb);
                displayOptions.setAttribute("id","p-displayOptions");
                displayOptions.id="p-displayOptions"; /* */
		optlist = document.getElementById("optlist");
	}
	return optlist;
};


/*** Cookies (depreciated, use jQuery.cookie) ***/

mw.log.deprecate( window, 'SetCookie', function (name,value) {
	$.cookie(name, value);
}, 'Use jQuery.cookie instead.' );

mw.log.deprecate( window, 'GetCookie', function (name) {
	return $.cookie(name);
}, 'Use jQuery.cookie instead.' );


/**** Display options ****/

function OptionText() {
   var SpanElem, OptionTitle, OptionStyle, CookieDisplayOption;
   // iterate over all < span >-elements until class "OptionText" is found
   for(
           var i=0; 
           SpanElem = document.getElementsByTagName("span")[i]; 
           i++
       ) {
       // if found an option span
       if (SpanElem.className == "OptionText") {
          //SpanElem.style.display = 'none';
          OptionTitle = SpanElem.title;
          OptionStyle = SpanElem.firstChild.getAttribute('style');

          //check if option was already encountered...
          if(!document.getElementById(OptionTitle)){

            //read cookie
            var DisplayOptionDefault = true;
            CookieDisplayOption = $.cookie("Display"+OptionTitle);
            if (CookieDisplayOption ) {
            if (CookieDisplayOption == "false") DisplayOptionDefault = false; }

            var PageDisplay = document.createElement("li");
            PageDisplay.setAttribute('id', OptionTitle);
            var PageDisplayLink = document.createElement("a");

            var OptionTextNode = document.createTextNode("Désactiver "+OptionTitle);
            PageDisplayLink.appendChild(OptionTextNode);
            PageDisplayLink.setAttribute('href','javascript:void(0)');
            $(PageDisplayLink).click(function() {
                        displayOptionText(OptionTitle, OptionStyle, DisplayOptionDefault);
            });
            PageDisplay.appendChild(PageDisplayLink);

            var optlist = get_optlist();
            optlist.appendChild(PageDisplay);

            displayOptionText(OptionTitle,OptionStyle,DisplayOptionDefault);
          }
       }
   }
 }

 function displayOptionText(optiontitle, optionstyle, bool) {
  var SpanElem;
  $.cookie("Display"+optiontitle,bool);
  if( ! document.getElementById(optiontitle) ) return;
  var PageDisplayLink =  document.getElementById(optiontitle).firstChild;

   // iterate over all < span >-elements
   for(
           var i=0; 
           SpanElem = document.getElementsByTagName("span")[i]; 
           i++
       ) {
        // if found an option text
        if ((SpanElem.className == "OptionText") && (SpanElem.title == optiontitle)) {
          if(bool) {
            SpanElem.setAttribute('style',optionstyle);
            PageDisplayLink.firstChild.data = (optiontitle);
            PageDisplayLink.setAttribute('href', 'javascript:void(0)');
            $(PageDisplayLink).off('click');
            $(PageDisplayLink).click(function() {
                        displayOptionText(optiontitle, optionstyle, false);
            });
          }
          else {
            SpanElem.setAttribute('style','null');
            PageDisplayLink.firstChild.data = (optiontitle);
            PageDisplayLink.setAttribute('href', 'javascript:void(0)');
            $(PageDisplayLink).off('click');
            $(PageDisplayLink).click(function() {
                        displayOptionText(optiontitle, optionstyle, true);
            });

         }        
      }
   }
 }

$( OptionText );

function restore_lst(){
        var editbox = document.getElementById('wpTextbox1');
        var search = /##[\s]*(.*?)[\s]*##[\s]*\n/;
        var a = editbox.value.split(search);
        var s = a[0];
        var m = parseInt(a.length/2);
        for( var i = 0 ; i < m ; i++ ) {
            var title = a[i*2+1];
            // Ensure we'll don't get twice quote.
            title = title.replace(/^"(.*)"$/, "$1");
            title = title.replace(/^'(.*)'$/, "$1");
            var content = a[i*2+2]; 
            if( title && content.substring(0,2)=='{|' ) content = '\n' + content;
            if(title) s = s + '<section begin="'+title+'"/>' + content + '<section end="'+title+'"/>\n';
            else s = s + content;
            /* if( i < m-1 ) s = s + "----\n"; */
        }
        editbox.value = s; 
}
 
/*
 * easy lst : hide section markers
 */
function easy_lst() {
        var editbox = document.getElementById('wpTextbox1');
        if(editbox) {

	var search = /<section\sbegin=[\s]*(.*?)[\s]*\/>/;
        var a = editbox.value.split(search);
        var s = a[0]; 
        var ok = true;
        for( var i = 0 ; i < parseInt(a.length/2) ; i++ ) {
            var title = a[i*2+1];
            var content = a[i*2+2]; 
            var r2 = /^([\s\S]*?)<section\send=(.*?)\/>(\n|)[\s]*([\s\S]*?)$/;
            var m2 = content.match(r2);
            if( m2 ) {
               title = title.replace(/^"(.*)"$/, "$1");
               title = title.replace(/^'(.*)'$/, "$1");
               if(s && s.charAt(s.length-1)!='\n' && s.charAt(s.length-1)!='|' ) s = s+'\n';
               s = s + "## " + title + " ##\n"+ m2[1] ;
               if( m2[4] ) { 
                  if( m2[4]!='----\n' ) {
                    if(s && s.charAt(s.length-1)!='\n' ) s = s+'\n';
                    s = s + "####\n" + m2[4] ;
                  } 
               }
             } else { 
               ok = false; /* alert("error"+title);*/
             }
        }
	if(ok) { 
             editbox.value = s; 
        } 
	var saveButton = document.getElementById("wpSave"); 
	var previewButton = document.getElementById("wpPreview"); 
	var diffButton = document.getElementById("wpDiff");
	if(saveButton){
                saveButton.onclick = restore_lst;
		previewButton.onclick = restore_lst;
		diffButton.onclick = restore_lst;
        }
   }
}

function easy_lst_setup() {
        if (self.proofreadpage_raw_lst || mw.config.get( 'wgCanonicalNamespace' ) !== 'Page')
            return;

        if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
            mw.loader.using('ext.proofreadpage.page', function () {
            // mimic code in the extension, there is a conditionnal deps on ext.wikiEditor.toolbar.
            if (mw.user.options.get('usebetatoolbar') && $.inArray('ext.wikiEditor', mw.loader.getModuleNames()) > -1) {
                mw.loader.using('ext.wikiEditor', function() {
                   easy_lst();
                });
            } else {
                easy_lst();
            }
        });
    }
}


$( easy_lst_setup );