aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/loader.reel/loader.js
blob: 2ff0e42cdc202611574c42172981ebc825dc2682 (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
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

 /**
    @module montage/ui/loader
 */

var Montage = require("core/core").Montage,
    Component = require("ui/component").Component,
    logger = require("core/logger").logger("loader"),
    bootstrappingTimeoutPropertyName = "_montageStartBootstrappingTimeout",
    MONTAGE_BOOTSTRAPPER_ELEMENT_ID = "montage-app-bootstrapper",
    MONTAGE_LOADER_ELEMENT_ID = "montage-app-loader",
    BOOTSTRAPPING_CLASS_NAME = "montage-app-bootstrapping",
    LOADING_CLASS_NAME = "montage-app-loading",
    LOADED_CLASS_NAME = "montage-app-loaded",
    PRELOADING = 0,
    BOOTSTRAPPING = 1,
    LOADING = 2,
    LOADED = 3;

/**
 @class module:montage/ui/loader.Loader
 @extends module:montage/ui/component.Component
 */

exports.Loader = Montage.create(Component, /** @lends module:montage/ui/loader.Loader# */ {

    // Configuration Properties

/**
    The main module to require
*/
    mainModule: {
        enumerable: false,
        value: "main.reel"
    },

/**
    The name of the object to read from the mainModule exports
*/
    mainName: {
        enumerable: false,
        value: "Main"
    },

/**
    Whether or not to include framework modules in the collection of required and initialized modules
*/
    includeFrameworkModules: {
        enumerable: false,
        value: false
    },

/**
    The minimum amount of time the bootstrapping indicator must be shown for
*/
    minimumBootstrappingDuration: {
        enumerable: false,
        value: 1500
    },

/**
    The minimum amount of time the loading indicator must be shown for
*/
    minimumLoadingDuration: {
        enumerable: false,
        value: 2000
    },

    _initializedModules: {
        enumerable: false,
        value: null
    },

/**
    The initialized modules...FIXME
*/
    initializedModules: {
        dependencies: ["includeFrameworkModules"],
        enumerable: false,
        get: function() {
            if (!this._initializedModules || this.includeFrameworkModules) {
                return this._initializedModules;
            } else {
                return this._initializedModules.slice(this._frameworkModuleCount - 1);
            }
        },
        set: function(value) {
            this._initializedModules = value;
        }
    },

    _requiredModules: {
        enumerable: false,
        value: null
    },

/**
    The required modules for this application ... FIXME
*/
    requiredModules: {
        dependencies: ["includeFrameworkModules"],
        enumerable: false,
        get: function() {
            if (!this._requiredModules || this.includeFrameworkModules) {
                return this._requiredModules;
            } else {
                return this._requiredModules.slice(this._frameworkModuleCount - 1);
            }
        },
        set: function(value) {
            this._requiredModules = value;
        }
    },

    // States

    _currentStage: {
        enumerable: false,
        value: PRELOADING
    },

/**
    Current loading stage.
*/
    currentStage: {
        enumerable: false,
        get: function() {
            return this._currentStage;
        },
        set: function(value) {
            if (value === this._currentStage) {
                return;
            }

            if (logger.isDebug) {
                logger.debug(this, "CURRENT STAGE: " + value);
            }
            this._currentStage = value;
            this.needsDraw = true;
        }
    },

    _readyToShowLoader: {
        enumerable: false,
        value: false
    },

/**
    Boolean that specifies whether the loader is loading the application's main component.
*/
    isLoadingMainComponent: {
        enumerable: false,
        value: null
    },

/**
    Specifies whether the loader is ready to show the loading graphic...FIXME
*/
    readyToShowLoader: {
        enumerable: false,
        get: function() {
            return this._readyToShowLoader;
        },
        set: function(value) {
            if (value !== this._readyToShowLoader) {
                return;
            }

            this._readyToShowLoader = value;
            this.needsDraw = true;
        }
    },

/**
    Specifies whether the main component is ready to be displayed.
*/
    readyToShowMainComponent: {
        enumerable: false,
        get: function() {
            return !!this._mainComponent;
        }
    },

    // Internal Properties

    _frameworkModuleCount: {
        enumerable: false,
        value: null
    },

    hasTemplate: {
        enumerable: false,
        value: false
    },

    _mainComponent: {
        enumerable: false,
        value: null
    },

    _showLoadingTimeout: {
        enumerable: false,
        value: null
    },

    _showMainComponentTimeout: {
        enumerable: false,
        value: null
    },

    // Implementation

    templateDidLoad: {
        value: function() {

            if (logger.isDebug) {
                logger.debug(this, "templateDidLoad");
            }

            if (!this.element) {
                this.element = document.documentElement;
            }
            this.readyToShowLoader = true;

            var timing = document._montageTiming,
                remainingBootstrappingDelay,
                self = this;

            if (timing.bootstrappingStartTime) {

                if (logger.isDebug) {
                    logger.debug(this, "had already started bootstrapping");
                }

                // We just found out we were bootstrapping…
                this.currentStage = BOOTSTRAPPING;

                // but we're technically done bootstrapping and can show loader now if we should
                timing.bootstrappingEndTime = Date.now();

                remainingBootstrappingDelay = this.minimumBootstrappingDuration - (timing.bootstrappingEndTime - timing.bootstrappingStartTime)

                if (remainingBootstrappingDelay > 0) {
                    if (logger.isDebug) {
                        logger.debug(this, "still need to show bootstrapper for another " + remainingBootstrappingDelay + "ms");
                    }
                    this._showLoadingTimeout = setTimeout(function() {
                        self._revealLoader();
                    }, remainingBootstrappingDelay);
                } else {
                    this._revealLoader();
                }
            } else {
                // The bootstrapper hasn't decided to show yet, that's fine let's try to load main
                if (logger.isDebug) {
                    logger.debug(this, "bootstrapping has not started yet…");
                }
                this._loadMainComponent();
            }
        }
    },

    _revealLoader: {
        value: function() {

            if (logger.isDebug) {
                logger.debug(this, "_revealLoader");
            }

            this.currentStage = LOADING;
            document._montageTiming.loadingStartTime = Date.now();

            this._frameworkModuleCount = window.require.progress.requiredModules.length;

            Object.defineBinding(this, "initializedModules", {
                boundObject: window.require,
                boundObjectPropertyPath: "progress.initializedModules",
                oneway: true
            });

            Object.defineBinding(this, "requiredModules", {
                boundObject: window.require,
                boundObjectPropertyPath: "progress.requiredModules",
                oneway: true
            });

            var i,
                loaderElement = document.getElementById(MONTAGE_LOADER_ELEMENT_ID),
                children,
                iChild,
                iComponent;

            if (loaderElement) {
                children = loaderElement.children;

                for (i = 0; (iChild = children[i]); i++) {
                    if ((iComponent = iChild.controller)) {
                        iComponent.attachToParentComponent();
                        iComponent.needsDraw = true;
                    }
                }
            }

        }
    },

    _revealMainComponent: {
        value: function() {
            if (logger.isDebug) {
                logger.debug(this, "_revealMainComponent");
            }
            this.currentStage = LOADED;
        }
    },

    _loadMainComponent: {
        value: function() {
            if (logger.isDebug) {
                logger.debug(this, "_loadMainComponent");
            }
            this.isLoadingMainComponent = true;
            var self = this;
            window.require.async(this.mainModule)
            .then(function (exports) {
                return self._mainLoadedCallback(exports);
            })
            .end();
        }
    },

    _mainLoadedCallback: {
        value: function(exports) {
            if (logger.isDebug) {
                logger.debug(this, "_mainLoadedCallback");
            }
            // We've loaded the class for the mainComponent
            // instantiate it and lets find out what else we need to load
            // based on its template
            this._mainComponent = exports[this.mainName].create();
            this.childComponents.push(this._mainComponent);
            this._mainComponent.setElementWithParentComponent(document.createElement("div"), this);
            this._mainComponent.needsDraw = true;
        }
    },

    childComponentWillPrepareForDraw: {
        value: function(child) {
            var self = this,
                insertionElement;

            // if the mainComponent is ready to draw...
            if (child === this._mainComponent) {

                if (logger.isDebug) {
                    logger.debug(this, "main preparing to draw");
                }
                this.isLoadingMainComponent = false;

                // Determine old content
                this._contentToRemove = document.createRange();

                // If installing classnames on the documentElement (to affect as high a level as possible)
                // make sure content only ends up inside the body
                insertionElement = this.element === document.documentElement ? document.body : this.element;
                this._contentToRemove.selectNodeContents(insertionElement);

                // Add new content so mainComponent can actually draw
                this.childComponents = [this._mainComponent];
                insertionElement.appendChild(this._mainComponent.element);

                var startBootstrappingTimeout = document[bootstrappingTimeoutPropertyName],
                    timing = document._montageTiming,
                    remainingBootstrappingDelay,
                    remainingLoadingDelay;

                // if we hadn't even started to say we were bootstrapping…
                if (!timing.bootstrappingStartTime) {
                    // don't bother showing bootstrapping, just show the mainComponent
                    if (logger.isDebug) {
                        logger.debug(this, "bootstrapper never shown");
                    }
                    clearTimeout(startBootstrappingTimeout);
                    startBootstrappingTimeout = null;
                    this._revealMainComponent();
                }

                // Otherwise if we started bootstrapping, but never started loading…
                else if (timing.bootstrappingStartTime && !timing.loadingStartTime) {

                    // don't ever show the loader and wait until we've bootstrapped for the minimumBootstrappingDuration
                    clearTimeout(this._showLoadingTimeout);
                    this._showLoadingTimeout = null;

                    timing.bootstrappingEndTime = Date.now();

                    if ((remainingBootstrappingDelay = this.minimumBootstrappingDuration - (timing.bootstrappingEndTime - timing.bootstrappingStartTime)) > 0) {
                        if (logger.isDebug) {
                            logger.debug(this, "show bootstrapper for another " + remainingBootstrappingDelay + "ms");
                        }
                        this._showMainComponentTimeout = setTimeout(function () {
                            if (logger.isDebug) {
                                logger.debug(this, "ok, shown bootstrapper long enough");
                            }
                            self._revealMainComponent();
                        }, remainingBootstrappingDelay);
                    } else {
                        setTimeout(function () {
                            if (logger.isDebug) {
                                logger.debug(this, "ok, showing bootstrapper now");
                            }
                            self._revealMainComponent();
                        }, 0);
                    }
                }

                //Otherwise, we apparently started showing loading progress…
                else if (timing.loadingStartTime) {
                    timing.loadingEndTime = Date.now();

                    // wait until we've loaded for the minimumLoadingDuration
                    // TODO this is not precise, but it's a decent start for scheduling the delay
                    if ((remainingLoadingDelay = this.minimumLoadingDuration - (timing.loadingEndTime - timing.loadingStartTime)) > 0) {
                        if (logger.isDebug) {
                            logger.debug(this, "show loader for another " + remainingLoadingDelay + "ms");
                        }
                        this._showMainComponentTimeout = setTimeout(function () {
                            if (logger.isDebug) {
                                logger.debug(this, "ok, shown loader long enough");
                            }
                            self._revealMainComponent();;
                        }, remainingLoadingDelay);
                    } else {
                        // or we showed loading long enough, go ahead and show mainComponent
                        this._revealMainComponent();
                    }
                }


            }
        }
    },

/**
    Boolean that specifies whether to remove the loading content when load is completed
*/
    removeContentOnLoad: {
        value: true
    },

    _forceContentRemoval: {
        enumerable: false,
        value: false
    },

    _contentToRemove: {
        enumerable: false,
        value: null
    },

/**
    Forces a manual removal of loading content
*/
    removeContent: {
        value: function() {
            this._forceContentRemoval = true;
            this.needsDraw = true;
        }
    },

    draw: {
        value: function() {
            // start loading the mainComponent if we haven't already
            if (!this.readyToShowMainComponent && !this.isLoadingMainComponent) {
                if (logger.isDebug) {
                    logger.debug(this, "draw; start loading main component");
                }
                this._loadMainComponent();
            }

            // Reflect the current loading stage
            if (LOADING === this.currentStage) {

                this.element.classList.remove(BOOTSTRAPPING_CLASS_NAME);
                this.element.classList.add(LOADING_CLASS_NAME);

            } else if (LOADED === this.currentStage && this._contentToRemove) {

                this.element.classList.remove(BOOTSTRAPPING_CLASS_NAME);
                this.element.classList.remove(LOADING_CLASS_NAME);

                if(this.removeContentOnLoad || this._forceContentRemoval) {
                    this._contentToRemove.extractContents();
                    this._contentToRemove.detach();
                    this._contentToRemove = null;
                }

                this.element.classList.add(LOADED_CLASS_NAME);
            }

        }
    }

});