dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20976
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9816: Added adapter for indexed-db (still needs more testing). Changed interface methods on storage ada...
------------------------------------------------------------
revno: 9816
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-02-17 11:06:34 +0700
message:
Added adapter for indexed-db (still needs more testing). Changed interface methods on storage adapters.
modified:
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.dom.js
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.idb.js
dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.js
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.dom.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.dom.js 2013-02-14 14:38:04 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.dom.js 2013-02-17 04:06:34 +0000
@@ -1,10 +1,10 @@
-// web storage support (localStorage)
+// dom storage support (localStorage)
dhis2.storage.Store.adapter( 'dom', (function () {
var storage = window.localStorage;
- var indexer = function ( name ) {
+ var indexer = function ( dbname, name ) {
return {
- key: name + '.__index__',
+ key: dbname + '.' + name + '.__index__',
all: function () {
var a = storage.getItem( this.key );
@@ -52,12 +52,12 @@
},
init: function ( options, callback ) {
- this.indexer = indexer( this.name );
+ this.indexer = indexer( this.dbname, this.name );
if ( callback ) callback.call( this, this, options );
},
- save: function ( key, obj, callback ) {
- var key = this.name + '.' + key;
+ add: function ( key, obj, callback ) {
+ var key = this.dbname + '.' + this.name + '.' + key;
if ( this.indexer.find( key ) == -1 ) this.indexer.add( key );
storage.setItem( key, JSON.stringify( obj ) );
if ( callback ) callback.call( this, this, obj );
@@ -66,7 +66,7 @@
},
remove: function ( key, callback ) {
- var key = this.name + '.' + key;
+ var key = this.dbname + '.' + this.name + '.' + key;
this.indexer.remove( key );
storage.removeItem( key );
if ( callback ) callback.call( this, this );
@@ -75,7 +75,7 @@
},
exists: function ( key, callback ) {
- key = this.name + '.' + key;
+ var key = this.dbname + '.' + this.name + '.' + key;
var success = storage.getItem( key ) != null;
if ( callback ) callback.call( this, this, success );
@@ -85,7 +85,7 @@
keys: function ( callback ) {
var that = this;
var keys = this.indexer.all().map( function ( r ) {
- return r.replace( that.name + '.', '' )
+ return r.replace( that.dbname + '.' + that.name + '.', '' )
} );
if ( callback ) callback.call( this, this, keys );
@@ -93,8 +93,8 @@
return this;
},
- load: function ( key, callback ) {
- key = this.name + '.' + key;
+ fetch: function ( key, callback ) {
+ var key = this.dbname + '.' + this.name + '.' + key;
var obj = storage.getItem( key );
if ( obj ) {
@@ -105,7 +105,7 @@
return this;
},
- all: function ( callback ) {
+ fetchAll: function ( callback ) {
var idx = this.indexer.all();
var arr = [];
@@ -116,6 +116,16 @@
if ( callback ) callback.call( this, this, arr );
return this;
+ },
+
+ destroy: function () {
+ this.keys( function ( store, keys ) {
+ for ( var key in keys ) {
+ this.remove( key );
+ }
+ } );
+
+ localStorage.removeItem( this.dbname + '.' + this.name + '.__index__' );
}
};
})() );
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.idb.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.idb.js 2013-02-14 14:38:04 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.idb.js 2013-02-17 04:06:34 +0000
@@ -1,37 +1,122 @@
// web storage support (indexedDb)
dhis2.storage.Store.adapter( 'indexed-db', (function () {
- function getIDB() {
- return window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
+ window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
+ window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
+ window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
+
+ var IDB = IDB || {};
+ IDB.TransactionModes = {
+ READ_ONLY: 'readonly',
+ READ_WRITE: 'readwrite'
+ };
+
+ function getTransaction( mode ) {
+ return IDB.db.transaction( [ IDB.options.name ], mode );
+ }
+
+ function getReadOnlyObjectStore() {
+ return getTransaction( IDB.TransactionModes.READ_ONLY ).objectStore( IDB.options.name );
+ }
+
+ function getReadWriteObjectStore() {
+ return getTransaction( IDB.TransactionModes.READ_WRITE ).objectStore( IDB.options.name );
}
return {
valid: function () {
- return false;
- // return !!getIDB();
+ return !!(window.indexedDB && window.IDBTransaction && window.IDBKeyRange);
},
init: function ( options, callback ) {
- throw 'Init not implemented'
+ var that = this;
+ IDB.options = options;
+ var request = window.indexedDB.open( this.dbname, "1" );
+
+ request.onupgradeneeded = function ( e ) {
+ IDB.db = e.target.result;
+
+ if ( IDB.db.objectStoreNames.contains( that.name ) ) {
+ IDB.db.deleteObjectStore( that.name );
+ }
+
+ IDB.db.createObjectStore( that.name );
+ };
+
+ request.onsuccess = function ( e ) {
+ IDB.db = e.target.result;
+ if ( callback ) callback.call( that, that );
+ };
+
+ request.onerror = function ( e ) {
+ console.log( "error:", e );
+ };
+
+ request.onblocked = function ( e ) {
+ console.log( "blocked:", e );
+ };
},
- save: function ( key, obj, callback ) {
- throw 'Save not implemented'
+ add: function ( key, obj, callback ) {
+ var that = this;
+ var request = getReadWriteObjectStore().put( obj, key );
+
+ request.onsuccess = function ( e ) {
+ if ( callback ) callback.call( that, that, obj );
+ };
},
remove: function ( key, callback ) {
- throw 'Remove not implemented'
+ var that = this;
+ var request = getReadWriteObjectStore().delete( key );
+
+ request.onsuccess = function ( e ) {
+ if ( callback ) callback.call( that, that );
+ };
},
exists: function ( key, callback ) {
- throw 'Exists not implemented'
- },
-
- load: function ( key, callback ) {
- throw 'Load not implemented'
- },
-
- all: function ( callback ) {
- throw 'All not implemented'
+ var that = this;
+ var request = getReadOnlyObjectStore().get( key );
+
+ request.onsuccess = function ( e ) {
+ if ( callback ) callback.call( that, that, e.target.result != null );
+ };
+ },
+
+ fetch: function ( key, callback ) {
+ var that = this;
+ var request = getReadOnlyObjectStore().get( key );
+
+ request.onsuccess = function ( e ) {
+ if ( callback ) callback.call( that, that, e.target.result );
+ };
+ },
+
+ fetchAll: function ( callback ) {
+ var that = this;
+ var records = [];
+ var request = getReadOnlyObjectStore().openCursor();
+
+ request.onsuccess = function ( e ) {
+ var cursor = e.target.result;
+
+ if ( cursor ) {
+ records.push( cursor.value );
+ cursor.continue();
+ } else {
+ if ( callback ) callback.call( that, that, records );
+ }
+ };
+ },
+
+ destroy: function ( callback ) {
+ var that = this;
+ IDB.db.close();
+ var request = window.indexedDB.deleteDatabase( IDB.options.dbname );
+
+ request.onsuccess = function ( e ) {
+ if ( callback ) callback.call( that, that );
+ }
}
};
})() );
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.js 2013-02-14 14:38:04 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.storage.js 2013-02-17 04:06:34 +0000
@@ -7,6 +7,7 @@
var Store = dhis2.storage.Store;
this.name = options.name || 'records';
+ this.dbname = options.dbname || 'dhis2';
this.record = options.record || 'record';
if ( arguments.length <= 2 && arguments.length > 0 ) {
@@ -52,10 +53,9 @@
dhis2.storage.Store.adapter = function ( id, obj ) {
var Store = dhis2.storage.Store;
- var adapter_interface = "init save remove exists load all".split( ' ' );
+ var adapter_interface = "init add remove exists fetch fetchAll destroy".split( ' ' );
var missing_functions = [];
-
// verify adapter
for ( var i in adapter_interface ) {
if ( !obj.hasOwnProperty( adapter_interface[i] ) || typeof obj[adapter_interface[i]] !== 'function' ) {