summaryrefslogtreecommitdiff
path: root/html/assets/js/require/Require.js
blob: f75558fc682394c595bc699b9a4fdf42b5b946cb (plain)
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
(function (requireFiles) {
    var require, _helpers;
    if (typeof (window.require) != 'undefined') {
        return;
    }

    function isArraylike(obj) {
        return Array.isArray(obj);
    }
    _helpers = {
        isFunction: function (obj) {
            return typeof (obj) == 'function';
        },
        isArray: isArraylike,
        grep: function (elems, callback, inv) {
            var retVal,
                ret = [],
                i = 0,
                length = elems.length;
            inv = !!inv;

            for (; i < length; i++) {
                retVal = !!callback(elems[i], i);
                if (inv !== retVal) {
                    ret.push(elems[i]);
                }
            }

            return ret;
        },
        map: function (elems, callback, arg) {
            var value,
                i = 0,
                length = elems.length,
                isArray = isArraylike(elems),
                ret = [];

            // Go through the array, translating each of the items to their
            if (isArray) {
                for (; i < length; i++) {
                    value = callback(elems[i], i, arg);

                    if (value != null) {
                        ret[ret.length] = value;
                    }
                }

                // Go through every key on the object,
            } else {
                for (i in elems) {
                    value = callback(elems[i], i, arg);

                    if (value != null) {
                        ret[ret.length] = value;
                    }
                }
            }

            // Flatten any nested arrays
            return [].concat(ret);
        }
    };

    function configuration(versions, speculativeDependencies, modules) {
        this.modules = modules || {};
        this.modulePaths = {};
        this.bundles = [];
        this.bundlesByName = {};

        for (var i = 0; i < versions.length; i++) {
            var v = versions[i];
            for (var k in v) {
                var moduleName = k;
                var url = v[k];
                if (typeof (url) == 'object') {
                    this.modulePaths[moduleName] = url.Url;
                    var bundle = {
                        name: moduleName,
                        url: url.Url,
                        modules: url.Contents,
                        usages: []
                    };
                    
                    this.bundles.push(bundle);
                    this.bundlesByName[bundle.name] = bundle;
                    continue;
                }
                this.modulePaths[moduleName] = url;

            }
        }

        this.getSpeculativeDependencies = function (moduleNames) {
            var result = [];
            if (!isArraylike(moduleNames)) {
                result.push(moduleNames);
                for (var i = 0; i < speculativeDependencies.length; i++) {
                    var deps = speculativeDependencies[i][moduleNames];
                    if (deps) {
                        for (var j = 0; j < deps.length; j++) {
                            if (result.indexOf(deps[j]) == -1)
                                result.push(deps[j]);
                        }
                    }
                }
            }
            else {
                for (var i = 0; i < moduleNames.length; i++) {
                    var deps = this.getSpeculativeDependencies(moduleNames[i]);
                    for (var j = 0; j < deps.length; j++) {
                        if (result.indexOf(deps[j]) === -1)
                            result.push(deps[j]);
                    }
                }
            }
            return result;
        }


        this.updateState = function (state, moduleDependencies, callback) {
            var config = this;
            state.moduleDependencies = moduleDependencies;
            state.dependencies = _helpers.map(_helpers.grep(
                moduleDependencies,
                function (x) {
                    return x && x.indexOf('exports') == -1 && x.indexOf('require') == -1;
                }
            ), function (x) {
                return config.getOrCreateState(x);
            });
            state.loaded = true;
            state.readyCallback = function () {
                state.ready = true;
                if (callback) {
                    state.object = invokeCallback(callback, state.moduleDependencies, state.name);
                    document.dispatchEvent(new CustomEvent("require.moduleLoaded", { detail: state }));
                }

            };
            if (state.readyCheck) {
                state.readyCheck();
            }


            if (state.dependencies.length) {
                for (var i = 0; i < state.dependencies.length; i++) {
                    state.dependencies[i].callbacks.push(function () {
                        if (state.readyCheck) {
                            state.readyCheck();
                        }
                    });
                }
                var dependendentModuleNames = _helpers.map(state.dependencies, function (x) { return x.name });

                require(
                    dependendentModuleNames,
                    function () {
                        if (state.readyCheck) {
                            state.readyCheck();
                        }
                    }
                );
            }
        }

        this.getOrCreateState = function (moduleName) {
            var state = window.define.amd.getProp(moduleName);
            if (!state) {
                window.define.amd[moduleName] = state = this.createState(moduleName);
            }

            return state;
        }
        this.createState = function (moduleName) {
            var bundle = this.bundlesByName[moduleName];
            return {
                isBundle: typeof (bundle) !== 'undefined',
                url: this.modulePaths.getProp(this.modules[moduleName] || moduleName),
                name: moduleName,
                loaded: false,
                callbacks: [],
                ready: false,
                dependencies: [],
                doCallBacks: function () {
                    for (var i = 0; i < this.callbacks.length; i++) {
                        this.callbacks[i].apply(this, []);
                    }
                    this.callbacks = [];
                },
                removeReadyDependencies: function () {
                    this.dependencies = _helpers.grep(this.dependencies, function (x) { return !x.ready; });
                },
                readyCheck: function () {
                    this.removeReadyDependencies();
                    if (!this.dependencies.length && this.loaded) {
                        if (!this.ready) {
                            if (this.readyCallback) {
                                var rc = this.readyCallback;
                                rc.apply(this, []);
                            }
                            else {
                                this.ready = true;
                            }
                            this.doCallBacks();
                        }
                    }
                },
                hasUrl: function () {
                    if (!this.url) {
                        if (debug)
                            console.warn('could not resolve ' + moduleName + ' into a url');
                        return false;
                    }
                    return true;
                },
                callPending: function () {
                    this.pending = true;
                    if (bundle) {
                        for (var i = 0; i < bundle.modules.length; i++) {
                            var state = config.getOrCreateState(bundle.modules[i]);
                            state.callPending();
                        }
                    }
                },
                callDone: function () {
                    if (this.isBundle) {

                        if (!this.loaded) {
                            define(this.name, [], function () { });
                            if (!this.loaded) {
                                console.warn('should be loaded');
                            }
                            for (var i = 0; i < bundle.modules.length; i++) {
                                var state = config.getOrCreateState(bundle.modules[i]);
                                state.callDone();
                            }
                        }
                        return;
                    }
                    if (!this.loaded) {
                        completeDefine(this.name);
                        if (!this.loaded) {
                            console.warn('should be loaded');
                        }
                    }
                }
            };
        }
        this.bundle = function (moduleNames, noLoadedBias, ignoredBundles) {
            var self = this;

            var scoredBundles = _helpers.map(_helpers.grep(this.bundles, function (x) { return typeof (ignoredBundles) === 'undefined' || ignoredBundles.indexOf(x.name) == -1; }), function (bundle) {
                return {
                    bundle: bundle,
                    state: self.getOrCreateState(bundle.name),
                    hits: _helpers.grep(bundle.modules, function (f) {
                        return moduleNames.indexOf(f) >= 0;
                    }),
                    waste: _helpers.grep(bundle.modules, function (f) {
                        return moduleNames.indexOf(f) === -1;
                    }),
                    wasteAmount: function () {
                        if (noLoadedBias) {
                            return this.waste.length;
                        }
                        if (this.state.pending) {
                            return 0;
                        }
                        return this.waste.length;
                    },
                    score: function () {
                        return this.hits.length - this.wasteAmount();
                    }
                }
            });
            scoredBundles.sort(function (a, b) {
                return (b.score()) - (a.score());
            });
            var matchingBundles = _helpers.grep(scoredBundles, function (x) {
                if (noLoadedBias) {
                    return x.hits.length > 1;
                }
                if (x.state.pending) {
                    return x.hits.length > 0;
                }
                return x.hits.length > 1;
            });
            if (!matchingBundles.length) {
                if (debug)
                    console.log('no bundles available', moduleNames, scoredBundles);
                return moduleNames;
            }


            var moduleStack = moduleNames.slice();
            var bundleStack = matchingBundles.slice();
            var candidates = matchingBundles.slice();
            var rewrittenModuleNames = [];
            var usedBundles = [];

            var iteration = 0;
            while (moduleStack.length > 0 && bundleStack.length > 0) {
                iteration++;
                var bundle = bundleStack.shift();
                var considerationState = {
                    hits: bundle.hits.length,
                    pending: bundle.state.pending,
                    noLoadedBias: noLoadedBias,
                    hitModules: bundle.hits.slice()
                }
                if (considerationState.pending && !considerationState.noLoadedBias) {
                    if (considerationState.hits === 0) {
                        if (debug)
                            console.log('skipping bundle', bundle, bundle.hits, moduleStack, moduleNames);
                        considerationState.rejected = true;
                        continue;
                    }
                }
                else {
                    if (considerationState.hits <= 1) {
                        if (debug)
                            console.log('skipping bundle because it is not pending and only has one hit', bundle, bundle.hits);
                        considerationState.rejected = true;
                        continue;
                    }
                }

                bundle.bundle.usages.push({
                    considerationState: considerationState,
                    moduleStack: moduleStack.slice(),
                    hits: bundle.hits.slice(),
                    hitCount: bundle.hits.length,
                    waste: bundle.waste.slice(),
                    wasteAmount: bundle.wasteAmount(),
                    wasPending: bundle.state.pending,
                    score: bundle.score(),
                    candidates: _helpers.map(candidates, function (bundle) { return { hits: bundle.hits.slice(), waste: bundle.waste, wasteAmount: bundle.wasteAmount(), wasPending: bundle.state.pending, score: bundle.score() }; }), remainingCandidates: _helpers.map(bundleStack, function (bundle) { return { hits: bundle.hits.slice(), waste: bundle.waste, wasteAmount: bundle.wasteAmount(), wasPending: bundle.state.pending, score: bundle.score() }; })
                });

                for (var i = 0; i < bundle.hits.length; i++) {
                    var moduleName = bundle.hits[i];

                    var index = moduleStack.indexOf(moduleName);
                    moduleStack.splice(index, 1);
                }
                rewrittenModuleNames.push(bundle.bundle.name);
                usedBundles.push(bundle);

                for (var i = 0; i < bundleStack.length; i++) {
                    (function (bundle) {
                        bundle.hits = _helpers.grep(bundle.hits, function (f) {
                            return (moduleStack.indexOf(f) >= 0);
                        });
                        bundle.waste = _helpers.grep(bundle.bundle.modules, function (f) {
                            return bundle.hits.indexOf(f) == -1;
                        });
                    })(bundleStack[i]);
                }
                bundleStack.sort(function (a, b) {
                    return (b.score()) - (a.score());
                });
            }
            for (var i = 0; i < moduleStack.length; i++) {
                rewrittenModuleNames.push(moduleStack[i]);
            }

            if (!usedBundles.length) {
                if (debug)
                    console.log('no bundles found', moduleNames, matchingBundles, moduleStack, bundleStack);
                return moduleNames;
            }
            return rewrittenModuleNames;
        }

    }
    var debug = false;
    var config = new configuration(
        requireFiles || [],
        []
    );

    Object.defineProperty(Object.prototype, "getProp", {
        value: function (prop) {
            if (!prop || (typeof (prop) !== typeof ('') && !(prop instanceof String))) {
                console.warn('invalid property access', prop, this);
                return null;
            }
            var key, self = this;
            for (key in self) {
                if (key.toLowerCase() === prop.toLowerCase()) {
                    return self[key];
                }
            }
        },
        //this keeps jquery happy
        enumerable: false
    });

    
    
    function invokeCallback(callback, moduleNames, exportsName) {
        var callbackArguments = [];
        var resultFactory = function (r) { return r; }
        for (var i = 0; i < moduleNames.length; i++) {
            switch (moduleNames[i]) {
                case 'exports':
                    if (!window.exports) {
                        window.exports = {};
                    }
                    var e = window.exports[exportsName];
                    if (!e) {
                        e = window.exports[exportsName] = {};
                    }
                    resultFactory = function () { return e; };
                    callbackArguments.push(e);
                    break;
                case 'require':
                    callbackArguments.push(require);
                    break;
                default:
                    var state = config.getOrCreateState(moduleNames[i]);
                    callbackArguments.push(state.object);
            }



        }
        var result = callback.apply(window, callbackArguments);
        return resultFactory(result);
    }

    function taskCompletionChecker(callback, moduleNames) {
        this.pendingStates = getPendingStates(moduleNames);
        moduleNames = config.bundle(moduleNames);
        var self = this;

        this.pendingModules = getPendingStates(moduleNames);

        function getPendingStates(moduleNames) {
            var pendingStates = [];
            for (var i = 0; i < moduleNames.length; i++) {
                var script = moduleNames[i];
                var state = config.getOrCreateState(script);
                if (!state.ready) {
                    state.callbacks.push(function () {
                        self.check();
                    });
                    pendingStates.push(state);
                }
            }
            return pendingStates;
        }

        this.check = function () {
            if (this.finished) {
                return true;
            }

            this.pendingStates = _helpers.grep(this.pendingStates, function (x) { return !x.ready; });

            if (!this.pendingStates.length) {
                this.done();
                return true;
            } else {
                notDone();
                return false;
            }
        };
        this.done = function () {
            this.finished = true;
            var s = _helpers.map(moduleNames, function (x) { return config.getOrCreateState(x); });
            for (var i = 0; i < s.length; i++) {
                var state = s[i];
                if (state.readyCheck) {
                    state.readyCheck();
                }
            }
            callback();
            if (debug)
                console.log('done', moduleNames);
        }
        function notDone() {
            if (debug)
                console.log('not done', _helpers.grep(self.pendingStates, function (x) { return !x.ready; }));
        }

    }


    function getScriptCached (url, callback) {
        var d = document, t = 'script',
            o = d.createElement(t),
            s = d.getElementsByTagName(t)[0];
        o.src = url;
        o.defer = true;
        if (callback) {

            o.addEventListener('load', function (e) {
                if (debug)
                    console.log('loaded script', url, e);
                callback(null, e);
            }, false);
        } else {
            console.warn('loading script without callback', url);
        }

        s.parentNode.insertBefore(o, s);
    }
    
    window.require = require = function require (moduleNames, callback, log) {
        if (typeof (moduleNames) === typeof ('') || moduleNames instanceof String) {
            moduleNames = [moduleNames];
        }
        callback = (function (callback, moduleNames) {
            return function () {
                invokeCallback(callback, moduleNames);
            }
        })(callback, moduleNames);
        moduleNames = config.getSpeculativeDependencies(moduleNames);
        var taskChecker = new taskCompletionChecker(callback, moduleNames);
        if (taskChecker.check()) {
            return;
        }

        var pendingStates = _helpers.grep(taskChecker.pendingModules, function (x) { return x.hasUrl() && !x.pending });
        if (!pendingStates.length) {
            return;
        }

        for (var i = 0; i < pendingStates.length; i++) {
            (function (state) {
                if (state.loaded && !state.ready) {
                    state.callbacks.push(function () {
                        taskChecker.check();
                    });
                } else {
                    state.callPending();
                    getScriptCached(state.url, function () {
                        state.callDone();
                    });   
                }
            })(pendingStates[i]);
        }

    };

    require.getScriptCached = getScriptCached;

    require.getStyleSheet = function (path, fn, scope) {
        var head = document.getElementsByTagName('head')[0], // reference to document.head for appending/ removing link nodes
            link = document.createElement('link');           // create the link node
        link.setAttribute('href', path);
        link.setAttribute('rel', 'stylesheet');
        link.setAttribute('type', 'text/css');

        var sheet, cssRules;
        // get the correct properties to check for depending on the browser
        if ('sheet' in link) {
            sheet = 'sheet'; cssRules = 'cssRules';
        }
        else {
            sheet = 'styleSheet'; cssRules = 'rules';
        }

        var interval_id = setInterval(function () {                     // start checking whether the style sheet has successfully loaded
                try {
                    if (link[sheet] && link[sheet][cssRules].length) { // SUCCESS! our style sheet has loaded
                        clearInterval(interval_id);                      // clear the counters
                        clearTimeout(timeout_id);
                        fn.call(scope || window, true, link);           // fire the callback with success == true
                    }
                } catch (e) { } finally { }
            }, 10),                                                   // how often to check if the stylesheet is loaded
            timeout_id = setTimeout(function () {       // start counting down till fail
                clearInterval(interval_id);             // clear the counters
                clearTimeout(timeout_id);
                head.removeChild(link);                // since the style sheet didn't load, remove the link node from the DOM
                fn.call(scope || window, false, link); // fire the callback with success == false
            }, 15000);                                 // how long to wait before failing

        head.appendChild(link);  // insert the link node into the DOM and start loading the style sheet

        return link; // return the link node;
    }
    
    require.resource = function (scripts, callback) {
        function flattenArray(arr) {
            if (!_helpers.isArray(arr)) {
                return arr;
            }

            return _helpers.map(arr, function (n) {
                return flattenArray(n)
            });
        }
        scripts = flattenArray(scripts).reverse();
        if (typeof (scripts) == 'undefined') {
            console.error('$.require called without scripts', arguments);
            return;
        }
        function next() {
            if (!scripts.length) {
                callback();
                return;
            }

            if (!window.loadedScripts) {
                window.loadedScripts = [];
            }
            if (!window.loadingScriptCallbacks) {
                window.loadingScriptCallbacks = {};
            }

            var script = scripts.pop();
            var css = script.indexOf('.css') != -1;

            var originalScript = script;
            if (script.indexOf('~/') == 0) {
                script = script.replace('~/', $siteRoot());
            }

            if (window.loadingScriptCallbacks[script]) {
                window.loadingScriptCallbacks[script].push(function () {
                    next();
                });
                return;
            }

            if (!css) {
                if (document.querySelector('script[src="' + script + '"]') || window.loadedScripts.indexOf(script) != -1) {
                    next();
                    return;
                }
            } else {
                if (document.querySelector('link[href="' + script + '"]') || window.loadedScripts.indexOf(script) != -1) {
                    next();
                    return;
                }
            }

            window.loadingScriptCallbacks[script] = [function () {
                next();
            }];

            function done() {
                window.loadedScripts.push(script);
                document.dispatchEvent(new CustomEvent("require.scriptLoaded", { detail: originalScript }));
                for (var i = 0; i < window.loadingScriptCallbacks[script].length; i++) {
                    window.loadingScriptCallbacks[script][i]();
                }
                delete window.loadingScriptCallbacks[script];
            }

            if (css) {
                require.getStyleSheet(script, function () {
                    done();
                });
            }
            else {
                require.getScriptCached(script, function () {
                    done();
                });
            }
        }
        next();
    };

    _helpers.getOnce = function (options) {
        if (!window.getOnceCallbacks) {
            window.getOnceCallbacks = {};
        }
        if (options.data) {
            var s = _helpers.param(options.data, options.traditional);
            if (options.url.indexOf('?') == -1) {
                options.url += '?' + s;
            }
            else {
                options.url += '&' + s;
            }
            delete options.data;
        }
        if (window.getOnceCallbacks[options.url]) {
            window.getOnceCallbacks[options.url].push({
                success: options.success,
                error: options.error
            });
            return;
        }

        window.getOnceCallbacks[options.url] = [{
            success: options.success,
            error: options.error
        }];
        function done(m, a) {
            var callbacks = window.getOnceCallbacks[options.url];
            for (var i = 0; i < callbacks.length; i++) {
                callbacks[i][m].apply(this, a);
            }
            delete window.getOnceCallbacks[options.url];
        }

        options.success = function () {
            done.apply(this, ['success', arguments]);

        };
        options.error = function () {
            done.apply(this, ['error', arguments]);
        };
        _helpers.ajax(options);
    }

    function getModuleName(url) {
        var u = url;
        u = u.substring(u.lastIndexOf('/') + 1);
        u = u.substring(0, u.lastIndexOf('.'));
        return u;
    }

    function completeDefine(moduleName) {
        if (typeof (window._pendingDefine) === 'undefined') {
            define(moduleName, [], function () { });
            return;
        }
        var moduleDependencies = window._pendingDefine.moduleDependencies;
        var callback = window._pendingDefine.callback;
        delete window._pendingDefine;
        var state = config.getOrCreateState(moduleName);

        config.updateState(state, moduleDependencies, callback);
    }

    var amd = window.define && window.define.amd ? window.define.amd : {};

    window.define = function (moduleName, moduleDependencies, callback) {
        if (typeof (moduleName) === "string") {
            if (_helpers.isFunction(moduleDependencies)) {
                callback = moduleDependencies;
            }
        } else if (_helpers.isArray(moduleName)) {
            callback = moduleDependencies;
            moduleDependencies = moduleName;
            moduleName = null;
        } else if (_helpers.isFunction(moduleName)) {
            callback = moduleName;
            moduleDependencies = [];
            moduleName = null;
        }
        if (debug)
            console.log('define', moduleName, moduleDependencies);

        window._pendingDefine = {
            moduleDependencies: moduleDependencies,
            callback: callback
        };
        // if (moduleName == null && document.currentScript){
        //     try {
        //         moduleName = getModuleName(document.currentScript.src);
        //         if (moduleName.indexOf(".min") === moduleName.length - 4){
        //             moduleName = moduleName.substring(0, moduleName.length - 4);
        //         }
        //     } catch (e) {
        //         console && console.warn("Could not resolve current script");
        //     }
        //
        // }
        if (moduleName != null) {
            completeDefine(moduleName);
        }


    }
    window.define.amd = amd;


    function getFlatDependencies(moduleName) {
        var state = window.define.amd[moduleName];
        var dependencies = [];
        if (!state || !state.moduleDependencies) {
            return dependencies;
        }
        for (var i = 0; i < state.moduleDependencies.length; i++) {
            if (state.moduleDependencies[i].indexOf('/') != -1) {
                continue;
            }
            dependencies.push(state.moduleDependencies[i]);
            var deps = getFlatDependencies(state.moduleDependencies[i]);
            for (var j = 0; j < deps.length; j++) {
                if (dependencies.indexOf(deps[j]) === -1)
                    dependencies.push(deps[j]);
            }
        }
        return dependencies;
    }

    window.flatDependencies = function (moduleName, ignoredBundles) {
        var r = {};
        for (var k in window.define.amd) {
            if (k.indexOf('anonymous') === -1) {
                var deps = getFlatDependencies(k);
                if (deps.length)
                    r[k] = deps;
            }
        }
        if (typeof (moduleName) != 'undefined') {
            var deps = r[moduleName];
            if (typeof (deps) == 'undefined') {
                console.warn('no dependencies for', moduleName, r);
                return;
            }
            deps = deps.slice();
            deps.push(moduleName);
            deps = config.bundle(deps, true, ignoredBundles);
            var s = '\n';
            for (var i = 0; i < deps.length; i++) {
                var state = config.getOrCreateState(deps[i]);
                if (state.isBundle) {
                    continue;
                }
                if (state.url)
                    s += '///' + ' <reference path="' + state.url.split('?')[0] + '" />\n';
            }
            console.log(s);
        }
        return r;
    };
    window.pendingModules = function () {
        var states = [];
        for (var k in window.define.amd) {
            states.push(window.define.amd[k]);
        }

        return {
            blocked: _helpers.grep(states, function (x) {
                return (x.pending && !x.ready) || _helpers.grep(x.dependencies || [], function (d) { return !x.ready; }).length;
            }),
            missing: _helpers.grep(states, function (x) {
                return !x.isBundle && !x.loaded;
            })
        };
    }
    window.explainBundle = function (name) {
        var bundle = config.bundlesByName[name];
        if (!bundle) {
            console.warn('no bundle named', name);
        }
        console.log(bundle.usages);
    };
})(window.__requireFiles);