ingenta-lens
Version:
A novel way of seeing content.
111 lines (83 loc) • 2.69 kB
JavaScript
;
var util = require("../../../substance/util");
var SRegExp = util.RegExp;
// Substance.Application.ElementRenderer
// ==========================================================================
//
// This is just a simple helper that allows us to create DOM elements
// in a data-driven way
var ElementRenderer = function(attributes) {
this.attributes = attributes;
// Pull off preserved properties from attributes
// --------
this.tagName = attributes.tag;
this.children = attributes.children || [];
this.text = attributes.text || "";
this.html = attributes.html;
delete attributes.children;
delete attributes.text;
delete attributes.html;
delete attributes.tag;
return this.render();
};
ElementRenderer.Prototype = function() {
// Do the actual rendering
// --------
this.render = function() {
var el = document.createElement(this.tagName);
if (this.html) {
el.innerHTML = this.html;
} else {
el.textContent = this.text;
}
// Set attributes based on element spec
for(var attrName in this.attributes) {
var val = this.attributes[attrName];
el.setAttribute(attrName, val);
}
// Append childs
for (var i=0; i<this.children.length; i++) {
var child = this.children[i];
el.appendChild(child);
}
// Remember element
// Probably we should ditch this
this.el = el;
return el;
};
};
// Provides a shortcut syntax interface to ElementRenderer
// --------
var $$ = function(descriptor, options) {
var options = options || {};
// Extract tagName, defaults to 'div'
var tagName = /^([a-zA-Z0-9]*)/.exec(descriptor);
options.tag = tagName && tagName[1] ? tagName[1] : 'div';
// Any occurence of #some_chars
var id = /#([a-zA-Z0-9_]*)/.exec(descriptor);
if (id && id[1]) options.id = id[1];
// Any occurence of .some-chars
// if (!options.class) {
// var re = new RegExp(/\.([a-zA-Z0-9_-]*)/g);
// var classes = [];
// var classMatch;
// while (classMatch = re.exec(descriptor)) {
// classes.push(classMatch[1]);
// }
// options.class = classes.join(' ');
// }
// Any occurence of .some-chars
var matchClasses = new SRegExp(/\.([a-zA-Z0-9_-]*)/g);
// options.class = options.class ? options.class+' ' : '';
if (!options.class) {
options.class = matchClasses.match(descriptor).map(function(m) {
return m.match[1];
}).join(' ');
}
return new ElementRenderer(options);
};
ElementRenderer.$$ = $$;
// Setup prototype chain
ElementRenderer.Prototype.prototype = util.Events;
ElementRenderer.prototype = new ElementRenderer.Prototype();
module.exports = ElementRenderer;