From: Krinkle Date: Wed, 27 Oct 2010 15:35:55 +0000 (+0000) Subject: Fixed unfinished line from r75275, commented addPortletLink() more, moved prototypes... X-Git-Tag: 1.31.0-rc.0~34266 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/comptes/journal.php?a=commitdiff_plain;h=bf08a8f2a4f1ce57640e789ea67d66fb69d90585;p=lhc%2Fweb%2Fwiklou.git Fixed unfinished line from r75275, commented addPortletLink() more, moved prototypes from mediawiki.util.js to mediawiki.js, added String.prototype.escapeRE() --- diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index 2c8f973fea..a3f44dc517 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -26,13 +26,6 @@ this.tooltipAccessKeyPrefix = 'alt-shift-'; } - // Prototype enhancements - if (typeof String.prototype.ucFirst === 'undefined') { - String.prototype.ucFirst = function () { - return this.substr(0, 1).toUpperCase() + this.substr(1, this.length); - }; - } - // Any initialisation after the DOM is ready $(function () { @@ -62,7 +55,7 @@ * * @param String str string to be encoded */ - 'rawurlencode' : function (str) { + 'rawurlencode' : function( str ) { str = (str + '').toString(); return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28') .replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/~/g, '%7E'); @@ -75,7 +68,7 @@ * * @param String str string to be encoded */ - 'wikiUrlencode' : function (str) { + 'wikiUrlencode' : function( str ) { return this.rawurlencode(str).replace(/%20/g, '_').replace(/%3A/g, ':').replace(/%2F/g, '/'); }, @@ -84,7 +77,7 @@ * * @param String str pagename to link to */ - 'getWikilink' : function (str) { + 'getWikilink' : function( str ) { return wgServer + wgArticlePath.replace('$1', this.wikiUrlencode(str)); }, @@ -94,7 +87,7 @@ * * @param Mixed v the variable to check for empty ness */ - 'isEmpty' : function (v) { + 'isEmpty' : function( v ) { var key; if (v === "" || v === 0 || v === "0" || v === null || v === false || typeof v === 'undefined') { return true; @@ -119,7 +112,7 @@ * @param String param paramter name * @param String url url to search through (optional) */ - 'getParamValue' : function (param, url) { + 'getParamValue' : function( param, url ) { url = url ? url : document.location.href; var re = new RegExp('[^#]*[&?]' + param + '=([^&#]*)'); // Get last match, stop at hash var m = re.exec(url); @@ -135,7 +128,7 @@ * @param String str text to escape * @param Bool quotes if true escapes single and double quotes aswell (by default false) */ - 'htmlEscape' : function (str, quotes) { + 'htmlEscape' : function( str, quotes ) { str = $('
').text(str).html(); if (typeof quotes === 'undefined') { quotes = false; @@ -151,7 +144,7 @@ * * @param String str text to unescape */ - 'htmlUnescape' : function (str) { + 'htmlUnescape' : function( str ) { return $('
').html(str).text(); }, @@ -170,19 +163,19 @@ * * @param Mixed nodeList jQuery object, or array of elements */ - 'updateTooltipAccessKeys' : function (nodeList) { + 'updateTooltipAccessKeys' : function( nodeList ) { var $nodes; if (nodeList instanceof jQuery) { $nodes = nodeList; } else if (nodeList) { $nodes = $(nodeList); } else { - // Rather than scanning all links, just - $('#column-one a, #mw-head a, #mw-panel a, #p-logo a'); + // Rather than scanning all links, just the elements that contain the relevant links + this.updateTooltipAccessKeys( $('#column-one a, #mw-head a, #mw-panel a, #p-logo a') ); // these are rare enough that no such optimization is needed - this.updateTooltipAccessKeys($('input')); - this.updateTooltipAccessKeys($('label')); + this.updateTooltipAccessKeys( $('input') ); + this.updateTooltipAccessKeys( $('label') ); return; } @@ -223,11 +216,13 @@ * * @return Node the DOM node of the new item (a LI element, or A element for older skins) or null */ - 'addPortletLink' : function (portlet, href, text, id, tooltip, accesskey, nextnode) { + 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) { + + // Setup the anchor tag var $link = $('').attr('href', href).text(text); - // Some skins don't have portlets - // Just add it to the bottom of their 'sidebar' element ignoring the specified portlet target + // Some skins don't have any portlets + // just add it to the bottom of their 'sidebar' element as a fallback switch (skin) { case 'standard' : case 'cologneblue' : @@ -236,30 +231,41 @@ case 'nostalgia' : $("#searchform").before($link).before(' | '); return $link.get(0); - default : // chick, modern, monobook, myskin, simple, vector... + default : // Skins like chick, modern, monobook, myskin, simple, vector... + // Select the specified portlet var $portlet = $('#' + portlet); if ($portlet.length === 0) { return null; } + // Select the first (most likely only) unordered list inside the portlet var $ul = $portlet.find('ul').eq(0); + + // If it didn't have an unordered list yet, create it if ($ul.length === 0) { + // If there's no
inside, append it to the portlet directly if ($portlet.find('div').length === 0) { - $portlet.append('
    '); + $portlet.append('
      '); } else { - $portlet.find('div').eq(-1).append('
        '); + // otherwise if there's a div (such as div.body or div.pBody) append the
          to last (most likely only) div + $portlet.find('div').eq(-1).append('
            '); } + // Select the created element $ul = $portlet.find('ul').eq(0); } + // Just in case.. if ($ul.length === 0) { return null; } - // unhide portlet if it was hidden before + // Unhide portlet if it was hidden before $portlet.removeClass('emptyPortlet'); + // Wrap the anchor tag in a and create a list item for it + // and back up the selector to the list item var $item = $link.wrap('
          • ').parent().parent(); + // Implement the properties passed to the function if (id) { $item.attr('id', id); } diff --git a/resources/mediawiki.util/mediawiki.util.test.js b/resources/mediawiki.util/mediawiki.util.test.js index 5122dbf05f..9db7853e6c 100644 --- a/resources/mediawiki.util/mediawiki.util.test.js +++ b/resources/mediawiki.util/mediawiki.util.test.js @@ -69,7 +69,11 @@ 'function (string)'); mw.test.addTest('\'mediawiki\'.ucFirst()', 'Mediawiki (string)'); - mw.test.addTest('typeof $.fn.enableCheckboxShiftClick', + mw.test.addTest('typeof String.prototype.escapeRE', + 'function (string)'); + mw.test.addTest('\'.st{e}$st\'.escapeRE()', + '\\.st\\{e\\}\\$st (string)'); + mw.test.addTest('typeof $.fn.checkboxShiftClick', 'function (string)'); mw.test.addTest('typeof mw.util.rawurlencode', 'function (string)'); diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 59035415ac..dcb8c791c4 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -21,6 +21,25 @@ if ( typeof String.prototype.trimRight === 'undefined' ) { }; } +/* + * Prototype enhancements + */ + +// Capitalize the first character of the given string +if ( typeof String.prototype.ucFirst === 'undefined' ) { + String.prototype.ucFirst = function() { + return this.substr(0, 1).toUpperCase() + this.substr(1, this.length); + }; +} + +// Escape all RegExp special characters such that the result can be safely used +// in a RegExp as a literal. +if ( typeof String.prototype.escapeRE === 'undefined' ) { + String.prototype.escapeRE = function() { + return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1"); + }; +} + /* * Core MediaWiki JavaScript Library */