← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11669: wip, factor out storage handling in ouwt

 

------------------------------------------------------------
revno: 11669
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-08-15 09:43:46 +0200
message:
  wip, factor out storage handling in ouwt
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.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-07-11 05:42:16 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/ouwt/ouwt.js	2013-08-15 07:43:46 +0000
@@ -20,6 +20,23 @@
 var selection = new Selection();
 var subtree = new Subtree();
 
+var dhis2 = dhis2 || {};
+dhis2.ou = dhis2.ou || {};
+
+dhis2.ou.store = new dhis2.storage.Store( {
+    name: 'dhis2',
+    objectStores: [ {
+        name: 'ou',
+        adapters: [ /* dhis2.storage.IndexedDBAdapter, */ dhis2.storage.DomLocalStorageAdapter, dhis2.storage.InMemoryAdapter ]
+    }, {
+        name: 'ouPartial',
+        adapters: [ /* dhis2.storage.IndexedDBAdapter, */ dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter ]
+    }, {
+        name: 'ouConfig',
+        adapters: [ /* dhis2.storage.IndexedDBAdapter, */ dhis2.storage.DomSessionStorageAdapter, dhis2.storage.InMemoryAdapter ]
+    } ]
+} );
+
 $( document ).ready( function ()
 {
     selection.load();
@@ -39,73 +56,130 @@
     var realRoot = true;
     var includeChildren = false;
 
-    this.setListenerFunction = function ( listenerFunction_, skipInitialCall )
-    {
+    this.setListenerFunction = function( listenerFunction_, skipInitialCall ) {
         listenerFunction = listenerFunction_;
 
-        if ( !skipInitialCall )
-        {
-            $( "#orgUnitTree" ).one( "ouwtLoaded", function ()
-            {
+        if( !skipInitialCall ) {
+            $( "#orgUnitTree" ).one( "ouwtLoaded", function() {
                 selection.responseReceived();
             } );
         }
     };
 
-    this.setMultipleSelectionAllowed = function ( allowed )
-    {
+    this.setMultipleSelectionAllowed = function( allowed ) {
         multipleSelectionAllowed = allowed;
     };
 
-    this.setUnselectAllowed = function ( allowed )
-    {
+    this.setUnselectAllowed = function( allowed ) {
         unselectAllowed = allowed;
     };
 
-    this.setRootUnselectAllowed = function ( allowed )
-    {
+    this.setRootUnselectAllowed = function( allowed ) {
         rootUnselectAllowed = allowed;
     };
 
-    this.setAutoSelectRoot = function ( autoSelect )
-    {
+    this.setAutoSelectRoot = function( autoSelect ) {
         autoSelectRoot = autoSelect;
     };
 
-    this.setIncludeChildren = function( children )
-    {
+    this.setIncludeChildren = function( children ) {
         includeChildren = children;
     };
 
+    this.getSelected = function() {
+        var selected = sessionStorage[getTagId( "Selected" )];
+        selected = selected ? JSON.parse( selected ) : [];
+        selected = $.isArray( selected ) ? selected : [ selected ];
+
+        return  selected;
+    };
+
+    this.clearSelected = function() {
+        sessionStorage.removeItem( getTagId( "Selected" ) );
+    };
+
+    this.setSelected = function( selected ) {
+        sessionStorage[getTagId( "Selected" )] = JSON.stringify( selected );
+    };
+
+    this.selectedExists = function() {
+        return sessionStorage[getTagId( "Selected" )] != null;
+    };
+
+    this.getRoots = function() {
+        var roots = localStorage[getTagId( "Roots" )];
+        return roots ? JSON.parse( roots ) : [];
+    };
+
+    this.rootsExists = function() {
+        return localStorage[getTagId( "Roots" )] != null;
+    };
+
+    this.setRoots = function(roots) {
+        localStorage[getTagId( "Roots" )] = JSON.stringify( roots );
+    };
+
+    this.getVersion = function() {
+        return localStorage[getTagId( "Version" )] ? localStorage[getTagId( "Version" )] : 0;
+    };
+
+    this.setVersion = function( version ) {
+        localStorage[getTagId( "Version" )] = version;
+    };
+
+    this.versionExists = function() {
+        return localStorage[getTagId( "Version" )] != null;
+    };
+
+    this.getOrganisationUnits = function() {
+        var organisationUnits = localStorage["organisationUnits"];
+        return organisationUnits ? JSON.parse( organisationUnits ) : [];
+    };
+
+    this.setOrganisationUnits = function( organisationUnits ) {
+        localStorage["organisationUnits"] = JSON.stringify( organisationUnits );
+    };
+
+    this.organisationUnitsExists = function() {
+        return localStorage["organisationUnits"] != null;
+    };
+
+    this.ajaxOrganisationUnits = function( versionOnly, format ) {
+        format = format || "json";
+
+        return $.ajax( {
+            url: '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action',
+            data: {
+                versionOnly: versionOnly
+            },
+            method: 'POST',
+            dataType: format
+        } );
+    };
+
     this.load = function ()
     {
         function sync_and_reload()
         {
-            var roots = JSON.parse( localStorage[getTagId( "Roots" )] );
+            var roots = selection.getRoots();
 
-            if ( sessionStorage[getTagId( "Selected" )] == null && roots.length > 0 )
-            {
-                if ( autoSelectRoot )
-                {
-                    if ( multipleSelectionAllowed )
-                    {
-                        sessionStorage[getTagId( "Selected" )] = roots;
+            if( !selection.selectedExists() && roots.length > 0 ) {
+                if( autoSelectRoot ) {
+                    if( multipleSelectionAllowed ) {
+                        selection.setSelected( roots );
                     }
-                    else
-                    {
-                        sessionStorage[getTagId( "Selected" )] = roots[0];
+                    else {
+                        selection.setSelected( roots[0] );
                     }
                 }
-                else
-                {
+                else {
                     selection.sync( true );
                 }
             }
 
-            organisationUnits = JSON.parse( localStorage["organisationUnits"] );
+            organisationUnits = selection.getOrganisationUnits();
 
-            if ( sessionStorage['organisationUnits'] !== undefined )
-            {
+            if( sessionStorage['organisationUnits'] !== undefined ) {
                 $.extend( organisationUnits, JSON.parse( sessionStorage["organisationUnits"] ) );
             }
 
@@ -115,28 +189,23 @@
             $( "#ouwt_loader" ).hide();
         }
 
-        function update_required( remoteVersion, remoteRoots )
-        {
-            var localVersion = localStorage[getTagId( "Version" )] ? localStorage[getTagId( "Version" )] : 0;
-            var localRoots = localStorage[getTagId( "Roots" )] ? JSON.parse( localStorage[getTagId( "Roots" )] ) : [];
+        function update_required( remoteVersion, remoteRoots ) {
+            var localVersion = selection.getVersion();
+            var localRoots = selection.getRoots();
 
-            if ( localVersion != remoteVersion )
-            {
+            if ( localVersion != remoteVersion ) {
                 return true;
             }
 
-            if ( localRoots == null || localRoots.length == 0 )
-            {
+            if ( localRoots == null || localRoots.length == 0 ) {
                 return true;
             }
 
             localRoots.sort();
             remoteRoots.sort();
 
-            for ( var i in localRoots )
-            {
-                if ( remoteRoots[i] == null || localRoots[i] != remoteRoots[i] )
-                {
+            for ( var i in localRoots ) {
+                if ( remoteRoots[i] == null || localRoots[i] != remoteRoots[i] ) {
                     return true;
                 }
             }
@@ -146,124 +215,83 @@
 
         var should_update = false;
 
-        $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action', {
-                "versionOnly":true
-            },
-            function ( data, textStatus, jqXHR )
-            {
-                if ( data.indexOf( "<!DOCTYPE" ) != 0 )
-                {
-                    data = JSON.parse( data );
-                    realRoot = data.realRoot;
-                    should_update = update_required( data.version, data.roots );
-                }
-            }, "text" ).complete(
-            function ()
-            {
-                if ( should_update )
-                {
-                    $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action',
-                        function ( data, textStatus, jqXHR )
-                        {
-                            localStorage[getTagId( "Roots" )] = JSON.stringify( data.roots );
-                            localStorage[getTagId( "Version" )] = data.version;
-                            localStorage["organisationUnits"] = JSON.stringify( data.organisationUnits );
-                        } ).complete( function ()
-                        {
-                            sync_and_reload();
-                            $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
-                        } );
-                }
-                else
-                {
-                    sync_and_reload();
-                    $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
-                }
-            } );
+        selection.ajaxOrganisationUnits( true, 'text' ).done(function( data ) {
+            if ( data.indexOf( "<!DOCTYPE" ) != 0 ) {
+                data = JSON.parse( data );
+                realRoot = data.realRoot;
+                should_update = update_required( data.version, data.roots );
+            }
+        } ).always( function() {
+            if( should_update ) {
+                selection.ajaxOrganisationUnits( false ).done(function( data ) {
+                    selection.setRoots( data.roots );
+                    selection.setVersion( data.version );
+                    selection.setOrganisationUnits( data.organisationUnits );
+                } ).always( function() {
+                        sync_and_reload();
+                        $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
+                    } );
+            }
+            else {
+                sync_and_reload();
+                $( "#orgUnitTree" ).trigger( "ouwtLoaded" );
+            }
+        } );
     };
 
     // server = true : sync from server
     // server = false : sync to server
     this.sync = function ( server, fn )
     {
-        if ( fn === undefined )
-        {
-            fn = function ()
-            {
-            };
+        if ( fn === undefined ) {
+            fn = function () {};
         }
 
-        if ( server )
-        {
-            sessionStorage.removeItem( getTagId( "Selected" ) );
+        if( server ) {
+            selection.clearSelected();
 
-            $.post( organisationUnitTreePath + "getselected.action", function ( data )
-            {
-                if ( data["selectedUnits"].length < 1 )
-                {
+            $.post( organisationUnitTreePath + "getselected.action", function( data ) {
+                if( data["selectedUnits"].length < 1 ) {
                     return;
                 }
 
-                if ( multipleSelectionAllowed )
-                {
+                if( multipleSelectionAllowed ) {
                     var selected = [];
-                    $.each( data["selectedUnits"], function ( i, item )
-                    {
+
+                    $.each( data["selectedUnits"], function( i, item ) {
                         selected.push( item.id );
                     } );
 
-                    sessionStorage[getTagId( "Selected" )] = JSON.stringify( selected );
+                    selection.setSelected( selected );
                 }
-                else
-                {
+                else {
                     var ou = data["selectedUnits"][0];
-                    sessionStorage[getTagId( "Selected" )] = ou.id;
+                    selection.setSelected( ou.id );
                 }
 
                 subtree.reloadTree();
             } );
-        }
-        else
-        {
-            $.post( organisationUnitTreePath + "clearselected.action", function ()
-            {
-                if ( sessionStorage[getTagId( "Selected" )] == null )
-                {
+        } else {
+            $.post( organisationUnitTreePath + "clearselected.action", function() {
+                if( sessionStorage[getTagId( "Selected" )] == null ) {
                     return;
                 }
 
-                var selected = sessionStorage[getTagId( "Selected" )];
-
-                if ( selected != null )
-                {
-                    selected = JSON.parse( selected );
-
-                    if ( multipleSelectionAllowed )
-                    {
-                        if ( !$.isArray( selected ) )
-                        {
-                            selected = [ selected ];
-                        }
-
-                        $.each( selected,
-                            function ( i, item )
-                            {
-                                $.post( organisationUnitTreePath + "addorgunit.action", {
-                                    id:item
-                                } );
-                            } ).complete( fn );
-                    }
-                    else
-                    {
-                        if ( $.isArray( selected ) )
-                        {
-                            selected = selected[0];
-                        }
-
-                        $.post( organisationUnitTreePath + "setorgunit.action", {
-                            id:selected
-                        } ).complete( fn );
-                    }
+                var selected = selection.getSelected();
+
+                if( multipleSelectionAllowed ) {
+                    $.each( selected, function( i, item ) {
+                        $.post( organisationUnitTreePath + "addorgunit.action", {
+                            id: item
+                        } );
+                    } ).complete( fn );
+                }
+                else {
+                    selected = $.isArray( selected ) ? selected[0] : selected;
+
+                    $.post( organisationUnitTreePath + "setorgunit.action", {
+                        id: selected
+                    } ).complete( fn );
                 }
             } );
         }
@@ -271,40 +299,36 @@
 
     this.clear = function ()
     {
-        sessionStorage.removeItem( getTagId( "Selected" ) );
-
-        var roots = JSON.parse( localStorage[getTagId( "Roots" )] );
-
-        if ( roots.length > 1 )
-        {
-            sessionStorage[getTagId( "Selected" )] = roots;
-        }
-        else
-        {
-            sessionStorage[getTagId( "Selected" )] = roots[0];
-        }
+        selection.clearSelected();
+
+        var roots = selection.getRoots();
+        selection.getRoots().length > 1 ? selection.setSelected( roots ) : selection.setSelected( roots[0] );
+
+        /*
+        if( roots.length > 1 ) {
+            selection.setSelected( roots );
+        }
+        else {
+            selection.setSelected( roots[0] );
+        }
+        */
 
         subtree.reloadTree();
 
         $.post( organisationUnitTreePath + "clearselected.action" ).complete( this.responseReceived );
     };
 
-    this.getSelected = function()
-    {
-        return JSON.parse( sessionStorage[getTagId( "Selected" )] );
-    };
-
     this.select = function ( unitId )
     {
         var $linkTag = $( "#" + getTagId( unitId ) ).find( "a" ).eq( 0 );
 
         if ( $linkTag.hasClass( "selected" ) && ( unselectAllowed || rootUnselectAllowed ) )
         {
-            var selected = JSON.parse( sessionStorage[getTagId( "Selected" )] );
+            var selected = selection.getSelected();
 
             if ( rootUnselectAllowed && !unselectAllowed && !multipleSelectionAllowed )
             {
-                var roots = JSON.parse( localStorage[getTagId( "Roots" )] );
+                var roots = selection.getRoots();
 
                 if ( $.inArray(selected, roots) == -1 )
                 {
@@ -351,7 +375,7 @@
         {
             if ( multipleSelectionAllowed )
             {
-                var selected = JSON.parse( sessionStorage[getTagId( "Selected" )] );
+                var selected = selection.getSelected();
 
                 if ( selected )
                 {
@@ -408,16 +432,10 @@
             return;
         }
 
-        var selected = [];
         var children = [];
-
-        if ( sessionStorage[getTagId( "Selected" )] != null )
-        {
-            selected = JSON.parse( sessionStorage[getTagId( "Selected" )] );
-        }
-
         var ids = [];
         var names = [];
+        var selected = selection.getSelected();
 
         if ( $.isArray( selected ) )
         {
@@ -656,12 +674,8 @@
         var $treeTag = $( "#orgUnitTree" );
         $treeTag.children().eq( 0 ).remove();
 
-        var roots = localStorage[getTagId( "Roots" )];
-        var selected = sessionStorage[getTagId( "Selected" )];
-
-        roots = roots ? JSON.parse( roots ) : [];
-        selected = selected ? JSON.parse( selected ) : [];
-        selected = $.isArray( selected ) ? selected : [ selected ];
+        var roots = selection.getRoots();
+        var selected = selection.getSelected();
 
         expandTreeAtOrgUnits( roots );
         expandTreeAtOrgUnits( selected );
@@ -701,7 +715,7 @@
     }
 
     this.getChildren = function( parentId ) {
-        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId, function ( data, textStatus, jqXHR )
+        return $.post( '../dhis-web-commons-ajax-json/getOrganisationUnitTree.action?parentId=' + parentId, function ( data )
             {
                 // load additional organisationUnits into sessionStorage
                 if ( sessionStorage["organisationUnits"] === undefined )
@@ -718,7 +732,7 @@
                 $.extend(organisationUnits, data.organisationUnits);
             }
         );
-    }
+    };
 
     function getAndCreateChildren(parentTag, parent)
     {