-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
230 lines (187 loc) · 4.33 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
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
'use strict';
var hasOwn = Object.prototype.hasOwnProperty;
function noop() {
return '';
}
function getStack(context) {
return context.$$layoutStack || (
context.$$layoutStack = []
);
}
function applyStack(context) {
var stack = getStack(context);
while (stack.length) {
stack.shift()(context);
}
}
function getActions(context) {
return context.$$layoutActions || (
context.$$layoutActions = {}
);
}
function getActionsByName(context, name) {
var actions = getActions(context);
return actions[name] || (
actions[name] = []
);
}
function applyAction(val, action) {
var context = this;
function fn() {
return action.fn(context, action.options);
}
switch (action.mode) {
case 'append': {
return val + fn();
}
case 'prepend': {
return fn() + val;
}
case 'replace': {
return fn();
}
default: {
return val;
}
}
}
function mixin(target) {
var arg, key,
len = arguments.length,
i = 1;
for (; i < len; i++) {
arg = arguments[i];
if (!arg) {
continue;
}
for (key in arg) {
// istanbul ignore else
if (hasOwn.call(arg, key)) {
target[key] = arg[key];
}
}
}
return target;
}
/**
* Generates an object of layout helpers.
*
* @type {Function}
* @param {Object} handlebars Handlebars instance.
* @return {Object} Object of helpers.
*/
function layouts(handlebars) {
var helpers = {
/**
* @method extend
* @param {String} name
* @param {?Object} customContext
* @param {Object} options
* @param {Function(Object)} options.fn
* @param {Object} options.hash
* @return {String} Rendered partial.
*/
extend: function (name, customContext, options) {
// Make `customContext` optional
if (arguments.length < 3) {
options = customContext;
customContext = null;
}
options = options || {};
var fn = options.fn || noop,
context = mixin({}, this, customContext, options.hash),
data = handlebars.createFrame(options.data),
template = handlebars.partials[name];
// Partial template required
if (template == null) {
throw new Error('Missing partial: \'' + name + '\'');
}
// Compile partial, if needed
if (typeof template !== 'function') {
template = handlebars.compile(template);
}
// Add overrides to stack
getStack(context).push(fn);
// Render partial
return template(context, { data: data });
},
/**
* @method embed
* @param {String} name
* @param {?Object} customContext
* @param {Object} options
* @param {Function(Object)} options.fn
* @param {Object} options.hash
* @return {String} Rendered partial.
*/
embed: function () {
var context = mixin({}, this || {});
// Reset context
context.$$layoutStack = null;
context.$$layoutActions = null;
// Extend
return helpers.extend.apply(context, arguments);
},
/**
* @method block
* @param {String} name
* @param {Object} options
* @param {Function(Object)} options.fn
* @return {String} Modified block content.
*/
block: function (name, options) {
options = options || {};
var fn = options.fn || noop,
data = handlebars.createFrame(options.data),
context = this || {};
applyStack(context);
return getActionsByName(context, name).reduce(
applyAction.bind(context),
fn(context, { data: data })
);
},
/**
* @method content
* @param {String} name
* @param {Object} options
* @param {Function(Object)} options.fn
* @param {Object} options.hash
* @param {String} options.hash.mode
* @return {String} Always empty.
*/
content: function (name, options) {
options = options || {};
var fn = options.fn,
data = handlebars.createFrame(options.data),
hash = options.hash || {},
mode = hash.mode || 'replace',
context = this || {};
applyStack(context);
// Getter
if (!fn) {
return name in getActions(context);
}
// Setter
getActionsByName(context, name).push({
options: { data: data },
mode: mode.toLowerCase(),
fn: fn
});
}
};
return helpers;
}
/**
* Registers layout helpers on a Handlebars instance.
*
* @method register
* @param {Object} handlebars Handlebars instance.
* @return {Object} Object of helpers.
* @static
*/
layouts.register = function (handlebars) {
var helpers = layouts(handlebars);
handlebars.registerHelper(helpers);
return helpers;
};
module.exports = layouts;