UNPKG

halvalla

Version:

Generate React elements with CoffeeScript functions

718 lines (658 loc) 24.8 kB
// Generated by CoffeeScript 1.12.7 /* * Halvalla -- bindings for element creation and expression via teact and teacup */ (function() { var BagMan, GreatEmptiness, Halvalla, allTags, doctypes, dummyComponent, dummyElement, elements, escape, mergeElements, nameMine, normalizeArray, propertyName, quote, ref, teacup, slice = [].slice, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; nameMine = require('../lib/name-mine'); /* * The oracle, a globally supplied object to this module has this signature Examples of oracle -- the default is to do Teacup to HTML ReactDom = require 'react-dom' Oracle = summoner: React name: 'React' isValidElement: React.isValidElement Component: React.Component createElement: React.createElement conjurer: ReactDom.renderToString Mithril = require 'mithril' Oracle = name: 'Mithril' isValidElement: (c)->c.view? createElement: Mithril Component: {} */ ref = require('../lib/html-tags'), doctypes = ref.doctypes, elements = ref.elements, normalizeArray = ref.normalizeArray, mergeElements = ref.mergeElements, allTags = ref.allTags, escape = ref.escape, quote = ref.quote, BagMan = ref.BagMan; teacup = require('../lib/teacup'); propertyName = 'props'; GreatEmptiness = null; dummyComponent = null; dummyElement = null; Halvalla = (function() { var oracle; oracle = null; function Halvalla(Oracle) { var Component, Element; if (Oracle == null) { Oracle = null; } this.bagMan = new BagMan; GreatEmptiness = GreatEmptiness = (function() { function GreatEmptiness(instantiator, Oracle) { var Component, Element, defaultObject, key, ref1, value; if (Oracle == null) { Oracle = {}; } defaultObject = { isValidElement: function(c) { return c.view != null; }, name: Oracle.name || 'great-emptiness', Component: Oracle.Component || (Component = (function() { function Component() {} return Component; })()), Element: Oracle.Element || (Element = (function() { function Element() {} return Element; })()), createElement: function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return (function(func, args, ctor) { ctor.prototype = func.prototype; var child = new ctor, result = func.apply(child, args); return Object(result) === result ? result : child; })(dummyElement, args, function(){}); }, summoner: null, getChildren: function(element) { return element.children; }, getProp: function(element) { return element.attrs; }, getName: function(element) { var ref1; return element.type || ((ref1 = element._Halvalla) != null ? ref1.tagName : void 0) || element.tagName; }, propertyName: 'attrs', preInstantiate: true, conjurer: null }; ref1 = Object.assign(defaultObject, Oracle); for (key in ref1) { value = ref1[key]; GreatEmptiness.prototype[key] = value; } this.teacup = new teacup(instantiator, defaultObject); if (!this.conjurer) { this.conjurer = this.teacup.render.bind(this.teacup); } this; } return GreatEmptiness; })(); oracle = new GreatEmptiness(((function(_this) { return function(component) { return _this.create(component); }; })(this)), Oracle); propertyName = oracle.propertyName; dummyComponent = Component = (function(superClass) { extend(Component, superClass); function Component() { var children, properties, tagName; tagName = arguments[0], properties = arguments[1], children = 3 <= arguments.length ? slice.call(arguments, 2) : []; Component.__super__.constructor.apply(this, [properties].concat(slice.call(children))); this.tagName = tagName; if (!this[propertyName]) { this[propertyName] = properties; } this.children = this.render; this._Halvalla = { birthName: 'Bishop ' + nameMine.getName(), propertyName: propertyName, children: this.render, tagname: tagName[0].toLowerCase() + tagName.slice(1) }; return; } Component.prototype.render = function() {}; return Component; })(oracle.Component); dummyElement = Element = (function(superClass) { extend(Element, superClass); function Element() { var children1, name, properties, tagName; tagName = arguments[0], properties = arguments[1], children1 = 3 <= arguments.length ? slice.call(arguments, 2) : []; if (properties == null) { properties = {}; } this.children = children1; Element.__super__.constructor.apply(this, [properties].concat(slice.call(this.children))); this.tagName = tagName; this.tag = tagName; this[propertyName] = properties; if (propertyName === 'attr') { this.props = properties; } if (this.children.length === 1) { this.children = this.children[0]; } if (typeof tagName === 'object') { name = tagName.name || tagName.constructor.name; } else { name = tagName; } try { name = name[0].toLowerCase() + name.slice(1); } catch (error) { name = "badSeed"; } this._Halvalla = { birthName: 'Acolyte ' + nameMine.getName(), tagName: name[0].toLowerCase() + name.slice(1), propertyName: propertyName, children: this[propertyName].children }; return; } Element.prototype.view = function() {}; return Element; })(oracle.Component); } Halvalla.prototype.mutator = function(tagName, destination, withThis) { var thing; if (withThis == null) { withThis = null; } (function(_this) { return (function(tagName) { return Halvalla.prototype[tagName] = function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return destination.apply(_this, [tagName].concat(args)); }; }); })(this)(tagName); thing = Halvalla.prototype[tagName]; thing.Halvalla = { tagName: tagName, boss: oracle.name }; allTags[tagName] = thing; return thing; }; Halvalla.prototype.escape = escape; Halvalla.prototype.quote = quote; Halvalla.prototype.pureComponent = function(contents) { return (function(_this) { return function() { var stackHad; _this.bagMan.context([]); _this.bagMan.shipOut(contents.apply(_this, arguments)); stackHad = _this.bagMan.harvest(); return stackHad; }; })(this); }; Halvalla.prototype.coffeescript = function(fn) { return this.raw("<script type=\"text/javascript\">(function() {\n var slice = [].slice,\n extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },\n hasProp = {}.hasOwnProperty,\n indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };\n (" + (fn.toString().replace(/<\//g, "<\\/")) + ")();\n})();</script>"); }; Halvalla.prototype.comment = function(text) { if (!text.toString) { throw new Error("comment tag allows text only: expected a string"); } return this.raw("<!--" + (escape(text)) + "-->"); }; Halvalla.prototype.raw = function(text) { var el; if (text == null) { text = ""; } if (!text.toString) { throw new Error("raw allows text only: expected a string"); } if (oracle.trust) { el = oracle.trust(text); } else { el = oracle.createElement('text', { dangerouslySetInnerHTML: { __html: text.toString() } }); } return this.bagMan.shipOut(el); }; Halvalla.prototype.doctype = function(type) { if (type == null) { type = 5; } return this.raw(doctypes[type]); }; Halvalla.prototype.ie = function(condition, contents) { return this.crel('ie', { condition: condition }, contents); }; Halvalla.prototype.tag = function() { var args, tagName; tagName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; if (!((tagName != null) && 'string' === typeof tagName)) { throw new Error("HTML tag type is invalid: expected a string but got " + (typeof (tagName != null))); } return this.crel.apply(this, [tagName].concat(slice.call(args))); }; Halvalla.prototype.bless = function(component, itsName) { var name; if (itsName == null) { itsName = null; } if (component.__esModule && component["default"]) { component = component["default"]; } name = itsName || component.name; return allTags[name] = Halvalla.prototype[name] = (function(_this) { return function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return _this.crel.apply(_this, [component].concat(slice.call(args))); }; })(this); }; Halvalla.prototype.renderContents = function() { var contents, ref1, rest; contents = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : []; if (contents == null) { return; } if (typeof contents === 'function') { contents = contents.apply(this, rest); } if (typeof contents === 'number') { return this.bagMan.shipOut(new Number(contents)); } if (typeof contents === 'string') { return this.bagMan.shipOut(new String(contents)); } if (contents.length > 0) { return (ref1 = this.bagMan).shipOut.apply(ref1, contents); } return []; }; Halvalla.prototype.component = function(func) { return (function(_this) { return function() { var args, attrs, child, contents, ref1, selector; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; ref1 = _this.normalizeArgs(args), selector = ref1.selector, attrs = ref1.attrs, contents = ref1.contents; child = function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; args.unshift(contents[0]); return _this.renderContents.apply(_this, args); }; return func.apply(_this, [selector, attrs, child]); }; })(this); }; Halvalla.prototype.crelVoid = function() { var args, attrs, contents, ref1, tagName; tagName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; ref1 = this.normalizeArgs(args), attrs = ref1.attrs, contents = ref1.contents; if (contents.length > 0) { throw new Error("Element type is invalid: must not have content: " + tagName); } return this.crel.apply(this, [tagName].concat(slice.call(args))); }; Halvalla.prototype.crel = function() { var args, attrs, children, contents, el, el1, name, oldBagger, ref1, tagConstructor, tagName; tagName = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; if (tagName == null) { throw new Error("Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: " + tagName); } ref1 = this.normalizeArgs(args), attrs = ref1.attrs, contents = ref1.contents; children = contents.length > 0 ? (oldBagger = this.bagMan, this.bagMan = new BagMan, this.bagMan.context(contents), this.march(this.bagMan), contents = this.bagMan.harvest(), this.bagMan = oldBagger, contents) : []; if (typeof tagName !== 'function') { name = tagName; el = oracle.createElement.apply(oracle, [tagName, attrs].concat(slice.call(children))); } else { tagConstructor = tagName; name = tagName.name; tagName = name[0].toLowerCase() + name.slice(1); if (!Halvalla.prototype[tagName]) { Halvalla.prototype[tagName] = function() { var args, component; component = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; return this.crel.apply(this, [tagName, component].concat(slice.call(args))); }; } if (oracle.preInstantiate) { el1 = new tagConstructor(tagName); el = oracle.createElement(el1, attrs, null); } else { name = tagName; el = oracle.createElement.apply(oracle, [tagName, attrs].concat(slice.call(children))); } } if (!el._Halvalla) { el._Halvalla = { birthName: 'Supplicant ' + nameMine.getName(), tagName: tagName, propertyName: oracle.propertyName, children: oracle.getChildren(el) }; } this.bagMan.shipOut(el); return el; }; Halvalla.prototype.text = function(s) { if (!(s != null ? s.toString : void 0)) { return s; } return this.bagMan.shipOut(s.toString()); }; Halvalla.prototype.isSelector = function(string) { var ref1; return string.length > 1 && ((ref1 = string.charAt(0)) === '#' || ref1 === '.'); }; Halvalla.prototype.parseSelector = function(selector) { var classes, i, id, klass, len, ref1, ref2, token; id = null; classes = []; ref1 = selector.split('.'); for (i = 0, len = ref1.length; i < len; i++) { token = ref1[i]; token = token.trim(); if (id) { classes.push(token); } else { ref2 = token.split('#'), klass = ref2[0], id = ref2[1]; if (klass !== '') { classes.push(token); } } } return { id: id, classes: classes }; }; Halvalla.prototype.normalizeArgs = function(args) { var arg, attrs, classes, contents, dataAttrs, i, id, index, k, len, parsedSelector, selector, v; if (args.length == null) { args = [args]; } attrs = {}; selector = null; contents = null; for (index = i = 0, len = args.length; i < len; index = ++i) { arg = args[index]; if (arg != null) { switch (typeof arg) { case 'string': if (index === 0 && this.isSelector(arg)) { selector = arg; parsedSelector = this.parseSelector(arg); } else { contents = new String(arg); } break; case 'number': case 'boolean': contents = new String(arg); break; case 'function': contents = arg; break; case 'object': if (arg.constructor === Object) { attrs = arg; } if (arg["default"] && arg.__esModule) { arg = arg["default"]; } if (arg.constructor === Object && !oracle.isValidElement(arg)) { attrs = Object.keys(arg).reduce(function(clone, key) { clone[key] = arg[key]; return clone; }, {}); } if ((typeof arg.toString === "function" ? arg.toString() : void 0) !== '[object Object]') { contents = arg.toString(); } else if (arg.length != null) { contents = arg; } break; default: contents = arg; } } } if (parsedSelector != null) { id = parsedSelector.id, classes = parsedSelector.classes; if (id != null) { attrs.id = id; } if (classes != null ? classes.length : void 0) { if (attrs["class"]) { classes.push(attrs["class"]); } attrs["class"] = classes.join(' '); if (attrs.className) { classes.push(attrs.className); } attrs.className = classes.join(' '); } } dataAttrs = attrs.data; if (typeof dataAttrs === 'object') { delete attrs.data; for (k in dataAttrs) { v = dataAttrs[k]; attrs["data-" + k] = v; } } contents = normalizeArray(contents); return { attrs: attrs, contents: contents, selector: selector }; }; Halvalla.prototype.use = function(plugin) { return plugin(this); }; Halvalla.prototype.renderable = function(stuff) { return (function(_this) { return function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return _this.create(stuff.apply(null, args)); }; })(this); }; Halvalla.prototype.cede = function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return this.render.apply(this, args); }; Halvalla.prototype.march = function(bag) { var attrs, component, crellB, n, tagConstructor, tagName, tagNameLC, w, x, y, z; while (component = bag.inspect()) { switch (n = component.constructor.name) { case 'Function': y = bag.harvest(); x = component(); if ('function' === typeof x) { w = x(); x = w; } z = bag.harvest(); /* we shold not reinspect more than just the single return from x() Why did the entire z need to be put in? bag.reinspect if z.length>0 then z else x */ bag.reinspect(x); break; case 'String': case 'Number': bag.shipOut(component); break; case n[0].toLowerCase() + n.slice(1): bag.shipOut(component); break; default: tagName = oracle.getName(component); if ('string' === typeof tagName) { bag.shipOut(component); break; } if ('function' === typeof tagName) { tagConstructor = tagName; tagName = tagConstructor.name; tagNameLC = tagName[0].toLowerCase() + tagName.slice(1); if (component.attrs) { attrs = component.attrs; } else { attrs = component.props; } if (tagName[0] !== tagNameLC[0]) { if (!Halvalla.prototype[tagNameLC]) { Halvalla.prototype[tagNameLC] = function() { var args, component; component = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; return this.crel.apply(this, [tagNameLC, component].concat(slice.call(args))); }; } crellB = this[tagNameLC](attrs, oracle.getChildren(component)); bag.shipOut(crellB); break; } else { bag.shipOut(this[tagNameLC]('.selectorCase', attrs, oracle.getChildren(component))); } } } } return null; }; Halvalla.prototype.create = function() { var node, oldBagger, rest, structure; node = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : []; if ('function' === typeof node) { oldBagger = this.bagMan; this.bagMan = new BagMan; this.bagMan.context((function(_this) { return function() { return node.apply(null, rest); }; })(this)); this.march(this.bagMan); structure = this.bagMan.harvest(); this.bagMan = oldBagger; } else if ('object' === typeof node) { structure = node; } else { structure = new String(node); } return structure; }; Halvalla.prototype.render = function() { var element, funct, rest, result, structure; funct = arguments[0], rest = 2 <= arguments.length ? slice.call(arguments, 1) : []; structure = this.create.apply(this, [funct].concat(slice.call(rest))); if (!structure.length) { structure = [structure]; } result = (function() { var i, len, results; results = []; for (i = 0, len = structure.length; i < len; i++) { element = structure[i]; results.push(oracle.conjurer(element)); } return results; })(); return result.join(''); }; Halvalla.prototype.tags = function() { var bound, boundMethodNames, fn1, fn2, fn3, fn4, fn5, i, j, l, len, len1, len2, len3, len4, m, method, o, ref1, ref2, ref3, ref4, tagName; bound = {}; bound.Oracle = oracle; bound.Component = dummyComponent; bound.Element = dummyElement; boundMethodNames = [].concat('create bless cede component doctype escape ie normalizeArgs pureComponent raw render renderable tag text use'.split(' '), mergeElements('regular', 'obsolete', 'raw', 'void', 'obsolete_void', 'script', 'coffeescript', 'comment')); fn1 = (function(_this) { return function(method) { return allTags[method] = bound[method] = function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (!_this[method]) { throw "no method named " + method + " in Halvalla"; } return _this[method].apply(_this, args); }; }; })(this); for (i = 0, len = boundMethodNames.length; i < len; i++) { method = boundMethodNames[i]; fn1(method); } ref1 = mergeElements('regular', 'obsolete'); fn2 = (function(_this) { return function(tagName) { return _this.mutator(tagName, _this.crel); }; })(this); for (j = 0, len1 = ref1.length; j < len1; j++) { tagName = ref1[j]; fn2(tagName); } ref2 = mergeElements('raw'); fn3 = (function(_this) { return function(tagName) { return _this.mutator(tagName, _this.crel); }; })(this); for (l = 0, len2 = ref2.length; l < len2; l++) { tagName = ref2[l]; fn3(tagName); } ref3 = mergeElements('script'); fn4 = (function(_this) { return function(tagName) { return _this.mutator(tagName, _this.crel); }; })(this); for (m = 0, len3 = ref3.length; m < len3; m++) { tagName = ref3[m]; fn4(tagName); } ref4 = mergeElements('void', 'obsolete_void'); fn5 = (function(_this) { return function(tagName) { return _this.mutator(tagName, _this.crelVoid); }; })(this); for (o = 0, len4 = ref4.length; o < len4; o++) { tagName = ref4[o]; fn5(tagName); } return bound; }; return Halvalla; })(); if (typeof module !== "undefined" && module !== null ? module.exports : void 0) { module.exports = new Halvalla().tags(); module.exports.Halvalla = Halvalla; } else { window.Halvalla = new Halvalla().tags(); window.Halvalla.Halvalla = Halvalla; } }).call(this);