Created
August 27, 2015 19:21
-
-
Save mattkelley/80d06b3693f5554bf009 to your computer and use it in GitHub Desktop.
element_interface.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import lo-dash lang category | |
import lang from 'lodash/lang'; | |
// Import lo-dash template string function | |
import stringTemplate from 'lodash/string/template'; | |
// import assign from 'lodash/object/assign'; | |
import forEach from 'lodash/collection/foreach'; | |
// Loop through configs stored and return the matching config (value) element (key) | |
function reverseMapFind(id, map) { | |
let resp = false; | |
for (var [node, config] of map.entries()) { | |
if (config.id === id) { | |
resp = node; | |
break; | |
} | |
} | |
return resp; | |
} | |
// Store elements and config object associations | |
function Elements(parent, data) { | |
// this._data = new WeakMap().set(this._parent, data); | |
// this._data.set(this._parent, data); | |
this.parent = parent; | |
// I wonder if we can just use a weakmap for storing templates | |
// and we use the parent element as the single key | |
// this._config = new WeakMap().set(this.parent, data); | |
this._nodeConfig = new Map(); | |
this._templates = new Map(); | |
// Add the templates | |
forEach(data.templates, function(config) { | |
this.addTemplate(config) | |
}, this); | |
return this; | |
}; | |
// Store the config object | |
Elements.prototype.addConfig = function(node, config) { | |
this._nodeConfig.set(node, config); | |
}; | |
// Get the config object | |
Elements.prototype.getConfig = function(node) { | |
return this._nodeConfig.get(node); | |
}; | |
// Remove the element and config association / storage | |
Elements.prototype.removeConfig = function(node) { | |
return this._nodeConfig.delete(node); | |
}; | |
// Get the element (key) by reverse lookup with config ID | |
Elements.prototype.getConfigElement = function(configID) { | |
return reverseMapFind(configID, this._nodeConfig); | |
}; | |
// Maybe a better name for what we are doing here | |
Elements.prototype.getElementByConfigId = Elements.prototype.getConfigElement; | |
// Add a Template config | |
Elements.prototype.addTemplate = function(config) { | |
this._templates.set(config.id, config); | |
}; | |
// Get a Template config | |
Elements.prototype.getTemplate = function(configID) { | |
return this._templates.get(configID); | |
}; | |
// Render a template | |
Elements.prototype.template = function(itemConfig) { | |
// require a template key to do anything | |
// return empty string for easy API use | |
if (!itemConfig.hasOwnProperty('template')) { | |
return ''; | |
} | |
// An itemConfig can contain either a template ID reference or a complete template | |
let template = lang.isPlainObject(itemConfig.template) ? itemConfig.template : this.getTemplate(itemConfig.template); | |
let text = template.hasOwnProperty('text') ? template.text : ''; | |
let data = itemConfig.hasOwnProperty('data') ? itemConfig.data : {}; | |
let compiled = stringTemplate(text); | |
// return templated markup string | |
return compiled(itemConfig.data); | |
}; | |
export default Elements; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment