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

4 lines
87 KiB
JavaScript
Raw Normal View History

2017-10-04 00:11:49 -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=JSON.parse(JSON.stringify(transform[idx]));resolvedTransform.push(Utils.resolveTransformObject(clonedStep,params))}return resolvedTransform}};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(aeqHelper(prop1,prop2))return 0;if(ltHelper(prop1,prop2,false)){return desc?1:-1}if(gtHelper(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(obj,i){return obj&&obj[i]||undefined},obj2)}else{val1=obj1[field];val2=obj2[fie
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{this.persistenceAdapter.saveDatabase(this.filename,self.serialize(),function saveDatabasecallback(err){self.autosaveClearFlags();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.filename,function deleteDatabaseCallback(err){cFun(err)})}else{cFun(new Error("persistenceAdapter not configured"))}};Loki.prototype.autosaveDirty=function(){for(var idx=0;idx<this.collections.length;idx++){if(this.collections[idx].dirty){return true}}return false};Loki.prototype.autosaveClearFlags=function(){for(var idx=0;idx<this.collections.length;idx++){this.collections[idx].dirty=false}};Loki.prototype.autosaveEnable=function(options,callback){this.autosave=true;var delay=5e3,self=this;if(typeof this.autosaveInterval!=="undefined"&&this.autosaveInterval!==null){delay=this.autosaveInterval}this.autosaveHandle=setInterval(function autosaveHandleInterval(){if(self.autosaveDirty()){self.saveDatabase(callback)}},delay)};Loki.prototype.autosaveDisable=function(){if(typeof this.autosaveHandle!=="undefined"&&this.autosaveHandle!==null){clearInterval(this.autosaveHandle);this.autosaveHandle=null}};function Resultset(collection,options){options=options||{};this.collection=collection;this.filteredrows=[];this.filterInitialized=false;return this}Resultset.prototype.reset=function(){if(this.filteredrows.length>0){this.filteredrows=[]}this.filterInitialized=false;return this};Resultset.prototype.toJSON=function(){var copy=this.copy();copy.collection=null;return copy};Resultset.prototyp
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]}if(val1!==val2){if(ltHelper(val1,val2,false))return-1;if(gtHelper(val1,val2,false))return 1}return 0}}(property,this.data);index.values.sort(wrappedComparer);index.dirty=false;this.dirty=true};Collection.prototype.getSequencedIndexValues=function(property){var idx,idxvals=this.binaryIndices[property].values;var result="";for(idx=0;idx<idxvals.length;idx++){result+=" ["+idx+"] "+this.data[idxvals[idx]][property]}return result};Collection.prototype.ensureUniqueIndex=function(field){var index=this.constraints.unique[field];if(!index){if(this.uniqueNames.indexOf(field)==-1){this.uniqueNames.push(field)}}this.constraints.unique[field]=index=new UniqueIndex(field);this.data.forEach(function(obj){index.set(obj)});return index};Collection.prototype.ensureAllIndexes=function(force){var key,bIndices=this.binaryIndices;for(key in bIndices){if(hasOwnProperty.call(bIndices,key)){this.ensureIndex(key,force)}}};Collection.prototype.flagBinaryIndexesDirty=function(){var key,bIndices=this.binaryIndices;for(key in bIndices){if(hasOwnProperty.call(bIndices,key)){bIndices[key].dirty=true}}};Collection.prototype.flagBinaryIndexDirty=function(index){if(this.binaryIndices[index])this.binaryIndices[index].dirty=true};Collection.prototype.count=function(query){if(!query){return this.data.length}return this.chain().find(query).filteredrows.length};Collection.prototype.ensureId=function(){var len=this.data.length,i=0;this.idIndex=[];for(i;i<len;i+=1){this.idIndex.push(this.data[i].$loki)}};Collection.prototype.ensureIdAsync=function(callback){this.async(function(){this.ensureId()},callback)};Collection.prototype.addDynamicView=function(name,options){var dv=new DynamicView(this,name,options);this.DynamicViews.push(dv);return dv};Collection.prototype.removeDynamicView=function(name){for(var idx=0;idx<this.DynamicViews.length;idx++){if(this.DynamicViews[idx].name===name){this.DynamicViews.splice(idx,1)}}};Collection.prototype.getDynamicView=function(name){for(var idx=0;idx<this.DynamicViews.length;idx++){if(this.DynamicViews[idx].name===name){return this.DynamicViews[idx]}}return null};Collection.prototype.findAndUpdate=function(filterObject,updateFunction){if(typeof filterObject==="function"){this.updateWhere(filterObject,updateFunction)}else{this.chain().find(filterObject).update(updateFunction)}};Collection.prototype.findAndRemove=function(filterObject){this.chain().find(filterObject).remove()};Collection.prototype.insert=function(doc){if(!Array.isArray(doc)){return this.insertOne(doc)}var obj;var results=[];this.emit("pre-insert",doc);for(var i=0,len=doc.length;i<len;i++){obj=this.insertOne(doc[i],true);if(!obj){return undefined}results.push(obj)}this.emit("insert",results);results=this.cloneObjects?clone(results,this.cloneMethod):results;return results.length===1?results[0]:results};Collection.prototype.insertOne=function(doc,bulkInsert){var err=null;var returnObj;if(typeof doc!=="object"){err=new TypeError("Document needs to be an object")}else if(doc===null){err=new TypeError("Object cannot be null")}if(err!==null){this.emit("error",err);throw err}var obj=this.cloneObjects?clone(doc,this.cloneMethod):doc;if(typeof obj.meta==="un