aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/montage.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /node_modules/montage/montage.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'node_modules/montage/montage.js')
-rwxr-xr-xnode_modules/montage/montage.js362
1 files changed, 362 insertions, 0 deletions
diff --git a/node_modules/montage/montage.js b/node_modules/montage/montage.js
new file mode 100755
index 00000000..07975ae8
--- /dev/null
+++ b/node_modules/montage/montage.js
@@ -0,0 +1,362 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6
7document._montageTiming = {}
8document._montageTiming.loadStartTime = Date.now();
9
10// Give a threshold before we decide we need to show the bootstrapper progress
11// Applications that use our loader will interact with this timeout
12// and class name to coordinate a nice loading experience. Applications that do not will
13// just go about business as usual and draw their content as soon as possible.
14window.addEventListener("DOMContentLoaded", function() {
15 var bootstrappingDelay = 1000;
16 document._montageStartBootstrappingTimeout = setTimeout(function() {
17 document._montageStartBootstrappingTimeout = null;
18
19 var root = document.documentElement;
20 if(!!root.classList) {
21 root.classList.add("montage-app-bootstrapping");
22 } else {
23 root.className = root.className + " montage-app-bootstrapping";
24 }
25
26 document._montageTiming.bootstrappingStartTime = Date.now();
27 }, bootstrappingDelay);
28});
29
30(function (definition) {
31 if (typeof require !== "undefined") {
32 // CommonJS / NodeJS
33 definition(require, exports, module);
34 } else {
35 // <script>
36 definition({}, {}, {});
37 }
38})(function (require, exports, module) {
39
40 // The global context object, works for the browser and for node.
41 // XXX Will not work in strict mode
42 var global = (function() {
43 return this;
44 })();
45
46 /**
47 * Initializes Montage and creates the application singleton if necessary.
48 * @param options
49 * @param callback
50 */
51 exports.initMontage = function () {
52 var platform = exports.getPlatform();
53 var params = platform.getParams();
54 var config = platform.getConfig();
55
56 // Platform dependent
57 platform.loadCJS(function (CJS, Q, URL) {
58
59 // setup the reel loader
60 config.makeLoader = function (config) {
61 return exports.ReelLoader(config,
62 CJS.DefaultLoaderConstructor(config));
63 };
64
65 // setup serialization compiler
66 config.makeCompiler = function (config) {
67 return exports.TemplateCompiler(config,
68 exports.SerializationCompiler(config,
69 CJS.DefaultCompilerConstructor(config)));
70 };
71
72 var location = URL.resolve(window.location, params["package"] || ".");
73
74 CJS.PackageSandbox(params.montageBase, config)
75 .then(function (montageRequire) {
76 montageRequire.config.modules["core/promise"] = {exports: Q};
77 montageRequire.config.modules["core/url"] = {exports: URL};
78 montageRequire.config.modules["core/shim/timers"] = {exports: {}};
79 return montageRequire.loadPackage(location)
80 .then(function (applicationRequire) {
81 global.require = applicationRequire;
82 global.montageRequire = montageRequire;
83 platform.initMontage(montageRequire, applicationRequire, params);
84 })
85 })
86 .end();
87
88 });
89
90 };
91
92 /**
93 Adds "_montage_metadata" property to all objects and function attached to
94 the exports object.
95 @see Compiler middleware in require/require.js
96 @param config
97 @param compiler
98 */
99 exports.SerializationCompiler = function(config, compiler) {
100 return function(def) {
101 def = compiler(def);
102 var defaultFactory = def.factory;
103 def.factory = function(require, exports, module) {
104 defaultFactory.call(this, require, exports, module);
105 for (var symbol in exports) {
106 // avoid attempting to reinitialize an aliased property
107 if (
108 Object.prototype.hasOwnProperty.call(
109 exports[symbol],
110 "_montage_metadata"
111 )
112 ) {
113 exports[symbol]._montage_metadata.aliases.push(symbol);
114 exports[symbol]._montage_metadata.objectName = symbol;
115 } else if (!Object.isSealed(exports[symbol])) {
116 Object.defineProperty(
117 exports[symbol],
118 "_montage_metadata",
119 {
120 value: {
121 require: require,
122 moduleId: module.id,
123 objectName: symbol,
124 aliases: [symbol],
125 isInstance: false
126 }
127 }
128 );
129 }
130 }
131 };
132 return def;
133 };
134 };
135
136 /**
137 * Allows reel directories to load the contained eponymous JavaScript
138 * module.
139 * @see Loader middleware in require/require.js
140 * @param config
141 * @param loader the next loader in the chain
142 */
143 var reelExpression = /([^\/]+)\.reel$/;
144 exports.ReelLoader = function (config, loader) {
145 return function (id, callback) {
146 var match = reelExpression.exec(id);
147 if (match) {
148 return loader(id + "/" + match[1], callback);
149 } else {
150 return loader(id, callback);
151 }
152 };
153 };
154
155 /**
156 Allows the reel's html file to be loaded via require.
157 @see Compiler middleware in require/require.js
158 @param config
159 @param compiler
160 */
161 exports.TemplateCompiler = function(config, compiler) {
162 return function(def) {
163 var root = def.path.match(/(.*\/)?(?=[^\/]+\.html$)/);
164 if (root) {
165 def.dependencies = def.dependencies || [];
166 var originalFactory = def.factory;
167 def.factory = function(require, exports, module) {
168 if (originalFactory) {
169 originalFactory(require, exports, module);
170 }
171 // Use module.exports in case originalFactory changed it.
172 module.exports.root = module.exports.root || root;
173 module.exports.content = module.exports.content || def.text;
174 };
175 return def;
176 } else {
177 return compiler(def);
178 }
179 };
180 };
181
182 // Bootstrapping for multiple-platforms
183
184 exports.getPlatform = function () {
185 if (typeof window !== "undefined" && window && window.document) {
186 return browser;
187 } else {
188 throw new Error("Platform not supported.");
189 }
190 };
191
192 var browser = {
193
194 getConfig: function() {
195 return {
196 lib: ".",
197 base: window.location,
198 // Disable XHR loader for file://
199 xhr: window.location.protocol.indexOf("file:") !== 0
200 };
201 },
202
203 getParams: function() {
204 var i, j,
205 match,
206 script,
207 attr,
208 name;
209 if (!this._params) {
210 this._params = {};
211 // Find the <script> that loads us, so we can divine our
212 // parameters from its attributes.
213 var scripts = document.getElementsByTagName("script");
214 for (i = 0; i < scripts.length; i++) {
215 script = scripts[i];
216 if (script.src && (match = script.src.match(/^(.*)montage.js(?:[\?\.]|$)/i))) {
217 this._params.montageBase = match[1];
218 if (script.dataset) {
219 for (name in script.dataset) {
220 this._params[name] = script.dataset[name];
221 }
222 } else if (script.attributes) {
223 for (j = 0; j < script.attributes.length; j++) {
224 attr = script.attributes[j];
225 match = attr.name.match(/^data-(.*)$/);
226 if (match) {
227 this._params[match[1]] = attr.value;
228 }
229 }
230 }
231 // Permits multiple montage.js <scripts>; by