forked from kaltura/mwEmbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwEmbed.js
3339 lines (3036 loc) · 94.8 KB
/
mwEmbed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Add support for html5 / mwEmbed elements to browsers that do not support the elements natively
// For discussion and comments, see: http://ejohn.org/blog/html5-shiv/
'video audio source track'.replace(/\w+/g, function(n){ document.createElement(n); });
/**
* @license mwEmbed Dual licensed under the MIT or GPL Version 2 licenses.
*
* @copyright (C) 2010 Kaltura
* @author Michael Dale ( michael.dale at kaltura.com )
*
* @url http://www.kaltura.org/project/HTML5_Video_Media_JavaScript_Library
*
* Libraries used include code license in headers
*/
/**
* Setup the "mw" global:
*/
if ( typeof window.mw == 'undefined' ) {
window.mw = { };
}
// set up mediaWiki pointer ( to help transition to new resource loader )
window.mediaWiki = window.mw;
/**
* Set the mwEmbedVersion ( set by default settings can be removed once we use the new RL )
*/
var MW_EMBED_VERSION = '1.6';
// Globals to pre-set ready functions in dynamic loading of mwEmbed
if( typeof window.preMwEmbedReady == 'undefined'){
window.preMwEmbedReady = [];
}
// Globals to pre-set config values in dynamic loading of mwEmbed
if( typeof window.preMwEmbedConfig == 'undefined') {
window.preMwEmbedConfig = {};
}
/**
* The global mw object:
*/
( function( mw ) {
// Use strict ECMAScript 5
"use strict";
// The version of mwEmbed
mw.version = MW_EMBED_VERSION;
// List valid skins here:
mw.validSkins = [ 'mvpcf', 'kskin' ];
// Storage variable for loaded style sheet keys
if( ! mw.style ){
mw.style = { };
}
/**
* Configuration System:
*/
// Local scope configuration var:
if( !mwConfig ){
var mwConfig = { };
}
if( !mwNonDefaultConfigList ){
var mwNonDefaultConfigList = [];
}
// mw scope mwUserConfig var. Stores user configuration
var mwUserConfig = { };
/**
* Setter for configuration values
*
* @param [Mixed]
* name Name of configuration value {Object} Will iderate through
* each key and call setConfig {String} Will set configuration by
* string name to value
* @param {String}
* value Value of configuration name {Object} value Set of values
* to be merged
*/
mw.setConfig = function ( name, value ) {
if( typeof name == 'object' ) {
for( var i in name ) {
mw.setConfig( i, name[ i ] );
}
return ;
}
mwConfig[ name ] = value;
mwNonDefaultConfigList.push( name );
};
// Apply any pre-setup config:
mw.setConfig( window.preMwEmbedConfig );
/**
* Merge in a configuration value:
*/
mw.mergeConfig = function( name, value ){
if( typeof name == 'object' ) {
$j.each( name, function( inx, val) {
mw.setConfig( inx, val );
});
return ;
}
// Check if we should "merge" the config
if( typeof value == 'object' && typeof mwConfig[ name ] == 'object' ) {
if ( value.constructor.toString().indexOf("Array") != -1 &&
mwConfig[ name ].constructor.toString().indexOf("Array") != -1 ){
// merge in the array
mwConfig[ name ] = $j.merge( mwConfig[ name ], value );
} else {
for( var i in value ){
mwConfig[ name ][ i ] = value[ i ];
}
}
return ;
}
// Else do a normal setConfig
mwConfig[ name ] = value;
mwNonDefaultConfigList.push( name );
};
/**
* Set a default config value Will only update configuration if no value is
* present
*
* @param [Mixed]
* value Set configuration name to value {Object} Will idorate
* through each key and call setDefaultConfig {String} Will set
* configuration by string name to value
*/
mw.setDefaultConfig = function( name, value ) {
if( typeof name == 'object' ) {
for( var i in name ) {
mw.setDefaultConfig( i, name[ i ] );
}
return ;
}
if( typeof mwConfig[ name ] == 'undefined' ) {
mwConfig[ name ] = value;
return ;
}
};
/**
* Simple inheritance. We will move to something like
* http://javascriptmvc.com/docs.html#&who=jQuery.Class
* in the near future. This is just a stop gap.
*/
mw.inherit = function( _this, inhertParent ){
for ( var method in inhertParent ) {
if ( _this[ method ] ) {
_this['parent_' + method] = inhertParent[method];
} else {
_this[ method ] = inhertParent[method];
}
}
};
/**
* Getter for configuration values
*
* @param {String}
* name of configuration value to get
* @return {Mixed} value of configuration key returns "false" if key not
* found
*/
mw.getConfig = function ( name ) {
if( ! name )
return mwConfig;
if( typeof mwConfig[ name ] != 'undefined' ){
return mwConfig[ name ];
}
return null;
};
/**
* Get all the non-default configuration ( useful for passing state to
* iframes in limited hash url length of a few K )
*/
mw.getNonDefaultConfigObject = function(){
var nonDefaultConfig = {};
for( var i =0 ; i < mwNonDefaultConfigList.length; i ++){
var configKey = mwNonDefaultConfigList[i];
nonDefaultConfig[ configKey ] = mw.getConfig( configKey );
}
return nonDefaultConfig;
};
/**
* Loads the mwUserConfig from a cookie.
*
* Modules that want to use "User Config" should call this setup function in
* their moduleLoader code.
*
* For performance interfaces using "user config" should load '$j.cookie' &
* 'JSON' in their module loader
*
* By abstracting user preference we could eventually integrate a persistent
* per-account preference system on the server.
*
* @parma {Function} callback Function to be called once userPrefrences are
* loaded
*/
var setupUserConfigFlag = false;
mw.setupUserConfig = function( callback ) {
if( setupUserConfigFlag ) {
if( callback ) {
callback();
}
return ;
}
// Do Setup user config:
mw.load( [ '$j.cookie', 'JSON' ], function() {
if( $j.cookie( 'mwUserConfig' ) ) {
mwUserConfig = JSON.parse( $j.cookie( 'mwUserConfig' ) );
}
setupUserConfigFlag = true;
if( callback ) {
callback();
}
});
};
/**
* Save a user configuration var to a cookie & local global variable Loads
* the cookie plugin if not already loaded
*
* @param {String}
* name Name of user configuration value
* @param {String}
* value Value of configuration name
*/
mw.setUserConfig = function ( name, value, cookieOptions ) {
if( ! setupUserConfigFlag ) {
mw.log( "Error: userConfig not setup" );
return false;
}
// Update local value
mwUserConfig[ name ] = value;
// Update the cookie ( '$j.cookie' & 'JSON' should already be loaded )
$j.cookie( 'mwUserConfig', JSON.stringify( mwUserConfig ), {'expires' : mw.getConfig("Mw.UserPreferenceExpireDays") } );
};
/**
* Save a user configuration var to a cookie & local global variable
*
* @param {String}
* name Name of user configuration value
* @return value of the configuration name false if the configuration name
* could not be found
*/
mw.getUserConfig = function ( name ) {
if( mwUserConfig[ name ] )
return mwUserConfig[ name ];
return false;
};
/**
* Add a hook system for a target object / interface
*
* depricated you should instead use jQuery's bind and trigger
*
* @param {Object}
* targetObj Interface Object to add hook system to.
*/
mw.addHookSystem = function( targetObj ) {
// Setup the target object hook holder:
targetObj[ 'hooks' ] = { };
/**
* Adds a hook to the target object
*
* Should be called by clients to setup named hooks
*
* @param {String}
* hookName Name of hook to be added
* @param {Function}
* hookFunction Function to be called at hook time
*/
targetObj.addHook = function( hookName, hookFunction ) {
if( ! this.hooks[ hookName ] ) {
this.hooks[ hookName ] = [ ];
}
this.hooks[ hookName ].push( hookFunction );
};
/**
* Runs all the hooks by a given name with reference to the host object
*
* Should be called by the host object at named execution points
*
* @param {String}
* hookName Name of hook to be called
* @return Value of hook result true interface should continue function
* execution false interface should stop or return from method
*/
targetObj.runHook = function( hookName, options ) {
if( this.hooks[ hookName ] ) {
for( var i =0; i < this.hooks[ hookName ].length; i ++ ) {
if( typeof( this.hooks[ hookName ][ i ] ) == 'function' ) {
this.hooks[ hookName ][ i ]( options );
}
}
}
};
};
// Add hooks system to the core "mw" object
mw.addHookSystem( mw );
// Stores callbacks for resource loader loading
var mwLoadDoneCB = { };
/**
* Top level loader prototype:
*/
mw.loader = {
/**
* Javascript Module Loader functions
*
* @key Name of Module
* @value function code to load module
*/
moduleLoaders : [],
/**
* Module resource list queue.
*
* @key Name of Module
* @value .resourceList list of resources to be loaded .functionQueue
* list of functions to be run once module is ready
*/
moduleLoadQueue: { },
/**
* Javascript Class Paths
*
* @key Name of resource
* @value Class file path
*/
resourcePaths : { },
/**
* Stores resources that have been requested ( to avoid re-requesting
* the same resources ) in concurrent requests )
*/
requestedResourceQueue: { },
/**
* javascript Resource Paths
*
* @key Name of resource
* @value Name of dependent style sheet
*/
resourceStyleDependency: { },
/**
* Core load function:
*
* @param {Mixed}
* loadRequest:
*
* {String} Name of a module to be loaded Modules are added via
* addModuleLoader and can define custom code needed to check config and
* return a list of resources to be loaded
*
* {String} Name of a resource to loaded. Resources are added via
* addResourcePaths function Using defined resource names avoids loading
* the same resource twice by first checking if the named resource is
* defined in the global javascript scope variable
*
* {String} Absolute or relative to url path The same file won't be
* loaded twice
*
* {Array} can be an array of any combination of the above strings. Will
* be loaded in-order or in a single resource loader request if
* scriptLoader is available.
*
* {Array} {Array} Can be a set of Arrays for loading. Some browsers
* execute included scripts out of order. This lets you chain sets of
* request for those browsers. If using the server side resource loader
* order is preserved in output and a single request will be used.
*
* @param {Function}
* callback Function called once loading is complete
*
*/
load: function( loadRequest, instanceCallback ) {
var _this = this;
// Throw out any loadRequests that are not strings
loadRequest = this.cleanLoadRequest( loadRequest );
// Ensure the callback is only called once per load instance
var callback = function(){
// mw.log( 'instanceCallback::running callback: ' +
// instanceCallback );
if( instanceCallback ){
// We pass the loadRequest back to the callback for easy
// debugging of concurrency issues.
// ( normally its not used )
instanceCallback( loadRequest );
instanceCallback = null;
}
};
// Check for empty loadRequest ( directly return the callback )
if( mw.isEmpty( loadRequest ) ) {
mw.log( 'mwEmbed::Empty load request: ( ' + loadRequest + ' ) ' );
callback( loadRequest );
return ;
}
// Check if its a multi-part request:
if( typeof loadRequest == 'object' ) {
if( loadRequest.length > 1 ) {
this.loadMany ( loadRequest, callback );
return ;
}else{
// If an array of length 1 set as first element
loadRequest = loadRequest[0];
}
}
// Check for the module name loader function
if( this.moduleLoaders[ loadRequest ] ) {
var resourceSet = this.getModuleResourceSet( loadRequest );
if( !resourceSet ){
mw.log( "mw.load:: Error with module loader: " + loadRequest + ' ( no resource set defined )' );
return ;
}
// xxx should use refactor "ready" stuff into a "domReady" class
// So we would not have local scope globals like this:
// if ( mwReadyFlag ) {
// Load the module directly if load request is after
// mw.ready has run
this.load( resourceSet, callback );
// } else {
// this.addToModuleLoaderQueue(
// loadRequest,
// resourceSet,
// callback
// );
// }
return ;
}
// Check for javascript resource
if( this.getResourcePath( loadRequest ) ) {
this.loadResource( loadRequest, callback );
return ;
}
// Try loading as a "file" or via ScriptLoader
if( loadRequest ) {
// Check if this resource was already requested
if( typeof this.requestedResourceQueue[ loadRequest ] == 'object' ){
this.requestedResourceQueue[ loadRequest ].push( callback );
return ;
} else {
this.requestedResourceQueue[ loadRequest ] = [];
}
if( loadRequest.indexOf( '.js' ) == -1 && !mw.getResourceLoaderPath() ) {
mw.log( 'Error: are you sure ' + loadRequest + ' is a file ( is it missing a resource path? ) ' );
}
mw.getScript( loadRequest, function(){
// Check if we have requestedResources queue items:
while( _this.requestedResourceQueue[ loadRequest ].length ){
_this.requestedResourceQueue[ loadRequest ].shift()( loadRequest );
}
callback( loadRequest );
// empty the load request queue:
_this.requestedResourceQueue[ loadRequest ] = [];
});
return ;
}
// Possible error?
mw.log( "Error could not handle load request: " + loadRequest );
},
getModuleResourceSet: function( moduleName ){
// Check if the module loader is a function ~run that function~
if( typeof ( this.moduleLoaders[ moduleName ] ) == 'function' ) {
// Add the result of the module loader function
return this.moduleLoaders[ moduleName ]();
} else if( typeof ( this.moduleLoaders[ moduleName ] ) == 'object' ){
// set resourceSet directly
return this.moduleLoaders[ moduleName ];
}
return false;
},
/**
* Clean the loadRequest ( throw out any non-string items )
*/
cleanLoadRequest: function( loadRequest ){
var cleanRequest = [];
if( ! loadRequest ){
return [];
}
if( typeof loadRequest == 'string' )
return loadRequest;
for( var i =0;i < loadRequest.length; i++ ){
if( typeof loadRequest[i] == 'object' ) {
cleanRequest[i] = this.cleanLoadRequest( loadRequest[i] );
} else if( typeof loadRequest[i] == 'string' ){
cleanRequest[i] = $j.trim( loadRequest[i] );
} else{
// bad request type skip
}
}
return cleanRequest;
},
/**
* Load a set of scripts. Will issue many load requests or package the
* request for the resource loader
*
* @param {Object}
* loadSet Set of scripts to be loaded
* @param {Function}
* callback Function to call once all scripts are loaded.
*/
loadMany: function( loadSet, callback ) {
var _this = this;
// Setup up the local "loadStates"
var loadStates = { };
// Check if we can load via the "resource loader" ( mwEmbed was
// included via scriptLoader )
if( mw.getResourceLoaderPath() ) {
// Get the grouped loadStates variable
loadStates = this.getGroupLoadState( loadSet );
if( mw.isEmpty( loadStates ) ) {
// mw.log( 'loadMany:all resources already loaded');
callback();
return ;
}
}else{
// Check if its a dependency set ( nested objects )
if( typeof loadSet [ 0 ] == 'object' ) {
_this.dependencyChainCallFlag[ loadSet ] = false;
// Load sets of resources ( to preserver order for some
// browsers )
_this.loadDependencyChain( loadSet, callback );
return ;
}
// Set the initial load state for every item in the loadSet
for( var i = 0; i < loadSet.length ; i++ ) {
var loadName = loadSet[ i ];
loadStates[ loadName ] = 0;
}
}
// We are infact loading many:
// mw.log("mw.load: LoadMany:: " + loadSet );
// Issue the load request check check loadStates to see if we are
// "done"
for( var loadName in loadStates ) {
// mw.log("loadMany: load: " + loadName );
this.load( loadName, function ( loadName ) {
loadStates[ loadName ] = 1;
/*
* for( var i in loadStates ) { mw.log( loadName + '
* finished of: ' + i + ' : ' + loadStates[i] ); }
*/
// Check if all load request states are set 1
var loadDone = true;
for( var j in loadStates ) {
if( loadStates[ j ] === 0 )
loadDone = false;
}
// Run the parent scope callback for "loadMany"
if( loadDone ) {
callback( loadName );
}
} );
}
},
/**
* Get grouped load state for script loader
*
* Groups the scriptRequest where possible: Modules include "loader
* code" so they are separated into pre-condition code to be run for
* subsequent requests
*
* @param {Object}
* loadSet Loadset to return grouped
* @return {Object} grouped loadSet
*/
getGroupLoadState: function( loadSet ) {
var groupedLoadSet = [];
var loadStates = { };
// Merge load set into new groupedLoadSet
if( typeof loadSet[0] == 'object' ) {
for( var i = 0; i < loadSet.length ; i++ ) {
for( var j = 0; j < loadSet[i].length ; j++ ) {
// Make sure we have not already included it:
groupedLoadSet.push( loadSet[i][j] );
}
}
} else {
// Use the loadSet directly:
groupedLoadSet = loadSet;
}
// Setup grouped loadStates Set:
var groupClassKey = '';
var coma = '';
var uniqueResourceName = {};
for( var i=0; i < groupedLoadSet.length; i++ ) {
var loadName = groupedLoadSet[ i ];
if( this.getResourcePath( loadName ) ) {
// Check if not already in request queue and not defined in
// global namespace
if( !mw.isset( loadName ) && ! uniqueResourceName[ loadName] ){
groupClassKey += coma + loadName;
coma = ',';
// Check for style sheet dependencies
if( this.resourceStyleDependency[ loadName ] ){
groupClassKey += coma + this.resourceStyleDependency[ loadName ];
}
}
} else if ( this.moduleLoaders[ loadName ] ) {
// Module loaders break up grouped script requests ( add the
// current groupClassKey )
if( groupClassKey != '' ) {
loadStates[ groupClassKey ] = 0;
groupClassKey = coma = '';
}
if( ! uniqueResourceName[ loadName] ){
// Add the module to the loadSate
loadStates[ loadName ] = 0;
}
}
uniqueResourceName[ loadName] = true;
}
// Add groupClassKey if set:
if( groupClassKey != '' ) {
loadStates [ groupClassKey ] = 0;
}
return loadStates;
},
// Array to register that a callback has been called
dependencyChainCallFlag: { },
/**
* Load a sets of scripts satisfy dependency order for browsers that
* execute dynamically included scripts out of order
*
* @param {Object}
* loadChain A set of javascript arrays to be loaded. Sets
* are requested in array order.
*/
loadDependencyChain: function( loadChain, callback ) {
var _this = this;
// Load with dependency checks
var callSet = loadChain.shift();
this.load( callSet, function( cbname ) {
if ( loadChain.length != 0 ) {
_this.loadDependencyChain( loadChain, callback );
} else {
// NOTE: IE gets called twice so we have check the
// dependencyChainCallFlag before calling the callback
if( _this.dependencyChainCallFlag[ callSet ] == callback ) {
mw.log("... already called this callback for " + callSet );
return ;
}
_this.dependencyChainCallFlag[ callSet ] = callback;
callback( );
}
} );
},
/**
* Add to the module loader queue
*/
addToModuleLoaderQueue: function( moduleName, resourceSet, callback ) {
mw.log(" addToModuleLoaderQueue:: " + moduleName + ' resourceSet: ' + resourceSet );
if( this.moduleLoadQueue[ moduleName ] ){
// If the module is already in the queue just add its callback:
this.moduleLoadQueue[ moduleName ].functionQueue.push( callback );
} else {
// create the moduleLoadQueue item
this.moduleLoadQueue[ moduleName ] = {
'resourceSet' : resourceSet,
'functionQueue' : [ callback ],
'loaded' : false
};
}
},
/**
* Loops over all modules in queue, builds request sets based on config
* request type
*/
runModuleLoadQueue: function(){
var _this = this;
mw.log( "mw.runModuleLoadQueue:: " );
var runModuleFunctionQueue = function(){
// Run all the callbacks
for( var moduleName in _this.moduleLoadQueue ){
while( _this.moduleLoadQueue[moduleName].functionQueue.length ) {
_this.moduleLoadQueue[moduleName].functionQueue.shift()();
}
}
};
// Check for single request or javascript debug based loading:
if( !mw.getResourceLoaderPath() || mw.getConfig( 'loader.groupStrategy' ) == 'single' ){
// if not using the resource load just do a normal array merge
// ( for browsers like IE that don't follow first append first
// execute rule )
var fullResourceList = [];
for( var moduleName in this.moduleLoadQueue ) {
var resourceSet = this.moduleLoadQueue[ moduleName ].resourceSet;
// Lets try a global merge
fullResourceList = $j.merge( fullResourceList, resourceSet );
}
mw.load( fullResourceList, function(){
runModuleFunctionQueue();
});
return ;
}
// Else do per module group loading
if( mw.getConfig( 'loader.groupStrategy' ) == 'module' ) {
var fullResourceList = [];
var sharedResourceList = [];
for( var moduleName in this.moduleLoadQueue ) {
// Build a shared dependencies list and load that separately
// "first"
// ( in IE we have to wait until its "ready" since it does
// not follow dom order )
var moduleResourceList = this.getFlatModuleResourceList( moduleName );
// Build the sharedResourceList
for( var i=0; i < moduleResourceList.length; i++ ){
var moduleResource = moduleResourceList[i];
// Check if already in the full resource list if so add
// to shared.
if( fullResourceList[ moduleResource ] ){
if( $j.inArray( moduleResource, sharedResourceList ) == -1 ){
sharedResourceList.push( moduleResource );
}
}
// Add to the fullResourceList
fullResourceList[ moduleResource ] = true;
}
}
// Local module request set ( stores the actual request we will
// make after grouping shared resources
var moduleRequestSet = {};
// Only add non-shared to respective modules load requests
for( var moduleName in this.moduleLoadQueue ) {
moduleRequestSet[ moduleName ] = [];
var moduleResourceList = this.getFlatModuleResourceList( moduleName );
for( var i =0; i < moduleResourceList.length; i++ ){
var moduleResource = moduleResourceList[i];
if( $j.inArray( moduleResource, sharedResourceList ) == -1 ){
moduleRequestSet[ moduleName ].push( moduleResource );
}
}
}
var sharedResourceLoadDone = false;
// Check if modules are done
var checkModulesDone = function(){
if( !sharedResourceLoadDone ){
return false;
}
for( var moduleName in _this.moduleLoadQueue ) {
if( ! _this.moduleLoadQueue[ moduleName ].loaded ){
return false;
}
}
runModuleFunctionQueue();
};
// Local instance of load requests to retain resourceSet
// context:
var localLoadCallInstance = function( moduleName, resourceSet ){
mw.load( resourceSet, function(){
_this.moduleLoadQueue[ moduleName ].loaded = true;
checkModulesDone();
});
};
// Load the shared resources
mw.load( sharedResourceList, function(){
// mw.log("Shared Resources loaded");
// xxx check if we are in "IE" and dependencies need to be
// loaded "first"
sharedResourceLoadDone = true;
checkModulesDone();
});
// Load all module Request Set
for( var moduleName in moduleRequestSet ){
localLoadCallInstance( moduleName, moduleRequestSet[ moduleName ] );
}
}
// xxx Here we could also do some "intelligent" grouping
},
getFlatModuleResourceList: function( moduleName ){
var moduleList = [];
for( var j in this.moduleLoadQueue[moduleName].resourceSet ){
// Check if we have a multi-set array:
if( typeof this.moduleLoadQueue[moduleName].resourceSet[j] == 'object' ){
moduleList = $j.merge( moduleList, this.moduleLoadQueue[moduleName].resourceSet[j] );
} else {
moduleList = $j.merge( moduleList, [ this.moduleLoadQueue[moduleName].resourceSet[j] ] );
}
}
return moduleList;
},
/**
* Loads javascript or css associated with a resourceName
*
* @param {String}
* resourceName Name of resource to load
* @param {Function}
* callback Function to run once resource is loaded
*/
loadResource: function( resourceName , callback) {
// mw.log("LoadResource:" + resourceName );
var _this = this;
// Check for css dependency on resource name
if( this.resourceStyleDependency[ resourceName ] ) {
if( ! mw.isset( this.resourceStyleDependency[ resourceName ] )){
mw.log("loadResource:: dependent css resource: " + this.resourceStyleDependency[ resourceName ] );
_this.loadResource( this.resourceStyleDependency[ resourceName ] , function() {
// Continue the original loadResource request.
_this.loadResource( resourceName, callback );
});
return ;
}
}
// Make sure the resource is not already defined:
if ( mw.isset( resourceName ) ) {
// mw.log( 'Class ( ' + resourceName + ' ) already defined ' );
callback( resourceName );
return ;
}
// Setup the Script Request var:
var scriptRequest = null;
// If the scriptloader is enabled use the resourceName as the
// scriptRequest:
if( mw.getResourceLoaderPath() ) {
scriptRequest = resourceName;
}else{
// Get the resource url:
var baseClassPath = this.getResourcePath( resourceName );
// Add the mwEmbed path if not a root path or a full url
if( baseClassPath.indexOf( '/' ) !== 0 &&
baseClassPath.indexOf( '://' ) === -1 ) {
scriptRequest = mw.getMwEmbedPath() + baseClassPath;
}else{
scriptRequest = baseClassPath;
}
if( ! scriptRequest ) {
mw.log( "Error Could not get url for resource " + resourceName );
return false;
}
}
// Include resource defined check for older browsers
var resourceDone = false;
// Set the loadDone callback per the provided resourceName
mw.setLoadDoneCB( resourceName, callback );
// Issue the request to load the resource (include resource name in result callback:
mw.getScript( scriptRequest, function( scriptRequest ) {
// If its a "style sheet" manually set its resource to true
var ext = scriptRequest.substr( scriptRequest.split('?')[0].lastIndexOf( '.' ), 4 ).toLowerCase();
if( ext == '.css' && resourceName.substr(0,8) == 'mw.style' ){
mw.style[ resourceName.substr( 9 ) ] = true;
}
// Send warning if resourceName is not defined
if(! mw.isset( resourceName )
&& mwLoadDoneCB[ resourceName ] != 'done' ) {
// mw.log( 'Possible Error: ' + resourceName +' not set in
// time, or not defined in:' + "\n"
// + _this.getResourcePath( resourceName ) );
}
// If ( debug mode ) and the script include is missing resource
// messages
// do a separate request to retrieve the msgs
if( mw.currentClassMissingMessages ) {
mw.log( " resourceName " + resourceName + " is missing messages" );
// Reset the currentClassMissingMessages flag
mw.currentClassMissingMessages = false;
// Load msgs for this resource:
mw.loadResourceMessages( resourceName, function() {
// Run the onDone callback
mw.loadDone( resourceName );
} );
} else {
// If not using the resource loader make sure the
// resourceName is available before firing the loadDone
if( !mw.getResourceLoaderPath() ) {
mw.waitForObject( resourceName, function( resourceName ) {
// Once object is ready run loadDone
mw.loadDone( resourceName );
} );
} else {
// loadDone should be appended to the bottom of the
// resource loader response
// mw.loadDone( resourceName );
}
}
} );
},
/**
* Adds a module to the mwLoader object
*
* @param {String}
* name Name of module
* @param {Function}
* moduleLoader Function that loads dependencies for a module
*/
addModuleLoader: function( name, moduleLoader ) {
this.moduleLoaders [ name ] = moduleLoader;
},
/**
* Adds resource file path key value pairs
*
* @param {Object}
* resourceSet JSON formated list of resource name file path
* pairs.
*
* resourceSet must be strict JSON to allow the php scriptLoader to
* parse the file paths.
*/
addResourcePaths: function( resourceSet ) {
var prefix = ( mw.getConfig( 'loaderContext' ) )?
mw.getConfig( 'loaderContext' ): '';
for( var i in resourceSet ) {
this.resourcePaths[ i ] = prefix + resourceSet[ i ];
}
},
/*
* Adds a named style sheet dependency to a named resource
*
* @parma {Object} resourceSet JSON formated list of resource names and
* associated style sheet names
*/
addStyleResourceDependency: function( resourceSet ){
for( var i in resourceSet ){
this.resourceStyleDependency[ i ] = resourceSet[i];
}
},
/**
* Get a resource path from a resourceName if no resource found return
* false
*/
getResourcePath: function( resourceName ) {
if( this.resourcePaths[ resourceName ] ){
return this.resourcePaths[ resourceName ];
}
return false;
}
};
// Flag to register the "firstLoad" callback ( works around domready out of order binding issues )
var mwFirstLoadDoneCB = true;
/**
* Load done callback for script loader
*