Files
ShoppingList.dev/IndexedDBShim.min.js
2017-02-07 20:09:53 +00:00

4 lines
28 KiB
JavaScript

/*! IndexedDBShim - v0.1.2 - 2014-09-08 */
"use strict";var idbModules={},cleanInterface=!1;!function(){var a={test:!0};if(Object.defineProperty)try{Object.defineProperty(a,"test",{enumerable:!1}),a.test&&(cleanInterface=!0)}catch(b){}}(),function(a){function b(a,b,c,d){c.target=b,"function"==typeof b[a]&&b[a].apply(b,[c]),"function"==typeof d&&d()}function c(b,c,d){var e=new DOMException.prototype.constructor(0,c);throw e.name=b,e.message=c,a.DEBUG&&(console.log(b,c,d,e),console.trace&&console.trace()),e}var d=function(){this.length=0,this._items=[],cleanInterface&&Object.defineProperty(this,"_items",{enumerable:!1})};if(d.prototype={contains:function(a){return-1!==this._items.indexOf(a)},item:function(a){return this._items[a]},indexOf:function(a){return this._items.indexOf(a)},push:function(a){this._items.push(a),this.length+=1;for(var b=0;b<this._items.length;b++)this[b]=this._items[b]},splice:function(){this._items.splice.apply(this._items,arguments),this.length=this._items.length;for(var a in this)a===String(parseInt(a,10))&&delete this[a];for(a=0;a<this._items.length;a++)this[a]=this._items[a]}},cleanInterface)for(var e in{indexOf:!1,push:!1,splice:!1})Object.defineProperty(d.prototype,e,{enumerable:!1});a.util={throwDOMException:c,callback:b,quote:function(a){return"'"+a+"'"},StringList:d}}(idbModules),function(idbModules){var Sca=function(){return{decycle:function(object,callback){function checkForCompletion(){0===queuedObjects.length&&returnCallback(derezObj)}function readBlobAsDataURL(a,b){var c=new FileReader;c.onloadend=function(a){var c=a.target.result,d="blob";updateEncodedBlob(c,b,d)},c.readAsDataURL(a)}function updateEncodedBlob(dataURL,path,blobtype){var encoded=queuedObjects.indexOf(path);path=path.replace("$","derezObj"),eval(path+'.$enc="'+dataURL+'"'),eval(path+'.$type="'+blobtype+'"'),queuedObjects.splice(encoded,1),checkForCompletion()}function derez(a,b){var c,d,e;if(!("object"!=typeof a||null===a||a instanceof Boolean||a instanceof Date||a instanceof Number||a instanceof RegExp||a instanceof Blob||a instanceof String)){for(c=0;c<objects.length;c+=1)if(objects[c]===a)return{$ref:paths[c]};if(objects.push(a),paths.push(b),"[object Array]"===Object.prototype.toString.apply(a))for(e=[],c=0;c<a.length;c+=1)e[c]=derez(a[c],b+"["+c+"]");else{e={};for(d in a)Object.prototype.hasOwnProperty.call(a,d)&&(e[d]=derez(a[d],b+"["+JSON.stringify(d)+"]"))}return e}return a instanceof Blob?(queuedObjects.push(b),readBlobAsDataURL(a,b)):a instanceof Boolean?a={$type:"bool",$enc:a.toString()}:a instanceof Date?a={$type:"date",$enc:a.getTime()}:a instanceof Number?a={$type:"num",$enc:a.toString()}:a instanceof RegExp&&(a={$type:"regex",$enc:a.toString()}),a}var objects=[],paths=[],queuedObjects=[],returnCallback=callback,derezObj=derez(object,"$");checkForCompletion()},retrocycle:function retrocycle($){function dataURLToBlob(a){var b,c,d,e=";base64,";if(-1===a.indexOf(e))return c=a.split(","),b=c[0].split(":")[1],d=c[1],new Blob([d],{type:b});c=a.split(e),b=c[0].split(":")[1],d=window.atob(c[1]);for(var f=d.length,g=new Uint8Array(f),h=0;f>h;++h)g[h]=d.charCodeAt(h);return new Blob([g.buffer],{type:b})}function rez(value){var i,item,name,path;if(value&&"object"==typeof value)if("[object Array]"===Object.prototype.toString.apply(value))for(i=0;i<value.length;i+=1)item=value[i],item&&"object"==typeof item&&(path=item.$ref,value[i]="string"==typeof path&&px.test(path)?eval(path):rez(item));else if(void 0!==value.$type)switch(value.$type){case"blob":case"file":value=dataURLToBlob(value.$enc);break;case"bool":value=Boolean("true"===value.$enc);break;case"date":value=new Date(value.$enc);break;case"num":value=Number(value.$enc);break;case"regex":value=eval(value.$enc)}else for(name in value)"object"==typeof value[name]&&(item=value[name],item&&(path=item.$ref,value[name]="string"==typeof path&&px.test(path)?eval(path):rez(item)));return value}var px=/^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;return rez($),$},encode:function(a,b){function c(a){b(JSON.stringify(a))}this.decycle(a,c)},decode:function(a){return this.retrocycle(JSON.parse(a))}}}();idbModules.Sca=Sca}(idbModules),function(a){var b=["","number","string","boolean","object","undefined"],c=function(){return{encode:function(a){return b.indexOf(typeof a)+"-"+JSON.stringify(a)},decode:function(a){return"undefined"==typeof a?void 0:JSON.parse(a.substring(2))}}},d={number:c("number"),"boolean":c(),object:c(),string:{encode:function(a){return b.indexOf("string")+"-"+a},decode:function(a){return""+a.substring(2)}},undefined:{encode:function(){return b.indexOf("undefined")+"-undefined"},decode:function(){return void 0}}},e=function(){return{encode:function(a){return d[typeof a].encode(a)},decode:function(a){return d[b[a.substring(0,1)]].decode(a)}}}();a.Key=e}(idbModules),function(a){var b=function(a,b){return{type:a,debug:b,bubbles:!1,cancelable:!1,eventPhase:0,timeStamp:new Date}};a.Event=b}(idbModules),function(a){var b=function(){this.onsuccess=this.onerror=this.result=this.error=this.source=this.transaction=null,this.readyState="pending"},c=function(){this.onblocked=this.onupgradeneeded=null};c.prototype=b,a.IDBRequest=b,a.IDBOpenRequest=c}(idbModules),function(a,b){var c=function(a,b,c,d){this.lower=a,this.upper=b,this.lowerOpen=c,this.upperOpen=d};c.only=function(a){return new c(a,a,!1,!1)},c.lowerBound=function(a,d){return new c(a,b,d,b)},c.upperBound=function(a){return new c(b,a,b,open)},c.bound=function(a,b,d,e){return new c(a,b,d,e)},a.IDBKeyRange=c}(idbModules),function(a,b){function c(c,d,e,f,g,h){!c||c instanceof a.IDBKeyRange||(c=new a.IDBKeyRange(c,c,!1,!1)),this.__range=c,this.source=this.__idbObjectStore=e,this.__req=f,this.key=b,this.direction=d,this.__keyColumnName=g,this.__valueColumnName=h,this.__valueDecoder="value"===h?a.Sca:a.Key,this.source.transaction.__active||a.util.throwDOMException("TransactionInactiveError - The transaction this IDBObjectStore belongs to is not active."),this.__offset=-1,this.__lastKeyContinued=b,this["continue"]()}c.prototype.__find=function(c,d,e,f,g){g=g||1;var h=this,i=["SELECT * FROM ",a.util.quote(h.__idbObjectStore.name)],j=[];i.push("WHERE ",h.__keyColumnName," NOT NULL"),!h.__range||h.__range.lower===b&&h.__range.upper===b||(i.push("AND"),h.__range.lower!==b&&(i.push(h.__keyColumnName+(h.__range.lowerOpen?" >":" >= ")+" ?"),j.push(a.Key.encode(h.__range.lower))),h.__range.lower!==b&&h.__range.upper!==b&&i.push("AND"),h.__range.upper!==b&&(i.push(h.__keyColumnName+(h.__range.upperOpen?" < ":" <= ")+" ?"),j.push(a.Key.encode(h.__range.upper)))),"undefined"!=typeof c&&(h.__lastKeyContinued=c,h.__offset=0),h.__lastKeyContinued!==b&&(i.push("AND "+h.__keyColumnName+" >= ?"),j.push(a.Key.encode(h.__lastKeyContinued))),i.push("ORDER BY ",h.__keyColumnName),i.push("LIMIT "+g+" OFFSET "+h.__offset),a.DEBUG&&console.log(i.join(" "),j),h.__prefetchedData=null,d.executeSql(i.join(" "),j,function(c,d){d.rows.length>1?(h.__prefetchedData=d.rows,h.__prefetchedIndex=0,a.DEBUG&&console.log("Preloaded "+h.__prefetchedData.length+" records for cursor"),h.__decode(d.rows.item(0),e)):1===d.rows.length?h.__decode(d.rows.item(0),e):(a.DEBUG&&console.log("Reached end of cursors"),e(b,b))},function(b,c){a.DEBUG&&console.log("Could not execute Cursor.continue"),f(c)})},c.prototype.__decode=function(b,c){var d=a.Key.decode(b[this.__keyColumnName]),e=this.__valueDecoder.decode(b[this.__valueColumnName]),f=a.Key.decode(b.key);c(d,e,f)},c.prototype["continue"]=function(c){var d=a.cursorPreloadPackSize||100,e=this;this.__idbObjectStore.transaction.__addToTransactionQueue(function(a,f,g,h){e.__offset++;var i=function(a,c,d){e.key=a,e.value=c,e.primaryKey=d,g("undefined"!=typeof e.key?e:b,e.__req)};return e.__prefetchedData&&(e.__prefetchedIndex++,e.__prefetchedIndex<e.__prefetchedData.length)?void e.__decode(e.__prefetchedData.item(e.__prefetchedIndex),i):void e.__find(c,a,i,h,d)})},c.prototype.advance=function(c){0>=c&&a.util.throwDOMException("Type Error - Count is invalid - 0 or negative",c);var d=this;this.__idbObjectStore.transaction.__addToTransactionQueue(function(a,e,f,g){d.__offset+=c,d.__find(b,a,function(a,c){d.key=a,d.value=c,f("undefined"!=typeof d.key?d:b,d.__req)},g)})},c.prototype.update=function(c){var d=this,e=this.__idbObjectStore.transaction.__createRequest(function(){});return a.Sca.encode(c,function(f){d.__idbObjectStore.transaction.__pushToQueue(e,function(e,g,h,i){d.__find(b,e,function(b,g,j){var k=d.__idbObjectStore,l=d.__idbObjectStore.transaction.db.__storeProperties,m=[f],n="UPDATE "+a.util.quote(k.name)+" SET value = ?",o=l[k.name]&&l[k.name].indexList;if(o)for(var p in o){var q=o[p];n+=", "+p+" = ?",m.push(a.Key.encode(c[q.keyPath]))}n+=" WHERE key = ?",m.push(a.Key.encode(j)),a.DEBUG&&console.log(n,f,b,j),e.executeSql(n,m,function(a,c){d.__prefetchedData=null,1===c.rowsAffected?h(b):i("No rows with key found"+b)},function(a,b){i(b)})},i)})}),e},c.prototype["delete"]=function(){var c=this;return this.__idbObjectStore.transaction.__addToTransactionQueue(function(d,e,f,g){c.__find(b,d,function(e,h,i){var j="DELETE FROM "+a.util.quote(c.__idbObjectStore.name)+" WHERE key = ?";a.DEBUG&&console.log(j,e,i),d.executeSql(j,[a.Key.encode(i)],function(a,d){c.__prefetchedData=null,1===d.rowsAffected?(c.__offset--,f(b)):g("No rows with key found"+e)},function(a,b){g(b)})},g)})},a.IDBCursor=c}(idbModules),function(idbModules,undefined){function IDBIndex(a,b){this.indexName=this.name=a,this.__idbObjectStore=this.objectStore=this.source=b;var c=b.transaction.db.__storeProperties[b.name],d=c&&c.indexList;this.keyPath=d&&d[a]&&d[a].keyPath||a,["multiEntry","unique"].forEach(function(b){this[b]=!!(d&&d[a]&&d[a].optionalParams&&d[a].optionalParams[b])},this)}IDBIndex.prototype.__createIndex=function(indexName,keyPath,optionalParameters){var me=this,transaction=me.__idbObjectStore.transaction;transaction.__addToTransactionQueue(function(tx,args,success,failure){me.__idbObjectStore.__getStoreProps(tx,function(){function error(){idbModules.util.throwDOMException(0,"Could not create new index",arguments)}2!==transaction.mode&&idbModules.util.throwDOMException(0,"Invalid State error, not a version transaction",me.transaction);var idxList=JSON.parse(me.__idbObjectStore.__storeProps.indexList);"undefined"!=typeof idxList[indexName]&&idbModules.util.throwDOMException(0,"Index already exists on store",idxList);var columnName=indexName;idxList[indexName]={columnName:columnName,keyPath:keyPath,optionalParams:optionalParameters},me.__idbObjectStore.__storeProps.indexList=JSON.stringify(idxList);var sql=["ALTER TABLE",idbModules.util.quote(me.__idbObjectStore.name),"ADD",columnName,"BLOB"].join(" ");idbModules.DEBUG&&console.log(sql),tx.executeSql(sql,[],function(tx,data){tx.executeSql("SELECT * FROM "+idbModules.util.quote(me.__idbObjectStore.name),[],function(tx,data){!function initIndexForRow(i){if(i<data.rows.length)try{var value=idbModules.Sca.decode(data.rows.item(i).value),indexKey=eval("value['"+keyPath+"']");tx.executeSql("UPDATE "+idbModules.util.quote(me.__idbObjectStore.name)+" set "+columnName+" = ? where key = ?",[idbModules.Key.encode(indexKey),data.rows.item(i).key],function(){initIndexForRow(i+1)},error)}catch(e){initIndexForRow(i+1)}else idbModules.DEBUG&&console.log("Updating the indexes in table",me.__idbObjectStore.__storeProps),tx.executeSql("UPDATE __sys__ set indexList = ? where name = ?",[me.__idbObjectStore.__storeProps.indexList,me.__idbObjectStore.name],function(){me.__idbObjectStore.__setReadyState("createIndex",!0),success(me)},error)}(0)},error)},error)},"createObjectStore")})},IDBIndex.prototype.openCursor=function(a,b){{var c=new idbModules.IDBRequest;new idbModules.IDBCursor(a,b,this.source,c,this.indexName,"value")}return c},IDBIndex.prototype.openKeyCursor=function(a,b){{var c=new idbModules.IDBRequest;new idbModules.IDBCursor(a,b,this.source,c,this.indexName,"key")}return c},IDBIndex.prototype.__fetchIndexData=function(a,b){var c=this;return c.__idbObjectStore.transaction.__addToTransactionQueue(function(d,e,f,g){var h=["SELECT * FROM ",idbModules.util.quote(c.__idbObjectStore.name)," WHERE",c.indexName,"NOT NULL"],i=[];"undefined"!=typeof a&&(h.push("AND",c.indexName," = ?"),i.push(idbModules.Key.encode(a))),idbModules.DEBUG&&console.log("Trying to fetch data for Index",h.join(" "),i),d.executeSql(h.join(" "),i,function(a,c){var d;d="count"===b?c.rows.length:0===c.rows.length?undefined:"key"===b?idbModules.Key.decode(c.rows.item(0).key):idbModules.Sca.decode(c.rows.item(0).value),f(d)},g)})},IDBIndex.prototype.get=function(a){return this.__fetchIndexData(a,"value")},IDBIndex.prototype.getKey=function(a){return this.__fetchIndexData(a,"key")},IDBIndex.prototype.count=function(a){return this.__fetchIndexData(a,"count")},idbModules.IDBIndex=IDBIndex}(idbModules),function(idbModules){var IDBObjectStore=function(a,b,c){this.name=a,this.transaction=b,this.__ready={},this.__setReadyState("createObjectStore","undefined"==typeof c?!0:c),this.indexNames=new idbModules.util.StringList;var d=b.db.__storeProperties;if(d[a]&&d[a].indexList){var e=d[a].indexList;for(var f in e)e.hasOwnProperty(f)&&this.indexNames.push(f)}};IDBObjectStore.prototype.__setReadyState=function(a,b){this.__ready[a]=b},IDBObjectStore.prototype.__waitForReady=function(a,b){var c=!0;if("undefined"!=typeof b)c="undefined"==typeof this.__ready[b]?!0:this.__ready[b];else for(var d in this.__ready)this.__ready[d]||(c=!1);if(c)a();else{idbModules.DEBUG&&console.log("Waiting for to be ready",b);var e=this;window.setTimeout(function(){e.__waitForReady(a,b)},100)}},IDBObjectStore.prototype.__getStoreProps=function(a,b,c){var d=this;this.__waitForReady(function(){d.__storeProps?(idbModules.DEBUG&&console.log("Store properties - cached",d.__storeProps),b(d.__storeProps)):a.executeSql("SELECT * FROM __sys__ where name = ?",[d.name],function(a,c){1!==c.rows.length?b():(d.__storeProps={name:c.rows.item(0).name,indexList:c.rows.item(0).indexList,autoInc:c.rows.item(0).autoInc,keyPath:c.rows.item(0).keyPath},idbModules.DEBUG&&console.log("Store properties",d.__storeProps),b(d.__storeProps))},function(){b()})},c)},IDBObjectStore.prototype.__deriveKey=function(tx,value,key,callback){function getNextAutoIncKey(){tx.executeSql("SELECT * FROM sqlite_sequence where name like ?",[me.name],function(a,b){callback(1!==b.rows.length?0:b.rows.item(0).seq)},function(a,b){idbModules.util.throwDOMException(0,"Data Error - Could not get the auto increment value for key",b)})}var me=this;me.__getStoreProps(tx,function(props){if(props||idbModules.util.throwDOMException(0,"Data Error - Could not locate defination for this table",props),props.keyPath)if("undefined"!=typeof key&&idbModules.util.throwDOMException(0,"Data Error - The object store uses in-line keys and the key parameter was provided",props),value)try{var primaryKey=eval("value['"+props.keyPath+"']");void 0===primaryKey?"true"===props.autoInc?getNextAutoIncKey():idbModules.util.throwDOMException(0,"Data Error - Could not eval key from keyPath"):callback(primaryKey)}catch(e){idbModules.util.throwDOMException(0,"Data Error - Could not eval key from keyPath",e)}else idbModules.util.throwDOMException(0,"Data Error - KeyPath was specified, but value was not");else"undefined"!=typeof key?callback(key):"false"===props.autoInc?idbModules.util.throwDOMException(0,"Data Error - The object store uses out-of-line keys and has no key generator and the key parameter was not provided. ",props):getNextAutoIncKey()})},IDBObjectStore.prototype.__insertData=function(tx,encoded,value,primaryKey,success,error){var paramMap={};"undefined"!=typeof primaryKey&&(paramMap.key=idbModules.Key.encode(primaryKey));var indexes=JSON.parse(this.__storeProps.indexList);for(var key in indexes)try{paramMap[indexes[key].columnName]=idbModules.Key.encode(eval("value['"+indexes[key].keyPath+"']"))}catch(e){error(e)}var sqlStart=["INSERT INTO ",idbModules.util.quote(this.name),"("],sqlEnd=[" VALUES ("],sqlValues=[];for(key in paramMap)sqlStart.push(key+","),sqlEnd.push("?,"),sqlValues.push(paramMap[key]);sqlStart.push("value )"),sqlEnd.push("?)"),sqlValues.push(encoded);var sql=sqlStart.join(" ")+sqlEnd.join(" ");idbModules.DEBUG&&console.log("SQL for adding",sql,sqlValues),tx.executeSql(sql,sqlValues,function(){success(primaryKey)},function(a,b){error(b)})},IDBObjectStore.prototype.add=function(a,b){var c=this,d=c.transaction.__createRequest(function(){});return idbModules.Sca.encode(a,function(e){c.transaction.__pushToQueue(d,function(d,f,g,h){c.__deriveKey(d,a,b,function(b){c.__insertData(d,e,a,b,g,h)})})}),d},IDBObjectStore.prototype.put=function(a,b){var c=this,d=c.transaction.__createRequest(function(){});return idbModules.Sca.encode(a,function(e){c.transaction.__pushToQueue(d,function(d,f,g,h){c.__deriveKey(d,a,b,function(b){var f="DELETE FROM "+idbModules.util.quote(c.name)+" where key = ?";d.executeSql(f,[idbModules.Key.encode(b)],function(d,f){idbModules.DEBUG&&console.log("Did the row with the",b,"exist? ",f.rowsAffected),c.__insertData(d,e,a,b,g,h)},function(a,b){h(b)})})})}),d},IDBObjectStore.prototype.get=function(a){var b=this;return b.transaction.__addToTransactionQueue(function(c,d,e,f){b.__waitForReady(function(){var d=idbModules.Key.encode(a);idbModules.DEBUG&&console.log("Fetching",b.name,d),c.executeSql("SELECT * FROM "+idbModules.util.quote(b.name)+" where key = ?",[d],function(a,b){idbModules.DEBUG&&console.log("Fetched data",b);try{if(0===b.rows.length)return e();e(idbModules.Sca.decode(b.rows.item(0).value))}catch(c){idbModules.DEBUG&&console.log(c),e(void 0)}},function(a,b){f(b)})})})},IDBObjectStore.prototype["delete"]=function(a){var b=this;return b.transaction.__addToTransactionQueue(function(c,d,e,f){b.__waitForReady(function(){var d=idbModules.Key.encode(a);idbModules.DEBUG&&console.log("Fetching",b.name,d),c.executeSql("DELETE FROM "+idbModules.util.quote(b.name)+" where key = ?",[d],function(a,b){idbModules.DEBUG&&console.log("Deleted from database",b.rowsAffected),e()},function(a,b){f(b)})})})},IDBObjectStore.prototype.clear=function(){var a=this;return a.transaction.__addToTransactionQueue(function(b,c,d,e){a.__waitForReady(function(){b.executeSql("DELETE FROM "+idbModules.util.quote(a.name),[],function(a,b){idbModules.DEBUG&&console.log("Cleared all records from database",b.rowsAffected),d()},function(a,b){e(b)})})})},IDBObjectStore.prototype.count=function(a){var b=this;return b.transaction.__addToTransactionQueue(function(c,d,e,f){b.__waitForReady(function(){var d="SELECT * FROM "+idbModules.util.quote(b.name)+("undefined"!=typeof a?" WHERE key = ?":""),g=[];"undefined"!=typeof a&&g.push(idbModules.Key.encode(a)),c.executeSql(d,g,function(a,b){e(b.rows.length)},function(a,b){f(b)})})})},IDBObjectStore.prototype.openCursor=function(a,b){{var c=new idbModules.IDBRequest;new idbModules.IDBCursor(a,b,this,c,"key","value")}return c},IDBObjectStore.prototype.index=function(a){var b=new idbModules.IDBIndex(a,this);return b},IDBObjectStore.prototype.createIndex=function(a,b,c){var d=this;c=c||{},d.__setReadyState("createIndex",!1);var e=new idbModules.IDBIndex(a,d);d.__waitForReady(function(){e.__createIndex(a,b,c)},"createObjectStore"),d.indexNames.push(a);var f=d.transaction.db.__storeProperties[d.name];return f.indexList[a]={keyPath:b,optionalParams:c},e},IDBObjectStore.prototype.deleteIndex=function(a){var b=new idbModules.IDBIndex(a,this,!1);return b.__deleteIndex(a),b},idbModules.IDBObjectStore=IDBObjectStore}(idbModules),function(a){var b=0,c=1,d=2,e=function(d,e,f){if("number"==typeof e)this.mode=e,2!==e&&a.DEBUG&&console.log("Mode should be a string, but was specified as ",e);else if("string"==typeof e)switch(e){case"readwrite":this.mode=c;break;case"readonly":this.mode=b;break;default:this.mode=b}this.storeNames="string"==typeof d?[d]:d;for(var g=0;g<this.storeNames.length;g++)f.objectStoreNames.contains(this.storeNames[g])||a.util.throwDOMException(0,"The operation failed because the requested database object could not be found. For example, an object store did not exist but was being opened.",this.storeNames[g]);this.__active=!0,this.__running=!1,this.__requests=[],this.__aborted=!1,this.db=f,this.error=null,this.onabort=this.onerror=this.oncomplete=null};e.prototype.__executeRequests=function(){if(this.__running&&this.mode!==d)return void(a.DEBUG&&console.log("Looks like the request set is already running",this.mode));this.__running=!0;var b=this;window.setTimeout(function(){2===b.mode||b.__active||a.util.throwDOMException(0,"A request was placed against a transaction which is currently not active, or which is finished",b.__active),b.db.__db.transaction(function(c){function d(b,c){c&&(g.req=c),g.req.readyState="done",g.req.result=b,delete g.req.error;var d=a.Event("success");a.util.callback("onsuccess",g.req,d),h++,f()}function e(){g.req.readyState="done",g.req.error="DOMError";var b=a.Event("error",arguments);a.util.callback("onerror",g.req,b),h++,f()}function f(){return h>=b.__requests.length?(b.__active=!1,void(b.__requests=[])):(g=b.__requests[h],void g.op(c,g.args,d,e))}b.__tx=c;var g=null,h=0;try{f()}catch(i){a.DEBUG&&console.log("An exception occured in transaction",arguments),"function"==typeof b.onerror&&b.onerror()}},function(){a.DEBUG&&console.log("An error in transaction",arguments),"function"==typeof b.onerror&&b.onerror()},function(){a.DEBUG&&console.log("Transaction completed",arguments),"function"==typeof b.oncomplete&&b.oncomplete()})},1)},e.prototype.__addToTransactionQueue=function(b,c){this.__active||this.mode===d||a.util.throwDOMException(0,"A request was placed against a transaction which is currently not active, or which is finished.",this.__mode);var e=this.__createRequest();return this.__pushToQueue(e,b,c),e},e.prototype.__createRequest=function(){var b=new a.IDBRequest;return b.source=this.db,b.transaction=this,b},e.prototype.__pushToQueue=function(a,b,c){this.__requests.push({op:b,args:c,req:a}),this.__executeRequests()},e.prototype.objectStore=function(b){return new a.IDBObjectStore(b,this)},e.prototype.abort=function(){!this.__active&&a.util.throwDOMException(0,"A request was placed against a transaction which is currently not active, or which is finished",this.__active)},e.prototype.READ_ONLY=0,e.prototype.READ_WRITE=1,e.prototype.VERSION_CHANGE=2,a.IDBTransaction=e}(idbModules),function(a){var b=function(b,c,d,e){this.__db=b,this.version=d,this.objectStoreNames=new a.util.StringList;for(var f=0;f<e.rows.length;f++)this.objectStoreNames.push(e.rows.item(f).name);for(this.__storeProperties={},f=0;f<e.rows.length;f++){var g=e.rows.item(f),h=this.__storeProperties[g.name]={};h.keyPath=g.keypath,h.autoInc="true"===g.autoInc,h.indexList=JSON.parse(g.indexList)}this.name=c,this.onabort=this.onerror=this.onversionchange=null};b.prototype.createObjectStore=function(b,c){var d=this;c=c||{},c.keyPath=c.keyPath||null;var e=new a.IDBObjectStore(b,d.__versionTransaction,!1),f=d.__versionTransaction;f.__addToTransactionQueue(function(f,g,h){function i(){a.util.throwDOMException(0,"Could not create new object store",arguments)}d.__versionTransaction||a.util.throwDOMException(0,"Invalid State error",d.transaction);var j=["CREATE TABLE",a.util.quote(b),"(key BLOB",c.autoIncrement?", inc INTEGER PRIMARY KEY AUTOINCREMENT":"PRIMARY KEY",", value BLOB)"].join(" ");a.DEBUG&&console.log(j),f.executeSql(j,[],function(a){a.executeSql("INSERT INTO __sys__ VALUES (?,?,?,?)",[b,c.keyPath,c.autoIncrement?!0:!1,"{}"],function(){e.__setReadyState("createObjectStore",!0),h(e)},i)},i)}),d.objectStoreNames.push(b);var g=d.__storeProperties[b]={};return g.keyPath=c.keyPath,g.autoInc=!!c.autoIncrement,g.indexList={},e},b.prototype.deleteObjectStore=function(b){var c=function(){a.util.throwDOMException(0,"Could not delete ObjectStore",arguments)},d=this;!d.objectStoreNames.contains(b)&&c("Object Store does not exist"),d.objectStoreNames.splice(d.objectStoreNames.indexOf(b),1);var e=d.__versionTransaction;e.__addToTransactionQueue(function(){d.__versionTransaction||a.util.throwDOMException(0,"Invalid State error",d.transaction),d.__db.transaction(function(d){d.executeSql("SELECT * FROM __sys__ where name = ?",[b],function(d,e){e.rows.length>0&&d.executeSql("DROP TABLE "+a.util.quote(b),[],function(){d.executeSql("DELETE FROM __sys__ WHERE name = ?",[b],function(){},c)},c)})})})},b.prototype.close=function(){},b.prototype.transaction=function(b,c){var d=new a.IDBTransaction(b,c||1,this);return d},a.IDBDatabase=b}(idbModules),function(a){var b=4194304;if(window.openDatabase){var c=window.openDatabase("__sysdb__",1,"System Database",b);c.transaction(function(a){a.executeSql("CREATE TABLE IF NOT EXISTS dbVersions (name VARCHAR(255), version INT);",[])},function(){a.DEBUG&&console.log("Error in sysdb transaction - when creating dbVersions",arguments)});var d={open:function(d,e){function f(){if(!i){var b=a.Event("error",arguments);h.readyState="done",h.error="DOMError",a.util.callback("onerror",h,b),i=!0}}function g(g){var i=window.openDatabase(d,1,d,b);h.readyState="done","undefined"==typeof e&&(e=g||1),(0>=e||g>e)&&a.util.throwDOMException(0,"An attempt was made to open a database using a lower version than the existing version.",e),i.transaction(function(b){b.executeSql("CREATE TABLE IF NOT EXISTS __sys__ (name VARCHAR(255), keyPath VARCHAR(255), autoInc BOOLEAN, indexList BLOB)",[],function(){b.executeSql("SELECT * FROM __sys__",[],function(b,j){var k=a.Event("success");h.source=h.result=new a.IDBDatabase(i,d,e,j),e>g?c.transaction(function(b){b.executeSql("UPDATE dbVersions set version = ? where name = ?",[e,d],function(){var b=a.Event("upgradeneeded");b.oldVersion=g,b.newVersion=e,h.transaction=h.result.__versionTransaction=new a.IDBTransaction([],2,h.source),a.util.callback("onupgradeneeded",h,b,function(){var b=a.Event("success");a.util.callback("onsuccess",h,b)})},f)},f):a.util.callback("onsuccess",h,k)},f)},f)},f)}var h=new a.IDBOpenRequest,i=!1;return c.transaction(function(a){a.executeSql("SELECT * FROM dbVersions where name = ?",[d],function(a,b){0===b.rows.length?a.executeSql("INSERT INTO dbVersions VALUES (?,?)",[d,e||1],function(){g(0)},f):g(b.rows.item(0).version)},f)},f),h},deleteDatabase:function(d){function e(b){if(!h){g.readyState="done",g.error="DOMError";var c=a.Event("error");c.message=b,c.debug=arguments,a.util.callback("onerror",g,c),h=!0}}function f(){c.transaction(function(b){b.executeSql("DELETE FROM dbVersions where name = ? ",[d],function(){g.result=void 0;var b=a.Event("success");b.newVersion=null,b.oldVersion=i,a.util.callback("onsuccess",g,b)},e)},e)}var g=new a.IDBOpenRequest,h=!1,i=null;return c.transaction(function(c){c.executeSql("SELECT * FROM dbVersions where name = ?",[d],function(c,h){if(0===h.rows.length){g.result=void 0;var j=a.Event("success");return j.newVersion=null,j.oldVersion=i,void a.util.callback("onsuccess",g,j)}i=h.rows.item(0).version;var k=window.openDatabase(d,1,d,b);k.transaction(function(b){b.executeSql("SELECT * FROM __sys__",[],function(b,c){var d=c.rows;!function g(c){c>=d.length?b.executeSql("DROP TABLE __sys__",[],function(){f()},e):b.executeSql("DROP TABLE "+a.util.quote(d.item(c).name),[],function(){g(c+1)},function(){g(c+1)})}(0)},function(){f()})},e)})},e),g},cmp:function(b,c){return a.Key.encode(b)>a.Key.encode(c)?1:b===c?0:-1}};a.shimIndexedDB=d}}(idbModules),function(a,b){"undefined"!=typeof a.openDatabase&&(a.shimIndexedDB=b.shimIndexedDB,a.shimIndexedDB&&(a.shimIndexedDB.__useShim=function(){a.indexedDB=b.shimIndexedDB,a.IDBDatabase=b.IDBDatabase,a.IDBTransaction=b.IDBTransaction,a.IDBCursor=b.IDBCursor,a.IDBKeyRange=b.IDBKeyRange,a.indexedDB!==b.shimIndexedDB&&Object.defineProperty&&Object.defineProperty(a,"indexedDB",{value:b.shimIndexedDB})},a.shimIndexedDB.__debug=function(a){b.DEBUG=a})),"indexedDB"in a||(a.indexedDB=a.indexedDB||a.webkitIndexedDB||a.mozIndexedDB||a.oIndexedDB||a.msIndexedDB);var c=!1;if((navigator.userAgent.match(/Android 2/)||navigator.userAgent.match(/Android 3/)||navigator.userAgent.match(/Android 4\.[0-3]/))&&(navigator.userAgent.match(/Chrome/)||(c=!0)),"undefined"!=typeof a.indexedDB&&!c||"undefined"==typeof a.openDatabase){a.IDBDatabase=a.IDBDatabase||a.webkitIDBDatabase,a.IDBTransaction=a.IDBTransaction||a.webkitIDBTransaction,a.IDBCursor=a.IDBCursor||a.webkitIDBCursor,a.IDBKeyRange=a.IDBKeyRange||a.webkitIDBKeyRange,a.IDBTransaction||(a.IDBTransaction={});try{a.IDBTransaction.READ_ONLY=a.IDBTransaction.READ_ONLY||"readonly",a.IDBTransaction.READ_WRITE=a.IDBTransaction.READ_WRITE||"readwrite"}catch(d){}}else a.shimIndexedDB.__useShim()}(window,idbModules);
//# sourceMappingURL=IndexedDBShim.min.js.map