forked from stealjs/steal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steal.js
2162 lines (1944 loc) · 60.4 KB
/
steal.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
(function(){
// Gets the window (even if there is none)
var win = (function(){return this}).call(null),
// String constants (for better minification)
STR_ONLOAD = "onload",
STR_ONERROR = "onerror",
STR_ONREADYSTATECHANGE = "onreadystatechange",
STR_CREATE_ELEMENT = 'createElement',
STR_GET_BY_TAG = 'getElementsByTagName',
// the document ( might not exist in rhino )
doc = win.document,
// creates a script tag
scriptTag = function() {
var start = doc[STR_CREATE_ELEMENT]('script');
start.type = 'text/javascript';
return start;
},
// a function that returns the head element
// creates and caches the lookup if necessary
head = function() {
var d = doc,
de = d.documentElement,
heads = d[STR_GET_BY_TAG]("head"),
hd = heads[0];
if (! hd ) {
hd = d[STR_CREATE_ELEMENT]('head');
de.insertBefore(hd, de.firstChild);
}
// replace head so it runs fast next time.
head = function(){
return hd;
}
return hd;
},
// extends one object with another
extend = function( d, s ) {
for ( var p in s ) {
d[p] = s[p];
}
return d;
},
// a jQuery-like $.each
each = function(arr, cb){
for(var i =0, len = arr.length; i <len; i++){
cb.call(arr[i],i,arr[i])
}
return arr;
},
// makes an array of things
makeArray = function(args){
var arr = [];
each(args, function(i, str){arr[i] = str});
return arr;
},
// testing support for various browser behaviors
support = {
// does onerror work in script tags?
error: doc && (function(){
var script = scriptTag();
script.setAttribute( "onerror", "return;" );
return typeof script["onerror"] === "function" ?
true : "onerror" in script
})(),
// If scripts support interactive ready state.
// This is set later.
interactive: false,
attachEvent : doc && scriptTag().attachEvent
},
// a startup function that will be called when steal is ready
startup = function(){},
// the old steal value
oldsteal = win.steal,
// if oldsteal is an object
// we use it as options to configure steal
opts = typeof oldsteal == 'object' ? oldsteal : {};
// =============================== STEAL ===============================
/**
* @class steal
* @parent stealjs
*
* __steal__ is a function that loads scripts, css, and
* other resources into your application.
*
* steal(FILE_or_FUNCTION, ...)
*
* ## Quick Walkthrough
*
* Add a script tag that loads <code>steal/steal.js</code> and add
* the path to the first file to load in the query string like:
*
* <script type='text/javascript'
* src='../steal/steal.js?myapp/myapp.js'>
* </script>
*
* Then, start loading things and using them like:
*
* steal('myapp/tabs.js',
* 'myapp/slider.js',
* 'myapp/style.css',function(){
*
* // tabs and slider have loaded
* $('#tabs').tabs();
* $('#slider').slider()
* })
*
* Make sure your widgets load their dependencies too:
*
* // myapp/tabs.js
* steal('jquery', function(){
* $.fn.tabs = function(){
* ...
* }
* })
*
* ## Examples:
*
* // Loads ROOT/jquery/controller/controller.js
* steal('jquery/controller')
* steal('jquery/controller/controller.js')
*
* // Loads coffee script type and a coffee file relative to
* // the current file
* steal('steal/coffee').then('./mycoffee.coffee')
*
* // Load 2 files and dependencies in parallel and
* // callback when both have completed
* steal('jquery/controller','jquery/model', function(){
* // $.Controller and $.Model are available
* })
*
* // Loads a coffee script with a non-standard extension (cf)
* // relative to the current page and instructs the build
* // system to not package it (but it will still be loaded).
* steal({
* src: "./foo.cf",
* packaged: false,
* type: "coffee"
* })
*
* The following is a longer walkthrough of how to install
* and use steal:
*
* ## Adding steal to a page
*
* After installing StealJS (or JavaScriptMVC),
* find the <code>steal</code> folder with
* <code>steal/steal.js</code>.
*
* To use steal, add a script tag
* to <code>steal/steal.js</code> to your
* html pages.
*
* This walkthrough assumes you have the steal script
* in <code>public/steal/steal.js</code> and a directory
* structure like:
*
* @codestart text
* /public
* /steal
* /pages
* myapp.html
* /myapp
* myapp.js
* jquery.js
* jquery.ui.tabs.js
* @codeend
*
* To use steal in <code>public/pages/myapp.html</code>,
* add a script tag in <code>myapp.html</code>:
*
* @codestart html
* <script type='text/javascript'
* src='../steal/steal.js'>
* </script>
* @codeend
*
* <div class='whisper'>PRO TIP: Bottom load your scripts. It
* will increase your application's percieved response time.</div>
*
* ## Loading the First Script
*
* Once steal has been added to your page, it's time
* to load scripts. We want to load <code>myapp.js</code>
* and have it load <code>jquery.js</code> and
* <code>jquery.ui.tabs.js</code>.
*
* By default, steal likes your scripts
* to be within in the [steal.static.root steal.root] folder. The [steal.root] the
* folder contains the <code>steal</code> folder. In this example,
* it is the <code>public</code> folder.
*
* To load <code>myapp/myapp.js</code>, we have two options:
*
* #### Add a script tag
*
* Add a script tag after the steal
* script that 'steals' <code>myapp.js</code> like:
*
* @codestart html
* <script type='text/javascript'>
* steal('myapp/myapp.js')
* </script>
* @codeend
*
* #### Add the script parameter
*
* The most common (and shortest) way to load <code>myapp.js</code>
* is to add the script path to the steal script's src after in the
* query params. So, instead of adding a script, we change
* the steal script from:
*
* @codestart html
* <script type='text/javascript'
* src='../steal/steal.js'>
* </script>
* @codeend
*
* To
*
* @codestart html
* <script type='text/javascript'
* src='../steal/steal.js?<b>myapp/myapp.js</b>'>
* </script>
* @codeend
*
* <div class='whisper'>PRO TIP: You can also just add
* <code>?myapp</code> to the query string.</div>
*
* ## Loading Scripts
*
* We want to load <code>jquery.js</code> and
* <code>jquery.ui.tabs.js</code> into the page and then
* add then create a tabs widget. First we need to load
* <code>jquery.js</code>.
*
* By default, steal loads script relative to [steal.root]. To
* load <code>myapp/jquery.js</code> we can the following to
* <code>myapp.js</code>:
*
* steal('myapp/jquery.js');
*
* But, we can also load relative to <code>myapp.js</code> like:
*
* steal('./jquery.js');
*
* Next, we need to load <code>jquery.ui.tabs.js</code>. You
* might expect something like:
*
* steal('./jquery.js','./jquery.ui.tabs.js')
*
* to work. But there are two problems / complications:
*
* - steal loads scripts in parallel and runs out of order
* - <code>jquery.ui.tabs.js</code> depends on jQuery being loaded
*
* This means that steal might load <code>jquery.ui.tabs.js</code>
* before <code>jquery.js</code>. But this is easily fixed.
*
* [steal.static.then] waits until all previous scripts have loaded and
* run before loading scripts after it. We can load <code>jquery.ui.tabs.js</code>
* after <code>jquery.js</code> like:
*
* steal('./jquery.js').then('./jquery.ui.tabs.js')
*
* Finally, we need to add tabs to the page after
* the tabs's widget has loaded. We can add a callback function to
* steal that will get called when all previous scripts have finished
* loading:
*
* steal('./jquery.js').then('./jquery.ui.tabs.js', function($){
* $('#tabs').tabs();
* })
*
* ## Other Info
*
* ### Exclude Code Blocks From Production
*
* To exclude code blocks from being included in
* production builds, add the following around
* the code blocks.
*
* //!steal-remove-start
* code to be removed at build
* //!steal-remove-end
*
* ### Lookup Paths
*
* By default steal loads resources relative
* to [steal.static.root steal.root]. For example, the following
* loads foo.js in <code>steal.root</code>:
*
* steal('foo.js'); // loads //foo.js
*
* This is the same as writing:
*
* steal('//foo.js');
*
* Steal uses <code>'//'</code> to designate the [steal.static.root steal.root]
* folder.
*
* To load relative to the current file, add <code>"./"</code> or
* <code>"../"</code>:
*
* steal("./bar.js","../folder/zed.js");
*
* Often, scripts can be found in a folder within the same
* name. For example, [jQuery.Controller $.Controller] is
* in <code>//jquery/controller/controller.js</code>. For convience,
* if steal is provided a path without an extension like:
*
* steal('FOLDER/PLUGIN');
*
* It is the same as writing:
*
* steal('FOLDER/PLUGIN/PLUGIN.js')
*
* This means that <code>//jquery/controller/controller.js</code>
* can be loaded like:
*
* steal('jquery/controller')
*
* ### Types
*
* steal can load resources other than JavaScript.
*
*
* @constructor
*
* Loads resources specified by each argument. By default, resources
* are loaded in parallel and run in any order.
*
*
* @param {String|Function|Object} resource...
*
* Each argument specifies a resource. Resources can
* be given as a:
*
* ### Object
*
* An object that specifies the loading and build
* behavior of a resource.
*
* steal({
* src: "myfile.cf",
* type: "coffee",
* packaged: true,
* unique: true,
* ignore: false,
* waits: false
* })
*
* The available options are:
*
* - __src__ {*String*} - the path to the resource.
*
* - __waits__ {*Boolean default=false*} - true the resource should wait
* for prior steals to load and run. False if the resource should load and run in
* parallel. This defaults to true for functions.
*
* - __unique__ {*Boolean default=true*} - true if this is a unique resource
* that 'owns' this url. This is true for files, false for functions.
*
* - __ignore__ {*Boolean default=false*} - true if this resource should
* not be built into a production file and not loaded in
* production. This is great for script that should only be available
* in development mode. This script is loaded during compression, but not
* added to the bundled script.
*
* - __packaged__ {*Boolean default=true*} - true if the script should be built
* into the production file. false if the script should not be built
* into the production file, but still loaded. This is useful for
* loading 'packages'. This script is loaded during compression, but not
* added to the bundled script. The difference with ignore is packaged still
* steals this file while production mode, from an external script.
*
* - __type__ {*String default="js"*} - the type of the resource. This
* is typically inferred from the src.
*
* ### __String__
*
* Specifies src of the resource. For example:
*
* steal('./file.js')
*
* Is the same as calling:
*
* steal({src: './file.js'})
*
* ### __Function__
*
* A callback function that runs when all previous steals
* have completed.
*
* steal('jquery', 'foo',function(){
* // jquery and foo have finished loading
* // and runing
* })
*
* @return {steal} the steal object for chaining
*/
function steal() {
// convert arguments into an array
var args = makeArray(arguments);
pending.push.apply(pending, args);
// steal.after is called everytime steal is called
// it kicks off loading these files
steal.after(args);
// return steal for chaining
return steal;
};
// =============================== PATHS .8 ============================
// things that matter ...
// - the current file being loaded
// - the location of the page
// - the file
/**
* @class
* Used for getting information out of a path
* @constructor
* Takes a path
* @param {String} path
*/
steal.File = function( path ) {
// if new was not used, use it.
if ( this.constructor != steal.File ) {
return new steal.File(path);
}
// save the path
this.path = typeof path == 'string'? path : path.path;
};
// alias steal.File to File
var File = steal.File,
// a reference to the current file
curFile;
// get and sets the current file
File.cur = function(newCurFile){
if(newCurFile !== undefined){
curFile = File(newCurFile);
}else{
return curFile || File("");
}
};
extend(File.prototype,
/**
* @prototype
*/
{
// Removes hash and querystring
clean: function() {
return this.path.match(/([^\?#]*)/)[1];
},
// gets the files extension
ext : function(){
var match = this.clean().match(/\.([\w\d]+)$/)
return match ? match[1] : "";
},
// Returns everything before the last /
dir: function() {
// remove any query string
var cleaned = this.clean(),
// get the last /
last = cleaned.lastIndexOf('/'),
// if there is a last /, get everything up to that point
dir = (last != -1) ? cleaned.substring(0, last) : '';
// make sure we aren't left with just https/ or file:/
return /^(https?:\/|file:\/)$/.test(dir) ? cleaned : dir;
},
// Returns everything after the last /
filename: function() {
var cleaned = this.clean(),
last = cleaned.lastIndexOf('/'),
filename = (last != -1) ? cleaned.substring(last+1, cleaned.length) : cleaned;
return /^(https?:\/|file:\/)$/.test(filename) ? cleaned : filename;
},
// Returns the domain for the current path.
// Returns null if the domain is a file.
domain: function() {
var http = this.path.match(/^(?:https?:\/\/)([^\/]*)/);
return http ? http[1] : null;
},
/**
* Joins a url onto a path. One way of understanding this is that your File object represents your current location, and calling join() is analogous to "cd" on a command line.
* @codestart
* new steal.File("d/e").join("../a/b/c"); // Yields the path "d/a/b/c"
* @codeend
* @param {String} url
*/
join: function( url ) {
return File(url).joinFrom(this.path);
},
/**
* Returns the path of this file referenced from another url or path.
*
* new steal.File('a/b.c').joinFrom('/d/e')//-> /d/e/a/b.c
*
* @param {String} url
* @param {Boolean} expand if the path should be expanded
* @return {String}
*/
joinFrom: function( url, expand ) {
var u = File(url);
// if this.path is absolutely referenced
if ( this.protocol() ) { //if we are absolutely referenced
//try to shorten the path as much as possible:
var firstDomain = this.domain(),
secondDomain = u.domain();
// if domains are equal, shorten
if ( firstDomain && firstDomain == secondDomain ) {
return this.toReferenceFromSameDomain(url);
} else {
// if there is no domain or not equal, use our path
return this.path;
}
// the path is the same as the folder the page is in
} else if ( url === steal.pageUrl().dir() && !expand ) {
return this.path;
} else if ( this.isLocalAbsolute() ) { // we are a path like /page.js
return (u.domain() ? u.protocol() + "//" + u.domain() : "" )+ this.path;
} else { //we have 2 relative paths, remove folders with every ../
if ( url === '' ) {
return this.path.replace(/\/$/, '');
}
var urls = url.split('/'),
paths = this.path.split('/'),
path = paths[0];
//if we are joining from a folder like cookbook/, remove the last empty part
if ( url.match(/\/$/) ) {
urls.pop();
}
// for each .. remove one folder
while ( path == '..' && paths.length > 0 ) {
// if we've emptied out, folders, just break
// leaving any additional ../s
if(! urls.pop() ){
break;
}
paths.shift();
path = paths[0];
}
return urls.concat(paths).join('/');
}
},
// Returns true if the file is relative to a domain or a protocol
relative: function() {
return this.path.match(/^(https?:|file:|\/)/) === null;
},
/**
* Returns the relative path between two paths with common folders.
* @codestart
* new steal.File('a/b/c/x/y').toReferenceFromSameDomain('a/b/c/d/e')//-> ../../x/y
* @codeend
* @param {Object} url
* @return {String}
*/
toReferenceFromSameDomain: function( url ) {
var parts = this.path.split('/'),
other_parts = url.split('/'),
result = '';
while ( parts.length > 0 && other_parts.length > 0 && parts[0] == other_parts[0] ) {
parts.shift();
other_parts.shift();
}
each(other_parts, function(){ result += '../'; })
return result + parts.join('/');
},
/**
* Is the file on the same domain as our page.
*/
isCrossDomain: function() {
return this.isLocalAbsolute() ? false : this.domain() != File(win.location.href).domain();
},
isLocalAbsolute: function() {
return this.path.indexOf('/') === 0;
},
protocol: function() {
var match = this.path.match(/^(https?:|file:)/);
return match && match[0];
},
/**
* For a given path, a given working directory, and file location, update the path so
* it points to a location relative to steal's root.
*
* We want everything relative to steal's root so the same app can work in multiple pages.
*
* ./files/a.js = steals a.js
* ./files/a = a/a.js
* files/a = //files/a/a.js
* files/a.js = loads //files/a.js
*/
normalize: function() {
var current = File.cur().dir(),
//if you are cross domain from the page, and providing a path that doesn't have an domain
path = this.path;
if (/^\/\//.test(this.path)) { //if path is rooted from steal's root (DEPRECATED)
path = this.path.substr(2);
}
else if (/^\.\//.test(this.path)) { // should be relative
this.path = this.path.substr(2);
path = this.joinFrom(current);
this.path = "./" + this.path;
}
else if (/^[^\.|\/]/.test(this.path)) {}
else {
if (this.relative() ||
File.cur().isCrossDomain() && //if current file is on another domain and
!this.protocol()) { //this file doesn't have a protocol
path = this.joinFrom(current);
}
}
return path;
}
});
var pending = [],
s = steal,
id = 0,
steals = {};
// this is for methods on a 'steal instance'. A file can be in one of a few states:
// created - the steal instance is created, but we haven't started loading it yet
// this happens when thens are used
// loading - (loading=true) By calling load, this will tell steal to load a file
// loaded - (isLoaded=true) The file has been run, but its dependency files have been completed
// complete - all of this files dependencies have loaded and completed.
steal.p = {
// adds a new steal and throws an error if the script doesn't load
// this also checks the steals map
make: function(options){
var stel = new steal.p.init(options),
rootSrc = stel.options.rootSrc;
if(stel.unique && rootSrc){
// the .js is b/c we are not adding that automatically until
// load because we defer 'type' determination until then
if(!steals[rootSrc] && ! steals[rootSrc+".js"]){ //if we haven't loaded it before
steals[rootSrc] = stel;
} else{ // already have this steal
stel = steals[rootSrc];
// extend the old stolen file with any new options
extend(stel.options, typeof options === "string" ? {} : options)
}
}
return stel;
},
init: function( options ) {
this.dependencies = [];
// id for debugging
this.id = (++id);
// if we have no options, we are the global init ... set ourselves up ...
if(!options){ //global init cur ...
this.options = {};
this.waits = false;
this.pack = "production.js";
}
//handle callback functions
else if ( typeof options == 'function' ) {
var path = File.cur().path;
this.options = {
fn : function() {
//set the path ..
File.cur(path);
// call the function, someday soon this will be requireJS-like
options(steal.send || win.jQuery || win.Zepto || steal);
},
rootSrc: path,
orig: options,
type: "fn"
}
// this has nothing to do with 'loading' options
this.waits = true;
this.unique = false;
} else {
// save the original options
this.orig = options;
this.options = steal.makeOptions(extend({},
typeof options == 'string' ? { src: options } : options));
this.waits = this.orig.waits || false;
this.unique = true;
}
},
complete : function(){
this.completed = true;
},
/**
* @hide
* After the script has been loaded and run
*
* - check what else has been stolen, load them
* - mark yourself as complete when everything is completed
* - this is where all the actions is
*/
loaded: function(script){
var myqueue,
stel,
src = (script && script.src) || this.options.src,
rootSrc = this.options.rootSrc;
//set yourself as the current file
File.cur(rootSrc);
// mark yourself as 'loaded'.
this.isLoaded = true;
// If we are IE, get the queue from interactives
// TODO move this out of this function
if (support.interactive && src) {
myqueue = interactives[src];
}
// is there a case in IE where, this makes sense?
// in other browsers, the queue of items to load is
// what is in pending
if(!myqueue){
myqueue = pending.slice(0);
pending = [];
}
// if we have nothing, mark us as complete (resolve if deferred)
if(!myqueue.length){
this.complete();
return;
}
// now we have to figure out how to wire up our pending steals
var self = this,
set = [],
// the current
joiner,
initial = [],
isProduction = steal.options.env == 'production',
files = [],
// a helper that basically does a join
// when everything in arr's func method is called,
// call func2 on obj
whenEach = function(arr, func, obj, func2){
var big = [obj, func2];
each(arr, function(i, item){
big.unshift(item, func)
});
when.apply(steal, big);
},
// a helper that does the oposite of a join. When
// obj's func method is called, call func2 on all items.
whenThe = function(obj, func, items, func2){
each(items, function(i, item){
when(obj, func, item, func2)
})
};
//now go through what you stole and hook everything up
//BUT, we go through backwards
each(myqueue.reverse(), function(i, item){
//in production, ignore ignored items (like steal/dev
if(isProduction && item.ignore){
return;
}
// make a steal object
stel = steal.p.make( item );
// add it as a dependency, circular are not allowed
self.dependencies.unshift(stel)
if(stel.waits === false){ // file
// on the current
files.push(stel);
}else{ // function
// essentially have to bind current files to call previous joiner's load
// and to wait for current stel's complete
if(!joiner){ // if no previous joiner, then we are at the start of a file
// when they are complete, complete the file
whenEach( files.concat(stel), "complete", self, "complete");
// if there was a function then files, then end, function loads all files
if(files.length){
whenThe(stel,"complete", files ,"load")
}
} else { // function, file1, file2, file3, joiner function
whenEach(files.concat(stel) , "complete", joiner, "load");
// make stel complete load files
whenThe(stel,"complete", files.length ? files : [joiner] ,"load")
}
// the joiner is the previous thing
joiner = stel;
files = [];
}
});
// now we should be left with the starting files
if(files.length){
// we have initial files
// if there is a joiner, we need to load it when the initial files are complete
if(joiner){
whenEach(files, "complete", joiner, "load");
} else {
whenEach(files, "complete", self, "complete");
}
// reverse it back and load each initial file
each(files.reverse(), function(){
this.load();
});
} else if(joiner){
// we have inital function
joiner.load()
} else {
// we had nothing
self.complete();
}
},
/**
* Loads this steal
*/
load: function(returnScript) {
// if we are already loading / loaded
if(this.loading || this.isLoaded){
return;
}
this.loading = true;
var self = this;
// used for the canjs build because env.js barfs on mootools/dojo/yui
if(steal.isBuilding && this.options._skip){
self.loaded();
return;
}
// get yourself
steal.require(this.options, function load_calling_loaded(script){
self.loaded(script);
}, function(error, src){
var abortFlag = self.options.abort,
errorCb = self.options.error;
// if an error callback was provided, fire it
if(errorCb){
errorCb.call(self.options);
}
win.clearTimeout && win.clearTimeout(self.completeTimeout)
// if abort: false, register the script as loaded, and don't throw
if(abortFlag === false){
self.loaded();
return;
}
throw "steal.js : "+self.options.src+" not completed"
});
}
};
steal.p.init.prototype = steal.p;
/**
* @add steal
*/
// =============================== STATIC API ===============================
var page;
/**
* @static
*/
extend(steal,{
/**
* @attribute root
* The location of the steal folder.
*/
root : File(""),
/**
* Gets or sets the path from the current page to
* steal's (or JavaScriptMVC's) root folder. When passed a src, it sets the root folder.
* Otherwise, it returns the path to the root folder.
*
* This is the path from which
* all plugins are stolen. When you steal a plugin like steal("jquery/controller"),
* the plugin path is joined with this rootUrl to create a full path
* to the controller.js file.
*
* By default, the rootUrl is calculated from the
* steal script and the window location. For example, if the
* script tag looks like this:
*
* <script type='text/javascript' src='../../steal/steal.js?ui/app'></script>
*
* rootUrl will be set to "../../".
* Setting the rootUrl can be useful if you want to have
* steal.js in a different location.
*
* ## Example
*
* The following sets steal root to a different folder.
*
* steal.rootUrl("../../jmvc/")
*
* This appends <code>"../../jmvc"</code> to paths
* loaded from [steal.static.root]. In some strange cases this might be desirable if
* plugin folders are in a different location from the steal directory.
*
* It also sets the current url to this directory so the first calls to steal work relative to the root JMVC directory.
*
* @param {String} [src] a relative path from the current page to the root directory of JMVC, like ../../
* @return {String} returns the last path passed to rootUrl
*/
rootUrl : function(src){
if (src !== undefined) {
steal.root = File(src);
// set cur with the location
var cleaned = steal.pageUrl(),
loc = cleaned.join(src);
File.cur( cleaned.toReferenceFromSameDomain(loc) );
return steal;
} else {
return steal.root.path;
}
},
extend : extend,
/**
* @function pageUrl
* Gets or sets the location of the page using
* steal. This defaults to the window's location.
*
* However, sometimes it is necessary to
* have steal believe it is making requests from
* another page.
*
* @param {String} [newPage] a path to the page using steal (probably the windows location)
* @return {steal.File} returns the last path to a page passed to pageUrl, converted to a steal.File object
*/
pageUrl : function(newPage){
if(newPage){
page = File( File(newPage).clean() );
return steal;
} else{
return page || File("");
}
},
//gets and sets which page steal thinks it's at
// TODO: make location change-able ...
/**
* Gets the currently running script location.
*
* @return String
*/
cur: function( file ) {
if (file === undefined) {
return File.cur();
} else {