@daytona/lds-engine
Advanced tools
| var id = 0; | ||
| module.exports = function guid(prefix) { | ||
| prefix = prefix || ''; | ||
| return id++; | ||
| } |
+100
-17
| var objectDeepMap = require('./lib/object-deep-map'); | ||
| var guid = require('./lib/guid'); | ||
| function obj2json (obj) { | ||
| return typeof obj === 'object' ? JSON.stringify(obj).replace(new RegExp('\"', 'g'), ""\;") : obj; | ||
| } | ||
| function json2obj (json) { | ||
| return typeof json === 'String' ? JSON.parse(json.replace(/\"\;/g, '\"')) : json; | ||
| } | ||
| module.exports = function Engine(options) { | ||
| options.registerHelper('jsonLine', function(obj){ | ||
| return typeof obj === 'object' ? JSON.stringify(obj).replace(new RegExp('\"', 'g'), '\\"') : obj; | ||
| }); | ||
| options.registerHelper('__resetLDSPartials', function(options) { | ||
| options.data.usedPartials = []; | ||
| }); | ||
| options.registerHelper('__getLDSPartials', function(options) { | ||
| return options.data.usedPartials; | ||
| }); | ||
| options.registerHelper('__LDSPartialStart', function(partialName, data, schema, options) { | ||
| if (options.data.root.editmode) { | ||
| if (!options.data.usedPartials) { | ||
| options.data.usedPartials = []; | ||
| } | ||
| options.data.usedPartials.push({ | ||
| partialName, | ||
| datapath: data.__objectPath, | ||
| data, | ||
| schema: json2obj(schema) | ||
| }); | ||
| // The exakt format of this comment is required by lds-editor | ||
| return `<!-- component="${partialName}" id="partial-${guid()}" data="${obj2json(data)}" -->`; | ||
| } | ||
| }); | ||
| options.registerHelper('__LDSPartialEnd', function(partialName, options) { | ||
| if (options.data.root.editmode) { | ||
| // The exakt format of this comment is required by lds-editor | ||
| return `<!-- /${partialName} -->`; | ||
| } | ||
| }); | ||
| options.registerHelper('__LDSEditScript', function(options) { | ||
| if (options.data.root.editmode) { | ||
| // The exakt format of this comment is required by lds-editor | ||
| return `<script src="/api/editscript"></script>`; | ||
| } | ||
| }); | ||
| if (options.helpers) { | ||
@@ -12,13 +64,20 @@ // Loop through helpers and register on templating engine | ||
| return { | ||
| setup : function(namespace) { | ||
| return function *(next) { | ||
| var structure = this[namespace].structure; | ||
| // Iterate LDS-structure to register all partials as partials like component:mycomponent | ||
| objectDeepMap(structure, (value) => { | ||
| engine: options, | ||
| setup : function(namespace, structure, editmode) { | ||
| // Iterate LDS-structure to register all partials as partials like component:mycomponent | ||
| objectDeepMap(structure, (value) => { | ||
| if (value && value.isLDSObject) { | ||
| // Register default template | ||
| if (value && value.template) { | ||
| options.registerPartial(value.partialName, value.template); | ||
| if (value.template) { | ||
| var template = value.template; | ||
| // Iterate LDS-structure to register all partials as partials like component:mycomponent | ||
| if (value.config && value.config.schema) { | ||
| template = `{{{__LDSPartialStart '${value.partialName}' this '${obj2json(value.config.schema)}' }}}${template}{{{__LDSPartialEnd '${value.partialName}'}}}`; | ||
| } | ||
| options.registerPartial(value.partialName, template); | ||
| } | ||
| // Loop through all template files and register as child path e.g. {{> component:mycomponent/child }} | ||
| if (value && value.templates && value.templates.length) { | ||
| if (value.templates && value.templates.length) { | ||
| value.templates.forEach((template) => { | ||
@@ -28,28 +87,52 @@ options.registerPartial(`${value.partialName}/${template.name}`, template.content); | ||
| } | ||
| } | ||
| return value; | ||
| }); | ||
| return value; | ||
| }); | ||
| return function *(next) { | ||
| var structure = this[namespace].structure; | ||
| var api = Object.assign(options, { | ||
| renderView(view, data, asReturn) { | ||
| renderView(view, data, asReturn, editmode) { | ||
| data = data || {}; | ||
| if (!view) { | ||
| view = structure.views['404'] || {template: '404 Page Not found', data: {}}; | ||
| } | ||
| var defaultData = this.defaultData || {}; | ||
| var viewData = Object.assign({layout: 'default'}, view.data, data); | ||
| var layout = viewData.layout ? structure.layouts[viewData.layout] : false; | ||
| if (editmode || data.editmode) { | ||
| objectDeepMap(viewData, (value, key, path) => { | ||
| // Is value an object | ||
| if (value === Object(value)) { | ||
| value.__objectPath = path.length ? path + '.' + key : key; | ||
| } | ||
| return value; | ||
| }); | ||
| } | ||
| var layout = viewData.layout && structure.layouts[viewData.layout] ? structure.layouts[viewData.layout] : false; | ||
| var layoutData = layout && layout.data || {}; | ||
| var pageData = Object.assign(defaultData, layoutData, viewData); | ||
| var template = layout ? layout.template.replace(/{{{@body}}}/, view.template) : view.template; | ||
| var viewTemplate = view.template; | ||
| if ((editmode || data.editmode) && view.config && view.config.schema) { | ||
| viewTemplate = `{{{__LDSPartialStart '${view.partialName}' this '${obj2json(view.config.schema)}' }}}${viewTemplate}{{{__LDSPartialEnd '${view.partialName}'}}}{{{__LDSEditScript}}}`; | ||
| } | ||
| var template = layout ? layout.template.replace(/{{{@body}}}/, viewTemplate) : viewTemplate; | ||
| var html = options.render(template, pageData); | ||
| if (asReturn) { | ||
| return this.render(template, pageData); | ||
| return html; | ||
| } | ||
| this.type = 'text/html; charset=utf-8'; | ||
| this.body = this.render(template, pageData); | ||
| this.body = html; | ||
| } | ||
| }); | ||
| this.render = options.render; | ||
| this.renderView = api.renderView; | ||
| this[namespace].render = options.render.bind(this); | ||
| this[namespace].renderView = api.renderView.bind(this); | ||
@@ -56,0 +139,0 @@ yield next; |
@@ -1,9 +0,9 @@ | ||
| function objectDeepMap(object, callback) { | ||
| function objectDeepMap(object, callback, path) { | ||
| path = path || ''; | ||
| Object.keys(object).map((key) => { | ||
| object[key] = callback(object[key], key); | ||
| object[key] = callback(object[key], key, path); | ||
| if (typeof object[key] === 'object') { | ||
| objectDeepMap(object[key], callback); | ||
| objectDeepMap(object[key], callback, path.length ? path+'.'+key : key); | ||
| } | ||
| }) | ||
| }); | ||
| return object; | ||
@@ -10,0 +10,0 @@ } |
+1
-1
| { | ||
| "name": "@daytona/lds-engine", | ||
| "version": "0.3.1", | ||
| "version": "0.4.0", | ||
| "description": "Templating engine for LDS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
6211
101.59%5
25%133
118.03%