MediaWiki:Common.js

From CruisersWiki

(Difference between revisions)
Jump to: navigation, search
Line 209: Line 209:
         addButton("/skins/common/images/button_nowiki.png","Ignore wiki formatting","\x3cnowiki\x3e","\x3c/nowiki\x3e","Insert non-formatted text here","mw-editbutton-nowiki");
         addButton("/skins/common/images/button_nowiki.png","Ignore wiki formatting","\x3cnowiki\x3e","\x3c/nowiki\x3e","Insert non-formatted text here","mw-editbutton-nowiki");
         // addButton("/skins/common/images/button_hr.png","Horizontal line (use sparingly)","\n----\n","","","mw-editbutton-hr");
         // addButton("/skins/common/images/button_hr.png","Horizontal line (use sparingly)","\n----\n","","","mw-editbutton-hr");
 +
       
 +
        addButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");
     };
     };
Line 365: Line 367:
// custom edit toolbar buttons
// custom edit toolbar buttons
-
function addCustomEditButton (imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
+
// function addCustomEditButton (imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
-
mwCustomEditButtons[mwCustomEditButtons.length] =
+
// mwCustomEditButtons[mwCustomEditButtons.length] =
-
{"imageId": imageId,
+
// {"imageId": imageId,
-
"imageFile": imageFile,
+
// "imageFile": imageFile,
-
"speedTip": speedTip,
+
// "speedTip": speedTip,
-
"tagOpen": tagOpen,
+
// "tagOpen": tagOpen,
-
"tagClose": tagClose,
+
// "tagClose": tagClose,
-
"sampleText": sampleText};
+
// "sampleText": sampleText};
-
}
+
// }
-
addCustomEditButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");
+
// addCustomEditButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");
if (wgUserName == 'Vadim') {
if (wgUserName == 'Vadim') {
     importScript('User:Vadim/common.js');
     importScript('User:Vadim/common.js');
}
}

Revision as of 12:51, 21 February 2016

// -------------------------------------------------------------------------------
//  Force Preview  JavaScript code - Start
//
//  To allow any group to bypass being forced to preview, 
//  enter the group name in the permittedGroups array.
//  E.g.
//    var permittedGroups = [];                       // force everyone
//    var permittedGroups = [ "user"];                // permit logged-in users 
//    var permittedGroups = [ "sysop", "bureaucrat"]; // permit sysop, bureaucrat 
// -------------------------------------------------------------------------------
var permittedGroups = [];
 
/* Array.prototype.intersects = function() { */ // avoid an assignment to Array.prototype due to a bug in Navionics webapi
var array_intersects = function() {

  // --------------------------------------------------------
  //  Returns true if any element in the argument array
  //  is the same as an element in this array
  // --------------------------------------------------------
  if( !arguments.length ){
    return false;
  }
  var array2 = arguments[0];
 
  var len1 = this.length;
  var len2 = array2.length;
  if ( len2 == 0 ){
    return false;
  }
 
  for(var i=0; i<len1; i++){
    for(var j=0; j<len2; j++) {
      if( this[i] === array2[j] ) {
        return true;
      }
    }
  }
  return false;
};
 
function forcePreview() 
{
  if( wgAction != "edit") return;
  if( wgUserGroups === null) {
    wgUserGroups = [];
  }
/*  if( wgUserGroups.intersects(permittedGroups) ) { */
  if( array_intersects.call(wgUserGroups, permittedGroups) ) {
    return;
  }
  var saveButton = document.getElementById("wpSave");
  if( !saveButton )
    return;
  saveButton.disabled = true;
  saveButton.value = "Save page (use preview first)";
  saveButton.style.fontWeight = "normal";
  document.getElementById("wpPreview").style.fontWeight = "bold";
}
 
addOnloadHook(forcePreview);
// -----------------------------------------------------
//  Force Preview  JavaScript code - End
// ---------------------------------------------

/* end of the original code*/

// define setZeroTimeout
if (! window.postMessage) {
    window.setZeroTimeout = function (cb) {
        setTimeout(cb, 100);
    };
} else { 
    (function() { // see http://dbaron.org/log/20100309-faster-timeouts
        // Only add setZeroTimeout to the window object, and hide everything
        // else in a closure.
        var timeouts = [];
        var messageName = "zero-timeout-message";
    
        // Like setTimeout, but only takes a function argument.  There's
        // no time argument (always zero) and no arguments (you have to
        // use a closure).
        function setZeroTimeout(fn) {
            timeouts.push(fn);
            window.postMessage(messageName, "*");
        }
    
        function handleMessage(event) {
            if (event.source == window && event.data == messageName) {
                event.stopPropagation();
                if (timeouts.length > 0) {
                    var fn = timeouts.shift();
                    fn();
                }
            }
        }
        window.addEventListener("message", handleMessage, true);
    
        // Add the one thing we want added to the window object.
        window.setZeroTimeout = setZeroTimeout;
    })();
}

// utility functions

function wikiUri(page, ctype) {
	return wgServer + wgScript + '?title=' +
		encodeURIComponent(page.replace(/ /g,'_')).replace('%2F','/').replace('%3A',':') +
		(ctype ? '&action=raw&ctype=' + ctype : '');
}

function wikiScript(page) {
	return wikiUri(page, 'text/javascript');
}

function wikiCss(page) {
	return wikiUri(page, 'text/css');
}

function loadScript(url, callback) {
    addOnloadHook( function () {
    	var elem = importScriptURI(url);
    	if (callback) {
        	if (elem) {
                elem.onload = callback;
            } else {
                callback();
            }
    	}
    });
}

function loadWikiScript(page, callback) {
    loadScript(wikiScript(page), callback);
}

var loadedCss = {}; // included-scripts tracker
function loadCss(url, callback) {
	if (loadedCss[url]) {
	    callback && callback();
		return null;
	}
	loadedCss[url] = true;
	var elem = document.createElement('link');
	document.getElementsByTagName('head')[0].appendChild(elem);
	elem.setAttribute('type','text/css');
	elem.setAttribute('rel','stylesheet');
	elem.setAttribute('href',url);
	
	if (callback) {
        elem.onload = callback;
	}
	return elem;
}

function loadWikiCss(page, callback) {
    loadCss(wikiCss(page), callback);
}

// load jQuery
	
(function () {
    
    // modify addOnloadHook from wikibits.js
	var onloadFunctsNew = onloadFuncts; // see wikibits.js
	onloadFuncts = [];
	var doneOnloadHookNew = false;

	var addOnloadHookOld = addOnloadHook;
	addOnloadHook = function (callback) {
	    var hookFunct = function () { // isolate ecach callback call
	        setZeroTimeout(callback);
	    }
	    // Allows add-on scripts to add onload functions
	    if(!doneOnloadHookNew) {
		    onloadFunctsNew[onloadFunctsNew.length] = hookFunct;
	    } else {
		    hookFunct();  // bug in MSIE script loading
	    }
	};
	
	// workaround for license badge src set to invalid host -- www.cruiserswiki.com: "http://www.cruiserswiki.com/images/f/fe/Creativecommons.png"
    var licenseBadgeFix = function () {
        console.log('licenseBadgeFix');
        $('img[src*="reativecommons.png"]').attr('src', '/images/f/fe/Creativecommons.png' );
    };

    // fix for toolbar element is sometimes not available in edit mode 
    var toolbarFix = function () {
        if ((window.wgAction == 'edit' || window.wgAction == 'submit') && $('#toolbar').length == 0) {
            $('<div id="toolbar">').insertBefore($('#editform'));
        }
    };
	
	
    // rearrange toolbar buttons
    
    var editToolBarFix = function () {
        mwEditButtons = [];
        
        addButton("/skins/common/images/button_sig.png","Your signature with timestamp","--[[User:Vadim|Vadim]] 12:38, 21 February 2016 (GMT)","","","mw-editbutton-signature");
        addButton("/skins/common/images/button_bold.png","Bold text","\'\'\'","\'\'\'","Bold text","mw-editbutton-bold");
        addButton("/skins/common/images/button_italic.png","Italic text","\'\'","\'\'","Italic text","mw-editbutton-italic");
        addButton("/skins/common/images/button_link.png","Internal link","[[","]]","Link title","mw-editbutton-link");
        addButton("/skins/common/images/button_extlink.png","External link (remember http:// prefix)","[","]","http://www.example.com link title","mw-editbutton-extlink");
        addButton("/skins/common/images/button_headline.png","Level 2 headline","\n== "," ==\n","Headline text","mw-editbutton-headline");
        addButton("/skins/common/images/button_image.png","Embedded file","[[File:","]]","Example.jpg","mw-editbutton-image");
        addButton("/skins/common/images/button_media.png","File link","[[Media:","]]","Example.ogg","mw-editbutton-media");
        // addButton("/skins/common/images/button_math.png","Mathematical formula (LaTeX)","\x3cmath\x3e","\x3c/math\x3e","Insert formula here","mw-editbutton-math");
        addButton("/skins/common/images/button_nowiki.png","Ignore wiki formatting","\x3cnowiki\x3e","\x3c/nowiki\x3e","Insert non-formatted text here","mw-editbutton-nowiki");
        // addButton("/skins/common/images/button_hr.png","Horizontal line (use sparingly)","\n----\n","","","mw-editbutton-hr");
        
        addButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");
    };
	
	// don't use loadScript here as it uses addOnloadHook
	
	var elem = importScript("MediaWiki:jquery-1.12.0.min.js");
// 	var elem = importScriptURI("https://code.jquery.com/jquery-1.12.0.min.js");
	if (elem) {
    	elem.onload = function () { // isolate ecach callback call
	       // console.log('onload jQuery ' + typeof jQuery  + ' ' + typeof $+' document.readyState ' + document.readyState);
	        if (typeof jQuery === 'undefined') {
    	        console.error('jQuery not loaded!');
	            return;
	        }
	        $.ajaxSetup({
	            cache: true
	        });
	        var jQueryReady = function(){
    		    console.log('document ready');
    		    
    		    licenseBadgeFix();
    		    toolbarFix();
    		    editToolBarFix();
    		    
    		    if (!doneOnloadHookNew) {
        			doneOnloadHookNew = true;
        			doneOnloadHook = false;
        			onloadFuncts = onloadFunctsNew;
        			onloadFunctsNew = [];
        			runOnloadHook();
    		    }
    		};
    // 		if ( is_safari /* from wikibits.js */ ) {
    		if ( document.readyState ) {
    		    var documentReady = function () {
        	        console.log('document.readyState ' + document.readyState);
        		    if (document.readyState == 'interactive' || document.readyState == 'complete') {
        		        setZeroTimeout(jQueryReady);
        		    } else {
        		        setTimeout(documentReady, 100);
        		    }
    		    }
    		    documentReady();
    		} else {
                $(jQueryReady);
    		}
        };
	}
})();

// chartlets

addOnloadHook(function () { 
    if ($('.cw-chartlet, .cw-chartlet-standalone').length > 0) {
        loadWikiScript('MediaWiki:Chartlet.js');
    }
});

addOnloadHook(function () { 
    if ($('.ol3-chartlet').length > 0) {
        loadWikiScript('CruisersWiki:Ol3chartlet.js');
    }
});

// add toggle poi sample link display to toolbox

addOnloadHook(function () { 
    if ($('.cw-poi').length > 0) {
        var $a = $('<a style="cursor:pointer">Show POI links</a>')
            .click(function () {
                $('body').toggleClass('cw-poi-sample-link-dislay');
            });
        $('#p-tb ul').append($('<li>').append($a));
    }
});

// add kml download link to toolbox

addOnloadHook(function () { 
    if ($('.h-geo').length > 0) {
        loadWikiScript('MediaWiki:GetKml.js');
    }
});

// remove broken fckeditor

addOnloadHook(function () { 
        $('script[src*="fckeditor"]').remove();
        $('#fckTools').remove();
});

// rewrite image links' titles

addOnloadHook(function () { 
    $('a.image>img[alt]').each(function(i) {
        var $this = $(this);
        var alt = $this.attr('alt');
        $this.attr('title', alt);
        // console.log(''+i+' '+alt);
    });
});

// after [[w:User:Rezonansowy/SimpleLightbox]]
    
addOnloadHook(function () { 
    // $('body').append(
    //     '<div id="lightbox" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999">' + 
    //         '<span style="display: inline-block; height: 100%; vertical-align: middle;" ></span>' + // http://stackoverflow.com/a/7310398
    //         '<img style="display:none;background:#fff;box-shadow: 0 0 25px #111;max-height:100%;max-width:100%;vertical-align:middle;cursor:pointer;" />' +
    //     '</div>'
    // );
    // $('#lightbox').on('click', function() {
    //     $(this).fadeOut('fast', function () {
    //         $('#lightbox img').hide();
    //     });
    // });
    // $('body').append('<div id="lightbox" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999"></div>');

    $('body').append(
        '<div id="cw-lightbox" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999">' + 
        '</div>'
    );

    $('a.image').click(function(e) {
        e.preventDefault();
        var src = $(this).find('img').attr('src');
        var thumb = src.match(/^(.*)(\/thumb)(.*)(\/[^\/]*)$/,'');
        if (thumb) {
      	    src = thumb[1] + thumb[3];
        } else if (src.match("/Special:Redirect/") ) {
            src = src.replace(/(\?width=.*)$/,'');
        }

        // $('#lightbox').show();
        // $('#lightbox img')
        //     .attr('src', src)
        //     .load(function () {
        //         $(this).fadeIn('fast');
        //     });
            
        $('#cw-lightbox')
            // .html('<img src="'+src+'" style="background:#fff;box-shadow: 0 0 25px #111;max-height:100%;max-width:100%;vertical-align:middle;cursor:pointer;" />')
            // .css('line-height', $(window).height()+'px')
            .html(
                '<span style="display: inline-block; height: 100%; vertical-align: middle;" ></span>' + // http://stackoverflow.com/a/7310398
                '<img style="background:#fff;box-shadow: 0 0 25px #111;max-height:100%;max-width:100%;cursor:pointer;"' +
                'src="' + src + '" />'
            )
            .fadeIn('fast')
            .on('click', function() {
                $(this).fadeOut('fast');
            });
    });
});

// custom edit toolbar buttons

// function addCustomEditButton (imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
// mwCustomEditButtons[mwCustomEditButtons.length] =
// 	{"imageId": imageId,
// 	 "imageFile": imageFile,
// 	 "speedTip": speedTip,
// 	 "tagOpen": tagOpen,
// 	 "tagClose": tagClose,
// 	 "sampleText": sampleText};
// }

// addCustomEditButton("/images/7/70/Button_POI.png","Add POI","{{poi | lat= | lon=\n| type=\n| name=","\n| text=\n}}\n","","mw-editbutton-poi");

if (wgUserName == 'Vadim') {
    importScript('User:Vadim/common.js');
}
Personal tools
advertisement
Friends of Cruisers Wiki