aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage-user/require/browser.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage-user/require/browser.js')
-rwxr-xr-xnode_modules/montage-user/require/browser.js112
1 files changed, 112 insertions, 0 deletions
diff --git a/node_modules/montage-user/require/browser.js b/node_modules/montage-user/require/browser.js
new file mode 100755
index 00000000..e249d355
--- /dev/null
+++ b/node_modules/montage-user/require/browser.js
@@ -0,0 +1,112 @@
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> */
6bootstrap("require/browser", function (require) {
7
8var Require = require("require/require");
9var Promise = require("core/promise").Promise;
10var URL = require("core/mini-url");
11
12var global = typeof global !== "undefined" ? global : window;
13
14Require.getLocation = function() {
15 return URL.resolve(window.location, ".");
16};
17
18Require.overlays = ["browser", "montage"];
19
20// Due to crazy variabile availability of new and old XHR APIs across
21// platforms, this implementation registers every known name for the event
22// listeners. The promise library ascertains that the returned promise
23// is resolved only by the first event.
24// http://dl.dropbox.com/u/131998/yui/misc/get/browser-capabilities.html
25Require.read = function (url) {
26
27 if (URL.resolve(window.location, url).indexOf("file:") === 0) {
28 throw new Error("XHR does not function for file: protocol");
29 }
30
31 var request = new XMLHttpRequest();
32 var response = Promise.defer();
33
34 function onload() {
35 if (xhrSuccess(request)) {
36 response.resolve(request.responseText);
37 } else {
38 onerror();
39 }
40 }
41
42 function onerror() {
43 response.reject("Can't XHR " + JSON.stringify(url));
44 }
45
46 try {
47 request.open("GET", url, true);
48 request.overrideMimeType("application/javascript");
49 request.onreadystatechange = function () {
50 if (request.readyState === 4) {
51 onload();
52 }
53 };
54 request.onload = request.load = onload;
55 request.onerror = request.error = onerror;
56 } catch (exception) {
57 response.reject(exception.message, exception);
58 }
59
60 request.send();
61 return response.promise;
62};
63
64// Determine if an XMLHttpRequest was successful
65// Some versions of WebKit return 0 for successful file:// URLs
66function xhrSuccess(req) {
67 return (req.status === 200 || (req.status === 0 && req.responseText));
68}
69
70// By using a named "eval" most browsers will execute in the global scope.
71// http://www.davidflanagan.com/2010/12/global-eval-in.html
72// Unfortunately execScript doesn't always return the value of the evaluated expression (at least in Chrome)
73var globalEval = /*this.execScript ||*/eval;
74// For Firebug evaled code isn't debuggable otherwise
75// http://code.google.com/p/fbug/issues/detail?id=2198
76if (global.navigator && global.navigator.userAgent.indexOf("Firefox") >= 0) {
77 globalEval = new Function("evalString", "return eval(evalString)");
78}
79
80Require.Compiler = function (config) {
81 return function(module) {
82 if (module.factory || module.text === void 0)
83 return module;
84
85 // Here we use a couple tricks to make debugging better in various browsers:
86 // TODO: determine if these are all necessary / the best options
87 // 1. name the function with something inteligible since some debuggers display the first part of each eval (Firebug)
88 // 2. append the "//@ sourceURL=location" hack (Safari, Chrome, Firebug)
89 // * http://pmuellr.blogspot.com/2009/06/debugger-friendly.html
90 // * http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/
91 // TODO: investigate why this isn't working in Firebug.
92 // 3. set displayName property on the factory function (Safari, Chrome)
93
94 var displayName = "__FILE__"+module.location.replace(/\.\w+$|\W/g, "__");
95
96 try {
97 module.factory = globalEval("(function "+displayName+"(require, exports, module) {"+module.text+"//*/\n})"+"\n//@ sourceURL="+module.location);
98 } catch (exception) {
99 throw new SyntaxError("in " + module.location + ": " + exception.message);
100 }
101
102 // This should work and would be simpler, but Firebug does not show scripts executed via "new Function()" constructor.
103 // TODO: sniff browser?
104 // module.factory = new Function("require", "exports", "module", module.text + "\n//*/"+sourceURLComment);
105
106 module.factory.displayName = displayName;
107
108 return module;
109 }
110};
111
112});