create.js
Advanced tools
Comparing version 0.0.9 to 0.0.11
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.create=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var template = require('./lib/replace'); | ||
var utils = require('./lib/utils'); | ||
var types = utils.types; | ||
var of = require('of'); | ||
var classes = require('clas'); | ||
var tags = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'shadow', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr']; | ||
var create = new Object; | ||
var create = {}; | ||
create.registry = []; | ||
tags.forEach(function(tag) { | ||
create[tag] = function(options) { | ||
var options = options || {}; | ||
var content; | ||
if (!of.object(options)) { | ||
content = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
var element = document.createElement(tag); | ||
@@ -17,10 +24,11 @@ var context = options.context || {}; | ||
var classes = options.classes || ''; | ||
var content = options.content || null; | ||
content = options.content || null; | ||
var appendTo = options.appendTo || null; | ||
var frag; | ||
delete options['context']; | ||
delete options['escape']; | ||
delete options['classes']; | ||
delete options['content']; | ||
delete options['appendTo']; | ||
delete options.context; | ||
delete options.escape; | ||
delete options.classes; | ||
delete options.content; | ||
delete options.appendTo; | ||
@@ -32,8 +40,8 @@ if (classes) { | ||
if (content) { | ||
if (types.isElement(content)) { | ||
if (of.element(content)) { | ||
element.appendChild(content); | ||
} else | ||
if (types.isString(content)) { | ||
var frag = create.fragment(); | ||
if (of.string(content)) { | ||
frag = create.fragment(); | ||
var string = context ? template(content, context) : content; | ||
@@ -43,3 +51,3 @@ element.innerHTML = string; | ||
if (types.isFunction(content)) { | ||
if (of.function(content)) { | ||
children = content.call(null, context); | ||
@@ -49,15 +57,15 @@ element.appendChild(children); | ||
if (types.isArray(content)) { | ||
if (of.array(content)) { | ||
for (var i = 0, l = content.length; i < l; i++) { | ||
var arg = content[i]; | ||
if (types.isElement(arg)) { | ||
if (of.element(arg)) { | ||
element.appendChild(arg); | ||
} else if (types.isString(arg)) { | ||
var frag = create.fragment(); | ||
} else if (of.string(arg)) { | ||
frag = create.fragment(); | ||
frag.innerHTML = arg; | ||
element.appendChild(frag); | ||
} else if (types.isFunction(arg)) { | ||
} else if (of.function(arg)) { | ||
var child = arg.call(); | ||
if (types.isElement(child)) { | ||
if (of.element(child)) { | ||
element.appendChild(child); | ||
@@ -72,6 +80,6 @@ } | ||
utils.add(element, options) | ||
utils.add(element, options); | ||
if (appendTo) { | ||
if (types.isElement(appendTo)) appendTo.appendChild(element); | ||
if (of.element(appendTo)) appendTo.appendChild(element); | ||
} | ||
@@ -90,3 +98,7 @@ | ||
for (var i = 0, l = array.length; i < l; i++) { | ||
var element = fn.call(null, array[i]); | ||
var ctx = {}; | ||
ctx.list = array; | ||
ctx.current = array[i]; | ||
ctx.index = i; | ||
var element = fn.call(ctx, ctx.current); | ||
fragment.appendChild(element); | ||
@@ -97,4 +109,8 @@ } | ||
create.component = function(elements, options, data) { | ||
} | ||
module.exports = create; | ||
},{"./lib/replace":2,"./lib/utils":3}],2:[function(require,module,exports){ | ||
},{"./lib/replace":2,"./lib/utils":3,"clas":4,"of":5}],2:[function(require,module,exports){ | ||
module.exports = format; | ||
@@ -128,7 +144,7 @@ | ||
if (typeof context[name] == 'function') { | ||
return context[name](); | ||
return escapeString(context[name]()); | ||
} | ||
return escapeString(context[name]); | ||
} | ||
}; | ||
} | ||
@@ -144,68 +160,8 @@ | ||
} | ||
return String(text).replace(/\{\{(.+?)\}\}/g, replace(context)) | ||
return String(text).replace(/\{\{(.+?)\}\}/g, replace(context)); | ||
} | ||
},{}],3:[function(require,module,exports){ | ||
var of = require('of'); | ||
var utils = {}; | ||
var types = { | ||
isElement : function(node) { | ||
return node.nodeName && (node.nodeType === 1 || node.nodeType === 9 || node.nodeType === 11); | ||
}, | ||
isString : function(value) { | ||
return Object.prototype.toString.call(value) === '[object String]'; | ||
}, | ||
isArray : function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
}, | ||
isObject : function(obj) { | ||
return obj !== null && Object.prototype.toString.call(obj) === '[object Object]'; | ||
}, | ||
isFunction : function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Function]'; | ||
} | ||
}; | ||
var classes = { | ||
get : function(element) { | ||
return element.classList ? element.classList : element.className.split(/\s+/); | ||
}, | ||
has : function(element, name) { | ||
return element.classList ? element.classList.contains(name) : !! ~utils.index(classes.get(element), name); | ||
}, | ||
add : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.add(name); | ||
} | ||
classes = classes.get(element); | ||
var index = utils.index(classes, name); | ||
if (!~index) classes.push(name); | ||
element.className = classes.join(' '); | ||
return element; | ||
}, | ||
remove : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.remove(name); | ||
} | ||
classes = classes.get(element); | ||
var index = index(classes, name); | ||
if (~index) classes.splice(index, 1); | ||
element.className = classes.join(' '); | ||
return element; | ||
}, | ||
toggleClass : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.toggle(name); | ||
} | ||
return classes.has(element, name) ? classes.remove(element, name) : classes.add(element, name); | ||
}, | ||
}; | ||
utils.types = types; | ||
utils.index = function(list, item) { | ||
@@ -225,7 +181,7 @@ return Array.prototype.indexOf.call(list, item); | ||
if (types.isString(arg)) { | ||
if (of.string(arg)) { | ||
classes.add(element, arg); | ||
} else | ||
if (types.isArray(arg)) { | ||
if (of.array(arg)) { | ||
for (var a = 0, b = arg.length; a < b; a++) { | ||
@@ -236,5 +192,5 @@ classes.add(element, arg[a]); | ||
if (types.isObject(arg)) { | ||
if (of.object(arg)) { | ||
for (var property in arg) { | ||
if (property !== null && arg[property] !== null && utils.hasProp(arg, property)) { | ||
if (property !== null && arg[property] !== undefined && utils.hasProp(arg, property)) { | ||
element.setAttribute(property, arg[property]); | ||
@@ -249,3 +205,66 @@ } | ||
module.exports = utils; | ||
},{"of":5}],4:[function(require,module,exports){ | ||
function indexOf(arr, item) { | ||
return Array.prototype.indexOf.call(arr, item); | ||
} | ||
var clas = { | ||
get : function(element) { | ||
return element.classList ? element.classList : element.className.split(/\s+/); | ||
}, | ||
has : function(element, name) { | ||
return element.classList ? element.classList.contains(name) : !! ~indexOf(clas.get(element), name); | ||
}, | ||
add : function(element, name) { | ||
if (element.classList) { | ||
element.classList.add(name); | ||
return element; | ||
} | ||
var classes = clas.get(element); | ||
name = name.trim(); | ||
var index = indexOf(classes, name); | ||
if (!~index) { | ||
classes.push(name); | ||
element.className = classes.join(' '); | ||
} | ||
return element; | ||
}, | ||
remove : function(element, name) { | ||
if (element.classList) { | ||
element.classList.remove(name); | ||
return element; | ||
} | ||
var classes = clas.get(element); | ||
name = name.trim(); | ||
var index = indexOf(classes, name); | ||
if (~index) { | ||
classes.splice(index, 1); | ||
element.className = classes.join(' '); | ||
} | ||
return element; | ||
}, | ||
toggle : function(element, name) { | ||
if (element.classList) { | ||
element.classList.toggle(name); | ||
return element; | ||
} | ||
name = name.trim(); | ||
return clas.has(element, name) ? clas.remove(element, name) : clas.add(element, name); | ||
}, | ||
}; | ||
module.exports = clas; | ||
},{}],5:[function(require,module,exports){ | ||
var of = {}, nodeTypes = [1, 3, 9, 11], types = ['string', 'array', 'object', 'function', 'boolean', 'number', 'undefined', 'arguments']; | ||
of.type = {}; | ||
var toString = function(obj) { return Object.prototype.toString.call(obj); }; | ||
of.element = of.type.element = function(node) { return node.nodeName && (nodeTypes.indexOf(node.nodeType) > -1); }; | ||
types.forEach(function(type) { | ||
of[type] = of.type[type] = function(obj) { | ||
type = type.charAt(0).toUpperCase() + type.slice(1); | ||
return obj !== null && toString(obj) === '[object ' + type + ']'; | ||
}; | ||
}); | ||
module.exports = of; | ||
},{}]},{},[1])(1) | ||
}); |
@@ -1,1 +0,1 @@ | ||
!function(b){if("object"==typeof exports&&"undefined"!=typeof module){module.exports=b()}else{if("function"==typeof define&&define.amd){define([],b)}else{var a;"undefined"!=typeof window?a=window:"undefined"!=typeof global?a=global:"undefined"!=typeof self&&(a=self),a.create=b()}}}(function(){var d,b,a;return(function c(f,k,h){function g(q,n){if(!k[q]){if(!f[q]){var m=typeof require=="function"&&require;if(!n&&m){return m(q,!0)}if(e){return e(q,!0)}var p=new Error("Cannot find module '"+q+"'");throw p.code="MODULE_NOT_FOUND",p}var i=k[q]={exports:{}};f[q][0].call(i.exports,function(l){var o=f[q][1][l];return g(o?o:l)},i,i.exports,c,f,k,h)}return k[q].exports}var e=typeof require=="function"&&require;for(var j=0;j<h.length;j++){g(h[j])}return g})({1:[function(h,k,f){var l=h("./lib/replace");var e=h("./lib/utils");var j=e.types;var g=["a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","content","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hr","html","i","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","menu","menuitem","meta","meter","nav","noscript","object","ol","optgroup","option","output","p","param","picture","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","shadow","small","source","span","strong","style","sub","summary","sup","table","tbody","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr"];var i=new Object;g.forEach(function(m){i[m]=function(z){var z=z||{};var s=document.createElement(m);var n=z.context||{};var v=z.escape||true;var q=z.classes||"";var u=z.content||null;var w=z.appendTo||null;delete z.context;delete z.escape;delete z.classes;delete z.content;delete z.appendTo;if(q){e.add(s,q)}if(u){if(j.isElement(u)){s.appendChild(u)}else{if(j.isString(u)){var y=i.fragment();var t=n?l(u,n):u;s.innerHTML=t}else{if(j.isFunction(u)){children=u.call(null,n);s.appendChild(children)}else{if(j.isArray(u)){for(var r=0,p=u.length;r<p;r++){var x=u[r];if(j.isElement(x)){s.appendChild(x)}else{if(j.isString(x)){var y=i.fragment();y.innerHTML=x;s.appendChild(y)}else{if(j.isFunction(x)){var o=x.call();if(j.isElement(o)){s.appendChild(o)}}}}}}}}}}e.add(s,z);if(w){if(j.isElement(w)){w.appendChild(s)}}return s}});i.fragment=function(){if(!arguments.length){return document.createDocumentFragment()}};i.list=function(r,q){var n=i.fragment();for(var p=0,m=r.length;p<m;p++){var o=q.call(null,r[p]);n.appendChild(o)}return n};k.exports=i},{"./lib/replace":2,"./lib/utils":3}],2:[function(g,j,f){j.exports=k;var i={"<":"<",">":">","'":"'","&":"&",'"':""","/":"/"};function e(l){return String(l).replace(/[&<>"'\/]/g,function(m){return i[m]})}function h(l){return function(m,n){if(m.substring(0,2)=="{{"&&m.substring(m.length-2)=="}}"){return"{"+n+"}"}if(!l.hasOwnProperty(n)){return m}if(typeof l[n]=="function"){return l[n]()}return e(l[n])}}function k(m){var l;if(arguments[1]&&typeof arguments[1]=="object"){l=arguments[1]}else{l=Array.prototype.slice.call(arguments,1)}return String(m).replace(/\{\{(.+?)\}\}/g,h(l))}},{}],3:[function(g,j,f){var e={};var i={isElement:function(k){return k.nodeName&&(k.nodeType===1||k.nodeType===9||k.nodeType===11)},isString:function(k){return Object.prototype.toString.call(k)==="[object String]"},isArray:function(k){return Object.prototype.toString.call(k)==="[object Array]"},isObject:function(k){return k!==null&&Object.prototype.toString.call(k)==="[object Object]"},isFunction:function(k){return Object.prototype.toString.call(k)==="[object Function]"}};var h={get:function(k){return k.classList?k.classList:k.className.split(/\s+/)},has:function(l,k){return l.classList?l.classList.contains(k):!!~e.index(h.get(l),k)},add:function(m,l){l=l.trim();if(m.classList){return m.classList.add(l)}h=h.get(m);var k=e.index(h,l);if(!~k){h.push(l)}m.className=h.join(" ");return m},remove:function(m,l){l=l.trim();if(m.classList){return m.classList.remove(l)}h=h.get(m);var k=k(h,l);if(~k){h.splice(k,1)}m.className=h.join(" ");return m},toggleClass:function(l,k){k=k.trim();if(l.classList){return l.classList.toggle(k)}return h.has(l,k)?h.remove(l,k):h.add(l,k)}};e.types=i;e.index=function(l,k){return Array.prototype.indexOf.call(l,k)};e.hasProp=function(k,l){return k!==null&&Object.prototype.hasOwnProperty.call(k,l)};e.add=function(r){var p=Array.prototype.slice.call(arguments,1);for(var q=0,o=p.length;q<o;q++){var m=p[q];if(i.isString(m)){h.add(r,m)}else{if(i.isArray(m)){for(var n=0,k=m.length;n<k;n++){h.add(r,m[n])}}else{if(i.isObject(m)){for(var s in m){if(s!==null&&m[s]!==null&&e.hasProp(m,s)){r.setAttribute(s,m[s])}}}}}}return r};j.exports=e},{}]},{},[1])(1)}); | ||
!function(b){if("object"==typeof exports&&"undefined"!=typeof module){module.exports=b()}else{if("function"==typeof define&&define.amd){define([],b)}else{var a;"undefined"!=typeof window?a=window:"undefined"!=typeof global?a=global:"undefined"!=typeof self&&(a=self),a.create=b()}}}(function(){var d,b,a;return(function c(f,k,h){function g(q,n){if(!k[q]){if(!f[q]){var m=typeof require=="function"&&require;if(!n&&m){return m(q,!0)}if(e){return e(q,!0)}var p=new Error("Cannot find module '"+q+"'");throw p.code="MODULE_NOT_FOUND",p}var i=k[q]={exports:{}};f[q][0].call(i.exports,function(l){var o=f[q][1][l];return g(o?o:l)},i,i.exports,c,f,k,h)}return k[q].exports}var e=typeof require=="function"&&require;for(var j=0;j<h.length;j++){g(h[j])}return g})({1:[function(h,k,f){var l=h("./lib/replace");var e=h("./lib/utils");var j=e.types;var g=["a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","content","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hr","html","i","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","menu","menuitem","meta","meter","nav","noscript","object","ol","optgroup","option","output","p","param","picture","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","shadow","small","source","span","strong","style","sub","summary","sup","table","tbody","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr"];var i=new Object;g.forEach(function(m){i[m]=function(z){var z=z||{};var s=document.createElement(m);var n=z.context||{};var v=z.escape||true;var q=z.classes||"";var u=z.content||null;var w=z.appendTo||null;delete z.context;delete z.escape;delete z.classes;delete z.content;delete z.appendTo;if(q){e.add(s,q)}if(u){if(j.isElement(u)){s.appendChild(u)}else{if(j.isString(u)){var y=i.fragment();var t=n?l(u,n):u;s.innerHTML=t}else{if(j.isFunction(u)){children=u.call(null,n);s.appendChild(children)}else{if(j.isArray(u)){for(var r=0,p=u.length;r<p;r++){var x=u[r];if(j.isElement(x)){s.appendChild(x)}else{if(j.isString(x)){var y=i.fragment();y.innerHTML=x;s.appendChild(y)}else{if(j.isFunction(x)){var o=x.call();if(j.isElement(o)){s.appendChild(o)}}}}}}}}}}e.add(s,z);if(w){if(j.isElement(w)){w.appendChild(s)}}return s}});i.fragment=function(){if(!arguments.length){return document.createDocumentFragment()}};i.list=function(r,q){var n=i.fragment();for(var p=0,m=r.length;p<m;p++){var o=q.call(null,r[p]);n.appendChild(o)}return n};k.exports=i},{"./lib/replace":2,"./lib/utils":3}],2:[function(g,j,f){j.exports=k;var i={"<":"<",">":">","'":"'","&":"&",'"':""","/":"/"};function e(l){return String(l).replace(/[&<>"'\/]/g,function(m){return i[m]})}function h(l){return function(m,n){if(m.substring(0,2)=="{{"&&m.substring(m.length-2)=="}}"){return"{"+n+"}"}if(!l.hasOwnProperty(n)){return m}if(typeof l[n]=="function"){return l[n]()}return e(l[n])}}function k(m){var l;if(arguments[1]&&typeof arguments[1]=="object"){l=arguments[1]}else{l=Array.prototype.slice.call(arguments,1)}return String(m).replace(/\{\{(.+?)\}\}/g,h(l))}},{}],3:[function(g,j,f){var e={};var i={isElement:function(k){return k.nodeName&&(k.nodeType===1||k.nodeType===9||k.nodeType===11)},isString:function(k){return Object.prototype.toString.call(k)==="[object String]"},isArray:function(k){return Object.prototype.toString.call(k)==="[object Array]"},isObject:function(k){return k!==null&&Object.prototype.toString.call(k)==="[object Object]"},isFunction:function(k){return Object.prototype.toString.call(k)==="[object Function]"}};var h={get:function(k){return k.classList?k.classList:k.className.split(/\s+/)},has:function(l,k){return l.classList?l.classList.contains(k):!!~e.index(h.get(l),k)},add:function(m,l){l=l.trim();if(m.classList){return m.classList.add(l)}h=h.get(m);var k=e.index(h,l);if(!~k){h.push(l)}m.className=h.join(" ");return m},remove:function(m,l){l=l.trim();if(m.classList){return m.classList.remove(l)}h=h.get(m);var k=k(h,l);if(~k){h.splice(k,1)}m.className=h.join(" ");return m},toggleClass:function(l,k){k=k.trim();if(l.classList){return l.classList.toggle(k)}return h.has(l,k)?h.remove(l,k):h.add(l,k)}};e.types=i;e.index=function(l,k){return Array.prototype.indexOf.call(l,k)};e.hasProp=function(k,l){return k!==null&&Object.prototype.hasOwnProperty.call(k,l)};e.add=function(r){var p=Array.prototype.slice.call(arguments,1);for(var q=0,o=p.length;q<o;q++){var m=p[q];if(i.isString(m)){h.add(r,m)}else{if(i.isArray(m)){for(var n=0,k=m.length;n<k;n++){h.add(r,m[n])}}else{if(i.isObject(m)){for(var s in m){if(s!==null&&m[s]!==undefined&&e.hasProp(m,s)){r.setAttribute(s,m[s])}}}}}}return r};j.exports=e},{}]},{},[1])(1)}); |
60
index.js
var template = require('./lib/replace'); | ||
var utils = require('./lib/utils'); | ||
var types = utils.types; | ||
var of = require('of'); | ||
var classes = require('clas'); | ||
var tags = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base', 'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'cite', 'code', 'col', 'colgroup', 'content', 'data', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'menu', 'menuitem', 'meta', 'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'shadow', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'template', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u', 'ul', 'var', 'video', 'wbr']; | ||
var create = new Object; | ||
var create = {}; | ||
create.registry = []; | ||
tags.forEach(function(tag) { | ||
create[tag] = function(options) { | ||
var options = options || {}; | ||
var content; | ||
if (!of.object(options)) { | ||
content = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
var element = document.createElement(tag); | ||
@@ -16,10 +23,11 @@ var context = options.context || {}; | ||
var classes = options.classes || ''; | ||
var content = options.content || null; | ||
content = options.content || null; | ||
var appendTo = options.appendTo || null; | ||
var frag; | ||
delete options['context']; | ||
delete options['escape']; | ||
delete options['classes']; | ||
delete options['content']; | ||
delete options['appendTo']; | ||
delete options.context; | ||
delete options.escape; | ||
delete options.classes; | ||
delete options.content; | ||
delete options.appendTo; | ||
@@ -31,8 +39,8 @@ if (classes) { | ||
if (content) { | ||
if (types.isElement(content)) { | ||
if (of.element(content)) { | ||
element.appendChild(content); | ||
} else | ||
if (types.isString(content)) { | ||
var frag = create.fragment(); | ||
if (of.string(content)) { | ||
frag = create.fragment(); | ||
var string = context ? template(content, context) : content; | ||
@@ -42,3 +50,3 @@ element.innerHTML = string; | ||
if (types.isFunction(content)) { | ||
if (of.function(content)) { | ||
children = content.call(null, context); | ||
@@ -48,15 +56,15 @@ element.appendChild(children); | ||
if (types.isArray(content)) { | ||
if (of.array(content)) { | ||
for (var i = 0, l = content.length; i < l; i++) { | ||
var arg = content[i]; | ||
if (types.isElement(arg)) { | ||
if (of.element(arg)) { | ||
element.appendChild(arg); | ||
} else if (types.isString(arg)) { | ||
var frag = create.fragment(); | ||
} else if (of.string(arg)) { | ||
frag = create.fragment(); | ||
frag.innerHTML = arg; | ||
element.appendChild(frag); | ||
} else if (types.isFunction(arg)) { | ||
} else if (of.function(arg)) { | ||
var child = arg.call(); | ||
if (types.isElement(child)) { | ||
if (of.element(child)) { | ||
element.appendChild(child); | ||
@@ -71,6 +79,6 @@ } | ||
utils.add(element, options) | ||
utils.add(element, options); | ||
if (appendTo) { | ||
if (types.isElement(appendTo)) appendTo.appendChild(element); | ||
if (of.element(appendTo)) appendTo.appendChild(element); | ||
} | ||
@@ -89,3 +97,7 @@ | ||
for (var i = 0, l = array.length; i < l; i++) { | ||
var element = fn.call(null, array[i]); | ||
var ctx = {}; | ||
ctx.list = array; | ||
ctx.current = array[i]; | ||
ctx.index = i; | ||
var element = fn.call(ctx, ctx.current); | ||
fragment.appendChild(element); | ||
@@ -96,2 +108,6 @@ } | ||
create.component = function(elements, options, data) { | ||
} | ||
module.exports = create; |
@@ -29,7 +29,7 @@ module.exports = format; | ||
if (typeof context[name] == 'function') { | ||
return context[name](); | ||
return escapeString(context[name]()); | ||
} | ||
return escapeString(context[name]); | ||
} | ||
}; | ||
} | ||
@@ -45,3 +45,3 @@ | ||
} | ||
return String(text).replace(/\{\{(.+?)\}\}/g, replace(context)) | ||
return String(text).replace(/\{\{(.+?)\}\}/g, replace(context)); | ||
} |
@@ -0,64 +1,4 @@ | ||
var of = require('of'); | ||
var utils = {}; | ||
var types = { | ||
isElement : function(node) { | ||
return node.nodeName && (node.nodeType === 1 || node.nodeType === 9 || node.nodeType === 11); | ||
}, | ||
isString : function(value) { | ||
return Object.prototype.toString.call(value) === '[object String]'; | ||
}, | ||
isArray : function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Array]'; | ||
}, | ||
isObject : function(obj) { | ||
return obj !== null && Object.prototype.toString.call(obj) === '[object Object]'; | ||
}, | ||
isFunction : function(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Function]'; | ||
} | ||
}; | ||
var classes = { | ||
get : function(element) { | ||
return element.classList ? element.classList : element.className.split(/\s+/); | ||
}, | ||
has : function(element, name) { | ||
return element.classList ? element.classList.contains(name) : !! ~utils.index(classes.get(element), name); | ||
}, | ||
add : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.add(name); | ||
} | ||
classes = classes.get(element); | ||
var index = utils.index(classes, name); | ||
if (!~index) classes.push(name); | ||
element.className = classes.join(' '); | ||
return element; | ||
}, | ||
remove : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.remove(name); | ||
} | ||
classes = classes.get(element); | ||
var index = index(classes, name); | ||
if (~index) classes.splice(index, 1); | ||
element.className = classes.join(' '); | ||
return element; | ||
}, | ||
toggleClass : function(element, name) { | ||
name = name.trim(); | ||
if (element.classList) { | ||
return element.classList.toggle(name); | ||
} | ||
return classes.has(element, name) ? classes.remove(element, name) : classes.add(element, name); | ||
}, | ||
}; | ||
utils.types = types; | ||
utils.index = function(list, item) { | ||
@@ -78,7 +18,7 @@ return Array.prototype.indexOf.call(list, item); | ||
if (types.isString(arg)) { | ||
if (of.string(arg)) { | ||
classes.add(element, arg); | ||
} else | ||
if (types.isArray(arg)) { | ||
if (of.array(arg)) { | ||
for (var a = 0, b = arg.length; a < b; a++) { | ||
@@ -89,5 +29,5 @@ classes.add(element, arg[a]); | ||
if (types.isObject(arg)) { | ||
if (of.object(arg)) { | ||
for (var property in arg) { | ||
if (property !== null && arg[property] !== null && utils.hasProp(arg, property)) { | ||
if (property !== null && arg[property] !== undefined && utils.hasProp(arg, property)) { | ||
element.setAttribute(property, arg[property]); | ||
@@ -94,0 +34,0 @@ } |
{ | ||
"name": "create.js", | ||
"version": "0.0.9", | ||
"version": "0.0.11", | ||
"description": "declarative dom creation", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"build": "browserify index.js --standalone create > build/create.js", | ||
"compress": "yuicompressor build/create.js -o build/create.min.js" | ||
"minify": "yuicompressor build/create.js -o build/create.min.js" | ||
}, | ||
@@ -20,3 +20,7 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/ergusto/create.js" | ||
"homepage": "https://github.com/ergusto/create.js", | ||
"dependencies": { | ||
"clas": "0.0.5", | ||
"of": "0.0.7" | ||
} | ||
} |
@@ -70,4 +70,4 @@ #create | ||
id: 'identifier', | ||
content: create.list(things, function(thing) { | ||
return create.li({content:thing}); | ||
content: create.list(things, function() { | ||
return create.li({content:this.current}); | ||
}), | ||
@@ -74,0 +74,0 @@ }); |
9
22593
2
379
+ Addedclas@0.0.5
+ Addedof@0.0.7
+ Addedclas@0.0.5(transitive)
+ Addedof@0.0.7(transitive)