empresa-libre/source/static/js/lokijs.min.js

5 lines
94 KiB
JavaScript
Raw Normal View History

2018-08-22 00:01:07 -05:00
(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.loki=factory()}})(this,function(){return function(){"use strict";var hasOwnProperty=Object.prototype.hasOwnProperty;var Utils={copyProperties:function(src,dest){var prop;for(prop in src){dest[prop]=src[prop]}},resolveTransformObject:function(subObj,params,depth){var prop,pname;if(typeof depth!=="number"){depth=0}if(++depth>=10)return subObj;for(prop in subObj){if(typeof subObj[prop]==="string"&&subObj[prop].indexOf("[%lktxp]")===0){pname=subObj[prop].substring(8);if(params.hasOwnProperty(pname)){subObj[prop]=params[pname]}}else if(typeof subObj[prop]==="object"){subObj[prop]=Utils.resolveTransformObject(subObj[prop],params,depth)}}return subObj},resolveTransformParams:function(transform,params){var idx,clonedStep,resolvedTransform=[];if(typeof params==="undefined")return transform;for(idx=0;idx<transform.length;idx++){clonedStep=clone(transform[idx],"shallow-recurse-objects");resolvedTransform.push(Utils.resolveTransformObject(clonedStep,params))}return resolvedTransform}};var Comparators={aeq:aeqHelper,lt:ltHelper,gt:gtHelper};function aeqHelper(prop1,prop2){var cv1,cv2,t1,t2;if(prop1===prop2)return true;if(!prop1||!prop2||prop1===true||prop2===true||prop1!==prop1||prop2!==prop2){switch(prop1){case undefined:t1=1;break;case null:t1=1;break;case false:t1=3;break;case true:t1=4;break;case"":t1=5;break;default:t1=prop1===prop1?9:0;break}switch(prop2){case undefined:t2=1;break;case null:t2=1;break;case false:t2=3;break;case true:t2=4;break;case"":t2=5;break;default:t2=prop2===prop2?9:0;break}if(t1!==9||t2!==9){return t1===t2}}cv1=Number(prop1);cv2=Number(prop2);if(cv1===cv1||cv2===cv2){return cv1===cv2}cv1=prop1.toString();cv2=prop2.toString();return cv1==cv2}function ltHelper(prop1,prop2,equal){var cv1,cv2,t1,t2;if(!prop1||!prop2||prop1===true||prop2===true||prop1!==prop1||prop2!==prop2){switch(prop1){case undefined:t1=1;break;case null:t1=1;break;case false:t1=3;break;case true:t1=4;break;case"":t1=5;break;default:t1=prop1===prop1?9:0;break}switch(prop2){case undefined:t2=1;break;case null:t2=1;break;case false:t2=3;break;case true:t2=4;break;case"":t2=5;break;default:t2=prop2===prop2?9:0;break}if(t1!==9||t2!==9){return t1===t2?equal:t1<t2}}cv1=Number(prop1);cv2=Number(prop2);if(cv1===cv1&&cv2===cv2){if(cv1<cv2)return true;if(cv1>cv2)return false;return equal}if(cv1===cv1&&cv2!==cv2){return true}if(cv2===cv2&&cv1!==cv1){return false}if(prop1<prop2)return true;if(prop1>prop2)return false;if(prop1==prop2)return equal;cv1=prop1.toString();cv2=prop2.toString();if(cv1<cv2){return true}if(cv1==cv2){return equal}return false}function gtHelper(prop1,prop2,equal){var cv1,cv2,t1,t2;if(!prop1||!prop2||prop1===true||prop2===true||prop1!==prop1||prop2!==prop2){switch(prop1){case undefined:t1=1;break;case null:t1=1;break;case false:t1=3;break;case true:t1=4;break;case"":t1=5;break;default:t1=prop1===prop1?9:0;break}switch(prop2){case undefined:t2=1;break;case null:t2=1;break;case false:t2=3;break;case true:t2=4;break;case"":t2=5;break;default:t2=prop2===prop2?9:0;break}if(t1!==9||t2!==9){return t1===t2?equal:t1>t2}}cv1=Number(prop1);cv2=Number(prop2);if(cv1===cv1&&cv2===cv2){if(cv1>cv2)return true;if(cv1<cv2)return false;return equal}if(cv1===cv1&&cv2!==cv2){return false}if(cv2===cv2&&cv1!==cv1){return true}if(prop1>prop2)return true;if(prop1<prop2)return false;if(prop1==prop2)return equal;cv1=prop1.toString();cv2=prop2.toString();if(cv1>cv2){return true}if(cv1==cv2){return equal}return false}function sortHelper(prop1,prop2,desc){if(Comparators.aeq(prop1,prop2))return 0;if(Comparators.lt(prop1,prop2,false)){return desc?1:-1}if(Comparators.gt(prop1,prop2,false)){return desc?-1:1}return 0}function compoundeval(properties,obj1,obj2){var res=0;var prop,field,val1,val2,arr;for(var i=0,len=properties.length;i<len;i++){prop=properties[i];field=prop[0];if(~field.indexOf(".")){arr=field.split(".");val1=arr.reduce(function(obj,i){return obj&&obj[i]||undefined},obj1);val2=arr.reduce(function(
;LokiLocalStorageAdapter.prototype.deleteDatabase=function deleteDatabase(dbname,callback){if(localStorageAvailable()){localStorage.removeItem(dbname);callback(null)}else{callback(new Error("localStorage is not available"))}};Loki.prototype.throttledSaveDrain=function(callback,options){var self=this;var now=(new Date).getTime();if(!this.throttledSaves){callback(true)}options=options||{};if(!options.hasOwnProperty("recursiveWait")){options.recursiveWait=true}if(!options.hasOwnProperty("recursiveWaitLimit")){options.recursiveWaitLimit=false}if(!options.hasOwnProperty("recursiveWaitLimitDuration")){options.recursiveWaitLimitDuration=2e3}if(!options.hasOwnProperty("started")){options.started=(new Date).getTime()}if(this.throttledSaves&&this.throttledSavePending){if(options.recursiveWait){this.throttledCallbacks.push(function(){if(self.throttledSavePending){if(options.recursiveWaitLimit&&now-options.started>options.recursiveWaitLimitDuration){callback(false);return}self.throttledSaveDrain(callback,options);return}else{callback(true);return}})}else{this.throttledCallbacks.push(callback);return}}else{callback(true)}};Loki.prototype.loadDatabaseInternal=function(options,callback){var cFun=callback||function(err,data){if(err){throw err}},self=this;if(this.persistenceAdapter!==null){this.persistenceAdapter.loadDatabase(this.filename,function loadDatabaseCallback(dbString){if(typeof dbString==="string"){var parseSuccess=false;try{self.loadJSON(dbString,options||{});parseSuccess=true}catch(err){cFun(err)}if(parseSuccess){cFun(null);self.emit("loaded","database "+self.filename+" loaded")}}else{if(!dbString){cFun(null);self.emit("loaded","empty database "+self.filename+" loaded");return}if(dbString instanceof Error){cFun(dbString);return}if(typeof dbString==="object"){self.loadJSONObject(dbString,options||{});cFun(null);self.emit("loaded","database "+self.filename+" loaded");return}cFun("unexpected adapter response : "+dbString)}})}else{cFun(new Error("persistenceAdapter not configured"))}};Loki.prototype.loadDatabase=function(options,callback){var self=this;if(!this.throttledSaves){this.loadDatabaseInternal(options,callback);return}this.throttledSaveDrain(function(success){if(success){self.throttledSavePending=true;self.loadDatabaseInternal(options,function(err){if(self.throttledCallbacks.length===0){self.throttledSavePending=false}else{self.saveDatabase()}if(typeof callback==="function"){callback(err)}});return}else{if(typeof callback==="function"){callback(new Error("Unable to pause save throttling long enough to read database"))}}},options)};Loki.prototype.saveDatabaseInternal=function(callback){var cFun=callback||function(err){if(err){throw err}return},self=this;if(this.persistenceAdapter!==null){if(this.persistenceAdapter.mode==="reference"&&typeof this.persistenceAdapter.exportDatabase==="function"){this.persistenceAdapter.exportDatabase(this.filename,this.copy({removeNonSerializable:true}),function exportDatabaseCallback(err){self.autosaveClearFlags();cFun(err)})}else{self.autosaveClearFlags();this.persistenceAdapter.saveDatabase(this.filename,self.serialize(),function saveDatabasecallback(err){cFun(err)})}}else{cFun(new Error("persistenceAdapter not configured"))}};Loki.prototype.saveDatabase=function(callback){if(!this.throttledSaves){this.saveDatabaseInternal(callback);return}if(this.throttledSavePending){this.throttledCallbacks.push(callback);return}var localCallbacks=this.throttledCallbacks;this.throttledCallbacks=[];localCallbacks.unshift(callback);this.throttledSavePending=true;var self=this;this.saveDatabaseInternal(function(err){self.throttledSavePending=false;localCallbacks.forEach(function(pcb){if(typeof pcb==="function"){setTimeout(function(){pcb(err)},1)}});if(self.throttledCallbacks.length>0){self.saveDatabase()}})};Loki.prototype.save=Loki.prototype.saveDatabase;Loki.prototype.deleteDatabase=function(options,callback){var cFun=callback||function(err,data){if(err){throw err}};if(typeof options==="function"&&!callback){cFun=options}if(this.persistenceAdapter!==null){this.persistenceAdapter.deleteDatabase(this.fil
obj:op=="U"&&!this.disableDeltaChangesApi?this.getChangeDelta(obj,old):JSON.parse(JSON.stringify(obj))})};Collection.prototype.insertMeta=function(obj){var len,idx;if(this.disableMeta||!obj){return}if(Array.isArray(obj)){len=obj.length;for(idx=0;idx<len;idx++){if(!obj[idx].hasOwnProperty("meta")){obj[idx].meta={}}obj[idx].meta.created=(new Date).getTime();obj[idx].meta.revision=0}return}if(!obj.meta){obj.meta={}}obj.meta.created=(new Date).getTime();obj.meta.revision=0};Collection.prototype.updateMeta=function(obj){if(this.disableMeta||!obj){return}obj.meta.updated=(new Date).getTime();obj.meta.revision+=1};Collection.prototype.createInsertChange=function(obj){this.createChange(this.name,"I",obj)};Collection.prototype.createUpdateChange=function(obj,old){this.createChange(this.name,"U",obj,old)};Collection.prototype.insertMetaWithChange=function(obj){this.insertMeta(obj);this.createInsertChange(obj)};Collection.prototype.updateMetaWithChange=function(obj,old){this.updateMeta(obj);this.createUpdateChange(obj,old)};Collection.prototype.console={log:function(){},warn:function(){},error:function(){}};Collection.prototype.addAutoUpdateObserver=function(object){if(!this.autoupdate||typeof Object.observe!=="function")return;Object.observe(object,this.observerCallback,["add","update","delete","reconfigure","setPrototype"])};Collection.prototype.removeAutoUpdateObserver=function(object){if(!this.autoupdate||typeof Object.observe!=="function")return;Object.unobserve(object,this.observerCallback)};Collection.prototype.addTransform=function(name,transform){if(this.transforms.hasOwnProperty(name)){throw new Error("a transform by that name already exists")}this.transforms[name]=transform};Collection.prototype.getTransform=function(name){return this.transforms[name]};Collection.prototype.setTransform=function(name,transform){this.transforms[name]=transform};Collection.prototype.removeTransform=function(name){delete this.transforms[name]};Collection.prototype.byExample=function(template){var k,obj,query;query=[];for(k in template){if(!template.hasOwnProperty(k))continue;query.push((obj={},obj[k]=template[k],obj))}return{$and:query}};Collection.prototype.findObject=function(template){return this.findOne(this.byExample(template))};Collection.prototype.findObjects=function(template){return this.find(this.byExample(template))};Collection.prototype.ttlDaemonFuncGen=function(){var collection=this;var age=this.ttl.age;return function ttlDaemon(){var now=Date.now();var toRemove=collection.chain().where(function daemonFilter(member){var timestamp=member.meta.updated||member.meta.created;var diff=now-timestamp;return age<diff});toRemove.remove()}};Collection.prototype.setTTL=function(age,interval){if(age<0){clearInterval(this.ttl.daemon)}else{this.ttl.age=age;this.ttl.ttlInterval=interval;this.ttl.daemon=setInterval(this.ttlDaemonFuncGen(),interval)}};Collection.prototype.prepareFullDocIndex=function(){var len=this.data.length;var indexes=new Array(len);for(var i=0;i<len;i+=1){indexes[i]=i}return indexes};Collection.prototype.configureOptions=function(options){options=options||{};if(options.hasOwnProperty("adaptiveBinaryIndices")){this.adaptiveBinaryIndices=options.adaptiveBinaryIndices;if(this.adaptiveBinaryIndices){this.ensureAllIndexes()}}};Collection.prototype.ensureIndex=function(property,force){if(typeof force==="undefined"){force=false}if(property===null||property===undefined){throw new Error("Attempting to set index without an associated property")}if(this.binaryIndices[property]&&!force){if(!this.binaryIndices[property].dirty)return}if(this.adaptiveBinaryIndices===true&&this.binaryIndices.hasOwnProperty(property)&&!force){return}var index={name:property,dirty:true,values:this.prepareFullDocIndex()};this.binaryIndices[property]=index;var wrappedComparer=function(prop,data){var val1,val2,arr;return function(a,b){if(~prop.indexOf(".")){arr=prop.split(".");val1=arr.reduce(function(obj,i){return obj&&obj[i]||undefined},data[a]);val2=arr.reduce(function(obj,i){return obj&&obj[i]||undefined},data[b])}else{val1=data[a][prop];val2=data[b][prop]}
results=results.concat(this.values[i])}return results},getPos:function(key){return binarySearch(this.keys,key,this.sort)},remove:function(key,value){var pos=binarySearch(this.keys,key,this.sort).index;var idxSet=this.values[pos];for(var i in idxSet){if(idxSet[i]==value)idxSet.splice(i,1)}if(idxSet.length<1){this.keys.splice(pos,1);this.values.splice(pos,1)}},clear:function(){this.keys=[];this.values=[]}};Loki.LokiOps=LokiOps;Loki.Collection=Collection;Loki.KeyValueStore=KeyValueStore;Loki.LokiMemoryAdapter=LokiMemoryAdapter;Loki.LokiPartitioningAdapter=LokiPartitioningAdapter;Loki.LokiLocalStorageAdapter=LokiLocalStorageAdapter;Loki.LokiFsAdapter=LokiFsAdapter;Loki.persistenceAdapters={fs:LokiFsAdapter,localStorage:LokiLocalStorageAdapter};Loki.aeq=aeqHelper;Loki.lt=ltHelper;Loki.gt=gtHelper;Loki.Comparators=Comparators;return Loki}()});