Skip to content

Instantly share code, notes, and snippets.

@mattkelley
Created August 27, 2015 19:21
Show Gist options
  • Save mattkelley/80d06b3693f5554bf009 to your computer and use it in GitHub Desktop.
Save mattkelley/80d06b3693f5554bf009 to your computer and use it in GitHub Desktop.

Revisions

  1. Matt Kelley renamed this gist Aug 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Matt Kelley created this gist Aug 27, 2015.
    80 changes: 80 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@

    // 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;