Make mwCustomEditButtons a no-op and mark it as deprecated
[lhc/web/wiklou.git] / skins / common / wikibits.js
1 /**
2 * MediaWiki legacy wikibits
3 */
4 /*jshint quotmark:false, onevar:false */
5 ( function ( mw, $ ) {
6 var isIE6, isGecko,
7 ua = navigator.userAgent.toLowerCase(),
8 uaMsg = 'Use feature detection or module jquery.client instead.';
9
10 /**
11 * User-agent sniffing.
12 * To be removed in MediaWiki 1.23.
13 *
14 * @deprecated since 1.17 Use jquery.client instead.
15 */
16 mw.log.deprecate( window, 'clientPC', ua, uaMsg );
17 $.each([
18 'is_gecko',
19 'is_chrome_mac',
20 'is_chrome',
21 'webkit_version',
22 'is_safari_win',
23 'is_safari',
24 'webkit_match',
25 'is_ff2',
26 'ff2_bugs',
27 'is_ff2_win',
28 'is_ff2_x11',
29 'opera95_bugs',
30 'opera7_bugs',
31 'opera6_bugs',
32 'is_opera_95',
33 'is_opera_preseven',
34 'is_opera',
35 'ie6_bugs'
36 ],
37 function ( i, key ) {
38 mw.log.deprecate( window, key, false, uaMsg );
39 }
40 );
41 if ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( ua ) && parseFloat( RegExp.$1 ) <= 6.0 ) {
42 isIE6 = true;
43 }
44 isGecko = /gecko/.test( ua ) && !/khtml|spoofer|netscape\/7\.0/.test( ua );
45
46 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
47 window.doneOnloadHook = undefined;
48
49 if ( !window.onloadFuncts ) {
50 window.onloadFuncts = [];
51 }
52
53 window.addOnloadHook = function( hookFunct ) {
54 // Allows add-on scripts to add onload functions
55 if( !window.doneOnloadHook ) {
56 window.onloadFuncts[window.onloadFuncts.length] = hookFunct;
57 } else {
58 hookFunct(); // bug in MSIE script loading
59 }
60 };
61
62 window.importScript = function( page ) {
63 var uri = mw.config.get( 'wgScript' ) + '?title=' +
64 mw.util.wikiUrlencode( page ) +
65 '&action=raw&ctype=text/javascript';
66 return window.importScriptURI( uri );
67 };
68
69 window.loadedScripts = {}; // included-scripts tracker
70 window.importScriptURI = function( url ) {
71 if ( window.loadedScripts[url] ) {
72 return null;
73 }
74 window.loadedScripts[url] = true;
75 var s = document.createElement( 'script' );
76 s.setAttribute( 'src', url );
77 s.setAttribute( 'type', 'text/javascript' );
78 document.getElementsByTagName('head')[0].appendChild( s );
79 return s;
80 };
81
82 window.importStylesheet = function( page ) {
83 return window.importStylesheetURI( mw.config.get( 'wgScript' ) + '?action=raw&ctype=text/css&title=' + mw.util.wikiUrlencode( page ) );
84 };
85
86 window.importStylesheetURI = function( url, media ) {
87 var l = document.createElement( 'link' );
88 l.rel = 'stylesheet';
89 l.href = url;
90 if ( media ) {
91 l.media = media;
92 }
93 document.getElementsByTagName('head')[0].appendChild( l );
94 return l;
95 };
96
97 window.appendCSS = function( text ) {
98 var s = document.createElement( 'style' );
99 s.type = 'text/css';
100 s.rel = 'stylesheet';
101 if ( s.styleSheet ) {
102 s.styleSheet.cssText = text; // IE
103 } else {
104 s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null
105 }
106 document.getElementsByTagName('head')[0].appendChild( s );
107 return s;
108 };
109
110 if ( mw.config.get( 'wgBreakFrames' ) ) {
111 // Note: In IE < 9 strict comparison to window is non-standard (the standard didn't exist yet)
112 // it works only comparing to window.self or window.window (http://stackoverflow.com/q/4850978/319266)
113 if ( window.top !== window.self ) {
114 // Un-trap us from framesets
115 window.top.location = window.location;
116 }
117 }
118
119 window.changeText = function( el, newText ) {
120 // Safari work around
121 if ( el.innerText ) {
122 el.innerText = newText;
123 } else if ( el.firstChild && el.firstChild.nodeValue ) {
124 el.firstChild.nodeValue = newText;
125 }
126 };
127
128 window.killEvt = function( evt ) {
129 evt = evt || window.event || window.Event; // W3C, IE, Netscape
130 if ( typeof evt.preventDefault !== 'undefined' ) {
131 evt.preventDefault(); // Don't follow the link
132 evt.stopPropagation();
133 } else {
134 evt.cancelBubble = true; // IE
135 }
136 return false; // Don't follow the link (IE)
137 };
138
139 window.mwEditButtons = [];
140 mw.log.deprecate( window, 'mwCustomEditButtons', [], 'Use mw.toolbar.addButton instead.' );
141
142 window.escapeQuotes = function( text ) {
143 var re = new RegExp( "'", "g" );
144 text = text.replace( re, "\\'" );
145 re = new RegExp( "\\n", "g" );
146 text = text.replace( re, "\\n" );
147 return window.escapeQuotesHTML( text );
148 };
149
150 window.escapeQuotesHTML = function( text ) {
151 var re = new RegExp( '&', "g" );
152 text = text.replace( re, "&amp;" );
153 re = new RegExp( '"', "g" );
154 text = text.replace( re, "&quot;" );
155 re = new RegExp( '<', "g" );
156 text = text.replace( re, "&lt;" );
157 re = new RegExp( '>', "g" );
158 text = text.replace( re, "&gt;" );
159 return text;
160 };
161
162 /**
163 * Accesskey prefix utilities.
164 * To be removed in MediaWiki 1.23.
165 *
166 * @deprecated since 1.17 Use mediawiki.util instead.
167 */
168 mw.log.deprecate( window, 'tooltipAccessKeyPrefix', 'alt-', 'Use mediawiki.util instead.' );
169 mw.log.deprecate( window, 'tooltipAccessKeyRegexp', /\[(alt-)?(.)\]$/, 'Use mediawiki.util instead.' );
170 mw.log.deprecate( window, 'updateTooltipAccessKeys', mw.util.updateTooltipAccessKeys, 'Use mediawiki.util instead.' );
171
172 /**
173 * Add a link to one of the portlet menus on the page, including:
174 *
175 * p-cactions: Content actions (shown as tabs above the main content in Monobook)
176 * p-personal: Personal tools (shown at the top right of the page in Monobook)
177 * p-navigation: Navigation
178 * p-tb: Toolbox
179 *
180 * This function exists for the convenience of custom JS authors. All
181 * but the first three parameters are optional, though providing at
182 * least an id and a tooltip is recommended.
183 *
184 * By default the new link will be added to the end of the list. To
185 * add the link before a given existing item, pass the DOM node of
186 * that item (easily obtained with document.getElementById()) as the
187 * nextnode parameter; to add the link _after_ an existing item, pass
188 * the node's nextSibling instead.
189 *
190 * @param portlet String id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
191 * @param href String link URL
192 * @param text String link text (will be automatically lowercased by CSS for p-cactions in Monobook)
193 * @param id String id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
194 * @param tooltip String text to show when hovering over the link, without accesskey suffix
195 * @param accesskey String accesskey to activate this link (one character, try to avoid conflicts)
196 * @param nextnode Node the DOM node before which the new item should be added, should be another item in the same list
197 *
198 * @return Node -- the DOM node of the new item (an LI element) or null
199 */
200 window.addPortletLink = function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
201 var root = document.getElementById( portlet );
202 if ( !root ) {
203 return null;
204 }
205 var uls = root.getElementsByTagName( 'ul' );
206 var node;
207 if ( uls.length > 0 ) {
208 node = uls[0];
209 } else {
210 node = document.createElement( 'ul' );
211 var lastElementChild = null;
212 for ( var i = 0; i < root.childNodes.length; ++i ) { /* get root.lastElementChild */
213 if ( root.childNodes[i].nodeType === 1 ) {
214 lastElementChild = root.childNodes[i];
215 }
216 }
217 if ( lastElementChild && lastElementChild.nodeName.match( /div/i ) ) {
218 /* Insert into the menu divs */
219 lastElementChild.appendChild( node );
220 } else {
221 root.appendChild( node );
222 }
223 }
224 if ( !node ) {
225 return null;
226 }
227
228 // unhide portlet if it was hidden before
229 root.className = root.className.replace( /(^| )emptyPortlet( |$)/, "$2" );
230
231 var link = document.createElement( 'a' );
232 link.appendChild( document.createTextNode( text ) );
233 link.href = href;
234
235 // Wrap in a span - make it work with vector tabs and has no effect on any other portlets
236 var span = document.createElement( 'span' );
237 span.appendChild( link );
238
239 var item = document.createElement( 'li' );
240 item.appendChild( span );
241 if ( id ) {
242 item.id = id;
243 }
244
245 if ( accesskey ) {
246 link.setAttribute( 'accesskey', accesskey );
247 tooltip += ' [' + accesskey + ']';
248 }
249 if ( tooltip ) {
250 link.setAttribute( 'title', tooltip );
251 }
252 if ( accesskey && tooltip ) {
253 mw.util.updateTooltipAccessKeys( [link] );
254 }
255
256 if ( nextnode && nextnode.parentNode === node ) {
257 node.insertBefore( item, nextnode );
258 } else {
259 node.appendChild( item ); // IE compatibility (?)
260 }
261
262 return item;
263 };
264
265 window.getInnerText = function( el ) {
266 if ( typeof el === 'string' ) {
267 return el;
268 }
269 if ( typeof el === 'undefined' ) {
270 return el;
271 }
272 // Custom sort value through 'data-sort-value' attribute
273 // (no need to prepend hidden text to change sort value)
274 if ( el.nodeType && el.getAttribute( 'data-sort-value' ) !== null ) {
275 // Make sure it's a valid DOM element (.nodeType) and that the attribute is set (!null)
276 return el.getAttribute( 'data-sort-value' );
277 }
278 if ( el.textContent ) {
279 return el.textContent; // not needed but it is faster
280 }
281 if ( el.innerText ) {
282 return el.innerText; // IE doesn't have textContent
283 }
284 var str = '';
285
286 var cs = el.childNodes;
287 var l = cs.length;
288 for ( var i = 0; i < l; i++ ) {
289 switch ( cs[i].nodeType ) {
290 case 1: // ELEMENT_NODE
291 str += window.getInnerText( cs[i] );
292 break;
293 case 3: // TEXT_NODE
294 str += cs[i].nodeValue;
295 break;
296 }
297 }
298 return str;
299 };
300
301 /**
302 * Toggle checkboxes with shift selection.
303 * To be removed in MediaWiki 1.23.
304 *
305 * @deprecated since 1.17 Use jquery.checkboxShiftClick instead.
306 */
307 $.each({
308 checkboxes: [],
309 lastCheckbox: null,
310 setupCheckboxShiftClick: $.noop,
311 addCheckboxClickHandlers: $.noop,
312 checkboxClickHandler: $.noop
313 }, function ( key, val ) {
314 mw.log.deprecate( window, key, val, 'Use jquery.checkboxShiftClick instead.' );
315 } );
316
317 /*
318 Written by Jonathan Snook, http://www.snook.ca/jonathan
319 Add-ons by Robert Nyman, http://www.robertnyman.com
320 Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
321 From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
322 */
323 window.getElementsByClassName = function( oElm, strTagName, oClassNames ) {
324 var arrReturnElements = [];
325 if ( typeof oElm.getElementsByClassName === 'function' ) {
326 /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
327 var arrNativeReturn = oElm.getElementsByClassName( oClassNames );
328 if ( strTagName === '*' ) {
329 return arrNativeReturn;
330 }
331 for ( var h = 0; h < arrNativeReturn.length; h++ ) {
332 if( arrNativeReturn[h].tagName.toLowerCase() === strTagName.toLowerCase() ) {
333 arrReturnElements[arrReturnElements.length] = arrNativeReturn[h];
334 }
335 }
336 return arrReturnElements;
337 }
338 var arrElements = ( strTagName === '*' && oElm.all ) ? oElm.all : oElm.getElementsByTagName( strTagName );
339 var arrRegExpClassNames = [];
340 if( typeof oClassNames === 'object' ) {
341 for( var i = 0; i < oClassNames.length; i++ ) {
342 arrRegExpClassNames[arrRegExpClassNames.length] =
343 new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)");
344 }
345 } else {
346 arrRegExpClassNames[arrRegExpClassNames.length] =
347 new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)");
348 }
349 var oElement;
350 var bMatchesAll;
351 for( var j = 0; j < arrElements.length; j++ ) {
352 oElement = arrElements[j];
353 bMatchesAll = true;
354 for( var k = 0; k < arrRegExpClassNames.length; k++ ) {
355 if( !arrRegExpClassNames[k].test( oElement.className ) ) {
356 bMatchesAll = false;
357 break;
358 }
359 }
360 if( bMatchesAll ) {
361 arrReturnElements[arrReturnElements.length] = oElement;
362 }
363 }
364 return ( arrReturnElements );
365 };
366
367 window.redirectToFragment = function( fragment ) {
368 var webKitVersion,
369 match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
370 if ( match ) {
371 webKitVersion = parseInt( match[1], 10 );
372 if ( webKitVersion < 420 ) {
373 // Released Safari w/ WebKit 418.9.1 messes up horribly
374 // Nightlies of 420+ are ok
375 return;
376 }
377 }
378 if ( !window.location.hash ) {
379 window.location.hash = fragment;
380
381 // Mozilla needs to wait until after load, otherwise the window doesn't
382 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
383 // There's no obvious way to detect this programmatically, so we use
384 // version-testing. If Firefox fixes the bug, they'll jump twice, but
385 // better twice than not at all, so make the fix hit future versions as
386 // well.
387 if ( isGecko ) {
388 $( function () {
389 if ( window.location.hash === fragment ) {
390 window.location.hash = fragment;
391 }
392 } );
393 }
394 }
395 };
396
397 /**
398 * Add a cute little box at the top of the screen to inform the user of
399 * something, replacing any preexisting message.
400 *
401 * @deprecated since 1.17 Use the 'mediawiki.notify' module instead.
402 * @param {string|HTMLElement} message To be put inside the message box.
403 */
404 mw.log.deprecate( window, 'jsMsg', mw.util.jsMessage, 'Use mediawiki.notify instead.' );
405
406 /**
407 * Inject a cute little progress spinner after the specified element
408 *
409 * @param element Element to inject after
410 * @param id Identifier string (for use with removeSpinner(), below)
411 */
412 window.injectSpinner = function( element, id ) {
413 var spinner = document.createElement( 'img' );
414 spinner.id = 'mw-spinner-' + id;
415 spinner.src = mw.config.get( 'stylepath' ) + '/common/images/spinner.gif';
416 spinner.alt = spinner.title = '...';
417 if( element.nextSibling ) {
418 element.parentNode.insertBefore( spinner, element.nextSibling );
419 } else {
420 element.parentNode.appendChild( spinner );
421 }
422 };
423
424 /**
425 * Remove a progress spinner added with injectSpinner()
426 *
427 * @param id Identifier string
428 */
429 window.removeSpinner = function( id ) {
430 var spinner = document.getElementById( 'mw-spinner-' + id );
431 if( spinner ) {
432 spinner.parentNode.removeChild( spinner );
433 }
434 };
435
436 window.runOnloadHook = function() {
437 // don't run anything below this for non-dom browsers
438 if ( window.doneOnloadHook || !( document.getElementById && document.getElementsByTagName ) ) {
439 return;
440 }
441
442 // set this before running any hooks, since any errors below
443 // might cause the function to terminate prematurely
444 window.doneOnloadHook = true;
445
446 // Run any added-on functions
447 for ( var i = 0; i < window.onloadFuncts.length; i++ ) {
448 window.onloadFuncts[i]();
449 }
450 };
451
452 /**
453 * Add an event handler to an element
454 *
455 * @param element Element to add handler to
456 * @param attach String Event to attach to
457 * @param handler callable Event handler callback
458 */
459 window.addHandler = function( element, attach, handler ) {
460 if( element.addEventListener ) {
461 element.addEventListener( attach, handler, false );
462 } else if( element.attachEvent ) {
463 element.attachEvent( 'on' + attach, handler );
464 }
465 };
466
467 window.hookEvent = function( hookName, hookFunct ) {
468 window.addHandler( window, hookName, hookFunct );
469 };
470
471 /**
472 * Add a click event handler to an element
473 *
474 * @param element Element to add handler to
475 * @param handler callable Event handler callback
476 */
477 window.addClickHandler = function( element, handler ) {
478 window.addHandler( element, 'click', handler );
479 };
480
481 /**
482 * Removes an event handler from an element
483 *
484 * @param element Element to remove handler from
485 * @param remove String Event to remove
486 * @param handler callable Event handler callback to remove
487 */
488 window.removeHandler = function( element, remove, handler ) {
489 if( window.removeEventListener ) {
490 element.removeEventListener( remove, handler, false );
491 } else if( window.detachEvent ) {
492 element.detachEvent( 'on' + remove, handler );
493 }
494 };
495 window.hookEvent( 'load', window.runOnloadHook );
496
497 if ( isIE6 ) {
498 window.importScriptURI( mw.config.get( 'stylepath' ) + '/common/IEFixes.js' );
499 }
500
501 }( mediaWiki, jQuery ) );