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 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 688× 688× 688× 688× 688× 688× 688× 688× 688× 688× 688× 688× 3× 3× 3× 3× 1× 3× 3× 2× 2× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 5× 5× 5× 4× 1× 1× 1× 1× 1× 1× 1× 1× 1× 2× 1× 1× 1× 1× 9× 4× 4× 9× 1× 1× 1× 1× 1× 1× 1× 2× 1× 1× 1× 1× 1× 4× 1× 1× 4× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /*global define */ (function (root, factory) { Iif (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define('montage', [], factory); } else Iif (typeof module === 'object' && module.exports) { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. module.exports = factory(require, exports, module); } else { // Browser globals (root is window) root.Montage = factory({}, {}, {}); } }(this, function (require, exports, module) { "use strict"; // reassigning causes eval to not use lexical scope. var globalEval = eval, /*jshint evil:true */ global = globalEval('this'); /*jshint evil:false */ // Here we expose global for legacy mop support. // TODO move to mr cause it's loader role to expose // TODO make sure mop closure has it also cause it's mop role to expose global.global = global; /** * Initializes Montage and creates the application singleton if * necessary. */ exports.initMontage = function () { var platform = exports.getPlatform(); // Platform dependent platform.bootstrap(function (Require, Promise, URL) { var params = platform.getParams(); var config = { // This takes <base> into account location: Require.getLocation() }; exports.Require = Require; var montageLocation = URL.resolve(config.location, params.montageLocation); var location = URL.resolve(config.location, params.package || "."); var applicationHash = params.applicationHash; Iif (typeof global.BUNDLE === "object") { var bundleDefinitions = {}; var getDefinition = function (name) { if(!bundleDefinitions[name]) { var defer = bundleDefinitions[name] = {}; var deferPromise = new Promise(function(resolve, reject) { defer.resolve = resolve; defer.reject = reject; }); defer.promise = deferPromise; return defer; } return bundleDefinitions[name]; }; global.bundleLoaded = function (name) { getDefinition(name).resolve(); }; var preloading = {}; var preloadingPromise = new Promise(function(resolve, reject) { preloading.resolve = resolve; preloading.reject = reject; }); preloading.promise = preloadingPromise; config.preloaded = preloading.promise; // preload bundles sequentially var preloaded = Promise.resolve(); global.BUNDLE.forEach(function (bundleLocations) { preloaded = preloaded.then(function () { return Promise.all(bundleLocations.map(function (bundleLocation) { browser.load(bundleLocation); return getDefinition(bundleLocation).promise; })); }); }); // then release the module loader to run normally preloading.resolve(preloaded.then(function () { delete global.BUNDLE; delete global.bundleLoaded; })); } var applicationRequirePromise; Eif (!("remoteTrigger" in params)) { Iif ("autoPackage" in params) { Require.injectPackageDescription(location, { dependencies: { montage: "*" } }, config); } else { // handle explicit package.json location Iif (location.slice(location.length - 5) === ".json") { var packageDescriptionLocation = location; location = URL.resolve(location, "."); Require.injectPackageDescriptionLocation( location, packageDescriptionLocation, config ); } } applicationRequirePromise = Require.loadPackage({ location: location, hash: applicationHash }, config); } else { // allows the bootstrapping to be remote controlled by the // parent window, with a dynamically generated package // description window.postMessage({ type: "montageReady" }, "*"); var trigger = new Promise(function(resolve) { var messageCallback = function (event) { if ( params.remoteTrigger === event.origin && (event.source === window || event.source === window.parent) ) { switch (event.data.type) { case "montageInit": window.removeEventListener("message", messageCallback); resolve([event.data.location, event.data.injections]); break; case "isMontageReady": // allow the injector to query the state in case // they missed the first message window.postMessage({ type: "montageReady" }, "*"); } } }; window.addEventListener("message", messageCallback); }); applicationRequirePromise = trigger.spread(function (location, injections) { var promise = Require.loadPackage({ location: location, hash: applicationHash }, config); if (injections) { promise = promise.then(function (applicationRequire) { location = URL.resolve(location, "."); var packageDescriptions = injections.packageDescriptions, packageDescriptionLocations = injections.packageDescriptionLocations, mappings = injections.mappings, dependencies = injections.dependencies, index, injectionsLength; if (packageDescriptions) { injectionsLength = packageDescriptions.length; for (index = 0; index < injectionsLength; index++) { applicationRequire.injectPackageDescription( packageDescriptions[index].location, packageDescriptions[index].description); } } if (packageDescriptionLocations) { injectionsLength = packageDescriptionLocations.length; for (index = 0; index < injectionsLength; index++) { applicationRequire.injectPackageDescriptionLocation( packageDescriptionLocations[index].location, packageDescriptionLocations[index].descriptionLocation); } } if (mappings) { injectionsLength = mappings.length; for (index = 0; index < injectionsLength; index++) { applicationRequire.injectMapping( mappings[index].dependency, mappings[index].name); } } if (dependencies) { injectionsLength = dependencies.length; for (index = 0; index < injectionsLength; index++) { applicationRequire.injectDependency( dependencies[index].name, dependencies[index].version); } } return applicationRequire; }); } return promise; }); } applicationRequirePromise.then(function (applicationRequire) { return applicationRequire.loadPackage({ location: montageLocation, hash: params.montageHash }) .then(function (montageRequire) { // load the promise package so we can inject the bootstrapped // promise library back into it var promiseLocation; Iif (params.promiseLocation) { promiseLocation = URL.resolve(Require.getLocation(), params.promiseLocation); } else { //promiseLocation = URL.resolve(montageLocation, "packages/mr/packages/q"); //node tools/build --features="core timers call_get" --browser promiseLocation = URL.resolve(montageLocation, "node_modules/bluebird"); } var result = [ montageRequire, montageRequire.loadPackage({ location: promiseLocation, hash: params.promiseHash }) ]; return result; }) .spread(function (montageRequire, promiseRequire) { montageRequire.inject("core/mini-url", URL); montageRequire.inject("core/promise", {Promise: Promise}); promiseRequire.inject("bluebird", Promise); // This prevents bluebird to be loaded twice by mousse's code promiseRequire.inject("js/browser/bluebird", Promise); // install the linter, which loads on the first error config.lint = function (module) { montageRequire.async("jslint/lib/jslint") .then(function (JSHINT) { if (!JSHINT.JSHINT(module.text)) { console.warn("JSHint Error: "+module.location); JSHINT.JSHINT.errors.forEach(function (error) { if (error) { console.warn("Problem at line "+error.line+" character "+error.character+": "+error.reason); if (error.evidence) { console.warn(" " + error.evidence); } } }); } }); }; // Expose global require and mr global.require = global.mr = applicationRequire; return platform.initMontage(montageRequire, applicationRequire, params); }); }); }); }; // Bootstrapping for multiple-platforms exports.getPlatform = function () { Eif (typeof window !== "undefined" && window && window.document) { return browser; } else if (typeof process !== "undefined") { return require("./node.js"); } else { throw new Error("Platform not supported."); } }; var browser = { makeResolve: function () { try { var testHost = "http://example.org", testPath = "/test.html", resolved = new URL(testPath, testHost).href; Eif (!resolved || resolved !== testHost + testPath) { throw new Error('NotSupported'); } return function (base, relative) { return new URL(relative, base).href; }; } catch (err) { var IS_ABSOLUTE_REG = /^[\w\-]+:/, head = document.querySelector("head"), currentBaseElement = head.querySelector("base"), baseElement = document.createElement("base"), relativeElement = document.createElement("a"), needsRestore = false; Eif(currentBaseElement) { needsRestore = true; } else { currentBaseElement = document.createElement("base"); } // Optimization, we won't check ogain if there's a base tag. baseElement.href = ""; return function (base, relative) { var restore; Iif (!needsRestore) { head.appendChild(currentBaseElement); } base = String(base); Iif (IS_ABSOLUTE_REG.test(base) === false) { throw new Error("Can't resolve from a relative location: " + JSON.stringify(base) + " " + JSON.stringify(relative)); } Eif(needsRestore) { restore = currentBaseElement.href; } currentBaseElement.href = base; relativeElement.href = relative; var resolved = relativeElement.href; Eif (needsRestore) { currentBaseElement.href = restore; } else { head.removeChild(currentBaseElement); } return resolved; }; } }, load: function (location,loadCallback) { var script = document.createElement("script"); script.src = location; script.onload = function () { if(loadCallback) { loadCallback(script); } // remove clutter script.parentNode.removeChild(script); }; document.getElementsByTagName("head")[0].appendChild(script); }, getParams: function () { var i, j, match, script, montage, attr, name; if (!this._params) { this._params = {}; // Find the <script> that loads us, so we can divine our // parameters from its attributes. var scripts = document.getElementsByTagName("script"); for (i = 0; i < scripts.length; i++) { script = scripts[i]; montage = false; Eif (script.src && (match = script.src.match(/^(.*)montage.js(?:[\?\.]|$)/i))) { this._params.montageLocation = match[1]; montage = true; } Iif (script.hasAttribute("data-montage-location")) { this._params.montageLocation = script.getAttribute("data-montage-location"); montage = true; } Eif (montage) { Eif (script.dataset) { for (name in script.dataset) { this._params[name] = script.dataset[name]; } } else if (script.attributes) { var dataRe = /^data-(.*)$/, // TODO cache RegEx letterAfterDash = /-([a-z])/g, upperCaseChar = function (_, c) { return c.toUpperCase(); }; for (j = 0; j < script.attributes.length; j++) { attr = script.attributes[j]; match = attr.name.match(dataRe); if (match) { this._params[match[1].replace(letterAfterDash, upperCaseChar)] = attr.value; } } } // Permits multiple montage.js <scripts>; by // removing as they are discovered, next one // finds itself. script.parentNode.removeChild(script); break; } } } return this._params; }, bootstrap: function (callback) { var Require, DOM, Promise, URL; var params = this.getParams(); var resolve = this.makeResolve(); // observe dom loading and load scripts in parallel // observe dom loaded function domLoad() { document.removeEventListener("DOMContentLoaded", domLoad, true); DOM = true; // Give a threshold before we decide we need to show the bootstrapper progress // Applications that use our loader will interact with this timeout // and class name to coordinate a nice loading experience. Applications that do not will // just go about business as usual and draw their content as soon as possible. var root = document.documentElement; Eif(!!root.classList) { root.classList.add("montage-app-bootstrapping"); } else { root.className = root.className + " montage-app-bootstrapping"; } document._montageTiming = document._montageTiming || {}; document._montageTiming.bootstrappingStartTime = Date.now(); callbackIfReady(); } // this permits montage.js to be injected after DOMContentLoaded // http://jsperf.com/readystate-boolean-vs-regex/2 Eif (/interactive|complete/.test(document.readyState)) { domLoad(); } else { document.addEventListener("DOMContentLoaded", domLoad, true); } // determine which scripts to load var pending = { "require": "node_modules/mr/require.js", "require/browser": "node_modules/mr/browser.js", "promise": "node_modules/bluebird/js/browser/bluebird.min.js" // "shim-string": "core/shim/string.js" // needed for the `endsWith` function. }; // register module definitions for deferred, // serial execution var definitions = {}; global.bootstrap = function (id, factory) { definitions[id] = factory; delete pending[id]; for (var module in pending) { // this causes the function to exit if there are any remaining // scripts loading, on the first iteration. consider it // equivalent to an array length check return; } allModulesLoaded(); }; // load in parallel, but only if we're not using a preloaded cache. // otherwise, these scripts will be inlined after already Eif (typeof global.BUNDLE === "undefined") { var montageLocation = resolve(global.location, params.montageLocation); //Special Case bluebird for now: browser.load(resolve(montageLocation, pending.promise), function() { delete pending.promise; //global.bootstrap cleans itself from global once all known are loaded. "bluebird" is not known, so needs to do it first global.bootstrap("bluebird", function (require, exports) { return global.Promise; }); global.bootstrap("promise", function (require, exports) { return global.Promise; }); for (var id in pending) { browser.load(resolve(montageLocation, pending[id])); } }); } else { global.nativePromise = global.Promise; Object.defineProperty(global, "Promise", { configurable: true, set: function(PromiseValue) { Object.defineProperty(global, "Promise", { value: PromiseValue }); global.bootstrap("bluebird", function (require, exports) { return global.Promise; }); global.bootstrap("promise", function (require, exports) { return global.Promise; }); } }); } // global.bootstrap("shim-string"); // one module loaded for free, for use in require.js, browser.js global.bootstrap("mini-url", function (require, exports) { exports.resolve = resolve; }); // miniature module system var bootModules = {}; function bootRequire(id) { if (!bootModules[id] && definitions[id]) { var exports = bootModules[id] = {}; bootModules[id] = definitions[id](bootRequire, exports) || exports; } return bootModules[id]; } // execute bootstrap scripts function allModulesLoaded() { URL = bootRequire("mini-url"); Promise = bootRequire("promise"); Require = bootRequire("require"); // if we get past the for loop, bootstrapping is complete. get rid // of the bootstrap function and proceed. delete global.bootstrap; callbackIfReady(); } function callbackIfReady() { if (DOM && Require) { callback(Require, Promise, URL); } } }, initMontage: function (montageRequire, applicationRequire, params) { //exports.Require.delegate = this; var dependencies = [ "core/core", "core/event/event-manager", "core/serialization/deserializer/montage-reviver", "core/logger" ]; var Promise = montageRequire("core/promise").Promise; var deepLoadPromises = []; for(var i=0,iDependency;(iDependency = dependencies[i]);i++) { deepLoadPromises.push(montageRequire.deepLoad(iDependency)); } return Promise.all(deepLoadPromises) .then(function () { for(var i=0,iDependency;(iDependency = dependencies[i]);i++) { montageRequire(iDependency); } var Montage = montageRequire("core/core").Montage; var EventManager = montageRequire("core/event/event-manager").EventManager; var MontageReviver = montageRequire("core/serialization/deserializer/montage-reviver").MontageReviver; var logger = montageRequire("core/logger").logger; var defaultEventManager, application; // Load the event-manager defaultEventManager = new EventManager().initWithWindow(window); // montageWillLoad is mostly for testing purposes Iif (typeof global.montageWillLoad === "function") { global.montageWillLoad(); } // Load the application var appProto = applicationRequire.packageDescription.applicationPrototype, applicationLocation, appModulePromise; Iif (appProto) { applicationLocation = MontageReviver.parseObjectLocationId(appProto); appModulePromise = applicationRequire.async(applicationLocation.moduleId); } else { appModulePromise = montageRequire.async("core/application"); } return appModulePromise.then(function (exports) { var Application = exports[(applicationLocation ? applicationLocation.objectName : "Application")]; application = new Application(); defaultEventManager.application = application; application.eventManager = defaultEventManager; return application._load(applicationRequire, function() { Eif (params.module) { // If a module was specified in the config then we initialize it now applicationRequire.async(params.module); } Iif (typeof global.montageDidLoad === "function") { global.montageDidLoad(); } }); }); }); } }; Eif (typeof window !== "undefined") { Iif (global.__MONTAGE_LOADED__) { console.warn("Montage already loaded!"); } else { global.__MONTAGE_LOADED__ = true; exports.initMontage(); } } else { // may cause additional exports to be injected: exports.getPlatform(); } return exports; })); |