forked from bigcommerce/paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
304 lines (246 loc) · 7.45 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
var _ = require('lodash');
var localizer = require('./lib/localizer');
var Path = require('path');
var Fs = require('fs');
var Handlebars = require('handlebars');
var Async = require('async');
var helpers = [];
var handlebarsOptions = {
preventIndent: true
};
// Load helpers (this only run once)
Fs.readdirSync(Path.join(__dirname, 'helpers')).forEach(function (file) {
helpers.push(require('./helpers/' + file));
});
/**
* Theme renderer constructor
* @param {Object} settings
* @param {Object} themeSettings
* @param {Object} assembler
*/
function Paper(settings, themeSettings, assembler) {
var self = this;
self.handlebars = Handlebars.create();
self.handlebars.templates = {};
self.translate = null;
self.inject = {};
self.decorators = [];
self.settings = settings || {};
self.themeSettings = themeSettings || {};
self.assembler = assembler || {};
_.each(helpers, function (helper) {
helper(self);
});
}
/**
* Renders a string with the given context
* @param {String} string
* @param {Object} context
*/
Paper.prototype.renderString = function (string, context) {
return this.handlebars.compile(string)(context);
};
Paper.prototype.loadTheme = function (paths, acceptLanguage, done) {
var self = this;
if (!_.isArray(paths)) {
paths = paths ? [paths] : [];
}
Async.parallel([
function (next) {
self.loadTranslations(acceptLanguage, next);
},
function (next) {
Async.map(paths, self.loadTemplates.bind(self), next);
}
], done);
};
/**
* Load Partials/Templates
* @param {Object} templates
* @param {Function} callback
*/
Paper.prototype.loadTemplates = function (path, callback) {
var self = this;
var processor = self.getTemplateProcessor();
self.assembler.getTemplates(path, processor, function (error, templates) {
if (error) {
return callback(error);
}
_.each(templates, function (precompiled, path) {
var template;
if (!self.handlebars.templates[path]) {
eval('template = ' + precompiled);
self.handlebars.templates[path] = self.handlebars.template(template);
}
});
self.handlebars.partials = self.handlebars.templates;
callback();
});
};
Paper.prototype.getTemplateProcessor = function () {
var self = this;
return function (templates) {
var precompiledTemplates = {};
_.each(templates, function (content, path) {
precompiledTemplates[path] = self.handlebars.precompile(content, handlebarsOptions);
});
return precompiledTemplates;
}
};
/**
* Load Partials/Templates used for test cases and stencil-cli
* @param {Object} templates
* @return {Object}
*/
Paper.prototype.loadTemplatesSync = function (templates) {
var self = this;
_.each(templates, function (content, fileName) {
self.handlebars.templates[fileName] = self.handlebars.compile(content, handlebarsOptions);
});
self.handlebars.partials = self.handlebars.templates;
return self;
};
/**
* @param {String} acceptLanguage
* @param {Object} translations
*/
Paper.prototype.loadTranslations = function (acceptLanguage, callback) {
var self = this;
self.assembler.getTranslations((error, translations) => {
if (error) {
return callback(error);
}
// Make translations available to the helpers
self.translate = localizer(acceptLanguage, translations);
callback();
});
};
/**
* Add CDN base url to the relative path
* @param {String} path Relative path
* @return {String} Url cdn
*/
Paper.prototype.cdnify = function (path) {
var cdnUrl = this.settings['cdn_url'] || '';
var versionId = this.settings['theme_version_id'];
var configId = this.settings['theme_config_id'];
var sessionId = this.settings['theme_session_id'];
var protocolMatch = /(.*!?:)/;
if (path instanceof Handlebars.SafeString) {
path = path.string;
}
if (!path) {
return '';
}
if (/^(?:https?:)?\/\//.test(path)) {
return path;
}
if (protocolMatch.test(path)) {
var match = path.match(protocolMatch);
path = path.slice(match[0].length, path.length);
if (path[0] === '/') {
path = path.slice(1, path.length);
}
if (match[0] === 'webdav:') {
return [cdnUrl, 'content', path].join('/');
}
if (this.themeSettings.cdn) {
var endpointKey = match[0].substr(0, match[0].length - 1);
if (this.themeSettings.cdn.hasOwnProperty(endpointKey)) {
if (cdnUrl) {
return [this.themeSettings.cdn[endpointKey], path].join('/');
}
return ['/assets/cdn', endpointKey, path].join('/');
}
}
if (path[0] !== '/') {
path = '/' + path;
}
return path;
}
if (path[0] !== '/') {
path = '/' + path;
}
if (!versionId || !configId) {
return path;
}
if (path.substr(0, 8) === '/assets/') {
path = path.substr(8, path.length);
}
if (sessionId) {
return [cdnUrl, 'stencil', versionId, configId, 'e', sessionId, path].join('/');
}
return [cdnUrl, 'stencil', versionId, configId, path].join('/');
};
/**
* @param {Function} decorator
*/
Paper.prototype.addDecorator = function (decorator) {
this.decorators.push(decorator);
};
/**
* @param {String} path
* @param {Object} context
* @return {String}
*/
Paper.prototype.render = function (path, context) {
var output;
context = context || {};
context.template = path;
if (this.translate) {
context.locale_name = this.translate.localeName;
}
output = this.handlebars.templates[path](context);
_.each(this.decorators, function (decorator) {
output = decorator(output);
});
return output;
};
/**
* Theme rendering logic
* @param {String|Array} templatePath
* @param {Object} data
* @return {String|Object}
*/
Paper.prototype.renderTheme = function(templatePath, data) {
var html,
output;
// Is an ajax request?
if (data.remote || _.isArray(templatePath)) {
if (data.remote) {
data.context = _.extend({}, data.context, data.remote_data);
}
// Is render_with ajax request?
if (templatePath) {
// if multiple render_with
if (_.isArray(templatePath)) {
// if templatePath is an array ( multiple templates using render_with option)
// compile all the template required files into a hash table
html = templatePath.reduce((table, file) => {
table[file] = this.render(file, data.context);
return table;
}, {});
} else {
html = this.render(templatePath, data.context);
}
if (data.remote) {
// combine the context & rendered html
output = {
data: data.remote_data,
content: html
};
} else {
output = html;
}
} else {
output = {
data: data.remote_data
};
}
} else {
output = this.render(templatePath, data.context);
}
return output;
}
module.exports = Paper;