@bigcommerce/stencil-paper
Advanced tools
Comparing version 2.0.2 to 2.0.3
414
index.js
@@ -73,170 +73,165 @@ 'use strict'; | ||
*/ | ||
function Paper(settings, themeSettings, assembler) { | ||
var self = this; | ||
class Paper { | ||
constructor(settings, themeSettings, assembler) { | ||
this.handlebars = Handlebars.create(); | ||
self.handlebars = Handlebars.create(); | ||
this.handlebars.templates = {}; | ||
this.translator = null; | ||
this.inject = {}; | ||
this.decorators = []; | ||
self.handlebars.templates = {}; | ||
self.translator = null; | ||
self.inject = {}; | ||
self.decorators = []; | ||
this.settings = settings || {}; | ||
this.themeSettings = themeSettings || {}; | ||
this.assembler = assembler || {}; | ||
this.contentServiceContext = {}; | ||
self.settings = settings || {}; | ||
self.themeSettings = themeSettings || {}; | ||
self.assembler = assembler || {}; | ||
helpers.forEach(helper => helper(this)); | ||
} | ||
_.each(helpers, function (helper) { | ||
helper(self); | ||
}); | ||
} | ||
/** | ||
* Renders a string with the given context | ||
* @param {String} string | ||
* @param {Object} context | ||
*/ | ||
renderString(string, context) { | ||
return this.handlebars.compile(string)(context); | ||
} | ||
/** | ||
* 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); | ||
}; | ||
loadTheme(paths, acceptLanguage, done) { | ||
if (!_.isArray(paths)) { | ||
paths = paths ? [paths] : []; | ||
} | ||
Paper.prototype.loadTheme = function (paths, acceptLanguage, done) { | ||
var self = this; | ||
if (!_.isArray(paths)) { | ||
paths = paths ? [paths] : []; | ||
Async.parallel([ | ||
(next) => { | ||
this.loadTranslations(acceptLanguage, next); | ||
}, | ||
(next) => { | ||
Async.map(paths, this.loadTemplates.bind(this), next); | ||
} | ||
], done); | ||
} | ||
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 | ||
*/ | ||
loadTemplates(path, callback) { | ||
let processor = this.getTemplateProcessor(); | ||
/** | ||
* Load Partials/Templates | ||
* @param {Object} templates | ||
* @param {Function} callback | ||
*/ | ||
Paper.prototype.loadTemplates = function (path, callback) { | ||
var self = this; | ||
this.assembler.getTemplates(path, processor, (error, templates) => { | ||
if (error) { | ||
return callback(error); | ||
} | ||
var processor = self.getTemplateProcessor(); | ||
_.each(templates, (precompiled, path) => { | ||
var template; | ||
if (!this.handlebars.templates[path]) { | ||
eval('template = ' + precompiled); | ||
this.handlebars.templates[path] = this.handlebars.template(template); | ||
} | ||
}); | ||
self.assembler.getTemplates(path, processor, function (error, templates) { | ||
if (error) { | ||
return callback(error); | ||
} | ||
this.handlebars.partials = this.handlebars.templates; | ||
_.each(templates, function (precompiled, path) { | ||
var template; | ||
if (!self.handlebars.templates[path]) { | ||
eval('template = ' + precompiled); | ||
self.handlebars.templates[path] = self.handlebars.template(template); | ||
} | ||
callback(); | ||
}); | ||
} | ||
self.handlebars.partials = self.handlebars.templates; | ||
getTemplateProcessor() { | ||
return (templates) => { | ||
let precompiledTemplates = {}; | ||
callback(); | ||
}); | ||
}; | ||
_.each(templates,(content, path) => { | ||
precompiledTemplates[path] = this.handlebars.precompile(content, handlebarsOptions); | ||
}); | ||
Paper.prototype.getTemplateProcessor = function () { | ||
var self = this; | ||
return precompiledTemplates; | ||
} | ||
} | ||
return function (templates) { | ||
var precompiledTemplates = {}; | ||
_.each(templates, function (content, path) { | ||
precompiledTemplates[path] = self.handlebars.precompile(content, handlebarsOptions); | ||
/** | ||
* Load Partials/Templates used for test cases and stencil-cli | ||
* @param {Object} templates | ||
* @return {Object} | ||
*/ | ||
loadTemplatesSync(templates) { | ||
_.each(templates,(content, fileName) => { | ||
this.handlebars.templates[fileName] = this.handlebars.compile(content, handlebarsOptions); | ||
}); | ||
return precompiledTemplates; | ||
} | ||
}; | ||
this.handlebars.partials = this.handlebars.templates; | ||
/** | ||
* Load Partials/Templates used for test cases and stencil-cli | ||
* @param {Object} templates | ||
* @return {Object} | ||
*/ | ||
Paper.prototype.loadTemplatesSync = function (templates) { | ||
var self = this; | ||
return this; | ||
}; | ||
_.each(templates, function (content, fileName) { | ||
self.handlebars.templates[fileName] = self.handlebars.compile(content, handlebarsOptions); | ||
}); | ||
/** | ||
* @param {String} acceptLanguage | ||
* @param {Object} translations | ||
*/ | ||
loadTranslations(acceptLanguage, callback) { | ||
this.assembler.getTranslations((error, translations) => { | ||
if (error) { | ||
return callback(error); | ||
} | ||
self.handlebars.partials = self.handlebars.templates; | ||
// Make translations available to the helpers | ||
this.translator = Translator.create(acceptLanguage, translations); | ||
return self; | ||
}; | ||
callback(); | ||
}); | ||
}; | ||
/** | ||
* @param {String} acceptLanguage | ||
* @param {Object} translations | ||
*/ | ||
Paper.prototype.loadTranslations = function (acceptLanguage, callback) { | ||
var self = this; | ||
/** | ||
* Add CDN base url to the relative path | ||
* @param {String} path Relative path | ||
* @return {String} Url cdn | ||
*/ | ||
cdnify(path) { | ||
const cdnUrl = this.settings['cdn_url'] || ''; | ||
const versionId = this.settings['theme_version_id']; | ||
const sessionId = this.settings['theme_session_id']; | ||
const protocolMatch = /(.*!?:)/; | ||
self.assembler.getTranslations((error, translations) => { | ||
if (error) { | ||
return callback(error); | ||
if (path instanceof Handlebars.SafeString) { | ||
path = path.string; | ||
} | ||
// Make translations available to the helpers | ||
self.translator = Translator.create(acceptLanguage, translations); | ||
if (!path) { | ||
return ''; | ||
} | ||
callback(); | ||
}); | ||
}; | ||
if (/^(?:https?:)?\/\//.test(path)) { | ||
return path; | ||
} | ||
/** | ||
* 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 sessionId = this.settings['theme_session_id']; | ||
var protocolMatch = /(.*!?:)/; | ||
if (protocolMatch.test(path)) { | ||
var match = path.match(protocolMatch); | ||
path = path.slice(match[0].length, path.length); | ||
if (path instanceof Handlebars.SafeString) { | ||
path = path.string; | ||
} | ||
if (path[0] === '/') { | ||
path = path.slice(1, path.length); | ||
} | ||
if (!path) { | ||
return ''; | ||
} | ||
if (match[0] === 'webdav:') { | ||
return [cdnUrl, 'content', path].join('/'); | ||
} | ||
if (/^(?:https?:)?\/\//.test(path)) { | ||
return path; | ||
} | ||
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('/'); | ||
} | ||
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('/'); | ||
} | ||
} | ||
return ['/assets/cdn', endpointKey, path].join('/'); | ||
if (path[0] !== '/') { | ||
path = '/' + path; | ||
} | ||
return path; | ||
} | ||
@@ -248,107 +243,102 @@ | ||
return path; | ||
} | ||
if (!versionId) { | ||
return path; | ||
} | ||
if (path[0] !== '/') { | ||
path = '/' + path; | ||
} | ||
if (path.match(/^\/assets\//)) { | ||
path = path.substr(8, path.length); | ||
} | ||
if (!versionId) { | ||
return path; | ||
} | ||
if (sessionId) { | ||
return [cdnUrl, 'stencil', versionId, 'e', sessionId, path].join('/'); | ||
} | ||
if (path.substr(0, 8) === '/assets/') { | ||
path = path.substr(8, path.length); | ||
} | ||
return [cdnUrl, 'stencil', versionId, path].join('/'); | ||
}; | ||
if (sessionId) { | ||
return [cdnUrl, 'stencil', versionId, 'e', sessionId, path].join('/'); | ||
} | ||
/** | ||
* @param {Function} decorator | ||
*/ | ||
addDecorator(decorator) { | ||
this.decorators.push(decorator); | ||
}; | ||
return [cdnUrl, 'stencil', versionId, path].join('/'); | ||
}; | ||
/** | ||
* @param {String} path | ||
* @param {Object} context | ||
* @return {String} | ||
*/ | ||
render(path, context) { | ||
let output; | ||
/** | ||
* @param {Function} decorator | ||
*/ | ||
Paper.prototype.addDecorator = function (decorator) { | ||
this.decorators.push(decorator); | ||
}; | ||
context = context || {}; | ||
context.template = path; | ||
/** | ||
* @param {String} path | ||
* @param {Object} context | ||
* @return {String} | ||
*/ | ||
Paper.prototype.render = function (path, context) { | ||
var output; | ||
if (this.translator) { | ||
context.locale_name = this.translator.getLocale(); | ||
} | ||
context = context || {}; | ||
context.template = path; | ||
output = this.handlebars.templates[path](context); | ||
if (this.translator) { | ||
context.locale_name = this.translator.getLocale(); | ||
} | ||
_.each(this.decorators, function (decorator) { | ||
output = decorator(output); | ||
}); | ||
output = this.handlebars.templates[path](context); | ||
return output; | ||
}; | ||
_.each(this.decorators, function (decorator) { | ||
output = decorator(output); | ||
}); | ||
/** | ||
* Theme rendering logic | ||
* @param {String|Array} templatePath | ||
* @param {Object} data | ||
* @return {String|Object} | ||
*/ | ||
renderTheme(templatePath, data) { | ||
let html; | ||
let output; | ||
return output; | ||
}; | ||
// Is an ajax request? | ||
if (data.remote || _.isArray(templatePath)) { | ||
/** | ||
* Theme rendering logic | ||
* @param {String|Array} templatePath | ||
* @param {Object} data | ||
* @return {String|Object} | ||
*/ | ||
Paper.prototype.renderTheme = function(templatePath, data) { | ||
var html, | ||
output; | ||
if (data.remote) { | ||
data.context = Object.assign({}, data.context, data.remote_data); | ||
} | ||
// Is an ajax request? | ||
if (data.remote || _.isArray(templatePath)) { | ||
// 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) { | ||
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; | ||
}, {}); | ||
if (data.remote) { | ||
// combine the context & rendered html | ||
output = { | ||
data: data.remote_data, | ||
content: html | ||
}; | ||
} else { | ||
output = html; | ||
} | ||
} else { | ||
html = this.render(templatePath, data.context); | ||
} | ||
if (data.remote) { | ||
// combine the context & rendered html | ||
output = { | ||
data: data.remote_data, | ||
content: html | ||
data: data.remote_data | ||
}; | ||
} else { | ||
output = html; | ||
} | ||
} else { | ||
output = { | ||
data: data.remote_data | ||
}; | ||
output = this.render(templatePath, data.context); | ||
} | ||
} else { | ||
output = this.render(templatePath, data.context); | ||
return output; | ||
} | ||
return output; | ||
} | ||
module.exports = Paper; |
{ | ||
"name": "@bigcommerce/stencil-paper", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A stencil plugin to register partials and helpers from handlebars and returns the compiled version for the stencil platform.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129624
80
3383