This repository has been archived by the owner on Nov 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathsystem.js
450 lines (405 loc) · 15.2 KB
/
system.js
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
/**
* The system module encapsulates the most basic features used by other modules.
* @module system
* @requires require
* @requires jquery
*/
define(['require', 'jquery'], function(require, $) {
var isDebugging = false,
nativeKeys = Object.keys,
hasOwnProperty = Object.prototype.hasOwnProperty,
toString = Object.prototype.toString,
system,
treatAsIE8 = false,
nativeIsArray = Array.isArray,
slice = Array.prototype.slice;
//polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
//see http://patik.com/blog/complete-cross-browser-console-log/
// Tell IE9 to use its built-in console
if (Function.prototype.bind && (typeof console === 'object' || typeof console === 'function') && typeof console.log == 'object') {
try {
['log', 'info', 'warn', 'error', 'assert', 'dir', 'clear', 'profile', 'profileEnd']
.forEach(function(method) {
console[method] = this.call(console[method], console);
}, Function.prototype.bind);
} catch (ex) {
treatAsIE8 = true;
}
}
// callback for dojo's loader
// note: if you wish to use Durandal with dojo's AMD loader,
// currently you must fork the dojo source with the following
// dojo/dojo.js, line 1187, the last line of the finishExec() function:
// (add) signal("moduleLoaded", [module.result, module.mid]);
// an enhancement request has been submitted to dojo to make this
// a permanent change. To view the status of this request, visit:
// http://bugs.dojotoolkit.org/ticket/16727
if (require.on) {
require.on("moduleLoaded", function(module, mid) {
system.setModuleId(module, mid);
});
}
// callback for require.js loader
if (typeof requirejs !== 'undefined') {
requirejs.onResourceLoad = function(context, map, depArray) {
system.setModuleId(context.defined[map.id], map.id);
};
}
var noop = function() { };
var log = function() {
try {
// Modern browsers
if (typeof console != 'undefined' && typeof console.log == 'function') {
// Opera 11
if (window.opera) {
var i = 0;
while (i < arguments.length) {
console.log('Item ' + (i + 1) + ': ' + arguments[i]);
i++;
}
}
// All other modern browsers
else if ((slice.call(arguments)).length == 1 && typeof slice.call(arguments)[0] == 'string') {
console.log((slice.call(arguments)).toString());
} else {
console.log.apply(console, slice.call(arguments));
}
}
// IE8
else if ((!Function.prototype.bind || treatAsIE8) && typeof console != 'undefined' && typeof console.log == 'object') {
Function.prototype.call.call(console.log, console, slice.call(arguments));
}
// IE7 and lower, and other old browsers
} catch (ignore) { }
};
var logError = function(error, err) {
var exception;
if(error instanceof Error){
exception = error;
} else {
exception = new Error(error);
}
exception.innerError = err;
//Report the error as an error, not as a log
try {
// Modern browsers (it's only a single item, no need for argument splitting as in log() above)
if (typeof console != 'undefined' && typeof console.error == 'function') {
console.error(exception);
}
// IE8
else if ((!Function.prototype.bind || treatAsIE8) && typeof console != 'undefined' && typeof console.error == 'object') {
Function.prototype.call.call(console.error, console, exception);
}
// IE7 and lower, and other old browsers
} catch (ignore) { }
throw exception;
};
/**
* @class SystemModule
* @static
*/
system = {
/**
* Durandal's version.
* @property {string} version
*/
version: "2.2.0",
/**
* A noop function.
* @method noop
*/
noop: noop,
/**
* Gets the module id for the specified object.
* @method getModuleId
* @param {object} obj The object whose module id you wish to determine.
* @return {string} The module id.
*/
getModuleId: function(obj) {
if (!obj) {
return null;
}
if (typeof obj == 'function' && obj.prototype) {
return obj.prototype.__moduleId__;
}
if (typeof obj == 'string') {
return null;
}
return obj.__moduleId__;
},
/**
* Sets the module id for the specified object.
* @method setModuleId
* @param {object} obj The object whose module id you wish to set.
* @param {string} id The id to set for the specified object.
*/
setModuleId: function(obj, id) {
if (!obj) {
return;
}
if (typeof obj == 'function' && obj.prototype) {
obj.prototype.__moduleId__ = id;
return;
}
if (typeof obj == 'string') {
return;
}
obj.__moduleId__ = id;
},
/**
* Resolves the default object instance for a module. If the module is an object, the module is returned. If the module is a function, that function is called with `new` and it's result is returned.
* @method resolveObject
* @param {object} module The module to use to get/create the default object for.
* @return {object} The default object for the module.
*/
resolveObject: function(module) {
if (system.isFunction(module)) {
return new module();
} else {
return module;
}
},
/**
* Gets/Sets whether or not Durandal is in debug mode.
* @method debug
* @param {boolean} [enable] Turns on/off debugging.
* @return {boolean} Whether or not Durandal is current debugging.
*/
debug: function(enable) {
if (arguments.length == 1) {
isDebugging = enable;
if (isDebugging) {
this.log = log;
this.error = logError;
this.log('Debug:Enabled');
} else {
this.log('Debug:Disabled');
this.log = noop;
this.error = noop;
}
}
return isDebugging;
},
/**
* Logs data to the console. Pass any number of parameters to be logged. Log output is not processed if the framework is not running in debug mode.
* @method log
* @param {object} info* The objects to log.
*/
log: noop,
/**
* Logs an error.
* @method error
* @param {string|Error} obj The error to report.
*/
error: noop,
/**
* Asserts a condition by throwing an error if the condition fails.
* @method assert
* @param {boolean} condition The condition to check.
* @param {string} message The message to report in the error if the condition check fails.
*/
assert: function (condition, message) {
if (!condition) {
system.error(new Error(message || 'Assert:Failed'));
}
},
/**
* Creates a deferred object which can be used to create a promise. Optionally pass a function action to perform which will be passed an object used in resolving the promise.
* @method defer
* @param {function} [action] The action to defer. You will be passed the deferred object as a paramter.
* @return {Deferred} The deferred object.
*/
defer: function(action) {
return $.Deferred(action);
},
/**
* Creates a simple V4 UUID. This should not be used as a PK in your database. It can be used to generate internal, unique ids. For a more robust solution see [node-uuid](https://github.com/broofa/node-uuid).
* @method guid
* @return {string} The guid.
*/
guid: function() {
var d = new Date().getTime();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d/16);
return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
});
},
/**
* Uses require.js to obtain a module. This function returns a promise which resolves with the module instance. You can pass more than one module id to this function or an array of ids. If more than one or an array is passed, then the promise will resolve with an array of module instances.
* @method acquire
* @param {string|string[]} moduleId The id(s) of the modules to load.
* @return {Promise} A promise for the loaded module(s).
*/
acquire: function() {
var modules,
first = arguments[0],
arrayRequest = false;
if(system.isArray(first)){
modules = first;
arrayRequest = true;
}else{
modules = slice.call(arguments, 0);
}
return this.defer(function(dfd) {
require(modules, function() {
var args = arguments;
setTimeout(function() {
if(args.length > 1 || arrayRequest){
dfd.resolve(slice.call(args, 0));
}else{
dfd.resolve(args[0]);
}
}, 1);
}, function(err){
dfd.reject(err);
});
}).promise();
},
/**
* Extends the first object with the properties of the following objects.
* @method extend
* @param {object} obj The target object to extend.
* @param {object} extension* Uses to extend the target object.
*/
extend: function(obj) {
var rest = slice.call(arguments, 1);
for (var i = 0; i < rest.length; i++) {
var source = rest[i];
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
}
return obj;
},
/**
* Uses a setTimeout to wait the specified milliseconds.
* @method wait
* @param {number} milliseconds The number of milliseconds to wait.
* @return {Promise}
*/
wait: function(milliseconds) {
return system.defer(function(dfd) {
setTimeout(dfd.resolve, milliseconds);
}).promise();
}
};
/**
* Gets all the owned keys of the specified object.
* @method keys
* @param {object} object The object whose owned keys should be returned.
* @return {string[]} The keys.
*/
system.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) {
throw new TypeError('Invalid object');
}
var keys = [];
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
keys[keys.length] = key;
}
}
return keys;
};
/**
* Determines if the specified object is an html element.
* @method isElement
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
system.isElement = function(obj) {
return !!(obj && obj.nodeType === 1);
};
/**
* Determines if the specified object is an array.
* @method isArray
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
system.isArray = nativeIsArray || function(obj) {
return toString.call(obj) == '[object Array]';
};
/**
* Determines if the specified object is...an object. ie. Not an array, string, etc.
* @method isObject
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
system.isObject = function(obj) {
return obj === Object(obj);
};
/**
* Determines if the specified object is a boolean.
* @method isBoolean
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
system.isBoolean = function(obj) {
return typeof(obj) === "boolean";
};
/**
* Determines if the specified object is a promise.
* @method isPromise
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
system.isPromise = function(obj) {
return obj && system.isFunction(obj.then);
};
/**
* Determines if the specified object is a function arguments object.
* @method isArguments
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
/**
* Determines if the specified object is a function.
* @method isFunction
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
/**
* Determines if the specified object is a string.
* @method isString
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
/**
* Determines if the specified object is a number.
* @method isNumber
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
/**
* Determines if the specified object is a date.
* @method isDate
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
/**
* Determines if the specified object is a boolean.
* @method isBoolean
* @param {object} object The object to check.
* @return {boolean} True if matches the type, false otherwise.
*/
//isArguments, isFunction, isString, isNumber, isDate, isRegExp.
var isChecks = ['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'];
function makeIsFunction(name) {
var value = '[object ' + name + ']';
system['is' + name] = function(obj) {
return toString.call(obj) == value;
};
}
for (var i = 0; i < isChecks.length; i++) {
makeIsFunction(isChecks[i]);
}
return system;
});