wikibits.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**
  2. * MediaWiki legacy wikibits
  3. */
  4. (function(){
  5. window.clientPC = navigator.userAgent.toLowerCase(); // Get client info
  6. window.is_gecko = /gecko/.test( clientPC ) &&
  7. !/khtml|spoofer|netscape\/7\.0/.test(clientPC);
  8. window.is_safari = window.is_safari_win = window.webkit_version =
  9. window.is_chrome = window.is_chrome_mac = false;
  10. window.webkit_match = clientPC.match(/applewebkit\/(\d+)/);
  11. if (webkit_match) {
  12. window.is_safari = clientPC.indexOf('applewebkit') != -1 &&
  13. clientPC.indexOf('spoofer') == -1;
  14. window.is_safari_win = is_safari && clientPC.indexOf('windows') != -1;
  15. window.webkit_version = parseInt(webkit_match[1]);
  16. // Tests for chrome here, to avoid breaking old scripts safari left alone
  17. // This is here for accesskeys
  18. window.is_chrome = clientPC.indexOf('chrome') !== -1 &&
  19. clientPC.indexOf('spoofer') === -1;
  20. window.is_chrome_mac = is_chrome && clientPC.indexOf('mac') !== -1
  21. }
  22. // For accesskeys; note that FF3+ is included here!
  23. window.is_ff2 = /firefox\/[2-9]|minefield\/3/.test( clientPC );
  24. window.ff2_bugs = /firefox\/2/.test( clientPC );
  25. // These aren't used here, but some custom scripts rely on them
  26. window.is_ff2_win = is_ff2 && clientPC.indexOf('windows') != -1;
  27. window.is_ff2_x11 = is_ff2 && clientPC.indexOf('x11') != -1;
  28. window.is_opera = window.is_opera_preseven = window.is_opera_95 =
  29. window.opera6_bugs = window.opera7_bugs = window.opera95_bugs = false;
  30. if (clientPC.indexOf('opera') != -1) {
  31. window.is_opera = true;
  32. window.is_opera_preseven = window.opera && !document.childNodes;
  33. window.is_opera_seven = window.opera && document.childNodes;
  34. window.is_opera_95 = /opera\/(9\.[5-9]|[1-9][0-9])/.test( clientPC );
  35. window.opera6_bugs = is_opera_preseven;
  36. window.opera7_bugs = is_opera_seven && !is_opera_95;
  37. window.opera95_bugs = /opera\/(9\.5)/.test( clientPC );
  38. }
  39. // As recommended by <http://msdn.microsoft.com/en-us/library/ms537509.aspx>,
  40. // avoiding false positives from moronic extensions that append to the IE UA
  41. // string (bug 23171)
  42. window.ie6_bugs = false;
  43. if ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( clientPC ) != null
  44. && parseFloat( RegExp.$1 ) <= 6.0 ) {
  45. ie6_bugs = true;
  46. }
  47. // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
  48. window.doneOnloadHook = undefined;
  49. if (!window.onloadFuncts) {
  50. window.onloadFuncts = [];
  51. }
  52. window.addOnloadHook = function( hookFunct ) {
  53. // Allows add-on scripts to add onload functions
  54. if( !doneOnloadHook ) {
  55. onloadFuncts[onloadFuncts.length] = hookFunct;
  56. } else {
  57. hookFunct(); // bug in MSIE script loading
  58. }
  59. };
  60. window.importScript = function( page ) {
  61. var uri = mw.config.get( 'wgScript' ) + '?title=' +
  62. mw.util.wikiUrlencode( page ) +
  63. '&action=raw&ctype=text/javascript';
  64. return importScriptURI( uri );
  65. };
  66. window.loadedScripts = {}; // included-scripts tracker
  67. window.importScriptURI = function( url ) {
  68. if ( loadedScripts[url] ) {
  69. return null;
  70. }
  71. loadedScripts[url] = true;
  72. var s = document.createElement( 'script' );
  73. s.setAttribute( 'src', url );
  74. s.setAttribute( 'type', 'text/javascript' );
  75. document.getElementsByTagName('head')[0].appendChild( s );
  76. return s;
  77. };
  78. window.importStylesheet = function( page ) {
  79. return importStylesheetURI( mw.config.get( 'wgScript' ) + '?action=raw&ctype=text/css&title=' + mw.util.wikiUrlencode( page ) );
  80. };
  81. window.importStylesheetURI = function( url, media ) {
  82. var l = document.createElement( 'link' );
  83. l.type = 'text/css';
  84. l.rel = 'stylesheet';
  85. l.href = url;
  86. if( media ) {
  87. l.media = media;
  88. }
  89. document.getElementsByTagName('head')[0].appendChild( l );
  90. return l;
  91. };
  92. window.appendCSS = function( text ) {
  93. var s = document.createElement( 'style' );
  94. s.type = 'text/css';
  95. s.rel = 'stylesheet';
  96. if ( s.styleSheet ) {
  97. s.styleSheet.cssText = text; // IE
  98. } else {
  99. s.appendChild( document.createTextNode( text + '' ) ); // Safari sometimes borks on null
  100. }
  101. document.getElementsByTagName('head')[0].appendChild( s );
  102. return s;
  103. };
  104. // Special stylesheet links for Monobook only (see bug 14717)
  105. var skinpath = mw.config.get( 'stylepath' ) + '/' + mw.config.get( 'skin' );
  106. if ( mw.config.get( 'skin' ) === 'monobook' ) {
  107. if ( opera6_bugs ) {
  108. importStylesheetURI( skinpath + '/Opera6Fixes.css' );
  109. } else if ( opera7_bugs ) {
  110. importStylesheetURI( skinpath + '/Opera7Fixes.css' );
  111. } else if ( opera95_bugs ) {
  112. importStylesheetURI( skinpath + '/Opera9Fixes.css' );
  113. } else if ( ff2_bugs ) {
  114. importStylesheetURI( skinpath + '/FF2Fixes.css' );
  115. }
  116. }
  117. if ( mw.config.get( 'wgBreakFrames' ) ) {
  118. // Un-trap us from framesets
  119. if ( window.top != window ) {
  120. window.top.location = window.location;
  121. }
  122. }
  123. window.changeText = function( el, newText ) {
  124. // Safari work around
  125. if ( el.innerText ) {
  126. el.innerText = newText;
  127. } else if ( el.firstChild && el.firstChild.nodeValue ) {
  128. el.firstChild.nodeValue = newText;
  129. }
  130. };
  131. window.killEvt = function( evt ) {
  132. evt = evt || window.event || window.Event; // W3C, IE, Netscape
  133. if ( typeof ( evt.preventDefault ) != 'undefined' ) {
  134. evt.preventDefault(); // Don't follow the link
  135. evt.stopPropagation();
  136. } else {
  137. evt.cancelBubble = true; // IE
  138. }
  139. return false; // Don't follow the link (IE)
  140. };
  141. window.mwEditButtons = [];
  142. window.mwCustomEditButtons = []; // eg to add in MediaWiki:Common.js
  143. window.escapeQuotes = function( text ) {
  144. var re = new RegExp( "'", "g" );
  145. text = text.replace( re, "\\'" );
  146. re = new RegExp( "\\n", "g" );
  147. text = text.replace( re, "\\n" );
  148. return escapeQuotesHTML( text );
  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. * Set the accesskey prefix based on browser detection.
  163. */
  164. window.tooltipAccessKeyPrefix = 'alt-';
  165. if ( is_opera ) {
  166. tooltipAccessKeyPrefix = 'shift-esc-';
  167. } else if ( is_chrome ) {
  168. tooltipAccessKeyPrefix = is_chrome_mac ? 'ctrl-option-' : 'alt-';
  169. } else if ( !is_safari_win && is_safari && webkit_version > 526 ) {
  170. tooltipAccessKeyPrefix = 'ctrl-alt-';
  171. } else if ( !is_safari_win && ( is_safari
  172. || clientPC.indexOf('mac') != -1
  173. || clientPC.indexOf('konqueror') != -1 ) ) {
  174. tooltipAccessKeyPrefix = 'ctrl-';
  175. } else if ( is_ff2 ) {
  176. tooltipAccessKeyPrefix = 'alt-shift-';
  177. }
  178. window.tooltipAccessKeyRegexp = /\[(ctrl-)?(alt-)?(shift-)?(esc-)?(.)\]$/;
  179. /**
  180. * Add the appropriate prefix to the accesskey shown in the tooltip.
  181. * If the nodeList parameter is given, only those nodes are updated;
  182. * otherwise, all the nodes that will probably have accesskeys by
  183. * default are updated.
  184. *
  185. * @param nodeList Array list of elements to update
  186. */
  187. window.updateTooltipAccessKeys = function( nodeList ) {
  188. if ( !nodeList ) {
  189. // Rather than scan all links on the whole page, we can just scan these
  190. // containers which contain the relevant links. This is really just an
  191. // optimization technique.
  192. var linkContainers = [
  193. 'column-one', // Monobook and Modern
  194. 'mw-head', 'mw-panel', 'p-logo' // Vector
  195. ];
  196. for ( var i in linkContainers ) {
  197. var linkContainer = document.getElementById( linkContainers[i] );
  198. if ( linkContainer ) {
  199. updateTooltipAccessKeys( linkContainer.getElementsByTagName( 'a' ) );
  200. }
  201. }
  202. // these are rare enough that no such optimization is needed
  203. updateTooltipAccessKeys( document.getElementsByTagName( 'input' ) );
  204. updateTooltipAccessKeys( document.getElementsByTagName( 'label' ) );
  205. return;
  206. }
  207. for ( var i = 0; i < nodeList.length; i++ ) {
  208. var element = nodeList[i];
  209. var tip = element.getAttribute( 'title' );
  210. if ( tip && tooltipAccessKeyRegexp.exec( tip ) ) {
  211. tip = tip.replace(tooltipAccessKeyRegexp,
  212. '[' + tooltipAccessKeyPrefix + "$5]");
  213. element.setAttribute( 'title', tip );
  214. }
  215. }
  216. };
  217. /**
  218. * Add a link to one of the portlet menus on the page, including:
  219. *
  220. * p-cactions: Content actions (shown as tabs above the main content in Monobook)
  221. * p-personal: Personal tools (shown at the top right of the page in Monobook)
  222. * p-navigation: Navigation
  223. * p-tb: Toolbox
  224. *
  225. * This function exists for the convenience of custom JS authors. All
  226. * but the first three parameters are optional, though providing at
  227. * least an id and a tooltip is recommended.
  228. *
  229. * By default the new link will be added to the end of the list. To
  230. * add the link before a given existing item, pass the DOM node of
  231. * that item (easily obtained with document.getElementById()) as the
  232. * nextnode parameter; to add the link _after_ an existing item, pass
  233. * the node's nextSibling instead.
  234. *
  235. * @param portlet String id of the target portlet ("p-cactions", "p-personal", "p-navigation" or "p-tb")
  236. * @param href String link URL
  237. * @param text String link text (will be automatically lowercased by CSS for p-cactions in Monobook)
  238. * @param id String id of the new item, should be unique and preferably have the appropriate prefix ("ca-", "pt-", "n-" or "t-")
  239. * @param tooltip String text to show when hovering over the link, without accesskey suffix
  240. * @param accesskey String accesskey to activate this link (one character, try to avoid conflicts)
  241. * @param nextnode Node the DOM node before which the new item should be added, should be another item in the same list
  242. *
  243. * @return Node -- the DOM node of the new item (an LI element) or null
  244. */
  245. window.addPortletLink = function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
  246. var root = document.getElementById( portlet );
  247. if ( !root ) {
  248. return null;
  249. }
  250. var uls = root.getElementsByTagName( 'ul' );
  251. var node;
  252. if ( uls.length > 0 ) {
  253. node = uls[0];
  254. } else {
  255. node = document.createElement( 'ul' );
  256. var lastElementChild = null;
  257. for ( var i = 0; i < root.childNodes.length; ++i ) { /* get root.lastElementChild */
  258. if ( root.childNodes[i].nodeType == 1 ) {
  259. lastElementChild = root.childNodes[i];
  260. }
  261. }
  262. if ( lastElementChild && lastElementChild.nodeName.match( /div/i ) ) {
  263. /* Insert into the menu divs */
  264. lastElementChild.appendChild( node );
  265. } else {
  266. root.appendChild( node );
  267. }
  268. }
  269. if ( !node ) {
  270. return null;
  271. }
  272. // unhide portlet if it was hidden before
  273. root.className = root.className.replace( /(^| )emptyPortlet( |$)/, "$2" );
  274. var link = document.createElement( 'a' );
  275. link.appendChild( document.createTextNode( text ) );
  276. link.href = href;
  277. // Wrap in a span - make it work with vector tabs and has no effect on any other portlets
  278. var span = document.createElement( 'span' );
  279. span.appendChild( link );
  280. var item = document.createElement( 'li' );
  281. item.appendChild( span );
  282. if ( id ) {
  283. item.id = id;
  284. }
  285. if ( accesskey ) {
  286. link.setAttribute( 'accesskey', accesskey );
  287. tooltip += ' [' + accesskey + ']';
  288. }
  289. if ( tooltip ) {
  290. link.setAttribute( 'title', tooltip );
  291. }
  292. if ( accesskey && tooltip ) {
  293. updateTooltipAccessKeys( [link] );
  294. }
  295. if ( nextnode && nextnode.parentNode == node ) {
  296. node.insertBefore( item, nextnode );
  297. } else {
  298. node.appendChild( item ); // IE compatibility (?)
  299. }
  300. return item;
  301. };
  302. window.getInnerText = function( el ) {
  303. if ( typeof el == 'string' ) {
  304. return el;
  305. }
  306. if ( typeof el == 'undefined' ) {
  307. return el;
  308. }
  309. // Custom sort value through 'data-sort-value' attribute
  310. // (no need to prepend hidden text to change sort value)
  311. if ( el.nodeType && el.getAttribute( 'data-sort-value' ) !== null ) {
  312. // Make sure it's a valid DOM element (.nodeType) and that the attribute is set (!null)
  313. return el.getAttribute( 'data-sort-value' );
  314. }
  315. if ( el.textContent ) {
  316. return el.textContent; // not needed but it is faster
  317. }
  318. if ( el.innerText ) {
  319. return el.innerText; // IE doesn't have textContent
  320. }
  321. var str = '';
  322. var cs = el.childNodes;
  323. var l = cs.length;
  324. for ( var i = 0; i < l; i++ ) {
  325. switch ( cs[i].nodeType ) {
  326. case 1: // ELEMENT_NODE
  327. str += getInnerText( cs[i] );
  328. break;
  329. case 3: // TEXT_NODE
  330. str += cs[i].nodeValue;
  331. break;
  332. }
  333. }
  334. return str;
  335. };
  336. window.checkboxes = undefined;
  337. window.lastCheckbox = undefined;
  338. window.setupCheckboxShiftClick = function() {
  339. checkboxes = [];
  340. lastCheckbox = null;
  341. var inputs = document.getElementsByTagName( 'input' );
  342. addCheckboxClickHandlers( inputs );
  343. };
  344. window.addCheckboxClickHandlers = function( inputs, start ) {
  345. if ( !start ) {
  346. start = 0;
  347. }
  348. var finish = start + 250;
  349. if ( finish > inputs.length ) {
  350. finish = inputs.length;
  351. }
  352. for ( var i = start; i < finish; i++ ) {
  353. var cb = inputs[i];
  354. if ( !cb.type || cb.type.toLowerCase() != 'checkbox' || ( ' ' + cb.className + ' ' ).indexOf( ' noshiftselect ' ) != -1 ) {
  355. continue;
  356. }
  357. var end = checkboxes.length;
  358. checkboxes[end] = cb;
  359. cb.index = end;
  360. addClickHandler( cb, checkboxClickHandler );
  361. }
  362. if ( finish < inputs.length ) {
  363. setTimeout( function() {
  364. addCheckboxClickHandlers( inputs, finish );
  365. }, 200 );
  366. }
  367. };
  368. window.checkboxClickHandler = function( e ) {
  369. if ( typeof e == 'undefined' ) {
  370. e = window.event;
  371. }
  372. if ( !e.shiftKey || lastCheckbox === null ) {
  373. lastCheckbox = this.index;
  374. return true;
  375. }
  376. var endState = this.checked;
  377. var start, finish;
  378. if ( this.index < lastCheckbox ) {
  379. start = this.index + 1;
  380. finish = lastCheckbox;
  381. } else {
  382. start = lastCheckbox;
  383. finish = this.index - 1;
  384. }
  385. for ( var i = start; i <= finish; ++i ) {
  386. checkboxes[i].checked = endState;
  387. if( i > start && typeof checkboxes[i].onchange == 'function' ) {
  388. checkboxes[i].onchange(); // fire triggers
  389. }
  390. }
  391. lastCheckbox = this.index;
  392. return true;
  393. };
  394. /*
  395. Written by Jonathan Snook, http://www.snook.ca/jonathan
  396. Add-ons by Robert Nyman, http://www.robertnyman.com
  397. Author says "The credit comment is all it takes, no license. Go crazy with it!:-)"
  398. From http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
  399. */
  400. window.getElementsByClassName = function( oElm, strTagName, oClassNames ) {
  401. var arrReturnElements = [];
  402. if ( typeof( oElm.getElementsByClassName ) == 'function' ) {
  403. /* Use a native implementation where possible FF3, Saf3.2, Opera 9.5 */
  404. var arrNativeReturn = oElm.getElementsByClassName( oClassNames );
  405. if ( strTagName == '*' ) {
  406. return arrNativeReturn;
  407. }
  408. for ( var h = 0; h < arrNativeReturn.length; h++ ) {
  409. if( arrNativeReturn[h].tagName.toLowerCase() == strTagName.toLowerCase() ) {
  410. arrReturnElements[arrReturnElements.length] = arrNativeReturn[h];
  411. }
  412. }
  413. return arrReturnElements;
  414. }
  415. var arrElements = ( strTagName == '*' && oElm.all ) ? oElm.all : oElm.getElementsByTagName( strTagName );
  416. var arrRegExpClassNames = [];
  417. if( typeof oClassNames == 'object' ) {
  418. for( var i = 0; i < oClassNames.length; i++ ) {
  419. arrRegExpClassNames[arrRegExpClassNames.length] =
  420. new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)");
  421. }
  422. } else {
  423. arrRegExpClassNames[arrRegExpClassNames.length] =
  424. new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)");
  425. }
  426. var oElement;
  427. var bMatchesAll;
  428. for( var j = 0; j < arrElements.length; j++ ) {
  429. oElement = arrElements[j];
  430. bMatchesAll = true;
  431. for( var k = 0; k < arrRegExpClassNames.length; k++ ) {
  432. if( !arrRegExpClassNames[k].test( oElement.className ) ) {
  433. bMatchesAll = false;
  434. break;
  435. }
  436. }
  437. if( bMatchesAll ) {
  438. arrReturnElements[arrReturnElements.length] = oElement;
  439. }
  440. }
  441. return ( arrReturnElements );
  442. };
  443. window.redirectToFragment = function( fragment ) {
  444. var match = navigator.userAgent.match(/AppleWebKit\/(\d+)/);
  445. if ( match ) {
  446. var webKitVersion = parseInt( match[1] );
  447. if ( webKitVersion < 420 ) {
  448. // Released Safari w/ WebKit 418.9.1 messes up horribly
  449. // Nightlies of 420+ are ok
  450. return;
  451. }
  452. }
  453. if ( window.location.hash == '' ) {
  454. window.location.hash = fragment;
  455. // Mozilla needs to wait until after load, otherwise the window doesn't
  456. // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
  457. // There's no obvious way to detect this programmatically, so we use
  458. // version-testing. If Firefox fixes the bug, they'll jump twice, but
  459. // better twice than not at all, so make the fix hit future versions as
  460. // well.
  461. if ( is_gecko ) {
  462. addOnloadHook(function() {
  463. if ( window.location.hash == fragment ) {
  464. window.location.hash = fragment;
  465. }
  466. });
  467. }
  468. }
  469. };
  470. /**
  471. * Add a cute little box at the top of the screen to inform the user of
  472. * something, replacing any preexisting message.
  473. *
  474. * @param message String -or- Dom Object HTML to be put inside the right div
  475. * @param className String Used in adding a class; should be different for each
  476. * call to allow CSS/JS to hide different boxes. null = no class used.
  477. * @return Boolean True on success, false on failure
  478. */
  479. window.jsMsg = function( message, className ) {
  480. if ( !document.getElementById ) {
  481. return false;
  482. }
  483. // We special-case skin structures provided by the software. Skins that
  484. // choose to abandon or significantly modify our formatting can just define
  485. // an mw-js-message div to start with.
  486. var messageDiv = document.getElementById( 'mw-js-message' );
  487. if ( !messageDiv ) {
  488. messageDiv = document.createElement( 'div' );
  489. if ( document.getElementById( 'column-content' )
  490. && document.getElementById( 'content' ) ) {
  491. // MonoBook, presumably
  492. document.getElementById( 'content' ).insertBefore(
  493. messageDiv,
  494. document.getElementById( 'content' ).firstChild
  495. );
  496. } else if ( document.getElementById( 'content' )
  497. && document.getElementById( 'article' ) ) {
  498. // Non-Monobook but still recognizable (old-style)
  499. document.getElementById( 'article').insertBefore(
  500. messageDiv,
  501. document.getElementById( 'article' ).firstChild
  502. );
  503. } else {
  504. return false;
  505. }
  506. }
  507. messageDiv.setAttribute( 'id', 'mw-js-message' );
  508. messageDiv.style.display = 'block';
  509. if( className ) {
  510. messageDiv.setAttribute( 'class', 'mw-js-message-' + className );
  511. }
  512. if ( typeof message === 'object' ) {
  513. while ( messageDiv.hasChildNodes() ) { // Remove old content
  514. messageDiv.removeChild( messageDiv.firstChild );
  515. }
  516. messageDiv.appendChild( message ); // Append new content
  517. } else {
  518. messageDiv.innerHTML = message;
  519. }
  520. return true;
  521. };
  522. /**
  523. * Inject a cute little progress spinner after the specified element
  524. *
  525. * @param element Element to inject after
  526. * @param id Identifier string (for use with removeSpinner(), below)
  527. */
  528. window.injectSpinner = function( element, id ) {
  529. var spinner = document.createElement( 'img' );
  530. spinner.id = 'mw-spinner-' + id;
  531. spinner.src = mw.config.get( 'stylepath' ) + '/common/images/spinner.gif';
  532. spinner.alt = spinner.title = '...';
  533. if( element.nextSibling ) {
  534. element.parentNode.insertBefore( spinner, element.nextSibling );
  535. } else {
  536. element.parentNode.appendChild( spinner );
  537. }
  538. };
  539. /**
  540. * Remove a progress spinner added with injectSpinner()
  541. *
  542. * @param id Identifier string
  543. */
  544. window.removeSpinner = function( id ) {
  545. var spinner = document.getElementById( 'mw-spinner-' + id );
  546. if( spinner ) {
  547. spinner.parentNode.removeChild( spinner );
  548. }
  549. };
  550. window.runOnloadHook = function() {
  551. // don't run anything below this for non-dom browsers
  552. if ( doneOnloadHook || !( document.getElementById && document.getElementsByTagName ) ) {
  553. return;
  554. }
  555. // set this before running any hooks, since any errors below
  556. // might cause the function to terminate prematurely
  557. doneOnloadHook = true;
  558. // Run any added-on functions
  559. for ( var i = 0; i < onloadFuncts.length; i++ ) {
  560. onloadFuncts[i]();
  561. }
  562. };
  563. /**
  564. * Add an event handler to an element
  565. *
  566. * @param element Element to add handler to
  567. * @param attach String Event to attach to
  568. * @param handler callable Event handler callback
  569. */
  570. window.addHandler = function( element, attach, handler ) {
  571. if( element.addEventListener ) {
  572. element.addEventListener( attach, handler, false );
  573. } else if( element.attachEvent ) {
  574. element.attachEvent( 'on' + attach, handler );
  575. }
  576. };
  577. window.hookEvent = function( hookName, hookFunct ) {
  578. addHandler( window, hookName, hookFunct );
  579. };
  580. /**
  581. * Add a click event handler to an element
  582. *
  583. * @param element Element to add handler to
  584. * @param handler callable Event handler callback
  585. */
  586. window.addClickHandler = function( element, handler ) {
  587. addHandler( element, 'click', handler );
  588. };
  589. /**
  590. * Removes an event handler from an element
  591. *
  592. * @param element Element to remove handler from
  593. * @param remove String Event to remove
  594. * @param handler callable Event handler callback to remove
  595. */
  596. window.removeHandler = function( element, remove, handler ) {
  597. if( window.removeEventListener ) {
  598. element.removeEventListener( remove, handler, false );
  599. } else if( window.detachEvent ) {
  600. element.detachEvent( 'on' + remove, handler );
  601. }
  602. };
  603. // note: all skins should call runOnloadHook() at the end of html output,
  604. // so the below should be redundant. It's there just in case.
  605. hookEvent( 'load', runOnloadHook );
  606. if ( ie6_bugs ) {
  607. importScriptURI( mw.config.get( 'stylepath' ) + '/common/IEFixes.js' );
  608. }
  609. })();