Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
define(
2
["./utils","./exception","exports"],
3
function(__dependency1__, __dependency2__, __exports__) {
4
"use strict";
5
var Utils = __dependency1__;
6
var Exception = __dependency2__["default"];
7
8
var VERSION = "3.0.0";
9
__exports__.VERSION = VERSION;var COMPILER_REVISION = 6;
10
__exports__.COMPILER_REVISION = COMPILER_REVISION;
11
var REVISION_CHANGES = {
12
1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
13
2: '== 1.0.0-rc.3',
14
3: '== 1.0.0-rc.4',
15
4: '== 1.x.x',
16
5: '== 2.0.0-alpha.x',
17
6: '>= 2.0.0-beta.1'
18
};
19
__exports__.REVISION_CHANGES = REVISION_CHANGES;
20
var isArray = Utils.isArray,
21
isFunction = Utils.isFunction,
22
toString = Utils.toString,
23
objectType = '[object Object]';
24
25
function HandlebarsEnvironment(helpers, partials) {
26
this.helpers = helpers || {};
27
this.partials = partials || {};
28
29
registerDefaultHelpers(this);
30
}
31
32
__exports__.HandlebarsEnvironment = HandlebarsEnvironment;HandlebarsEnvironment.prototype = {
33
constructor: HandlebarsEnvironment,
34
35
logger: logger,
36
log: log,
37
38
registerHelper: function(name, fn) {
39
if (toString.call(name) === objectType) {
40
if (fn) { throw new Exception('Arg not supported with multiple helpers'); }
41
Utils.extend(this.helpers, name);
42
} else {
43
this.helpers[name] = fn;
44
}
45
},
46
unregisterHelper: function(name) {
47
delete this.helpers[name];
48
},
49
50
registerPartial: function(name, partial) {
51
if (toString.call(name) === objectType) {
52
Utils.extend(this.partials, name);
53
} else {
54
if (typeof partial === 'undefined') {
55
throw new Exception('Attempting to register a partial as undefined');
56
}
57
this.partials[name] = partial;
58
}
59
},
60
unregisterPartial: function(name) {
61
delete this.partials[name];
62
}
63
};
64
65
function registerDefaultHelpers(instance) {
66
instance.registerHelper('helperMissing', function(/* [args, ]options */) {
67
if(arguments.length === 1) {
68
// A missing field in a {{foo}} constuct.
69
return undefined;
70
} else {
71
// Someone is actually trying to call something, blow up.
72
throw new Exception("Missing helper: '" + arguments[arguments.length-1].name + "'");
73
}
74
});
75
76
instance.registerHelper('blockHelperMissing', function(context, options) {
77
var inverse = options.inverse,
78
fn = options.fn;
79
80
if(context === true) {
81
return fn(this);
82
} else if(context === false || context == null) {
83
return inverse(this);
84
} else if (isArray(context)) {
85
if(context.length > 0) {
86
if (options.ids) {
87
options.ids = [options.name];
88
}
89
90
return instance.helpers.each(context, options);
91
} else {
92
return inverse(this);
93
}
94
} else {
95
if (options.data && options.ids) {
96
var data = createFrame(options.data);
97
data.contextPath = Utils.appendContextPath(options.data.contextPath, options.name);
98
options = {data: data};
99
}
100
101
return fn(context, options);
102
}
103
});
104
105
instance.registerHelper('each', function(context, options) {
106
if (!options) {
107
throw new Exception('Must pass iterator to #each');
108
}
109
110
var fn = options.fn, inverse = options.inverse;
111
var i = 0, ret = "", data;
112
113
var contextPath;
114
if (options.data && options.ids) {
115
contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.';
116
}
117
118
if (isFunction(context)) { context = context.call(this); }
119
120
if (options.data) {
121
data = createFrame(options.data);
122
}
123
124
function execIteration(key, i, last) {
125
if (data) {
126
data.key = key;
127
data.index = i;
128
data.first = i === 0;
129
data.last = !!last;
130
131
if (contextPath) {
132
data.contextPath = contextPath + key;
133
}
134
}
135
136
ret = ret + fn(context[key], {
137
data: data,
138
blockParams: Utils.blockParams([context[key], key], [contextPath + key, null])
139
});
140
}
141
142
if(context && typeof context === 'object') {
143
if (isArray(context)) {
144
for(var j = context.length; i<j; i++) {
145
execIteration(i, i, i === context.length-1);
146
}
147
} else {
148
var priorKey;
149
150
for(var key in context) {
151
if(context.hasOwnProperty(key)) {
152
// We're running the iterations one step out of sync so we can detect
153
// the last iteration without have to scan the object twice and create
154
// an itermediate keys array.
155
if (priorKey) {
156
execIteration(priorKey, i-1);
157
}
158
priorKey = key;
159
i++;
160
}
161
}
162
if (priorKey) {
163
execIteration(priorKey, i-1, true);
164
}
165
}
166
}
167
168
if(i === 0){
169
ret = inverse(this);
170
}
171
172
return ret;
173
});
174
175
instance.registerHelper('if', function(conditional, options) {
176
if (isFunction(conditional)) { conditional = conditional.call(this); }
177
178
// Default behavior is to render the positive path if the value is truthy and not empty.
179
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
180
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
181
if ((!options.hash.includeZero && !conditional) || Utils.isEmpty(conditional)) {
182
return options.inverse(this);
183
} else {
184
return options.fn(this);
185
}
186
});
187
188
instance.registerHelper('unless', function(conditional, options) {
189
return instance.helpers['if'].call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash});
190
});
191
192
instance.registerHelper('with', function(context, options) {
193
if (isFunction(context)) { context = context.call(this); }
194
195
var fn = options.fn;
196
197
if (!Utils.isEmpty(context)) {
198
if (options.data && options.ids) {
199
var data = createFrame(options.data);
200
data.contextPath = Utils.appendContextPath(options.data.contextPath, options.ids[0]);
201
options = {data:data};
202
}
203
204
return fn(context, options);
205
} else {
206
return options.inverse(this);
207
}
208
});
209
210
instance.registerHelper('log', function(message, options) {
211
var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
212
instance.log(level, message);
213
});
214
215
instance.registerHelper('lookup', function(obj, field) {
216
return obj && obj[field];
217
});
218
}
219
220
var logger = {
221
methodMap: { 0: 'debug', 1: 'info', 2: 'warn', 3: 'error' },
222
223
// State enum
224
DEBUG: 0,
225
INFO: 1,
226
WARN: 2,
227
ERROR: 3,
228
level: 1,
229
230
// Can be overridden in the host environment
231
log: function(level, message) {
232
if (typeof console !== 'undefined' && logger.level <= level) {
233
var method = logger.methodMap[level];
234
(console[method] || console.log).call(console, message);
235
}
236
}
237
};
238
__exports__.logger = logger;
239
var log = logger.log;
240
__exports__.log = log;
241
var createFrame = function(object) {
242
var frame = Utils.extend({}, object);
243
frame._parent = object;
244
return frame;
245
};
246
__exports__.createFrame = createFrame;
247
});
248