Index: trunk/phase3/resources/mediawiki/mediawiki.js |
— | — | @@ -2,15 +2,12 @@ |
3 | 3 | * Core MediaWiki JavaScript Library |
4 | 4 | */ |
5 | 5 | |
6 | | -// Attach to window and globally alias |
7 | | -window.mw = window.mediaWiki = new ( function( $, undefined ) { |
| 6 | +var mw = new ( function( $, undefined ) { |
| 7 | +"use strict"; |
8 | 8 | |
9 | 9 | /* Private Members */ |
10 | 10 | |
11 | | - /** |
12 | | - * @var object List of messages that have been requested to be loaded. |
13 | | - */ |
14 | | - var messageQueue = {}; |
| 11 | + var hasOwn = Object.prototype.hasOwnProperty; |
15 | 12 | |
16 | 13 | /* Object constructors */ |
17 | 14 | |
— | — | @@ -25,7 +22,7 @@ |
26 | 23 | * @return Map |
27 | 24 | */ |
28 | 25 | function Map( global ) { |
29 | | - this.values = ( global === true ) ? window : {}; |
| 26 | + this.values = global === true ? window : {}; |
30 | 27 | return this; |
31 | 28 | } |
32 | 29 | |
— | — | @@ -44,10 +41,12 @@ |
45 | 42 | * @return Values as a string or object, null if invalid/inexistant. |
46 | 43 | */ |
47 | 44 | get: function( selection, fallback ) { |
| 45 | + var results, i; |
| 46 | + |
48 | 47 | if ( $.isArray( selection ) ) { |
49 | 48 | selection = $.makeArray( selection ); |
50 | | - var results = {}; |
51 | | - for ( var i = 0; i < selection.length; i++ ) { |
| 49 | + results = {}; |
| 50 | + for ( i = 0; i < selection.length; i += 1 ) { |
52 | 51 | results[selection[i]] = this.get( selection[i], fallback ); |
53 | 52 | } |
54 | 53 | return results; |
— | — | @@ -70,13 +69,15 @@ |
71 | 70 | /** |
72 | 71 | * Sets one or multiple key/value pairs. |
73 | 72 | * |
74 | | - * @param selection mixed String key or array of keys to set values for. |
75 | | - * @param value mixed Value to set (optional, only in use when key is a string) |
76 | | - * @return bool This returns true on success, false on failure. |
| 73 | + * @param selection {mixed} String key or array of keys to set values for. |
| 74 | + * @param value {mixed} Value to set (optional, only in use when key is a string) |
| 75 | + * @return {Boolean} This returns true on success, false on failure. |
77 | 76 | */ |
78 | 77 | set: function( selection, value ) { |
| 78 | + var s; |
| 79 | + |
79 | 80 | if ( $.isPlainObject( selection ) ) { |
80 | | - for ( var s in selection ) { |
| 81 | + for ( s in selection ) { |
81 | 82 | this.values[s] = selection[s]; |
82 | 83 | } |
83 | 84 | return true; |
— | — | @@ -90,19 +91,21 @@ |
91 | 92 | /** |
92 | 93 | * Checks if one or multiple keys exist. |
93 | 94 | * |
94 | | - * @param selection mixed String key or array of keys to check |
95 | | - * @return boolean Existence of key(s) |
| 95 | + * @param selection {mixed} String key or array of keys to check |
| 96 | + * @return {Boolean} Existence of key(s) |
96 | 97 | */ |
97 | 98 | exists: function( selection ) { |
98 | | - if ( typeof selection === 'object' ) { |
99 | | - for ( var s = 0; s < selection.length; s++ ) { |
100 | | - if ( !( selection[s] in this.values ) ) { |
| 99 | + var s; |
| 100 | + |
| 101 | + if ( $.isArray( selection ) ) { |
| 102 | + for ( s = 0; s < selection.length; s += 1 ) { |
| 103 | + if ( this.values[selection[s]] === undefined ) { |
101 | 104 | return false; |
102 | 105 | } |
103 | 106 | } |
104 | 107 | return true; |
105 | 108 | } else { |
106 | | - return selection in this.values; |
| 109 | + return this.values[selection] !== undefined; |
107 | 110 | } |
108 | 111 | } |
109 | 112 | }; |
— | — | @@ -134,7 +137,8 @@ |
135 | 138 | * @return Message |
136 | 139 | */ |
137 | 140 | params: function( parameters ) { |
138 | | - for ( var i = 0; i < parameters.length; i++ ) { |
| 141 | + var i; |
| 142 | + for ( i = 0; i < parameters.length; i += 1 ) { |
139 | 143 | this.parameters.push( parameters[i] ); |
140 | 144 | } |
141 | 145 | return this; |
— | — | @@ -146,7 +150,6 @@ |
147 | 151 | * @return string Message as a string in the current form or <key> if key does not exist. |
148 | 152 | */ |
149 | 153 | toString: function() { |
150 | | - |
151 | 154 | if ( !this.map.exists( this.key ) ) { |
152 | 155 | // Use <key> as text if key does not exist |
153 | 156 | if ( this.format !== 'plain' ) { |
— | — | @@ -158,9 +161,9 @@ |
159 | 162 | var text = this.map.get( this.key ), |
160 | 163 | parameters = this.parameters; |
161 | 164 | |
162 | | - text = text.replace( /\$(\d+)/g, function( string, match ) { |
| 165 | + text = text.replace( /\$(\d+)/g, function( str, match ) { |
163 | 166 | var index = parseInt( match, 10 ) - 1; |
164 | | - return index in parameters ? parameters[index] : '$' + match; |
| 167 | + return parameters[index] !== undefined ? parameters[index] : '$' + match; |
165 | 168 | } ); |
166 | 169 | |
167 | 170 | if ( this.format === 'plain' ) { |
— | — | @@ -361,10 +364,11 @@ |
362 | 365 | } |
363 | 366 | |
364 | 367 | function compare( a, b ) { |
| 368 | + var i; |
365 | 369 | if ( a.length !== b.length ) { |
366 | 370 | return false; |
367 | 371 | } |
368 | | - for ( var i = 0; i < b.length; i++ ) { |
| 372 | + for ( i = 0; i < b.length; i += 1 ) { |
369 | 373 | if ( $.isArray( a[i] ) ) { |
370 | 374 | if ( !compare( a[i], b[i] ) ) { |
371 | 375 | return false; |
— | — | @@ -396,6 +400,8 @@ |
397 | 401 | * Recursively resolves dependencies and detects circular references |
398 | 402 | */ |
399 | 403 | function recurse( module, resolved, unresolved ) { |
| 404 | + var n, deps, len; |
| 405 | + |
400 | 406 | if ( registry[module] === undefined ) { |
401 | 407 | throw new Error( 'Unknown dependency: ' + module ); |
402 | 408 | } |
— | — | @@ -408,15 +414,17 @@ |
409 | 415 | } |
410 | 416 | } |
411 | 417 | // Tracks down dependencies |
412 | | - for ( var n = 0; n < registry[module].dependencies.length; n++ ) { |
413 | | - if ( $.inArray( registry[module].dependencies[n], resolved ) === -1 ) { |
414 | | - if ( $.inArray( registry[module].dependencies[n], unresolved ) !== -1 ) { |
| 418 | + deps = registry[module].dependencies; |
| 419 | + len = deps.length; |
| 420 | + for ( n = 0; n < len; n += 1 ) { |
| 421 | + if ( $.inArray( deps[n], resolved ) === -1 ) { |
| 422 | + if ( $.inArray( deps[n], unresolved ) !== -1 ) { |
415 | 423 | throw new Error( |
416 | 424 | 'Circular reference detected: ' + module + |
417 | | - ' -> ' + registry[module].dependencies[n] |
| 425 | + ' -> ' + deps[n] |
418 | 426 | ); |
419 | 427 | } |
420 | | - recurse( registry[module].dependencies[n], resolved, unresolved ); |
| 428 | + recurse( deps[n], resolved, unresolved ); |
421 | 429 | } |
422 | 430 | } |
423 | 431 | resolved[resolved.length] = module; |
— | — | @@ -431,22 +439,24 @@ |
432 | 440 | * @throws Error if circular reference is detected |
433 | 441 | */ |
434 | 442 | function resolve( module ) { |
| 443 | + var modules, m, deps, n, resolved; |
| 444 | + |
435 | 445 | // Allow calling with an array of module names |
436 | | - if ( typeof module === 'object' ) { |
437 | | - var modules = []; |
438 | | - for ( var m = 0; m < module.length; m++ ) { |
439 | | - var dependencies = resolve( module[m] ); |
440 | | - for ( var n = 0; n < dependencies.length; n++ ) { |
441 | | - modules[modules.length] = dependencies[n]; |
| 446 | + if ( $.isArray( module ) ) { |
| 447 | + modules = []; |
| 448 | + for ( m = 0; m < module.length; m += 1 ) { |
| 449 | + deps = resolve( module[m] ); |
| 450 | + for ( n = 0; n < deps.length; n += 1 ) { |
| 451 | + modules[modules.length] = deps[n]; |
442 | 452 | } |
443 | 453 | } |
444 | 454 | return modules; |
445 | 455 | } else if ( typeof module === 'string' ) { |
446 | 456 | // Undefined modules have no dependencies |
447 | | - if ( !( module in registry ) ) { |
| 457 | + if ( registry[module] === undefined ) { |
448 | 458 | return []; |
449 | 459 | } |
450 | | - var resolved = []; |
| 460 | + resolved = []; |
451 | 461 | recurse( module, resolved, [] ); |
452 | 462 | return resolved; |
453 | 463 | } |
— | — | @@ -464,12 +474,14 @@ |
465 | 475 | * @return array list of filtered module names |
466 | 476 | */ |
467 | 477 | function filter( states, modules ) { |
| 478 | + var list, module, s, m; |
| 479 | + |
468 | 480 | // Allow states to be given as a string |
469 | 481 | if ( typeof states === 'string' ) { |
470 | 482 | states = [states]; |
471 | 483 | } |
472 | 484 | // If called without a list of modules, build and use a list of all modules |
473 | | - var list = [], module; |
| 485 | + list = []; |
474 | 486 | if ( modules === undefined ) { |
475 | 487 | modules = []; |
476 | 488 | for ( module in registry ) { |
— | — | @@ -477,8 +489,8 @@ |
478 | 490 | } |
479 | 491 | } |
480 | 492 | // Build a list of modules which are in one of the specified states |
481 | | - for ( var s = 0; s < states.length; s++ ) { |
482 | | - for ( var m = 0; m < modules.length; m++ ) { |
| 493 | + for ( s = 0; s < states.length; s += 1 ) { |
| 494 | + for ( m = 0; m < modules.length; m += 1 ) { |
483 | 495 | if ( registry[modules[m]] === undefined ) { |
484 | 496 | // Module does not exist |
485 | 497 | if ( states[s] === 'undefined' ) { |
— | — | @@ -503,6 +515,8 @@ |
504 | 516 | * @param module string module name to execute |
505 | 517 | */ |
506 | 518 | function execute( module, callback ) { |
| 519 | + var style, media, i, script, markModuleReady, nestedAddScript; |
| 520 | + |
507 | 521 | if ( registry[module] === undefined ) { |
508 | 522 | throw new Error( 'Module has not been registered yet: ' + module ); |
509 | 523 | } else if ( registry[module].state === 'registered' ) { |
— | — | @@ -512,13 +526,13 @@ |
513 | 527 | } else if ( registry[module].state === 'ready' ) { |
514 | 528 | throw new Error( 'Module has already been loaded: ' + module ); |
515 | 529 | } |
| 530 | + |
516 | 531 | // Add styles |
517 | | - var style; |
518 | 532 | if ( $.isPlainObject( registry[module].style ) ) { |
519 | | - for ( var media in registry[module].style ) { |
| 533 | + for ( media in registry[module].style ) { |
520 | 534 | style = registry[module].style[media]; |
521 | 535 | if ( $.isArray( style ) ) { |
522 | | - for ( var i = 0; i < style.length; i++ ) { |
| 536 | + for ( i = 0; i < style.length; i += 1 ) { |
523 | 537 | getMarker().before( mw.html.element( 'link', { |
524 | 538 | 'type': 'text/css', |
525 | 539 | 'media': media, |
— | — | @@ -540,27 +554,27 @@ |
541 | 555 | } |
542 | 556 | // Execute script |
543 | 557 | try { |
544 | | - var script = registry[module].script, |
545 | | - markModuleReady = function() { |
546 | | - registry[module].state = 'ready'; |
547 | | - handlePending( module ); |
548 | | - if ( $.isFunction( callback ) ) { |
549 | | - callback(); |
550 | | - } |
551 | | - }, |
552 | | - nestedAddScript = function( arr, callback, i ) { |
553 | | - // Recursively call addScript() in its own callback |
554 | | - // for each element of arr. |
555 | | - if ( i >= arr.length ) { |
556 | | - // We're at the end of the array |
557 | | - callback(); |
558 | | - return; |
559 | | - } |
| 558 | + script = registry[module].script; |
| 559 | + markModuleReady = function() { |
| 560 | + registry[module].state = 'ready'; |
| 561 | + handlePending( module ); |
| 562 | + if ( $.isFunction( callback ) ) { |
| 563 | + callback(); |
| 564 | + } |
| 565 | + }; |
| 566 | + nestedAddScript = function( arr, callback, i ) { |
| 567 | + // Recursively call addScript() in its own callback |
| 568 | + // for each element of arr. |
| 569 | + if ( i >= arr.length ) { |
| 570 | + // We're at the end of the array |
| 571 | + callback(); |
| 572 | + return; |
| 573 | + } |
560 | 574 | |
561 | | - addScript( arr[i], function() { |
562 | | - nestedAddScript( arr, callback, i + 1 ); |
563 | | - } ); |
564 | | - }; |
| 575 | + addScript( arr[i], function() { |
| 576 | + nestedAddScript( arr, callback, i + 1 ); |
| 577 | + } ); |
| 578 | + }; |
565 | 579 | |
566 | 580 | if ( $.isArray( script ) ) { |
567 | 581 | registry[module].state = 'loading'; |
— | — | @@ -586,9 +600,11 @@ |
587 | 601 | * This is used when dependencies are satisfied, such as when a module is executed. |
588 | 602 | */ |
589 | 603 | function handlePending( module ) { |
| 604 | + var j, r; |
| 605 | + |
590 | 606 | try { |
591 | 607 | // Run jobs who's dependencies have just been met |
592 | | - for ( var j = 0; j < jobs.length; j++ ) { |
| 608 | + for ( j = 0; j < jobs.length; j += 1 ) { |
593 | 609 | if ( compare( |
594 | 610 | filter( 'ready', jobs[j].dependencies ), |
595 | 611 | jobs[j].dependencies ) ) |
— | — | @@ -597,11 +613,11 @@ |
598 | 614 | jobs[j].ready(); |
599 | 615 | } |
600 | 616 | jobs.splice( j, 1 ); |
601 | | - j--; |
| 617 | + j -= 1; |
602 | 618 | } |
603 | 619 | } |
604 | 620 | // Execute modules who's dependencies have just been met |
605 | | - for ( var r in registry ) { |
| 621 | + for ( r in registry ) { |
606 | 622 | if ( registry[r].state === 'loaded' ) { |
607 | 623 | if ( compare( |
608 | 624 | filter( ['ready'], registry[r].dependencies ), |
— | — | @@ -613,13 +629,13 @@ |
614 | 630 | } |
615 | 631 | } catch ( e ) { |
616 | 632 | // Run error callbacks of jobs affected by this condition |
617 | | - for ( var j = 0; j < jobs.length; j++ ) { |
| 633 | + for ( j = 0; j < jobs.length; j += 1 ) { |
618 | 634 | if ( $.inArray( module, jobs[j].dependencies ) !== -1 ) { |
619 | 635 | if ( $.isFunction( jobs[j].error ) ) { |
620 | 636 | jobs[j].error( e, module ); |
621 | 637 | } |
622 | 638 | jobs.splice( j, 1 ); |
623 | | - j--; |
| 639 | + j -= 1; |
624 | 640 | } |
625 | 641 | } |
626 | 642 | } |
— | — | @@ -634,15 +650,17 @@ |
635 | 651 | * @param error function callback to execute when any dependency fails |
636 | 652 | */ |
637 | 653 | function request( dependencies, ready, error ) { |
| 654 | + var regItemDeps, regItemDepLen, n; |
| 655 | + |
638 | 656 | // Allow calling by single module name |
639 | 657 | if ( typeof dependencies === 'string' ) { |
640 | 658 | dependencies = [dependencies]; |
641 | | - if ( dependencies[0] in registry ) { |
| 659 | + if ( registry[dependencies[0]] !== undefined ) { |
642 | 660 | // Cache repetitively accessed deep level object member |
643 | | - var regItemDeps = registry[dependencies[0]].dependencies, |
644 | | - // Cache to avoid looped access to length property |
645 | | - regItemDepLen = regItemDeps.length; |
646 | | - for ( var n = 0; n < regItemDepLen; n++ ) { |
| 661 | + regItemDeps = registry[dependencies[0]].dependencies; |
| 662 | + // Cache to avoid looped access to length property |
| 663 | + regItemDepLen = regItemDeps.length; |
| 664 | + for ( n = 0; n < regItemDepLen; n += 1 ) { |
647 | 665 | dependencies[dependencies.length] = regItemDeps[n]; |
648 | 666 | } |
649 | 667 | } |
— | — | @@ -660,7 +678,7 @@ |
661 | 679 | } |
662 | 680 | // Queue up any dependencies that are undefined or registered |
663 | 681 | dependencies = filter( ['undefined', 'registered'], dependencies ); |
664 | | - for ( var n = 0; n < dependencies.length; n++ ) { |
| 682 | + for ( n = 0; n < dependencies.length; n += 1 ) { |
665 | 683 | if ( $.inArray( dependencies[n], queue ) === -1 ) { |
666 | 684 | queue[queue.length] = dependencies[n]; |
667 | 685 | } |
— | — | @@ -672,12 +690,12 @@ |
673 | 691 | function sortQuery(o) { |
674 | 692 | var sorted = {}, key, a = []; |
675 | 693 | for ( key in o ) { |
676 | | - if ( o.hasOwnProperty( key ) ) { |
| 694 | + if ( hasOwn.call( o, key ) ) { |
677 | 695 | a.push( key ); |
678 | 696 | } |
679 | 697 | } |
680 | 698 | a.sort(); |
681 | | - for ( key = 0; key < a.length; key++ ) { |
| 699 | + for ( key = 0; key < a.length; key += 1 ) { |
682 | 700 | sorted[a[key]] = o[a[key]]; |
683 | 701 | } |
684 | 702 | return sorted; |
— | — | @@ -688,8 +706,8 @@ |
689 | 707 | * to a query string of the form foo.bar,baz|bar.baz,quux |
690 | 708 | */ |
691 | 709 | function buildModulesString( moduleMap ) { |
692 | | - var arr = [], p; |
693 | | - for ( var prefix in moduleMap ) { |
| 710 | + var arr = [], p, prefix; |
| 711 | + for ( prefix in moduleMap ) { |
694 | 712 | p = prefix === '' ? '' : prefix + '.'; |
695 | 713 | arr.push( p + moduleMap[prefix].join( ',' ) ); |
696 | 714 | } |
— | — | @@ -713,7 +731,7 @@ |
714 | 732 | script.setAttribute( 'src', src ); |
715 | 733 | script.setAttribute( 'type', 'text/javascript' ); |
716 | 734 | if ( $.isFunction( callback ) ) { |
717 | | - // Attach handlers for all browsers -- this is based on jQuery.ajax |
| 735 | + // Attach handlers for all browsers (based on jQuery.ajax) |
718 | 736 | script.onload = script.onreadystatechange = function() { |
719 | 737 | |
720 | 738 | if ( |
— | — | @@ -776,25 +794,30 @@ |
777 | 795 | * Requests dependencies from server, loading and executing when things when ready. |
778 | 796 | */ |
779 | 797 | this.work = function() { |
780 | | - // Build a list of request parameters common to all requests. |
781 | | - var reqBase = { |
782 | | - skin: mw.config.get( 'skin' ), |
783 | | - lang: mw.config.get( 'wgUserLanguage' ), |
784 | | - debug: mw.config.get( 'debug' ) |
785 | | - }, |
786 | | - // Split module batch by source and by group. |
787 | | - splits = {}, |
788 | | - maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 ); |
| 798 | + var reqBase, splits, maxQueryLength, q, b, bSource, bGroup, bSourceGroup, |
| 799 | + source, group, g, i, modules, maxVersion, sourceLoadScript, |
| 800 | + currReqBase, currReqBaseLength, moduleMap, l, |
| 801 | + lastDotIndex, prefix, suffix, bytesAdded; |
789 | 802 | |
| 803 | + // Build a list of request parameters common to all requests. |
| 804 | + reqBase = { |
| 805 | + skin: mw.config.get( 'skin' ), |
| 806 | + lang: mw.config.get( 'wgUserLanguage' ), |
| 807 | + debug: mw.config.get( 'debug' ) |
| 808 | + }; |
| 809 | + // Split module batch by source and by group. |
| 810 | + splits = {}; |
| 811 | + maxQueryLength = mw.config.get( 'wgResourceLoaderMaxQueryLength', -1 ); |
| 812 | + |
790 | 813 | // Appends a list of modules from the queue to the batch |
791 | | - for ( var q = 0; q < queue.length; q++ ) { |
| 814 | + for ( q = 0; q < queue.length; q += 1 ) { |
792 | 815 | // Only request modules which are undefined or registered |
793 | | - if ( !( queue[q] in registry ) || registry[queue[q]].state === 'registered' ) { |
| 816 | + if ( registry[queue[q]] === undefined || registry[queue[q]].state === 'registered' ) { |
794 | 817 | // Prevent duplicate entries |
795 | 818 | if ( $.inArray( queue[q], batch ) === -1 ) { |
796 | 819 | batch[batch.length] = queue[q]; |
797 | 820 | // Mark registered modules as loading |
798 | | - if ( queue[q] in registry ) { |
| 821 | + if ( registry[queue[q]] !== undefined ) { |
799 | 822 | registry[queue[q]].state = 'loading'; |
800 | 823 | } |
801 | 824 | } |
— | — | @@ -813,16 +836,16 @@ |
814 | 837 | batch.sort(); |
815 | 838 | |
816 | 839 | // Split batch by source and by group. |
817 | | - for ( var b = 0; b < batch.length; b++ ) { |
818 | | - var bSource = registry[batch[b]].source, |
819 | | - bGroup = registry[batch[b]].group; |
820 | | - if ( !( bSource in splits ) ) { |
| 840 | + for ( b = 0; b < batch.length; b += 1 ) { |
| 841 | + bSource = registry[batch[b]].source; |
| 842 | + bGroup = registry[batch[b]].group; |
| 843 | + if ( splits[bSource] === undefined ) { |
821 | 844 | splits[bSource] = {}; |
822 | 845 | } |
823 | | - if ( !( bGroup in splits[bSource] ) ) { |
| 846 | + if ( splits[bSource][bGroup] === undefined ) { |
824 | 847 | splits[bSource][bGroup] = []; |
825 | 848 | } |
826 | | - var bSourceGroup = splits[bSource][bGroup]; |
| 849 | + bSourceGroup = splits[bSource][bGroup]; |
827 | 850 | bSourceGroup[bSourceGroup.length] = batch[b]; |
828 | 851 | } |
829 | 852 | |
— | — | @@ -833,8 +856,6 @@ |
834 | 857 | // include modules which are already loaded. |
835 | 858 | batch = []; |
836 | 859 | |
837 | | - var source, group, modules, maxVersion, sourceLoadScript; |
838 | | - |
839 | 860 | for ( source in splits ) { |
840 | 861 | |
841 | 862 | sourceLoadScript = sources[source].loadScript; |
— | — | @@ -847,30 +868,30 @@ |
848 | 869 | |
849 | 870 | // Calculate the highest timestamp |
850 | 871 | maxVersion = 0; |
851 | | - for ( var g = 0; g < modules.length; g++ ) { |
| 872 | + for ( g = 0; g < modules.length; g += 1 ) { |
852 | 873 | if ( registry[modules[g]].version > maxVersion ) { |
853 | 874 | maxVersion = registry[modules[g]].version; |
854 | 875 | } |
855 | 876 | } |
856 | 877 | |
857 | | - var currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase ), |
858 | | - currReqBaseLength = $.param( currReqBase ).length, |
859 | | - moduleMap = {}, |
860 | | - // We may need to split up the request to honor the query string length limit, |
861 | | - // so build it piece by piece. |
862 | | - l = currReqBaseLength + 9; // '&modules='.length == 9 |
| 878 | + currReqBase = $.extend( { 'version': formatVersionNumber( maxVersion ) }, reqBase ); |
| 879 | + currReqBaseLength = $.param( currReqBase ).length; |
| 880 | + moduleMap = {}; |
| 881 | + // We may need to split up the request to honor the query string length limit, |
| 882 | + // so build it piece by piece. |
| 883 | + l = currReqBaseLength + 9; // '&modules='.length == 9 |
863 | 884 | |
864 | 885 | moduleMap = {}; // { prefix: [ suffixes ] } |
865 | 886 | |
866 | | - for ( var i = 0; i < modules.length; i++ ) { |
867 | | - // Determine how many bytes this module would add to the query string |
868 | | - var lastDotIndex = modules[i].lastIndexOf( '.' ), |
869 | | - // Note that these substr() calls work even if lastDotIndex == -1 |
870 | | - prefix = modules[i].substr( 0, lastDotIndex ), |
871 | | - suffix = modules[i].substr( lastDotIndex + 1 ), |
872 | | - bytesAdded = prefix in moduleMap |
873 | | - ? suffix.length + 3 // '%2C'.length == 3 |
874 | | - : modules[i].length + 3; // '%7C'.length == 3 |
| 887 | + for ( i = 0; i < modules.length; i += 1 ) { |
| 888 | + // Determine how many bytes this module would add to the query string |
| 889 | + lastDotIndex = modules[i].lastIndexOf( '.' ); |
| 890 | + // Note that these substr() calls work even if lastDotIndex == -1 |
| 891 | + prefix = modules[i].substr( 0, lastDotIndex ); |
| 892 | + suffix = modules[i].substr( lastDotIndex + 1 ); |
| 893 | + bytesAdded = moduleMap[prefix] !== undefined |
| 894 | + ? suffix.length + 3 // '%2C'.length == 3 |
| 895 | + : modules[i].length + 3; // '%7C'.length == 3 |
875 | 896 | |
876 | 897 | // If the request would become too long, create a new one, |
877 | 898 | // but don't create empty requests |
— | — | @@ -881,7 +902,7 @@ |
882 | 903 | moduleMap = {}; |
883 | 904 | l = currReqBaseLength + 9; |
884 | 905 | } |
885 | | - if ( !( prefix in moduleMap ) ) { |
| 906 | + if ( moduleMap[prefix] === undefined ) { |
886 | 907 | moduleMap[prefix] = []; |
887 | 908 | } |
888 | 909 | moduleMap[prefix].push( suffix ); |
— | — | @@ -904,9 +925,10 @@ |
905 | 926 | * @return {Boolean} |
906 | 927 | */ |
907 | 928 | this.addSource = function( id, props ) { |
| 929 | + var source; |
908 | 930 | // Allow multiple additions |
909 | 931 | if ( typeof id === 'object' ) { |
910 | | - for ( var source in id ) { |
| 932 | + for ( source in id ) { |
911 | 933 | mw.loader.addSource( source, id[source] ); |
912 | 934 | } |
913 | 935 | return true; |
— | — | @@ -933,9 +955,10 @@ |
934 | 956 | * @param source {String}: Name of the source. Defaults to local. |
935 | 957 | */ |
936 | 958 | this.register = function( module, version, dependencies, group, source ) { |
| 959 | + var m; |
937 | 960 | // Allow multiple registration |
938 | 961 | if ( typeof module === 'object' ) { |
939 | | - for ( var m = 0; m < module.length; m++ ) { |
| 962 | + for ( m = 0; m < module.length; m += 1 ) { |
940 | 963 | // module is an array of module names |
941 | 964 | if ( typeof module[m] === 'string' ) { |
942 | 965 | mw.loader.register( module[m] ); |
— | — | @@ -1027,10 +1050,10 @@ |
1028 | 1051 | /** |
1029 | 1052 | * Executes a function as soon as one or more required modules are ready |
1030 | 1053 | * |
1031 | | - * @param dependencies string or array of strings of modules names the callback |
1032 | | - * dependencies to be ready before executing |
1033 | | - * @param ready function callback to execute when all dependencies are ready (optional) |
1034 | | - * @param error function callback to execute when if dependencies have a errors (optional) |
| 1054 | + * @param dependencies {String|Array} Module name or array of modules names the callback |
| 1055 | + * dependends on to be ready before executing |
| 1056 | + * @param ready {Function} callback to execute when all dependencies are ready (optional) |
| 1057 | + * @param error {Function} callback to execute when if dependencies have a errors (optional) |
1035 | 1058 | */ |
1036 | 1059 | this.using = function( dependencies, ready, error ) { |
1037 | 1060 | var tod = typeof dependencies; |
— | — | @@ -1066,9 +1089,9 @@ |
1067 | 1090 | /** |
1068 | 1091 | * Loads an external script or one or more modules for future use |
1069 | 1092 | * |
1070 | | - * @param modules mixed either the name of a module, array of modules, |
| 1093 | + * @param modules {mixed} Either the name of a module, array of modules, |
1071 | 1094 | * or a URL of an external script or style |
1072 | | - * @param type string mime-type to use if calling with a URL of an |
| 1095 | + * @param type {String} mime-type to use if calling with a URL of an |
1073 | 1096 | * external script or style; acceptable values are "text/css" and |
1074 | 1097 | * "text/javascript"; if no type is provided, text/javascript is assumed. |
1075 | 1098 | */ |
— | — | @@ -1118,17 +1141,18 @@ |
1119 | 1142 | /** |
1120 | 1143 | * Changes the state of a module |
1121 | 1144 | * |
1122 | | - * @param module string module name or object of module name/state pairs |
1123 | | - * @param state string state name |
| 1145 | + * @param module {String|Object} module name or object of module name/state pairs |
| 1146 | + * @param state {String} state name |
1124 | 1147 | */ |
1125 | 1148 | this.state = function( module, state ) { |
| 1149 | + var m; |
1126 | 1150 | if ( typeof module === 'object' ) { |
1127 | | - for ( var m in module ) { |
| 1151 | + for ( m in module ) { |
1128 | 1152 | mw.loader.state( m, module[m] ); |
1129 | 1153 | } |
1130 | 1154 | return; |
1131 | 1155 | } |
1132 | | - if ( !( module in registry ) ) { |
| 1156 | + if ( registry[module] === undefined ) { |
1133 | 1157 | mw.loader.register( module ); |
1134 | 1158 | } |
1135 | 1159 | registry[module].state = state; |
— | — | @@ -1140,14 +1164,14 @@ |
1141 | 1165 | * @param module string name of module to get version for |
1142 | 1166 | */ |
1143 | 1167 | this.getVersion = function( module ) { |
1144 | | - if ( module in registry && 'version' in registry[module] ) { |
| 1168 | + if ( registry[module] !== undefined && registry[module].version !== undefined ) { |
1145 | 1169 | return formatVersionNumber( registry[module].version ); |
1146 | 1170 | } |
1147 | 1171 | return null; |
1148 | 1172 | }; |
1149 | 1173 | |
1150 | 1174 | /** |
1151 | | - * @deprecated use mw.loader.getVersion() instead |
| 1175 | + * @deprecated since 1.18 use mw.loader.getVersion() instead |
1152 | 1176 | */ |
1153 | 1177 | this.version = function() { |
1154 | 1178 | return mw.loader.getVersion.apply( mw.loader, arguments ); |
— | — | @@ -1159,7 +1183,7 @@ |
1160 | 1184 | * @param module string name of module to get state for |
1161 | 1185 | */ |
1162 | 1186 | this.getState = function( module ) { |
1163 | | - if ( module in registry && 'state' in registry[module] ) { |
| 1187 | + if ( registry[module] !== undefined && registry[module].state !== undefined ) { |
1164 | 1188 | return registry[module].state; |
1165 | 1189 | } |
1166 | 1190 | return null; |
— | — | @@ -1171,10 +1195,9 @@ |
1172 | 1196 | * @return {Array} |
1173 | 1197 | */ |
1174 | 1198 | this.getModuleNames = function() { |
1175 | | - var names = $.map( registry, function( i, key ) { |
| 1199 | + return $.map( registry, function( i, key ) { |
1176 | 1200 | return key; |
1177 | 1201 | } ); |
1178 | | - return names; |
1179 | 1202 | }; |
1180 | 1203 | |
1181 | 1204 | /** |
— | — | @@ -1246,8 +1269,9 @@ |
1247 | 1270 | * Returns <div><img src="<"/></div> |
1248 | 1271 | */ |
1249 | 1272 | this.element = function( name, attrs, contents ) { |
1250 | | - var v, s = '<' + name; |
1251 | | - for ( var attrName in attrs ) { |
| 1273 | + var v, attrName, s = '<' + name; |
| 1274 | + |
| 1275 | + for ( attrName in attrs ) { |
1252 | 1276 | v = attrs[attrName]; |
1253 | 1277 | // Convert name=true, to name=name |
1254 | 1278 | if ( v === true ) { |
— | — | @@ -1256,7 +1280,7 @@ |
1257 | 1281 | } else if ( v === false ) { |
1258 | 1282 | continue; |
1259 | 1283 | } |
1260 | | - s += ' ' + attrName + '="' + this.escape( '' + v ) + '"'; |
| 1284 | + s += ' ' + attrName + '="' + this.escape( String( v ) ) + '"'; |
1261 | 1285 | } |
1262 | 1286 | if ( contents === undefined || contents === null ) { |
1263 | 1287 | // Self close tag |
— | — | @@ -1273,7 +1297,7 @@ |
1274 | 1298 | case 'number': |
1275 | 1299 | case 'boolean': |
1276 | 1300 | // Convert to string |
1277 | | - s += '' + contents; |
| 1301 | + s += String( contents ); |
1278 | 1302 | break; |
1279 | 1303 | default: |
1280 | 1304 | if ( contents instanceof this.Raw ) { |
— | — | @@ -1303,8 +1327,11 @@ |
1304 | 1328 | // Alias $j to jQuery for backwards compatibility |
1305 | 1329 | window.$j = jQuery; |
1306 | 1330 | |
| 1331 | +// Attach to window and globally alias |
| 1332 | +window.mw = window.mediaWiki = mw; |
| 1333 | + |
1307 | 1334 | // Auto-register from pre-loaded startup scripts |
1308 | 1335 | if ( typeof startUp !== 'undefined' && jQuery.isFunction( startUp ) ) { |
1309 | 1336 | startUp(); |
1310 | | - delete startUp; |
| 1337 | + startUp = undefined; |
1311 | 1338 | } |