← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11701: ouwt, support idb for browser storage

 

------------------------------------------------------------
revno: 11701
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-08-19 16:27:44 +0200
message:
  ouwt, support idb for browser storage
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.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/ouwt/ouwt.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2013-08-16 11:10:51 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2013-08-19 14:27:44 +0000
@@ -24,20 +24,21 @@
 var dhis2 = dhis2 || {};
 dhis2.ou = dhis2.ou || {};
 
+var OU_STORE_NAME = "dhis2ou";
+var OU_KEY = "ou";
+var OU_PARTIAL_KEY = "ouPartial";
 var OU_ROOTS_KEY = "ouRoots";
 var OU_VERSION_KEY = "ouVersion";
 var OU_SELECTED_KEY = "ouSelected";
 
 dhis2.ou.store = new dhis2.storage.Store( {
-    name: 'dhis2',
-    objectStores: [
-        {
-            name: 'ou',
-            adapters: [ dhis2.storage.DomLocalStorageAdapter ]
-        },
-        {
-            name: 'ouPartial',
-            adapters: [ dhis2.storage.DomSessionStorageAdapter ]
+    name: OU_STORE_NAME,
+    objectStores: [ {
+            name: OU_KEY,
+            adapters: [ dhis2.storage.IndexedDBAdapter, dhis2.storage.DomLocalStorageAdapter, dhis2.storage.InMemoryAdapter ]
+        }, {
+            name: OU_PARTIAL_KEY,
+            adapters: [ dhis2.storage.IndexedDBAdapter, dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter ]
         }
     ]
 } );
@@ -134,16 +135,11 @@
         localStorage.removeItem( OU_VERSION_KEY );
     };
 
-    this.getOrganisationUnits = function() {
+    this.getAllOrganisationUnits = function() {
         var def = $.Deferred();
 
-        dhis2.ou.store.getAll( 'ou' ).done( function( all ) {
-            var ous = {};
-
-            $.each( all, function( i, item ) {
-                ous[item.id] = item;
-            } );
-
+        dhis2.ou.store.getAll( OU_KEY ).done( function( all ) {
+            var ous = selection.arrayToObjectIdMapper( all );
             def.resolveWith( window, [ ous ]);
         } );
 
@@ -151,20 +147,32 @@
     };
 
     this.setOrganisationUnits = function( ous ) {
+        $.extend( organisationUnits, ous );
         ous = ous ? _.values( ous ) : [];
-        return dhis2.ou.store.setAll( 'ou', ous );
-    };
-
-    this.getPartialOrganisationUnits = function() {
-        var def = $.Deferred();
-
-        dhis2.ou.store.getAll( 'ouPartial' ).done( function( all ) {
-            var ous = {};
-
-            $.each( all, function( i, item ) {
-                ous[item.id] = item;
-            } );
-
+        return dhis2.ou.store.setAll( OU_KEY, ous );
+    };
+
+    this.getOrganisationUnit = function( id ) {
+        var def = $.Deferred();
+
+        dhis2.ou.store.get( OU_KEY, id ).done( function( item ) {
+            if(item) {
+                var obj = {};
+                obj[item.id] = item;
+                def.resolveWith( window, [ obj ]);
+            } else {
+                def.resolveWith( window );
+            }
+        } );
+
+        return def.promise();
+    };
+
+    this.getAllPartialOrganisationUnits = function() {
+        var def = $.Deferred();
+
+        dhis2.ou.store.getAll( OU_PARTIAL_KEY ).done( function( all ) {
+            var ous = selection.arrayToObjectIdMapper( all );
             def.resolveWith( window, [ ous ]);
         } );
 
@@ -172,8 +180,20 @@
     };
 
     this.setPartialOrganisationUnits = function( ous ) {
+        $.extend( organisationUnits, ous );
         ous = ous ? _.values( ous ) : [];
-        return dhis2.ou.store.setAll( 'ouPartial', ous );
+        return dhis2.ou.store.setAll( OU_PARTIAL_KEY, ous );
+    };
+
+    // maps [ { id: 1, ... } ] => { '1': { id: 1, ... } }
+    this.arrayToObjectIdMapper = function( all ) {
+        var ous = {};
+
+        $.each( all, function( i, item ) {
+            ous[item.id] = item;
+        } );
+
+        return ous;
     };
 
     this.ajaxOrganisationUnits = function( versionOnly, format ) {
@@ -197,22 +217,17 @@
 
             if( !selection.selectedExists() && roots.length > 0 ) {
                 if( autoSelectRoot ) {
-                    if( multipleSelectionAllowed ) {
-                        selection.setSelected( roots );
-                    }
-                    else {
-                        selection.setSelected( roots[0] );
-                    }
+                    multipleSelectionAllowed ? selection.setSelected( roots ) : selection.setSelected( roots[0] );
                 }
                 else {
                     selection.sync( true );
                 }
             }
 
-            selection.getOrganisationUnits().done( function( all ) {
+            selection.getAllOrganisationUnits().done( function( all ) {
                 $.extend( organisationUnits, all );
 
-                selection.getPartialOrganisationUnits().done( function( all ) {
+                selection.getAllPartialOrganisationUnits().done( function( all ) {
                     $.extend( organisationUnits, all );
 
                     selection.sync();
@@ -262,11 +277,14 @@
                 selection.ajaxOrganisationUnits( false ).done(function( data ) {
                     selection.setRoots( data.roots );
                     selection.setVersion( data.version );
-                    selection.setOrganisationUnits( data.organisationUnits );
-                } ).always( function() {
+                    selection.setOrganisationUnits( data.organisationUnits ).done(function() {
                         sync_and_reload();
                         $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
-                    } );
+                    });
+                } ).fail( function() {
+                    sync_and_reload();
+                    $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
+                } );
             }
             else {
                 sync_and_reload();
@@ -430,30 +448,26 @@
         if( multipleSelectionAllowed ) {
             $.each( selected, function( i, item ) {
                 var name = organisationUnits[item].n;
-                ids.push( item );
+                ids.push( +item );
                 names.push( name );
             } );
 
             listenerFunction( ids, names, children );
         } else {
-            selected = selected[0];
-
-            // we only support includeChildren for single selects
-            if( includeChildren ) {
-                children = organisationUnits[selected].c;
-            }
-
-            selection.getPartialOrganisationUnits().done( function( all ) {
-                $.extend( organisationUnits, all );
+            selected = +selected[0];
+
+            if( 'undefined' !== typeof organisationUnits[selected]) {
+                // we only support includeChildren for single selects
+                if( includeChildren ) {
+                    children = organisationUnits[selected].c;
+                }
 
                 var name = organisationUnits[selected].n;
-                ids.push( +selected );
+                ids.push( selected );
                 names.push( name );
 
-                subtree.getChildren( selected ).done( function() {
-                    listenerFunction( ids, names, children );
-                } );
-            } );
+                listenerFunction( ids, names, children );
+            }
         }
     };
 
@@ -489,7 +503,7 @@
                 data: { byName: name }
             } ).done(function( data ) {
                 if( data.realRoot === undefined ) {
-                    selection.getPartialOrganisationUnits().done(function(all) {
+                    selection.getAllPartialOrganisationUnits().done(function(all) {
                         $.extend( all, data.organisationUnits );
 
                         selection.setPartialOrganisationUnits( all ).done(function() {
@@ -642,7 +656,7 @@
         var $children = $parentTag.find( "ul" );
 
         if( $children.length < 1 ) {
-            getAndCreateChildren( $parentTag, parent );
+            subtree.getAndCreateChildren( $parentTag, parent );
         }
         else {
             setVisible( $children.eq( 0 ), true );
@@ -650,43 +664,83 @@
         }
     }
 
+    this.ajaxGetChildren = function( parentId ) {
+        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId );
+    };
+
     this.getChildren = function( parentId ) {
-        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId, function( data ) {
-                selection.getPartialOrganisationUnits().done(function(all) {
-                    $.extend( all, data.organisationUnits );
-
-                    selection.setPartialOrganisationUnits( all ).done(function() {
-                        $.extend( organisationUnits, data.organisationUnits );
-                    });
-                });
-            }
-        );
+        var def = $.Deferred();
+        var p = def.promise();
+
+        p = p.then( function() {
+            var def = $.Deferred();
+
+            subtree.ajaxGetChildren( parentId ).done(function( data ) {
+                def.resolveWith( window, [ data ] );
+            });
+
+            return def.promise();
+        } );
+
+        p = p.then( function( data ) {
+            var def = $.Deferred();
+
+            selection.getAllPartialOrganisationUnits().done(function( all ) {
+                $.extend( all, data.organisationUnits );
+                def.resolveWith( window, [ all ] );
+            });
+
+            return def.promise();
+        });
+
+        p = p.then( function( data ) {
+            var def = $.Deferred();
+
+            selection.setPartialOrganisationUnits( data ).done(function() {
+                $.extend( organisationUnits, data.organisationUnits );
+                def.resolveWith( window, [ organisationUnits ] );
+            });
+
+            return def.promise();
+        });
+
+        def.resolve();
+
+        return p;
     };
 
-    function getAndCreateChildren( parentTag, parent ) {
-        if( parent.c !== undefined ) {
-            if( organisationUnits[parent.c[0]] !== undefined ) {
-                createChildren( parentTag, parent );
-            }
-            else {
-                subtree.getChildren( parent.id ).done( function() {
+    this.getAndCreateChildren = function( parentTag, parent ) {
+        var def = $.Deferred();
+
+        if( 'undefined' !== typeof organisationUnits[parent.c[0]] ) {
+            createChildren( parentTag, parent );
+            def.resolve();
+        }
+        else {
+            selection.getOrganisationUnit( parent.c[0] ).done(function(item) {
+                if(item) {
+                    $.extend( organisationUnits, item );
                     createChildren( parentTag, parent );
-                } );
-            }
+                    def.resolve();
+                } else {
+                    subtree.getChildren( parent.id ).done( function() {
+                        createChildren( parentTag, parent );
+                        def.resolve();
+                    } );
+                }
+            });
         }
 
-    }
+        return def.promise();
+    };
 
-    function createChildren( parentTag, parent )
-    {
+    function createChildren( parentTag, parent ) {
         var $childrenTag = $( "<ul/>" );
 
-        $.each( parent.c, function ( i, item )
-        {
+        $.each( parent.c, function( i, item ) {
             var ou = organisationUnits[item];
 
-            if ( ou !== undefined )
-            {
+            if( ou !== undefined ) {
                 $childrenTag.append( createTreeElementTag( ou ) );
             }
         } );

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2013-08-16 11:10:51 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2013-08-19 14:27:44 +0000
@@ -54,7 +54,7 @@
 var multiOrganisationUnit = false;
 
 // local cache of organisationUnits (used for name lookup)
-var organisationUnits = [];
+// var organisationUnits = [];
 
 var COLOR_GREEN = '#b9ffb9';
 var COLOR_YELLOW = '#fffe8c';
@@ -104,6 +104,8 @@
     };
 } )( jQuery );
 
+selection.setListenerFunction( organisationUnitSelected );
+
 /**
  * Page init. The order of events is:
  *
@@ -117,18 +119,13 @@
         cache: false
     } );
 
-    selection.setListenerFunction( organisationUnitSelected );
     $( '#loaderSpan' ).show();
 
     $( '#orgUnitTree' ).one( 'ouwtLoaded', function()
     {
         log( 'Ouwt loaded' );
-
-        selection.getOrganisationUnits().done(function(all) {
-            organisationUnits = all;
-            loadMetaData();
-        });
-    } );
+        loadMetaData();
+   } );
 
     $( document ).bind( 'dhis2.online', function( event, loggedIn )
 	{