Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Minimized version.

  • Loading branch information...
commit 200ac64fd9b8e01e89ed1e920d0628e4c96fe5e5 1 parent d2aec94
Michael Aufreiter authored
Showing with 11 additions and 38 deletions.
  1. +11 −38 data.min.js
View
49 data.min.js
@@ -1,38 +1,11 @@
-(function(){var e;e=typeof exports!=="undefined"?exports:this.Data={};e.VERSION="0.5.0";var g=this._;if(!g&&typeof require!=="undefined")g=require("underscore");e.VALUE_TYPES=["string","object","number","boolean","date"];e.isValueType=function(a){return g.include(e.VALUE_TYPES,a)};e.matches=function(a,b){b=g.isArray(b)?b:[b];var c=false;g.each(b,function(d){if(!c){var f=false;g.each(d,function(h,i){if(!f){var k,l=i.match(/^([a-z_]{1,30})(=|==|!=|>|>=|<|<=|\|=|&=)?$/),j=l[1];l=l[2]||(j=="type"||g.isArray(h)?
-"|=":"=");if(l==="|="){l=g.isArray(h)?h:[h];var m=g.isArray(a[j])?a[j]:[a[j]];k=false;g.each(l,function(o){if(g.include(m,o))k=true})}else if(l==="&="){l=g.isArray(h)?h:[h];m=g.isArray(a[j])?a[j]:[a[j]];k=g.intersect(m,l).length===l.length}else switch(l){case "!=":k=!g.isEqual(a[j],h);break;case ">":k=a[j]>h;break;case ">=":k=a[j]>=h;break;case "<":k=a[j]<h;break;case "<=":k=a[j]<=h;break;default:k=g.isEqual(a[j],h);break}if(!k)return f=true}});if(!f)return c=true}});return c};e.uuid=function(a){for(var b=
-"0123456789abcdefghijklmnopqrstuvwxyz".split(""),c=[],d=0;d<32;d++)c[d]=b[0|Math.random()*16];return(a?a:"")+c.join("")};g.Events={bind:function(a,b){this._callbacks||(this._callbacks={});(this._callbacks[a]||(this._callbacks[a]=[])).push(b);return this},unbind:function(a,b){var c;if(a){if(c=this._callbacks)if(b){c=c[a];if(!c)return this;for(var d=0,f=c.length;d<f;d++)if(b===c[d]){c.splice(d,1);break}}else c[a]=[]}else this._callbacks={};return this},trigger:function(a){var b,c,d,f;if(!(c=this._callbacks))return this;
-if(b=c[a]){d=0;for(f=b.length;d<f;d++)b[d].apply(this,Array.prototype.slice.call(arguments,1))}if(b=c.all){d=0;for(f=b.length;d<f;d++)b[d].apply(this,arguments)}return this}};var q=function(){};g.inherits=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){return a.apply(this,arguments)};q.prototype=a.prototype;d.prototype=new q;b&&g.extend(d.prototype,b);c&&g.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d};e.Hash=function(a){var b=this;this.data=
-{};this.keyOrder=[];this.length=0;if(a instanceof Array)g.each(a,function(c,d){b.set(d,c)});else a instanceof Object&&g.each(a,function(c,d){b.set(d,c)});this.initialize&&this.initialize(attributes,options)};g.extend(e.Hash.prototype,g.Events,{clone:function(){var a=new e.Hash;a.length=this.length;g.each(this.data,function(b,c){a.data[c]=b});a.keyOrder=this.keyOrder.slice(0,this.keyOrder.length);return a},set:function(a,b,c){var d;if(a===undefined)return this;if(this.data[a])d=this.index(a);else{if(c!==
-undefined){d=this.select(function(h,i,k){return k<c});var f=this.select(function(h,i,k){return k>=c});this.keyOrder=[].concat(d.keyOrder);this.keyOrder.push(a);this.keyOrder=this.keyOrder.concat(f.keyOrder)}else this.keyOrder.push(a);d=this.length;this.length+=1}this.data[a]=b;this[d]=this.data[a];this.trigger("set",a);return this},del:function(a){if(this.data.hasOwnProperty(a)){var b=this.length,c=this.index(a);delete this.data[a];this.keyOrder.splice(c,1);Array.prototype.splice.call(this,c,1);this.length=
-b-1;this.trigger("del",a)}return this},get:function(a){return this.data.hasOwnProperty(a)?this.data[a]:undefined},at:function(a){return this.data[this.keyOrder[a]]},first:function(){return this.at(0)},range:function(a,b){for(var c=new e.Hash,d=a;d<=b;d++)c.set(this.key(d),this.at(d));return c},rest:function(a){return this.range(a,this.length-1)},last:function(){return this.at(this.length-1)},key:function(a){return this.keyOrder[a]},index:function(a){return this.keyOrder.indexOf(a)},each:function(a){var b=
-this;g.each(this.keyOrder,function(c,d){a.call(b,b.data[c],c,d)});return this},values:function(){var a=[];this.each(function(b){a.push(b)});return a},keys:function(){return g.clone(this.keyOrder)},toArray:function(){var a=[];this.each(function(b,c){a.push({key:c,value:b})});return a},toJSON:function(){var a={};this.each(function(b,c){a[c]=b.toJSON?b.toJSON():b});return a},map:function(a){var b=this.clone(),c=this;b.each(function(d,f,h){b.data[c.key(h)]=a.call(b,d)});return b},select:function(a){var b=
-new e.Hash,c=this;this.each(function(d,f,h){a.call(c,d,f,h)&&b.set(f,d)});return b},sort:function(a){var b=this.clone();sortedKeys=b.toArray().sort(a);b.keyOrder=g.map(sortedKeys,function(c){return c.key});return b},intersect:function(a){var b=new e.Hash,c,d;if(a.length<this.length){c=a;d=this}else{c=this;d=a}g.each(c.keyOrder,function(f){d.get(f)&&b.set(f,a.get(f))});return b},union:function(a){var b=new e.Hash;this.each(function(c,d){b.set(d,c)});a.each(function(c,d){b.get(d)||b.set(d,c)});return b},
-difference:function(a){result=new e.Hash;this.each(function(b,c){a.get(c)||result.set(c,b)});return result}});e.Comparators={};e.Comparators.ASC=function(a,b){return a.value===b.value?0:a.value<b.value?-1:1};e.Comparators.DESC=function(a,b){return a.value===b.value?0:a.value>b.value?-1:1};e.Aggregators={};e.Aggregators.SUM=function(a){var b=0;a.each(function(c){if(g.isNumber(c))b+=c});return b};e.Aggregators.MIN=function(a){var b=Infinity;a.each(function(c){if(g.isNumber(c)&&c<b)b=c});return b};e.Aggregators.MAX=
-function(a){var b=-Infinity;a.each(function(c){if(g.isNumber(c)&&c>b)b=c});return b};e.Aggregators.AVG=function(a){var b=0,c=0;a.each(function(d){if(g.isNumber(d)){b+=d;c+=1}});return c===0?0:b/c};e.Aggregators.COUNT=function(a){return a.length};e.Modifiers={};e.Modifiers.DEFAULT=function(a){return a};e.Modifiers.MONTH=function(a){return a.getMonth()};e.Modifiers.QUARTER=function(a){return Math.floor(a.getMonth()/3)+1};e.Transformers={group:function(a,b,c,d){function f(j){var m=new e.Hash;g.each(c,
-function(r,n){var p=k[c[n]].get(j[n]).referencedObjects;m=n===0?m.union(p):m.intersect(p)});if(j.length===0)m=a.objects();if(m.length===0)return null;var o={type:b._id};g.each(i[b._id].properties,function(r,n){if(g.include(c,n))o[n]=j[g.indexOf(c,n)];else{var p=m.map(function(s){return s.get(d[n].property||n)});o[n]=(d[n].aggregator||e.Aggregators.SUM)(p)}});return o}function h(j,m){if(j===c.length-1){var o=f(m);o&&l.set(m.join("::"),o)}else{j+=1;k[c[j]].each(function(r,n){h(j,m.concat([n]))})}}var i=
-{};b=a.get(b);var k={};i[b._id]={type:"/type/type",properties:{},indexes:b.indexes};g.each(c,function(j){i[b._id].properties[j]=b.properties().get(j).toJSON()});g.each(d,function(j,m){var o=b.properties().get(j.property||m).toJSON();if(j.name)o.name=j.name;i[b._id].properties[m]=o});var l=new e.Graph(i);g.each(c,function(j){k[j]=b.properties().get(j).all("values")});h(-1,[]);return l}};e.Node=function(a){this.nodeId=e.Node.generateId();if(a)this.val=a.value;this._properties={};this.initialize&&this.initialize(a)};
-e.Node.nodeCount=0;e.Node.generateId=function(){return e.Node.nodeCount+=1};g.extend(e.Node.prototype,g.Events,{identity:function(){return this.nodeId},replace:function(a,b){this._properties[a]=b},set:function(a,b,c){this._properties[a]||(this._properties[a]=new e.Hash);this._properties[a].set(b,c instanceof e.Node?c:new e.Node({value:c}));return this},get:function(a,b){if(b!==undefined&&this._properties[a]!==undefined)return this._properties[a].get(b)},all:function(a){return this._properties[a]},
-first:function(a){return(a=this._properties[a])?a.first():null},value:function(a){return this.values(a).first()},values:function(a){if(!this.all(a))return new e.Hash;return this.all(a).map(function(b){return b.val})}});e.Adapter=function(a){this.config=a};e.Adapters={};e.Property=g.inherits(e.Node,{constructor:function(a,b,c){e.Node.call(this);this._id=this.key=b;this.type=a;this.unique=c.unique;this.name=c.name;this.meta=c.meta||{};this.validator=c.validator;this.sync=c.sync;this.required=c.required;
-this["default"]=c["default"];this.expectedTypes=g.isArray(c.type)?c.type:[c.type];this.replace("values",new e.Hash)},isValueType:function(){return e.isValueType(this.expectedTypes[0])},isObjectType:function(){return!this.isValueType()},registerValues:function(a,b){var c=this,d=new e.Hash;g.each(a,function(f,h){if(f!==undefined){var i;if(c.isValueType()&&c.expectedTypes[0]==="object"){i=new e.Node({value:f});d.set(h,i)}else{if(b.all(c.key))i=b.all(c.key).get(f);if(!i){i=c.get("values",f);if(!i){if(c.isObjectType()){if(typeof f===
-"object")f=c.type.g.set(f)._id;i=c.type.g.get("nodes",f);if(!i){i=new e.Object(c.type.g,f);c.type.g.set("nodes",f,i)}}else{i=new e.Node({value:f});i.referencedObjects=new e.Hash}c.set("values",f,i)}i.referencedObjects.set(b._id,b)}d.set(f,i)}}});b.all(c.key)&&this.unregisterValues(b.all(c.key).difference(d),b);return d},unregisterValues:function(a,b){var c=this;a.each(function(d,f){d.referencedObjects&&d.referencedObjects.length>1?d.referencedObjects.del(b._id):c.all("values").del(f)})},aggregate:function(a){return a(this.values("values"))},
-toJSON:function(){return{name:this.name,type:this.expectedTypes,unique:this.unique,meta:this.meta,sync:this.sync,validator:this.validator,required:this.required,"default":this["default"]}}});e.Type=g.inherits(e.Node,{constructor:function(a,b,c){var d=this;e.Node.call(this);this.g=a;this._id=this.key=b;this._rev=c._rev;this._conflicted=c._conflicted;this.type=c.type;this.name=c.name;this.meta=c.meta||{};this.indexes=c.indexes;d.replace("properties",new e.Hash);g.each(c.properties,function(f,h){d.set("properties",
-h,new e.Property(d,h,f))})},properties:function(){return this.all("properties")},objects:function(){return this.all("nodes")},toJSON:function(){var a={_id:this._id,type:"/type/type",name:this.name,properties:{}};if(this._rev)a._rev=this._rev;if(this.meta&&g.keys(this.meta).length>0)a.meta=this.meta;if(this.indexes&&g.keys(this.indexes).length>0)a.indexes=this.indexes;this.all("properties").each(function(b){var c=a.properties[b.key]={name:b.name,unique:b.unique,type:b.expectedTypes,required:b.required?
-true:false};if(b["default"])c["default"]=b["default"];if(b.validator)c.validator=b.validator;if(b.meta&&g.keys(b.meta).length>0)c.meta=b.meta});return a}});e.Object=g.inherits(e.Node,{constructor:function(a,b,c){e.Node.call(this);this.g=a;this._id=this.key=b;this.html_id=b.replace(/\//g,"_");this.meta=c?c.meta||{}:{};this.errors=[];this._types=new e.Hash;this.referencedObjects=new e.Hash;if(c)this.data=c},types:function(){return this._types},toString:function(){return this.get("name")||this.val||
-this._id},properties:function(){var a=new e.Hash;this._types.each(function(b){b.all("properties").each(function(c){a.set(c.key,c)})});return a},build:function(){var a=this,b=g.isArray(this.data.type)?this.data.type:[this.data.type];if(!this.data)throw Error("Object has no data, and cannot be built");this._rev=this.data._rev;this._conflicted=this.data._conflicted;this._deleted=this.data._deleted;this.type=this.g.get("nodes",g.last(b));g.each(b,function(c){a._types.set(c,a.g.get("nodes",c));a._types.get(c).all("properties").each(function(d,
-f){function h(i){if(i){i=g.isArray(i)?i:[i];a.replace(d.key,d.registerValues(i,a))}else a.replace(d.key,new e.Hash)}if(a.data[f]!==undefined)h(a.data[f]);else d["default"]&&h(d["default"])})});this._dirty&&this.g.trigger("dirty",this)},validate:function(){if(this.type.key==="/type/type")return true;var a=this;this.errors=[];this.properties().each(function(b,c){if(a.get(c)===undefined||a.get(c)===null||a.get(c)==="")b.required&&a.errors.push({property:c,message:'Property "'+b.name+'" is required'});
-else{var d=b.expectedTypes,f=function(h,i){if(g.include(i,typeof h))return true;if(!h.data)return true;if(h instanceof e.Object&&g.intersect(i,h.types().keys()).length>0)return true;if(typeof h==="object"&&g.include(i,h.constructor.name.toLowerCase()))return true;return false};b.unique&&!f(a.get(c),d)&&a.errors.push({property:c,message:'Invalid type for property "'+b.name+'"'});!b.unique&&!g.all(a.get(c).values(),function(h){return f(h,d)})&&a.errors.push({property:c,message:'Invalid value type for property "'+
-b.name+'"'})}if(b.validator)RegExp(b.validator).test(a.get(c))||a.errors.push({property:c,message:'Invalid value for property "'+b.name+'"'})});return this.errors.length===0},get:function(a,b){if(!this.data)return null;var c=this.properties().get(a);if(!c)return null;return arguments.length===1?c.isObjectType()?c.unique?this.first(a):this.all(a):c.unique?this.value(a):this.values(a):e.Node.prototype.get.call(this,a,b)},set:function(a){var b=this;if(arguments.length===1)g.each(a,function(c,d){var f=
-b.properties().get(d);if(f){b.replace(f.key,f.registerValues(g.isArray(c)?c:[c],b));b._dirty=true;b.g.trigger("dirty",b);b.g.snapshot()}});else return e.Node.prototype.set.call(this,arguments[0],arguments[1],arguments[2])},toJSON:function(){var a=this;result={};g.each(this._properties,function(b,c){var d=a.properties().get(c);result[c]=d.isObjectType()?d.unique?a.all(c).keys()[0]:a.all(c).keys():d.unique?a.value(c):a.values(c).values()});result.type=this.types().keys();result._id=this._id;if(this._rev!==
-undefined)result._rev=this._rev;if(this._deleted)result._deleted=this._deleted;return result}});g.extend(e.Object.prototype,g.Events);e.Graph=g.inherits(e.Node,{constructor:function(a,b){e.Node.call(this);this.watchers={};this.replace("nodes",new e.Hash);if(a){this.merge(a,b&&b.dirty);this.syncMode=b&&b.syncMode?b.syncMode:"push";if(b&&b.persistent){this.persistent=b.persistent;this.restore()}}},connect:function(a,b){if(typeof exports!=="undefined")this.adapter=new (require(__dirname+"/adapters/"+
-a+"_adapter"))(this,b);else{if(!e.Adapters[a])throw Error('Adapter "'+a+'" not found');this.adapter=new e.Adapters[a](this,b)}return this},connected:function(a){if(this.adapter.realtime)this.connectedCallback=a;else a()},serve:function(a){require(__dirname+"/server").initialize(a,this)},watch:function(a,b,c){this.watchers[a]=c;this.adapter.watch(a,b,function(){})},unwatch:function(a){delete this.watchers[a];this.adapter.unwatch(a,function(){})},empty:function(){var a=this;g.each(this.objects().keys(),
-function(b){a.del(b);a.all("nodes").del(b)})},merge:function(a,b){var c=this;g.select(a,function(f,h){if(f.type==="/type/type"||f.type==="type"){if(!c.get("nodes",h)){c.set("nodes",h,new e.Type(c,h,f));c.get(h)._dirty=f._dirty?f._dirty:b}return true}return false});var d=g.select(a,function(f,h){if(f.type!=="/type/type"&&f.type!=="type"){var i=c.get("nodes",h),k=g.isArray(f.type)?f.type:[f.type];if(i)if(i._dirty){i._conflicted=true;i._conflicted_rev=f}else i.data=f;else{i=new e.Object(c,h,f);c.set("nodes",
-h,i)}g.each(k,function(l){if(!c.get("nodes",l))throw Error("Type '"+l+"' not found for "+h+"...");c.get("nodes",l).set("nodes",h,i)});c.get(h)._dirty=f._dirty?f._dirty:b;if(!f._id)f._id=h;return true}return false});g.each(d,function(f){f=c.get(f._id);f.data&&!f._conflicted&&f.build()});this.conflictedNodes().length>0&&this.trigger("conflicted");this.snapshot();return this},set:function(a){if(arguments.length===2)a=g.extend(arguments[1],{_id:arguments[0]});var b=g.isArray(a.type)?a.type:[a.type];if(arguments.length<=
-2){a._id=a._id?a._id:e.uuid("/"+g.last(g.last(b).split("/"))+"/");b=this.get(a._id)?this.get(a._id):new e.Object(this,a._id,g.clone(a),true);b.data=a;b._dirty=true;b.build();this.set("nodes",a._id,b);this.snapshot();return b}else return e.Node.prototype.set.call(this,arguments[0],arguments[1],arguments[2])},get:function(a){return arguments.length===1?this.get("nodes",a):e.Node.prototype.get.call(this,arguments[0],arguments[1])},del:function(a){var b=this.get(a);if(b){b._deleted=true;b._dirty=true;
-b.properties().each(function(c,d){var f=b.all(d);f&&c.unregisterValues(f,b)});this.trigger("dirty",b);this.snapshot()}},find:function(a){return this.objects().select(function(b){return e.matches(b.toJSON(),a)})},snapshot:function(){this.persistent&&localStorage.setItem("graph",JSON.stringify(this.toJSON(true)))},restore:function(){var a=JSON.parse(localStorage.getItem("graph"));a&&this.merge(a)},fetch:function(a,b,c){var d=this,f=new e.Hash;if(typeof b==="function"&&typeof c==="undefined"){c=b;b=
-{}}this.adapter.read(a,b,function(h,i){if(i){d.merge(i,false);g.each(i,function(k,l){f.set(l,d.get(l))})}h?c(h):c(null,f)})},sync:function(a,b){a=a||function(){};b=b||function(d){d()};var c=this;if(this.syncMode==="full")this.pull(function(){c.conflictedNodes().length>0?b(function(){c.push(a)}):c.push(a)});else this.syncMode==="pull"?this.pull(function(){c.conflictedNodes().length>0&&b(function(){})}):this.push(a)},push:function(a){var b=this,c=this.dirtyNodes(),d=new e.Hash;c.select(function(f,h){if(!f.validate||
-f.validate&&f.validate())d.set(h,f)});this.adapter.write(d.toJSON(),function(f,h){if(f)return a(f);d.each(function(k,l){if(h[l]){k._dirty=false;k._rejected=false}else k._rejected=true});b.merge(h,false);this.persistent&&b.snapshot();b.invalidNodes().length>0&&b.trigger("invalid");b.conflictedNodes().length>0&&b.trigger("conflicted");b.rejectedNodes().length>0&&b.trigger("rejected");var i=b.invalidNodes().union(b.conflictedNodes()).union(b.rejectedNodes()).length;a(i>0?i+" unsaved nodes":null)})},
-pull:function(a){var b=this,c={};this.objects().each(function(d){c[d._id]=d._rev||null});this.adapter.pull(c,function(d,f){b.merge(f,false);a()})},group:function(a,b,c){var d=new e.Collection;d.g=e.Transformers.group(this,a,b,c);return d},types:function(){return this.all("nodes").select(function(a){return a.type==="/type/type"||a.type==="type"})},objects:function(){return this.all("nodes").select(function(a){return a.type!=="/type/type"&&a.type!=="type"&&a.data&&!a._deleted})},dirtyNodes:function(){return this.all("nodes").select(function(a){return a._dirty&&
-(a.data||a instanceof e.Type)})},invalidNodes:function(){return this.all("nodes").select(function(a){return a.errors&&a.errors.length>0})},conflictedNodes:function(){return this.all("nodes").select(function(a){return a._conflicted})},rejectedNodes:function(){return this.all("nodes").select(function(a){return a._rejected})},toJSON:function(a){var b={};this.all("nodes").each(function(c,d){if(c.data||c instanceof e.Type){b[d]=c.toJSON();if(a){if(c._dirty)b[d]._dirty=true;if(c._deleted)b[d]._deleted=
-true;if(c._conflicted)b[d]._conflicted=true;if(c._rejected)b[d]._rejected=true}}});return b}});g.extend(e.Graph.prototype,g.Events);e.Collection=function(a){var b=this,c={"/type/item":{type:"/type/type",properties:{}}};if(a)c["/type/item"].indexes=a.indexes||{};if(a){g.each(a.properties,function(d,f){c["/type/item"].properties[f]=d});this.g=new e.Graph(c);g.each(a.items,function(d,f){b.set(f,d)})}else this.g=new e.Graph};g.extend(e.Collection.prototype,{get:function(){return this.g.get.apply(this.g,
-arguments)},set:function(a,b){this.g.set(a,g.extend(b,{type:"/type/item"}))},find:function(a){a["type|="]="/type/item";return this.g.find(a)},filter:function(a){return new e.Collection({properties:this.properties().toJSON(),items:this.find(a).toJSON()})},group:function(a,b){var c=new e.Collection;c.g=e.Transformers.group(this.g,"/type/item",a,b);return c},properties:function(){return this.g.get("nodes","/type/item").all("properties")},items:function(){return this.g.objects()},indexes:function(){return this.g.get("/type/item").indexes},
-toJSON:function(){return{properties:this.g.toJSON()["/type/item"].properties,items:this.g.objects().toJSON()}}})})();
+(function(){var f;f=typeof exports!=="undefined"?exports:this.Data={};f.VERSION="0.6.0";var c=this._;if(!c&&typeof require!=="undefined")c=require("underscore");f.VALUE_TYPES=["string","object","number","boolean","date"];f.isValueType=function(a){return c.include(f.VALUE_TYPES,c.last(a))};f.permute=function(a){if(a.length==1)return a[0];else{for(var b=[],e=f.permute(a.slice(1)),d=0;d<e.length;d++)for(var g=0;g<a[0].length;g++)b.push(a[0][g]+e[d]);return b}};f.wrap=function(a,b){var e=new f.Collection({type:a,
+objects:[]});e.objects=b;c.each(b,function(d,g){e.keys[d._id]=g})};f.uuid=function(a){for(var b="0123456789abcdefghijklmnopqrstuvwxyz".split(""),e=[],d=0;d<32;d++)e[d]=b[0|Math.random()*16];return(a?a:"")+e.join("")};c.Events={bind:function(a,b){this._callbacks||(this._callbacks={});(this._callbacks[a]||(this._callbacks[a]=[])).push(b);return this},unbind:function(a,b){var e;if(a){if(e=this._callbacks)if(b){e=e[a];if(!e)return this;for(var d=0,g=e.length;d<g;d++)if(b===e[d]){e.splice(d,1);break}}else e[a]=
+[]}else this._callbacks={};return this},trigger:function(a){var b,e,d,g;if(!(e=this._callbacks))return this;if(b=e[a]){d=0;for(g=b.length;d<g;d++)b[d].apply(this,Array.prototype.slice.call(arguments,1))}if(b=e.all){d=0;for(g=b.length;d<g;d++)b[d].apply(this,arguments)}return this}};f.Type=function(a){this._id=a._id;this.type="/type/type";this.name=a.name;this.objects=[];this.meta=a.meta||{};this.built=false;a.indexes&&this.setupIndexes(a.indexes);this.properties=a.properties;c.each(this.properties,
+c.bind(function(b){b.type=c.isArray(b.type)?b.type:[b.type];b.unique=c.isBoolean(b.unique)?b.unique:true},this))};c.extend(f.Type.prototype,c.Events,{setupIndexes:function(a){var b=this;this.indexes={};c.each(a,function(e,d){b.indexes[d]={properties:e,objects:{}}})},buildIndexes:function(){function a(e,d){var g=[];c.each(d.properties,function(h){h=e.properties[h];g.push(c.isArray(h)?h:[h])});c.each(f.permute(g),function(h){var i=d.objects[h];i||(i=d.objects[h]=[]);i.push(e)})}var b=this;c.each(this.objects,
+function(e){c.each(b.indexes,function(d){a(e,d)})});this.built=true},find:function(a){this.built||this.buildIndexes();var b=c.select(this.indexes,function(d){return c.intersect(Object.keys(a),d.properties).length===d.properties.length})[0];if(!b)return console.log("No index found.");var e="";c.each(b.properties,function(d){e+=a[d]});return b.objects[e]||[]},toJSON:function(){return{_id:this._id,type:"/type/type",properties:this.properties,meta:this.meta,indexes:c.map(this.indexes,function(a){return a.properties})}}});
+f.Object=function(a,b){this._id=a._id;this.host=b;this.properties={};this.set(a)};c.extend(f.Object.prototype,c.Events,{type:function(){return this.host.get(c.last(this.types))},toString:function(){return this.get("name")||this.val||this._id},property:function(a){var b=null;c.find(this.types.reverse(),c.bind(function(e){return b=this.host.get(e).properties[a]},this));return b},get:function(a){var b=this.property(a);a=this.properties[a];if(!b||!a)return null;return f.isValueType(b.type)?a:b.unique?
+this.host.get(a):c.map(a,c.bind(function(e){return this.host.get(e)},this))},set:function(a){var b=this;if(a.type)this.types=c.isArray(a.type)?a.type:[a.type];if(a.meta)this.meta=this.object.meta;c.each(a,c.bind(function(e,d){if(!(!b.property(d)||d==="type")){b.properties[d]=e;b._dirty=true}},this))},toJSON:function(){return c.extend(this.properties,{_id:this._id,type:this.types})}});f.Graph=function(a,b){this.nodes=[];this.objects=[];this.types=[];this.keys={};a&&this.merge(a,b&&b.dirty)};c.extend(f.Graph.prototype,
+c.Events,{merge:function(a){c.each(a,c.bind(function(b,e){this.set(c.extend(b,{_id:e}))},this));return this},find:function(a){var b=this.get(a.type);delete a.type;return Object.keys(a).length===0?b.objects:b.find(a)},set:function(a,b){function e(){return c.last(d)==="/type/type"?new f.Type(a):new f.Object(a,this)}if(b===undefined)b=true;var d=c.isArray(a.type)?a.type:[a.type];a._id=a._id?a._id:f.uuid("/"+c.last(c.last(d).split("/"))+"/");var g=this.get(a._id);if(g)g.set(a);else{g=e.apply(this);this.keys[a._id]=
+this.nodes.length;this.nodes.push(g);if(c.last(d)==="/type/type")this.types.push(g);else{this.objects.push(g);c.each(g.types,function(h){this.get(h).objects.push(g)},this)}}return g},get:function(a){return this.nodes[this.keys[a]]},del:function(a){if(a=this.get(a)){a._deleted=true;a._dirty=true}},toJSON:function(){var a={};c.each(this.nodes,function(b){a[b._id]=b.toJSON()});return a}});f.Collection=function(a){this.type=new f.Type(a.type,this);this.objects=this.type.objects;this.keys={};c.each(a.objects,
+c.bind(function(b){this.add(b)},this))};c.extend(f.Collection.prototype,{get:function(a){if(a.match("^/type/"))return this.type;return this.objects[this.keys[a]]},add:function(a){a._id=a._id?a._id:f.uuid("/"+c.last(this.type._id.split("/"))+"/");a.type=this.type._id;var b=this.get(a._id);if(b)b.set(a);else{b=new f.Object(a,this);this.keys[b._id]=this.objects.length;this.type.objects.push(b);this.objects.push(b)}return b},find:function(a){return this.type.find(a)},toJSON:function(){return{type:this.type.toJSON(),
+objects:c.map(this.objects,function(a){return a.toJSON()})}}})})();
Please sign in to comment.
Something went wrong with that request. Please try again.