distil 0.14.0.i → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/assets/distil.js CHANGED
@@ -3,335 +3,344 @@
3
3
  /** A resource module defined in the module that provides the module.
4
4
  */
5
5
 
6
- (function(distil, window, document){
7
-
8
- var moduleIndex= {};
9
- var fetched= {};
10
- var root= document.documentElement;
11
- var head= document.getElementsByTagName('head')[0]||root;
12
-
13
- var XHR= window.XMLHttpRequest || (function(){
14
- var progIdCandidates= ['Msxml2.XMLHTTP.4.0', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP'];
15
- var len= progIdCandidates.length;
16
-
17
- var progId;
18
- var xhr;
19
-
20
- function ConstructXhr()
21
- {
22
- return new window.ActiveXObject(ConstructXhr.progId);
23
- }
24
-
25
- while (len--)
26
- {
27
- try
6
+ (function(distil, window, document)
7
+ {
8
+
9
+ var moduleIndex = {};
10
+ var fetched = {};
11
+ var root = document.documentElement;
12
+ var head = document.getElementsByTagName('head')[0] || root;
13
+
14
+ var XHR = window.XMLHttpRequest || (function()
28
15
  {
29
- progId= progIdCandidates[len];
30
- xhr= new window.ActiveXObject(progId);
31
- // ActiveXObject constructor throws an exception
32
- // if the component isn't available.
33
- xhr= null;
34
- ConstructXhr.progId= progId;
35
- return ConstructXhr;
36
- }
37
- catch (e)
16
+ var progIdCandidates = ['Msxml2.XMLHTTP.4.0', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP'];
17
+ var len = progIdCandidates.length;
18
+
19
+ var progId;
20
+ var xhr;
21
+
22
+ function ConstructXhr()
23
+ {
24
+ return new window.ActiveXObject(ConstructXhr.progId);
25
+ }
26
+
27
+ while (len--)
28
+ {
29
+ try
30
+ {
31
+ progId = progIdCandidates[len];
32
+ xhr = new window.ActiveXObject(progId);
33
+ // ActiveXObject constructor throws an exception
34
+ // if the component isn't available.
35
+ xhr = null;
36
+ ConstructXhr.progId = progId;
37
+ return ConstructXhr;
38
+ }
39
+ catch (e)
40
+ {
41
+ // Ignore the error
42
+ }
43
+ }
44
+ throw new Error('No XMLHttpRequest implementation found');
45
+ })();
46
+
47
+ var noop = function()
48
+ {};
49
+ var fetchAsset = function(url, callback, scope, userData, sync)
38
50
  {
39
- // Ignore the error
40
- }
41
- }
42
- throw new Error('No XMLHttpRequest implementation found');
43
- })();
44
-
45
- var noop= function(){};
46
- var fetchAsset= function(url, callback, scope, userData)
47
- {
48
- if (url in fetched)
49
- {
50
- // The callback will ALWAYS be called after the calling program
51
- // finishes executing.
52
- window.setTimeout(function(){ callback.call(scope, userData); }, 0);
53
- return;
54
- }
55
-
56
- var xhr= new XHR();
57
- xhr.open('GET', url, true);
58
-
59
- xhr.onreadystatechange= function()
60
- {
61
- if (4!==xhr.readyState)
62
- return;
63
- fetched[url]= true;
64
- var status= xhr.status;
65
- var succeeded= 0==status || (status>=200 && status<300) || 304==status;
66
-
67
- if (!succeeded)
68
- throw new Error('Failed to load resource: status=' + status + ' url=' + url);
69
-
70
- callback.call(scope, userData);
71
- xhr.onreadystatechange= noop;
72
- xhr= null;
73
- }
74
- xhr.send(null);
75
- };
51
+ if (url in fetched)
52
+ {
53
+ // The callback will ALWAYS be called after the calling program
54
+ // finishes executing.
55
+ window.setTimeout(function()
56
+ {
57
+ callback.call(scope, userData);
58
+ },
59
+ 0);
60
+ return;
61
+ }
76
62
 
77
- var SCRIPT_TYPE= 'js';
78
- var JSNIB_TYPE= 'jsnib';
79
- var CSS_TYPE= 'css';
80
- var MODULE_TYPE= 'module';
81
- var NO_MODULE_ERROR= 'No module with name: ';
82
-
83
- var injectScript= distil.injectScript= function(url, callback, scope, userData)
84
- {
85
- var tag= document.createElement('script');
63
+ var xhr = new XHR();
64
+ xhr.open('GET', url, !sync);
86
65
 
87
- var complete= function()
88
- {
89
- if (callback)
90
- callback.call(scope, userData);
91
- tag.onreadystatechage= noop;
92
- tag= null;
93
- };
94
-
95
- window.__filename__= url;
96
-
97
- tag.onreadystatechange= function()
98
- {
99
- var readyState= tag && tag.readyState;
100
- if ('complete'===readyState || 'loaded'===readyState)
101
- complete();
102
- }
103
- tag.onload= complete;
104
- tag.type= "text/javascript";
105
- tag.src= url;
106
- // root.insertBefore(tag, root.firstChild);
107
- head.appendChild(tag);
108
- };
66
+ xhr.onreadystatechange = function()
67
+ {
68
+ if (4 !== xhr.readyState)
69
+ return;
70
+ fetched[url] = true;
71
+ var status = xhr.status;
72
+ var succeeded = (0 === status) || (status >= 200 && status < 300) || 304 == status;
109
73
 
110
- var injectStylesheet= distil.injectStylesheet= function(url, callback, scope, userData)
111
- {
112
- var link= document.createElement('link');
113
- link.type='text/css';
114
- link.rel='stylesheet';
115
- link.href='url';
116
- head.appendChild(link);
117
-
118
- if (callback)
119
- callback.call(scope, url, userData);
120
- };
121
-
122
- var getRunningScriptSource= function()
123
- {
124
- var scripts= document.getElementsByTagName("script");
125
- if (!scripts || !scripts.length)
126
- throw new Error("Could not find script");
74
+ if (!succeeded)
75
+ throw new Error('Failed to load resource: status=' + status + ' url=' + url);
127
76
 
128
- var l= scripts.length;
129
- var s, src, lastSlash;
77
+ callback.call(scope, userData, xhr);
78
+ xhr.onreadystatechange = noop;
79
+ xhr = null;
80
+ }
81
+ xhr.send(null);
82
+ };
130
83
 
131
- while (l--)
132
- {
133
- if ((src= scripts[l].src))
134
- return src;
135
- }
136
-
137
- throw new Error("No script tags with src attribute.");
138
- };
139
-
140
- var ResourceInfo= function(type, url, callback, scope, userData)
141
- {
142
- if (!type)
143
- {
144
- var lastDot= url.lastIndexOf('.');
145
- type= (-1!==lastDot)?url.substring(lastDot+1):"";
146
- type= type.split('?')[0];
147
- }
148
-
149
- var lastSlash= url.lastIndexOf('/');
150
- if (-1===lastSlash)
151
- throw new Error("Couldn't determine path from script src: " + url);
152
-
153
- return {
154
- type: type,
155
- url: url,
156
- callbacks: callback ? [callback]: [],
157
- scope: scope,
158
- userData: userData,
159
- loadQueue: [],
160
- fetched: false,
161
- injected: false,
162
- callbacksExecuted: false,
163
- path: url.substring(0, lastSlash+1),
164
- parent: null
165
- };
166
- };
84
+ var SCRIPT_TYPE = 'js';
85
+ var JSNIB_TYPE = 'jsnib';
86
+ var CSS_TYPE = 'css';
87
+ var MODULE_TYPE = 'module';
88
+ var NO_MODULE_ERROR = 'No module with name: ';
89
+
90
+ var injectScript = distil.injectScript = function(url, callback, scope, userData)
91
+ {
92
+ var tag = document.createElement('script');
93
+
94
+ var complete = function()
95
+ {
96
+ if (callback)
97
+ callback.call(scope, userData);
98
+ tag.onreadystatechage = noop;
99
+ tag = null;
100
+ };
101
+
102
+ window.__filename__ = url;
103
+
104
+ tag.onreadystatechange = function()
105
+ {
106
+ var readyState = tag && tag.readyState;
107
+ if ('complete' === readyState || 'loaded' === readyState)
108
+ complete();
109
+ }
110
+ tag.onload = complete;
111
+ tag.type = "text/javascript";
112
+ tag.src = url;
113
+ // root.insertBefore(tag, root.firstChild);
114
+ head.appendChild(tag);
115
+ };
116
+
117
+ var injectStylesheet = distil.injectStylesheet = function(url, callback, scope, userData)
118
+ {
119
+ var link = document.createElement('link');
120
+ link.type = 'text/css';
121
+ link.rel = 'stylesheet';
122
+ link.href = 'url';
123
+ head.appendChild(link);
124
+
125
+ if (callback)
126
+ callback.call(scope, url, userData);
127
+ };
128
+
129
+ var getRunningScriptSource = function()
130
+ {
131
+ var scripts = document.getElementsByTagName("script");
132
+ if (!scripts || !scripts.length)
133
+ throw new Error("Could not find script");
134
+
135
+ var l = scripts.length;
136
+ var s, src, lastSlash;
167
137
 
168
- var rootResource= ResourceInfo(SCRIPT_TYPE, getRunningScriptSource());
169
- rootResource.fetched= true;
170
- rootResource.injected= true;
171
-
172
- var args= (rootResource.url.split('?')[1]||"").split('&');
173
- var argsLen= args.length;
138
+ while (l--)
139
+ {
140
+ if ((src = scripts[l].src))
141
+ return src;
142
+ }
143
+
144
+ throw new Error("No script tags with src attribute.");
145
+ };
146
+
147
+ var ResourceInfo = function(type, url, callback, scope, userData)
148
+ {
149
+ if (!type)
150
+ {
151
+ var lastDot = url.lastIndexOf('.');
152
+ type = (-1 !== lastDot) ? url.substring(lastDot + 1) : "";
153
+ type = type.split('?')[0];
154
+ }
155
+
156
+ var lastSlash = url.lastIndexOf('/');
157
+ if (-1 === lastSlash)
158
+ throw new Error("Couldn't determine path from script src: " + url);
159
+
160
+ return {
161
+ type: type,
162
+ url: url,
163
+ callbacks: callback ? [callback] : [],
164
+ scope: scope,
165
+ userData: userData,
166
+ loadQueue: [],
167
+ fetched: false,
168
+ injected: false,
169
+ callbacksExecuted: false,
170
+ path: url.substring(0, lastSlash + 1),
171
+ parent: null
172
+ };
173
+ };
174
+
175
+ var rootResource = ResourceInfo(SCRIPT_TYPE, getRunningScriptSource());
176
+ rootResource.fetched = true;
177
+ rootResource.injected = true;
178
+
179
+ var args = (rootResource.url.split('?')[1] || "").split('&');
180
+ var argsLen = args.length;
174
181
  while (argsLen--)
175
182
  {
176
- if (args[argsLen]==='sync=true')
177
- distil.sync= true;
183
+ if (args[argsLen] === 'sync=true')
184
+ distil.sync = true;
178
185
  }
179
-
186
+
180
187
  /** currentResource is the resource that is currently executing.
181
188
  */
182
- var currentResource= rootResource;
189
+ var currentResource = rootResource;
183
190
 
184
- var injectResource= function(resource)
185
- {
186
- resource.injected= true;
187
- switch (resource.type)
188
- {
189
- case SCRIPT_TYPE:
190
- case JSNIB_TYPE:
191
- injectScript(resource.url, injectionComplete, null, resource);
192
- break;
193
- case CSS_TYPE:
194
- injectStylesheet(resource.url, injectionComplete, null, resource);
195
- break;
196
- default:
197
- throw new Error('Unknown resource type: ' + resource.type);
198
- }
199
- };
191
+ var injectResource = function(resource)
192
+ {
193
+ resource.injected = true;
194
+ switch (resource.type)
195
+ {
196
+ case SCRIPT_TYPE:
197
+ case JSNIB_TYPE:
198
+ injectScript(resource.url, injectionComplete, null, resource);
199
+ break;
200
+ case CSS_TYPE:
201
+ injectStylesheet(resource.url, injectionComplete, null, resource);
202
+ break;
203
+ default:
204
+ throw new Error('Unknown resource type: ' + resource.type);
205
+ }
206
+ };
200
207
 
201
208
  /** The execution complete callback for the script.
202
209
  */
203
- var injectionComplete= function(resource)
204
- {
205
- while (resource)
206
- {
207
- if (resource.loadQueue.length)
210
+ var injectionComplete = function(resource)
208
211
  {
209
- currentResource= resource= resource.loadQueue.shift();
210
- resource.complete= true;
211
- if (!resource.fetched)
212
- return;
213
- if (MODULE_TYPE===resource.type)
214
- continue;
215
- injectResource(resource);
216
- return;
217
- }
212
+ while (resource)
213
+ {
214
+ if (resource.loadQueue.length)
215
+ {
216
+ currentResource = resource = resource.loadQueue.shift();
217
+ resource.complete = true;
218
+ if (!resource.fetched)
219
+ return;
220
+ if (MODULE_TYPE === resource.type)
221
+ continue;
222
+ injectResource(resource);
223
+ return;
224
+ }
218
225
 
219
- resource.callbacksExecuted= true;
220
- while (resource.callbacks.length)
221
- (resource.callbacks.shift()).call(resource.scope, resource.userData);
222
-
223
- resource= resource.parent;
224
- }
225
-
226
- currentResource= resource ? resource : rootResource;
227
- };
228
-
229
- var fetchComplete= function(resource)
230
- {
231
- resource.fetched= true;
232
- var parent= resource.parent;
233
-
234
- if (resource===currentResource)
235
- injectResource(resource);
236
- };
237
-
238
- var loadResource= function(url, callback, scope, userData, parent)
239
- {
240
- if (distil.debug)
241
- url+= '?'+(new Date()).valueOf();
242
-
243
- var resource= ResourceInfo(null, url, callback, scope, userData);
244
- parent= parent||currentResource;
245
- resource.parent= parent;
246
- parent.loadQueue.push(resource);
247
- fetchAsset(url, fetchComplete, null, resource);
248
- };
226
+ resource.callbacksExecuted = true;
227
+ while (resource.callbacks.length)
228
+ {
229
+ (resource.callbacks.shift()).call(resource.scope, resource.userData);
230
+ }
249
231
 
250
- var loadFiles= function(module)
251
- {
252
- var files= (module.loadQueue||[]).concat(module.required);
253
- var resource= module.resource;
254
- var path= module.path;
255
-
256
- for (var i=0, len=files.length; i<len; ++i)
257
- {
258
- // loadResource(path + files[i], null, null, null, resource);
259
- if (distil.sync && '.js'===files[i].slice(-3).toLowerCase())
260
- document.write('<script src="'+path+files[i]+'"></script>');
261
- else
262
- loadResource(path + files[i], null, null, null, resource);
263
- }
264
- };
265
-
266
- distil.moduleCount= 0;
267
-
268
- distil.module= function(name, def)
232
+ resource = resource.parent;
233
+ }
234
+
235
+ currentResource = resource ? resource : rootResource;
236
+ };
237
+
238
+ var fetchComplete = function(resource)
239
+ {
240
+ resource.fetched = true;
241
+ var parent = resource.parent;
242
+
243
+ if (resource === currentResource)
244
+ injectResource(resource);
245
+ };
246
+
247
+ var loadResource = function(url, callback, scope, userData, parent)
248
+ {
249
+ if (distil.debug)
250
+ url += '?' + (new Date()).valueOf();
251
+
252
+ var resource = ResourceInfo(null, url, callback, scope, userData);
253
+ parent = parent || currentResource;
254
+ resource.parent = parent;
255
+ parent.loadQueue.push(resource);
256
+ fetchAsset(url, fetchComplete, null, resource);
257
+ };
258
+
259
+ var loadFiles = function(module)
260
+ {
261
+ var files = (module.loadQueue || []).concat(module.required);
262
+ var resource = module.resource;
263
+ var path = module.path;
264
+
265
+ for (var i = 0, len = files.length; i < len; ++i)
266
+ {
267
+ // loadResource(path + files[i], null, null, null, resource);
268
+ if (distil.sync && '.js' === files[i].slice(-3).toLowerCase())
269
+ document.write('<script src="' + path + files[i] + '"></script>');
270
+ else
271
+ loadResource(path + files[i], null, null, null, resource);
272
+ }
273
+ };
274
+
275
+ distil.moduleCount = 0;
276
+
277
+ distil.module = function(name, def)
269
278
  {
270
279
  if (name in moduleIndex)
271
280
  {
272
- var module= moduleIndex[name];
281
+ var module = moduleIndex[name];
273
282
  for (var p in def)
274
- module[p]= def[p];
275
-
283
+ module[p] = def[p];
284
+
276
285
  distil.require(name);
277
286
  return;
278
287
  }
279
288
 
280
289
  distil.moduleCount++;
281
-
290
+
282
291
  if (distil.sync)
283
292
  {
284
- var url= getRunningScriptSource();
285
- var lastSlash= url.lastIndexOf('/');
286
- def.path= url.substring(0,lastSlash+1);
293
+ var url = getRunningScriptSource();
294
+ var lastSlash = url.lastIndexOf('/');
295
+ def.path = url.substring(0, lastSlash + 1);
287
296
  }
288
297
  else
289
- def.path= currentResource.path;
290
-
291
- if ('/'!==def.path.slice(-1))
292
- def.path+='/';
293
-
294
- def.callbacks= [];
295
- def.loadQueue= [];
296
-
298
+ def.path = currentResource.path;
299
+
300
+ if ('/' !== def.path.slice(-1))
301
+ def.path += '/';
302
+
303
+ def.callbacks = [];
304
+ def.loadQueue = [];
305
+
297
306
  if (!distil.mainModule)
298
- distil.mainModule= def;
307
+ distil.mainModule = def;
299
308
  else
300
309
  {
301
310
  // @HACK: This makes resources work in secondary bundles
302
311
  var key;
303
312
  var value;
304
- var main= distil.mainModule;
305
-
306
- for (key in def.assets||{})
313
+ var main = distil.mainModule;
314
+
315
+ for (key in def.assets || {})
307
316
  {
308
317
  if (key in main.assets)
309
318
  continue;
310
- main.assets[key]= def.assets[key];
319
+ main.assets[key] = def.assets[key];
311
320
  }
312
- for (key in def.asset_map||{})
321
+ for (key in def.asset_map || {})
313
322
  {
314
323
  if (key in main.asset_map)
315
324
  continue;
316
- main.asset_map[key]= def.asset_map[key];
325
+ main.asset_map[key] = def.asset_map[key];
317
326
  }
318
327
  }
319
-
320
- moduleIndex[name]= def;
328
+
329
+ moduleIndex[name] = def;
321
330
  if (def.required && def.required.length)
322
331
  distil.require(name);
323
332
  };
324
333
 
325
- distil.queue= function(name, fragment)
334
+ distil.queue = function(name, fragment)
326
335
  {
327
- var module= moduleIndex[name];
336
+ var module = moduleIndex[name];
328
337
  if (module.resource)
329
338
  loadResource(currentResource.path + fragment, null, null, null, module.resource);
330
339
  else
331
340
  module.loadQueue.push(fragment);
332
341
  };
333
-
334
- distil.onready= function(callback)
342
+
343
+ distil.onready = function(callback)
335
344
  {
336
345
  if (rootResource.callbacksExecuted)
337
346
  window.setTimeout(callback, 0);
@@ -339,71 +348,112 @@
339
348
  rootResource.callbacks.push(callback);
340
349
  }
341
350
 
342
- distil.complete= function(name)
351
+ distil.complete = function(name)
343
352
  {
344
- var module= moduleIndex[name];
353
+ var module = moduleIndex[name];
345
354
  if (module.loadQueue.length)
346
355
  distil.require(name);
347
- }
348
-
349
- distil.require= function(name, callback, scope, userData)
356
+ }
357
+
358
+ distil.require = function(name, callback, scope, userData)
350
359
  {
351
- var module= moduleIndex[name];
360
+ var module = moduleIndex[name];
352
361
  if (!module)
353
362
  throw new Error(NO_MODULE_ERROR + name);
354
363
 
355
- var complete= function()
356
- {
357
- if (callback)
358
- callback.call(scope, userData);
359
- module.loaded= true;
360
- module.resource= null;
361
- };
364
+ var complete = function()
365
+ {
366
+ if (callback)
367
+ callback.call(scope, userData);
368
+ module.loaded = true;
369
+ module.resource = null;
370
+ };
362
371
 
363
372
  if (module.loaded)
364
373
  {
365
374
  window.setTimeout(complete, 0);
366
375
  return;
367
376
  }
368
-
369
- var resource= module.resource= ResourceInfo(MODULE_TYPE, module.path, complete);
370
- resource.parent= currentResource;
371
- resource.fetched= true;
377
+
378
+ var resource = module.resource = ResourceInfo(MODULE_TYPE, module.path, complete);
379
+ resource.parent = currentResource;
380
+ resource.fetched = true;
372
381
  currentResource.loadQueue.push(resource);
373
382
 
374
383
  loadFiles(module);
375
-
376
- if (rootResource===currentResource)
384
+
385
+ if (rootResource === currentResource)
377
386
  injectionComplete(currentResource);
378
387
  }
379
388
 
380
- distil.moduleDidLoad= function(moduleName)
389
+ distil.moduleDidLoad = function(moduleName)
381
390
  {
382
391
  distil.moduleCount--;
383
- if (!distil.moduleCount && rootResource===currentResource)
392
+ if (!distil.moduleCount && rootResource === currentResource)
384
393
  injectionComplete(currentResource);
385
394
  }
386
-
387
- distil.kick= distil.moduleDidLoad;
388
-
389
- distil.urlForAssetWithNameInModule= function(asset, moduleName)
395
+
396
+ distil.kick = distil.moduleDidLoad;
397
+
398
+ distil.urlForAssetWithNameInModule = function(asset, moduleName)
390
399
  {
391
- var module= name ? moduleIndex[moduleName] : distil.mainModule;
400
+ var module = moduleName ? moduleIndex[moduleName] : distil.mainModule;
392
401
  if (!module)
393
402
  throw new Error(NO_MODULE_ERROR + moduleName);
403
+
404
+ var path;
405
+ if (distil.sync)
406
+ {
407
+ var url = getRunningScriptSource();
408
+ var lastSlash = url.lastIndexOf('/');
409
+ path = url.substring(0, lastSlash + 1);
410
+ }
411
+ else
412
+ path = module.path;
413
+
394
414
  if (!module.asset_map)
395
- return module.path + asset;
396
- return module.path + (module.asset_map[asset]||asset);
415
+ return path + asset;
416
+
417
+ return path + (module.asset_map[asset] || asset);
397
418
  }
398
419
 
399
- distil.dataForAssetWithNameInModule= function(asset, moduleName)
420
+ distil.dataForAssetWithNameInModule = function(asset, moduleName)
400
421
  {
401
- var module= name ? moduleIndex[moduleName] : distil.mainModule;
422
+ var module = moduleName ? moduleIndex[moduleName] : distil.mainModule;
402
423
  if (!module)
403
424
  throw new Error(NO_MODULE_ERROR + moduleName);
425
+
404
426
  if (!module.assets)
427
+ module.assets = {};
428
+
429
+ var data = module.assets[asset];
430
+ if (void(0) != data)
431
+ return data;
432
+
433
+ if (!module.asset_map)
405
434
  return null;
406
- return module.assets[asset]||null;
435
+
436
+ var path;
437
+ if (distil.sync)
438
+ {
439
+ var url = getRunningScriptSource();
440
+ var lastSlash = url.lastIndexOf('/');
441
+ path = url.substring(0, lastSlash + 1);
442
+ }
443
+ else
444
+ path = module.path;
445
+
446
+ var assetUrl = path + (module.asset_map[asset] || asset);
447
+
448
+ function oncomplete(unused, xhr)
449
+ {
450
+ module.assets[asset] = data = xhr.responseText;
451
+ }
452
+ fetchAsset(assetUrl, oncomplete, null, null, true);
453
+ return data;
407
454
  }
408
-
409
- })(window.distil={}, window, document);
455
+
456
+ distil.asset = distil.dataForAssetWithNameInModule;
457
+ distil.assetUrl = distil.urlForAssetWithNameInModule;
458
+
459
+ })(window.distil = {}, window, document);