-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
67 lines (60 loc) · 2.49 KB
/
index.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
// Includes full template and expression parsing in bundle
var parsing = require('derby/parsing');
var path = require('path');
var DerbyStandalone = require('derby/lib/DerbyStandalone');
var derby = new DerbyStandalone();
var App = derby.App;
global.derby = module.exports = derby;
// Can be overriden to customize how templates are loaded. However, it must be
// synchronous, because app.loadViews() is synchronous
App.prototype.getTemplate = function(filename) {
return document.getElementById(filename);
};
App.prototype.loadViews = function(filename, namespace) {
var resolved = this._resolveTemplate(filename);
if (!resolved) {
throw new Error('Cannot find template "' + filename + '"');
}
this._registerTemplate(resolved.template, namespace, resolved.filename);
};
App.prototype._registerTemplate = function(template, namespace, filename) {
var file = template.innerHTML;
var app = this;
function onImport(attrs) {
var dir = path.dirname(filename);
var sourceFilename = path.resolve(dir, attrs.src);
var resolved = app._resolveTemplate(sourceFilename);
if (!resolved) {
throw new Error('Cannot find template "' + attrs.src + '" from "' + filename + '"');
}
importNamespace = parsing.getImportNamespace(namespace, attrs, resolved.filename);
app._registerTemplate(resolved.template, importNamespace, resolved.filename);
}
var items = parsing.parseViews(file, namespace, filename, onImport);
parsing.registerParsedViews(this, items);
};
App.prototype._resolveTemplate = function(filename) {
var resolved;
resolved = this._attemptResolveTemplate(filename);
if (resolved) return resolved;
resolved = this._attemptResolveTemplate(filename + '.html');
if (resolved) return resolved;
resolved = this._attemptResolveTemplate(filename + '/index');
if (resolved) return resolved;
resolved = this._attemptResolveTemplate(filename + '/index.html');
return resolved;
};
App.prototype._attemptResolveTemplate = function(filename) {
var template = this.getTemplate(filename);
if (template) return {template: template, filename: filename};
};
// DEPRECATED: This function is a legacy format that should be removed. Use
// loadViews() instead.
App.prototype.registerViews = function(selector) {
selector || (selector = 'script[type="text/template"]');
var templates = document.querySelectorAll(selector);
for (var i = 0; i < templates.length; i++) {
var template = templates[i];
this.views.register(template.id, template.innerHTML, template.dataset);
}
};