=== 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( " 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) {