-
Notifications
You must be signed in to change notification settings - Fork 3
/
cell-0.3.min.js
36 lines (35 loc) · 5.56 KB
/
cell-0.3.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
if(typeof CELL==='undefined')
var CELL={};CELL.method=function(that,name,method,_super){if(_super){var m=that[name];_super[name]=function(){if(typeof m!=='function')
throw new Error('_super.'+name+'() is not a function');return m.apply(that,arguments);};}
that[name]=method;};CELL.getter=function(that,name,obj,prop){var getter=function(){return obj[prop];};that[name]=getter;};CELL.setter=function(that,name,obj,prop){var setter=function(arg){obj[prop]=arg;return that;};that['set'+name.substring(0,1).toUpperCase()+name.substring(1)]=setter;};CELL.responds=function(that,name){return(that[name]&&typeof that[name]==='function');};CELL.log=function(){if(console&&typeof console.log==='function')
return console.log.apply(console,arguments);};CELL.remove=function(that,e){"use strict";if(that===void 0||that===null||!Array.isArray(that))
throw new TypeError();for(var i=that.length-1;i>=0;i--)
if(e===that[i])that.splice(i,1);};CELL.unique=function(that){"use strict";if(that===void 0||that===null||!Array.isArray(that))
throw new TypeError();var a=that.concat();for(var i=0;i<a.length;i++){for(var j=i+1;j<a.length;j++){if(a[i]===a[j]){a.splice(j,1);j--;}}}
return a;};if(!Array.prototype.indexOf){Array.prototype.indexOf=function(searchElement){"use strict";if(this==null){throw new TypeError();}
var t=Object(this);var len=t.length>>>0;if(len===0){return-1;}
var n=0;if(arguments.length>0){n=Number(arguments[1]);if(n!=n){n=0;}else if(n!=0&&n!=Infinity&&n!=-Infinity){n=(n>0||-1)*Math.floor(Math.abs(n));}}
if(n>=len){return-1;}
var k=n>=0?n:Math.max(len-Math.abs(n),0);for(;k<len;k++){if(k in t&&t[k]===searchElement){return k;}}
return-1;}}
CELL.DEBUG=false;CELL.debug=function(){if(CELL.DEBUG&&console&&typeof console.log==='function')
return console.log.apply(console,arguments);};if(typeof CELL==='undefined')
var CELL={};CELL.emitter=function(spec,my){my=my||{};var _super={};my.handlers={};var on;var emit;var off;var that={};on=function(type,handler){if(typeof handler==='function'){my.handlers[type]=my.handlers[type]||[];my.handlers[type].push(handler);}
else{throw new Error('emitter: not a function');}};emit=function(){var args=Array.prototype.slice.call(arguments);var name=args.shift();if(my.handlers[name]){for(var i=0;i<my.handlers[name].length;i++){if(my.handlers[name][i])
my.handlers[name][i].apply(this,args);}}
if(my.handlers['*']){args.unshift(name);for(var i=0;i<my.handlers['*'].length;i++){if(my.handlers['*'][i])
my.handlers['*'][i].apply(this,args);}}};off=function(type,handler){if(my.handlers[type]){for(var i=my.handlers[type].length-1;i>=0;i--){if(handler===my.handlers[type][i]){my.handlers[type].splice(i,1);}}}};CELL.method(that,'on',on,_super);CELL.method(that,'emit',emit,_super);CELL.method(that,'off',off,_super);CELL.getter(that,'handlers',my,'handlers');return that;};if(typeof CELL==='undefined')
var CELL={};CELL.container=function(spec,my){var _super={};my=my||{};my.name=spec.name||'';my.children=my.children||{};my.json=my.json||{};var find;var load;var refresh;var batch;var that=CELL.emitter({});refresh=function(){for(var c in my.children){if(my.children.hasOwnProperty(c)&&typeof my.json[c]!=='undefined')
my.children[c].refresh(my.json[c]);}};find=function(path){CELL.debug(my.path+' find '+path);path=path.replace(/^\/+/,'');var comps=path.split('/');var next=comps.shift();if(next.length>0){if(my.children[next])
return my.children[next].find(comps.join('/'));else
return null;}
else
return that;};load=function(){throw new Error('`load` must be implemented : '+my.path);};batch=function(){var args=Array.prototype.slice.call(arguments);var fn=args.shift();for(var c in my.children){if(my.children.hasOwnProperty(c)){if(typeof fn==='string'&&typeof my.children[c][fn]==='function'){my.children[c][fn].apply(this,args);}
else if(typeof fn==='function'){var child_args=[my.children[c]].concat(args);fn.apply(this,child_args);}}}};CELL.method(that,'load',load,_super);CELL.method(that,'refresh',refresh,_super);CELL.method(that,'find',find,_super);CELL.method(that,'batch',batch,_super);CELL.getter(that,'children',my,'children');CELL.getter(that,'name',my,'name');CELL.getter(that,'json',my,'json');return that;};if(typeof CELL==='undefined')
var CELL={};CELL.cell=function(spec,my){var _super={};my=my||{};my.path=spec.path||'/';my.container=spec.container;my.children=my.children||{};my.json=my.json||{};my.element=null;var build;var refresh;var find;var bind;var that=CELL.emitter({},my);build=function(){throw new Error('`build` must be implemented : '+my.path);my.element=$('<div></div>');my.children['foo']=foo_c({path:my.path+'/foo',container:my.container});my.element.append(my.children['foo'].build());return my.element;};bind=function(){my.hdlr=my.hdlr||function(){var args=Array.prototype.slice.call(arguments);that['emit'].apply(this,args);};for(var c in my.children){if(my.children.hasOwnProperty(c)){my.children[c].off('*',my.hdlr);my.children[c].on('*',my.hdlr);}}};refresh=function(json){my.json=json;for(var c in my.children){if(my.children.hasOwnProperty(c)&&typeof my.json[c]!=='undefined')
my.children[c].refresh(my.json[c]);}
bind();};find=function(path){CELL.debug(my.path+' find '+path);path=path.replace(/^\/+/,'');var comps=path.split('/');var next=comps.shift();if(next.length>0){if(my.children[next])
return my.children[next].find(comps.join('/'));else
return null;}
else
return that;};CELL.method(that,'build',build,_super);CELL.method(that,'refresh',refresh,_super);CELL.method(that,'find',find,_super);CELL.method(that,'bind',bind,_super);CELL.getter(that,'children',my,'children');CELL.getter(that,'element',my,'element');CELL.getter(that,'path',my,'path');CELL.getter(that,'json',my,'json');return that;};