10 'initialised' : false,
11 'init' : function () {
12 if (this.initialised === false) {
13 this.initialised = true;
15 // Set tooltipAccessKeyPrefix
17 this.tooltipAccessKeyPrefix = 'shift-esc-';
18 } else if (is_chrome) {
19 this.tooltipAccessKeyPrefix = is_chrome_mac ? 'ctrl-option-' : 'alt-';
20 } else if (!is_safari_win && is_safari && webkit_version > 526) {
21 this.tooltipAccessKeyPrefix = 'ctrl-alt-';
22 } else if (!is_safari_win &&
23 (is_safari || clientPC.indexOf('mac') !== -1 || clientPC.indexOf('konqueror') !== -1)) {
24 this.tooltipAccessKeyPrefix = 'ctrl-';
26 this.tooltipAccessKeyPrefix = 'alt-shift-';
29 // Prototype enhancements
30 if (typeof String.prototype.ucFirst === 'undefined') {
31 String.prototype.ucFirst = function () {
32 return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
36 // Any initialisation after the DOM is ready
39 // Enable CheckboxShiftClick
40 $('input[type=checkbox]:not(.noshiftselect)').checkboxShiftClick();
42 // Fill bodyContant var
43 if ($('#bodyContent').length) {
44 mw.util.$content = $('#bodyContent');
45 } else if ($('#article').length) {
46 mw.util.$content = $('#article');
48 mw.util.$content = $('#content');
61 * Encodes the string like PHP's rawurlencode
63 * @param String str string to be encoded
65 'rawurlencode' : function (str) {
66 str = (str + '').toString();
67 return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28')
68 .replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/~/g, '%7E');
72 * Encode pagetitles for use in a URL
73 * We want / and : to be included as literal characters in our title URLs
74 * as they otherwise fatally break the title
76 * @param String str string to be encoded
78 'wikiUrlencode' : function (str) {
79 return this.rawurlencode(str).replace(/%20/g, '_').replace(/%3A/g, ':').replace(/%2F/g, '/');
83 * Get the full url to a pagename
85 * @param String str pagename to link to
87 'getWikilink' : function (str) {
88 return wgServer + wgArticlePath.replace('$1', this.wikiUrlencode(str));
92 * Check is a variable is empty. Support for strings, booleans, arrays and objects.
93 * String "0" is considered empty. String containing only whitespace (ie. " ") is considered not empty.
95 * @param Mixed v the variable to check for empty ness
97 'isEmpty' : function (v) {
99 if (v === "" || v === 0 || v === "0" || v === null || v === false || typeof v === 'undefined') {
102 if (v.length === 0) {
105 if (typeof v === 'object') {
116 * Grabs the url parameter value for the given parameter
117 * Returns null if not found
119 * @param String param paramter name
120 * @param String url url to search through (optional)
122 'getParamValue' : function (param, url) {
123 url = url ? url : document.location.href;
124 var re = new RegExp('[^#]*[&?]' + param + '=([^&#]*)'); // Get last match, stop at hash
125 var m = re.exec(url);
126 if (m && m.length > 1) {
127 return decodeURIComponent(m[1]);
133 * Converts special characters to their HTML entities
135 * @param String str text to escape
136 * @param Bool quotes if true escapes single and double quotes aswell (by default false)
138 'htmlEscape' : function (str, quotes) {
139 str = $('<div/>').text(str).html();
140 if (typeof quotes === 'undefined') {
143 if (quotes === true) {
144 str = str.replace(/'/g, ''').replace(/"/g, '"');
150 * Converts HTML entities back to text
152 * @param String str text to unescape
154 'htmlUnescape' : function (str) {
155 return $('<div/>').html(str).text();
159 // will be re-defined based on browser/operating system detection in mw.util.init()
160 'tooltipAccessKeyPrefix' : 'alt-',
162 // Regex to match accesskey tooltips
163 'tooltipAccessKeyRegexp': /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/,
166 * Add the appropriate prefix to the accesskey shown in the tooltip.
167 * If the nodeList parameter is given, only those nodes are updated;
168 * otherwise, all the nodes that will probably have accesskeys by
169 * default are updated.
171 * @param Mixed nodeList jQuery object, or array of elements
173 'updateTooltipAccessKeys' : function (nodeList) {
175 if (nodeList instanceof jQuery) {
177 } else if (nodeList) {
178 $nodes = $(nodeList);
180 // Rather than scanning all links, just
181 $('#column-one a, #mw-head a, #mw-panel a, #p-logo a');
183 // these are rare enough that no such optimization is needed
184 this.updateTooltipAccessKeys($('input'));
185 this.updateTooltipAccessKeys($('label'));
189 $nodes.each(function (i) {
190 var tip = $(this).attr('title');
191 if (!!tip && mw.util.tooltipAccessKeyRegexp.exec(tip)) {
192 tip = tip.replace(mw.util.tooltipAccessKeyRegexp, '[' + mw.util.tooltipAccessKeyPrefix + "$5]");
193 $(this).attr('title', tip);
198 // jQuery object that refers to the page-content element
199 // Populated by init()
204 * Add a link to a portlet menu on the page, such as:
206 * p-cactions (Content actions), p-personal (Personal tools), p-navigation (Navigation), p-tb (Toolbox)
208 * The first three paramters are required, others are optionals. Though
209 * providing an id and tooltip is recommended.
211 * By default the new link will be added to the end of the list. To add the link before a given existing item,
212 * pass the DOM node (document.getElementById('foobar') or the jQuery-selector ('#foobar') of that item.
214 * @example mw.util.addPortletLink('p-tb', 'http://mediawiki.org/', 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', '#t-print')
216 * @param String portlet id of the target portlet ('p-cactions' or 'p-personal' etc.)
217 * @param String href link URL
218 * @param String text link text (will be automatically lowercased by CSS for p-cactions in Monobook)
219 * @param String id id of the new item, should be unique and preferably have the appropriate prefix ('ca-', 'pt-', 'n-' or 't-')
220 * @param String tooltip text to show when hovering over the link, without accesskey suffix
221 * @param String accesskey accesskey to activate this link (one character, try to avoid conflicts. Use $('[accesskey=x').get() in the console to see if 'x' is already used.
222 * @param mixed nextnode DOM node or jQuery-selector of the item that the new item should be added before, should be another item in the same list will be ignored if not the so
224 * @return Node the DOM node of the new item (a LI element, or A element for older skins) or null
226 'addPortletLink' : function (portlet, href, text, id, tooltip, accesskey, nextnode) {
227 var $link = $('<a />').attr('href', href).text(text);
229 // Some skins don't have portlets
230 // Just add it to the bottom of their 'sidebar' element ignoring the specified portlet target
234 $("#quickbar").append($link.after('<br />'));
237 $("#searchform").before($link).before(' | ');
239 default : // chick, modern, monobook, myskin, simple, vector...
241 var $portlet = $('#' + portlet);
242 if ($portlet.length === 0) {
245 var $ul = $portlet.find('ul').eq(0);
246 if ($ul.length === 0) {
247 if ($portlet.find('div').length === 0) {
248 $portlet.append('<ul />');
250 $portlet.find('div').eq(-1).append('<ul />');
252 $ul = $portlet.find('ul').eq(0);
254 if ($ul.length === 0) {
258 // unhide portlet if it was hidden before
259 $portlet.removeClass('emptyPortlet');
261 var $item = $link.wrap('<li><span /></li>').parent().parent();
264 $item.attr('id', id);
267 $link.attr('accesskey', accesskey);
268 tooltip += ' [' + accesskey + ']';
271 $link.attr('title', tooltip);
273 if (accesskey && tooltip) {
274 this.updateTooltipAccessKeys($link);
277 // Append using DOM-element passing
278 if (nextnode && nextnode.parentNode == $ul.get(0)) {
279 $(nextnode).before($item);
281 // If the jQuery selector isn't found within the <ul>, just append it at the end
282 if ($ul.find(nextnode).length === 0) {
285 // Append using jQuery CSS selector
286 $ul.find(nextnode).eq(0).before($item);
296 mediaWiki.util.init();
298 })(jQuery, mediaWiki);