This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Builder.js
160 lines (135 loc) · 3.62 KB
/
Builder.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
(function () {
// Default settings for Builder
var settings = {
'template engine': function (tmpl) {
return tmpl;
},
'dom engine': function (content) {
return content;
}
};
// Create storage for before and after functions
var beforeFns = [],
afterFns = [];
/**
* Build chain for client side views. before -> template -> domify -> after -> return
* @param {String} tmpl Template to process through template engine
* @param {Object} [data] Data to pass through to template engine
* @returns {Mixed} Output from before -> template -> domify -> after -> return
*/
function Builder(tmpl, data) {
// Generate a this context for data
var that = {'tmpl': tmpl, 'data': data};
// Run the beforeFns on tmpl
tmpl = pre.call(that, tmpl);
// Convert the template into content
var content = template.call(that, tmpl, data);
// Pass the template through the dom engine
var $content = domify.call(that, content);
// Run the afterFns on $content
$content = post.call(that, $content);
// Return the $content
return $content;
}
/**
* Modify tmpl via beforeFns
* @param {String} tmpl Template string to modify
* @returns {String} Modified tmpl
*/
function pre(tmpl) {
// Iterate over the beforeFns
var i = 0,
len = beforeFns.length;
for (; i < len; i++) {
tmpl = beforeFns[i].call(this, tmpl) || tmpl;
}
// Return tmpl
return tmpl;
}
Builder.pre = pre;
Builder.beforeFns = beforeFns;
/**
* Parse template through its engine
* @param {String} tmpl Template to process through template engine
* @param {Object} [data] Data to pass through to template engine
*/
function template(tmpl, data) {
// Grab the template engine
var engine = settings['template engine'];
// Process the template through the template engine
var content = engine.call(this, tmpl, data);
// Return the content
return content;
}
Builder.template = template;
/**
* Convert HTML into HTMLElements, jQuery elements, or other
* @param {String} content HTML to pass through dom engine
*/
function domify(content) {
// Grab the dom engine
var engine = settings['dom engine'];
// Process the content through the dom engine
var $content = engine.call(this, content);
// Return the $content
return $content;
}
Builder.domify = domify;
/**
* Modify $content via afterFns
* @param {String} $content Content to modify
* @returns {String} Modified $content
*/
function post($content) {
// Iterate over the afterFns
var i = 0,
len = afterFns.length;
for (; i < len; i++) {
$content = afterFns[i].call(this, $content) || $content;
}
// Return tmpl
return $content;
}
Builder.post = post;
Builder.afterFns = afterFns;
/**
* Settings helper for Builder
* @param {String|Object} name If object, interpret as key-value pairs of settings. If string, save val under settings key.
* @param {Mixed} [val] Value to save under name
*/
function set(name, val) {
// If the name is an object
var key;
if (typeof name === 'object') {
// Iterate over its properties
for (key in name) {
if (name.hasOwnProperty(key)) {
// Set each one
set(key, name[key]);
}
}
} else {
// Otherwise, save to settings
settings[name] = val;
}
}
Builder.set = set;
Builder.settings = settings;
/**
* Helper method for saving new before methods
* @param {Function} fn Before method to add
*/
function before(fn) {
beforeFns.push(fn);
}
Builder.before = before;
/**
* Helper method for saving new after methods
* @param {Function} fn After method to add
*/
function after(fn) {
afterFns.push(fn);
}
Builder.after = after;
window.Builder = Builder;
}());