Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a packed version.

  • Loading branch information...
commit d687d89d37f7345b24887519fe8ba81bc9cf7111 1 parent 403c738
Michael Aufreiter authored
Showing with 35 additions and 0 deletions.
  1. +35 −0 data.min.js
View
35 data.min.js
@@ -0,0 +1,35 @@
+(function(){var e;e=typeof exports!=="undefined"?exports:this.Data={};e.VERSION="0.2.0";var f=this._;if(!f&&typeof require!=="undefined")f=require("underscore");e.VALUE_TYPES=["string","object","number","boolean","date"];e.isValueType=function(a){return f.include(e.VALUE_TYPES,a)};e.setAdapter=function(a,b){if(typeof exports!=="undefined"){var c=require("./adapters/"+a+"_adapter");e.adapter=new c(b)}else e.adapter=new window[a](b)};e.middleware={readgraph:[],writegraph:[]};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("")};f.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,g=c.length;d<g;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,g;if(!(c=this._callbacks))return this;
+if(b=c[a]){d=0;for(g=b.length;d<g;d++)b[d].apply(this,Array.prototype.slice.call(arguments,1))}if(b=c.all){d=0;for(g=b.length;d<g;d++)b[d].apply(this,arguments)}return this}};var q=function(){};f.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&&f.extend(d.prototype,b);c&&f.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)f.each(a,function(c,d){b.set(d,c)});else a instanceof Object&&f.each(a,function(c,d){b.set(d,c)});this.initialize&&this.initialize(attributes,options)};f.extend(e.Hash.prototype,f.Events,{clone:function(){var a=new e.Hash;a.length=this.length;f.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 g=this.select(function(h,i,k){return k>=c});this.keyOrder=[].concat(d.keyOrder);this.keyOrder.push(a);this.keyOrder=this.keyOrder.concat(g.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[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[a]},at:function(a){return this.data[this.keyOrder[a]]},first:function(){return this.at(0)},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;f.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 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()});return a},map:function(a){var b=this.clone(),c=this;b.each(function(d,g,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,g,h){a.call(c,d,g,h)&&b.set(g,d)});return b},sort:function(a){var b=this.clone();sortedKeys=b.toArray().sort(a);b.keyOrder=f.map(sortedKeys,function(c){return c.key});
+return b},intersect:function(a){var b=new e.Hash;this.each(function(c,d){a.each(function(g,h){d===h&&b.set(d,c)})});return b},union:function(a){var b=new e.Hash;this.each(function(c,d){b.get(d)||b.set(d,c)});a.each(function(c,d){b.get(d)||b.set(d,c)});return b}});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){b+=
+c});return b};e.Aggregators.MIN=function(a){var b=Infinity;a.each(function(c){if(c<b)b=c});return b};e.Aggregators.MAX=function(a){var b=-Infinity;a.each(function(c){if(c>b)b=c});return b};e.Aggregators.AVG=function(a){return e.Aggregators.SUM(a)/a.length};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 g(j){var l=new e.Hash;f.each(c,function(p,m){var o=k[c[m]].get(j[m]).referencedObjects;l=m===0?l.union(o):l.intersect(o)});var n={type:b._id};f.each(i[b._id].properties,function(p,m){if(f.include(c,m))n[m]=j[f.indexOf(c,m)];else{var o=l.map(function(r){return r.get(m)});n[m]=(d[m].aggregator||e.Aggregators.SUM)(o)}});return n}function h(j,l){if(j===c.length-1)i[l.join("::")]=g(l);else{j+=1;k[c[j]].each(function(n,p){h(j,l.concat([p]))})}}var i={};b=a.get(b);var k={};i[b._id]={type:"/type/type",
+properties:{}};f.each(c,function(j){i[b._id].properties[j]=b.properties().get(j).toJSON()});f.each(d,function(j,l){var n=b.properties().get(l).toJSON();if(j.name)n.name=j.name;i[b._id].properties[l]=n});f.each(c,function(j){k[j]=b.properties().get(j).all("values")});h(-1,[]);return new e.Graph(i)}};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};f.extend(e.Node.prototype,f.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};f.extend(e.Adapter.prototype,{flush:function(){},readGraph:function(){},writeGraph:function(){}});e.Property=f.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.required=c.required;this["default"]=c["default"];this.expectedTypes=
+f.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()},registerValue:function(a,b,c){if(this.isObjectType())this.set("values",a,b);else{var d=this.get("values",a);if(!d){d=new e.Node({value:b});d.referencedObjects=new e.Hash}c.set(this.key,a,d);d.referencedObjects.set(c.key,c);this.set("values",a,d)}},unregisterValue:function(a){this.isObjectType()&&this.all("values").del(a)},
+aggregate:function(a){return a(this.values("values"))},toJSON:function(){return{name:this.name,type:this.expectedTypes,unique:this.unique,meta:this.meta,valiate:this.validator,required:this.required,"default":this["default"]}}});e.Type=f.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||{};f.each(c.properties,function(g,h){d.set("properties",h,new e.Property(d,
+h,g))})},properties:function(){return this.all("properties")},objects:function(){return this.all("objects")},toJSON:function(){var a={_id:this._id,_rev:this._rev,type:"/type/type",name:this.name,properties:{},meta:this.meta};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&&f.keys(b.meta).length>0)c.meta=b.meta});
+return a}});e.Object=f.inherits(e.Node,{constructor:function(a,b,c){var d=this;e.Node.call(this);this.g=a;this._id=this.key=b;this.html_id=b.replace(/\//g,"_");this.dirty=true;this.errors=[];this._types=new e.Hash;this.referencedObjects=new e.Hash;if(c)this.data=c;this.bind("set",function(g,h,i){var k=this.properties().get(g);if(k.isObjectType()){f.each(i,function(j){k.unregisterValue(j,d.g.get(j),d)});f.each(h,function(j){k.registerValue(j,d.g.get(j),d)})}else{f.each(i,function(j){k.unregisterValue(j,
+j,d)});f.each(h,function(j){k.registerValue(j,j,d)})}})},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=f.isArray(this.data.type)?this.data.type:[this.data.type];if(!this.data)throw"object has no data, and cannot be built";var b=this;delete this.data._id;this._rev=this.data._rev;this._conflicted=
+this.data._conflicted;this._deleted=this.data._deleted;this.type=this.g.get("objects",f.last(a));f.each(a,function(c){b._types.set(c,b.g.get("objects",c));b._types.get(c).all("properties").each(function(d,g){function h(i){i=f.isArray(i)?i:[i];b.replace(d.key,new e.Hash);d.isObjectType()?b.setObjectProperty(d,i):b.setValueProperty(d,i)}if(b.data[g]!==undefined)h(b.data[g]);else d["default"]&&h(d["default"])})});this.dirty&&this.g.trigger("dirty")},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,g=function(h,i){if(f.include(i,typeof h))return true;if(!h.data)return true;if(h instanceof e.Object&&f.intersect(i,h.types().keys()).length>0)return true;if(typeof h==="object"&&f.include(i,h.constructor.name.toLowerCase()))return true;return false};b.unique&&!g(a.get(c),
+d)&&a.errors.push({property:c,message:'Invalid type for property "'+b.name+'"'});!b.unique&&!f.all(a.get(c).values(),function(h){return g(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},newReference:function(a){var b=this.g.get("objects",a);if(!b){b=new e.Object(this.g,a);this.g.set("objects",a,
+b)}b.referencedObjects.set(this.key,this);return b},setObjectProperty:function(a,b){var c=this;c.replace(a.key,new e.Hash);f.each(b,function(d){if(d){if(typeof d==="object")d=c.g.set(null,d)._id;d=c.newReference(d);var g=c.all(a.key).keys();c.set(a.key,d.key,d);c.trigger("set",a.key,c.all(a.key).keys(),g)}})},setValueProperty:function(a,b){var c=this;c.replace(a.key,new e.Hash);f.each(b,function(d){var g=a.get("values",d);if(!g){g=new e.Node({value:d});g.referencedObjects=new e.Hash}var h=c.all(a.key).keys();
+c.set(a.key,d,g);c.trigger("set",a.key,c.all(a.key).keys(),h)})},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)},getAsync:function(a,b){var c=this;this.data?b(null,c.get(a)):this.g.fetch({_id:this._id},{},function(d){d?b(d):b(null,c.get(a))})},set:function(a){var b=this;if(arguments.length===1)f.each(a,
+function(c,d){var g=b.all("key")?b.all(d).keys():[],h=b.properties().get(d);if(h){h.isObjectType()?b.setObjectProperty(h,f.isArray(c)?c:[c]):b.setValueProperty(h,f.isArray(c)?c:[c]);b.trigger("set",d,b.all(d).keys(),g);b.dirty=true;b.g.trigger("dirty")}});else return e.Node.prototype.set.call(this,arguments[0],arguments[1],arguments[2])},toJSON:function(){var a=this;result={};f.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}});f.extend(e.Object.prototype,f.Events);e.Graph=f.inherits(e.Node,{constructor:function(a,b){e.Node.call(this);this.replace("objects",new e.Hash);a&&this.merge(a,b)},merge:function(a,b){var c=this;f.select(a,function(d,g){if(d.type==="/type/type"||d.type==="type"){if(!c.get("objects",g)){c.set("objects",
+g,new e.Type(c,g,d));c.get(g).dirty=b}return true}return false});f.select(a,function(d,g){if(d.type!=="/type/type"&&d.type!=="type"){var h=c.get("objects",g),i=f.isArray(d.type)?d.type:[d.type];if(h)h.data=d;else{h=new e.Object(c,g,d);c.set("objects",g,h)}f.each(i,function(k){if(!c.get("objects",k)){console.log("Type '"+k+"' not found for "+g+"...");throw"Type '"+k+"' not found for "+g+"...";}c.get("objects",k).set("objects",g,h)});c.get(g).dirty=b;return true}return false});this.objects().each(function(d){d.data&&
+d.build()})},set:function(a,b){var c=f.isArray(b.type)?b.type:[b.type];if(arguments.length===2){a=a?a:e.uuid("/"+f.last(f.last(c).split("/"))+"/");c=new e.Object(this,a,b,true);c.dirty=true;c.build();this.set("objects",a,c);return this.get("objects",a)}else return e.Node.prototype.set.call(this,arguments[0],arguments[1],arguments[2])},get:function(a){return arguments.length===1?this.get("objects",a):e.Node.prototype.get.call(this,arguments[0],arguments[1])},getAsync:function(a,b){var c=this,d=this.get(a);
+!d||!d.data?c.fetch({_id:a},{},function(g){g?b(g):b(null,c.get(a))}):b(null,d)},del:function(a){if(a=this.get(a)){a._deleted=true;a.dirty=true;this.trigger("dirty")}},find:function(a){return this.objects().select(function(b){var c=b.toJSON(),d=false;f.each(a,function(g,h){var i,k=h.match(/^([a-z_]{1,30})(!=|>|>=|<|<=|\|=)?$/),j=k[1];if((k[2]||"==")==="|="){k=f.isArray(g)?g:[g];i=false;f.each(k,function(l){if(f.include(c[j],l))i=true})}else i=c[j]===g;i||(d=true)});return!d})},fetch:function(a,b,c){var d=
+this;e.adapter.readGraph(a,this,b,function(g,h){h&&d.merge(h,false);g?c(g):c(null,h)})},sync:function(a){a=a||function(){};var b=this,c=b.dirtyNodes(),d=new e.Hash,g=c.select(function(h,i){if(!h.validate||h.validate&&h.validate()){d.set(i,h);return false}else return true});e.adapter.writeGraph(d.toJSON(),function(h,i){if(h)a(h);else{b.merge(i);b.dirtyNodes().each(function(k){k.dirty=false});f.select(i,function(k){return k._conflicted}).length>0&&b.trigger("conflicted");a(g.length>0?"Some invalid nodes":
+null,g)}})},filter:function(a){var b={};this.types().each(function(c,d){b[d]=c.toJSON()});this.objects().each(function(c,d){f.include(c.types().keys(),a.type)||(b[d]=c.toJSON())});a.run(this).each(function(c,d){b[d]=c.toJSON()});return new e.Graph(b)},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("objects").select(function(a){return a.type==="/type/type"||a.type==="type"})},objects:function(){return this.all("objects").select(function(a){return a.type!==
+"/type/type"&&a.type!=="type"&&a.data&&!a._deleted})},dirtyNodes:function(){return this.all("objects").select(function(a){return a.dirty&&(a.data||a instanceof e.Type)})},invalidNodes:function(){return this.all("objects").select(function(a){return a.errors&&a.errors.length>0})},conflictedNodes:function(){return this.all("objects").select(function(a){return a._conflicted})},toJSON:function(){var a={};this.all("objects").each(function(b,c){if(b.data||b instanceof e.Type)a[c]=b.toJSON()});return a}});
+f.extend(e.Graph.prototype,f.Events);e.Collection=function(a){var b={"/type/item":{type:"/type/type",properties:{}}};if(a){f.each(a.properties,function(c,d){b["/type/item"].properties[d]=c});f.each(a.items,function(c,d){b[d]=c;b[d].type="/type/item"});this.g=new e.Graph(b)}else this.g=new e.Graph};f.extend(e.Collection.prototype,{get:function(){return this.g.get.apply(this.g,arguments)},set:function(a,b){this.g.set(a,f.extend(b,{type:"/type/item"}))},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("objects","/type/item").all("properties")},items:function(){return this.g.objects()},toJSON:function(){return{properties:this.g.toJSON()["/type/item"].properties,items:this.g.objects().toJSON()}}});e.Criterion=function(a,b,c,d){this.operator=a;this.type=b;this.property=c;this.value=d;this.children=[]};e.Criterion.operators={};f.extend(e.Criterion.operators,{AND:function(a,b){if(b.length===0)return new e.Hash;
+for(var c=b[0].run(a),d=1;d<b.length;d++)c=c.intersect(b[d].run(a));return c},OR:function(a,b){for(var c=new e.Hash,d=0;d<b.length;d++)c=c.union(b[d].run(a));return c},CONTAINS:function(a,b,c,d){return a.get("objects",b).get("properties",c).get("values",d).referencedObjects.select(function(g){return f.include(g.types().keys(),b)})},GT:function(a,b,c,d){a=a.get("objects",b).get("properties",c).all("values");var g=new e.Hash;a=a.select(function(h){return h.val>=d});a.each(function(h){g=g.union(h.referencedObjects)});
+return g}});f.extend(e.Criterion.prototype,{add:function(a){this.children.push(a);return this},run:function(a){return this.operator==="AND"?e.Criterion.operators.AND(a,this.children):this.operator==="OR"?e.Criterion.operators.OR(a,this.children):e.Criterion.operators[this.operator](a,this.type,this.property,this.value)}})})();
Please sign in to comment.
Something went wrong with that request. Please try again.