← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19726: tracker-capture: some refactoring in metadata download

 

------------------------------------------------------------
revno: 19726
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-08-06 12:10:48 +0200
message:
  tracker-capture: some refactoring in metadata download
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.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-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js	2015-07-15 16:38:42 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js	2015-08-06 10:10:48 +0000
@@ -1,4 +1,4 @@
-/* global dhis2, angular */
+/* global dhis2, angular, selection, i18n_ajax_login_failed, _ */
 
 dhis2.util.namespace('dhis2.tc');
 
@@ -151,40 +151,28 @@
     promise = promise.then( getMetaProgramIndicators );
     promise = promise.then( getProgramIndicators );
     promise = promise.then( getOrgUnitLevels );
-    promise.done(function() {
-        
+    promise.done(function() {        
         //Enable ou selection after meta-data has downloaded
-        $( "#orgUnitTree" ).removeClass( "disable-clicks" );
-        
+        $( "#orgUnitTree" ).removeClass( "disable-clicks" );        
         console.log( 'Finished loading meta-data' );        
         selection.responseReceived(); 
     });
 
-    def.resolve();
-    
+    def.resolve();    
 }
 
 function getUserRoles()
 {
-    var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
-    
+    var SessionStorageService = angular.element('body').injector().get('SessionStorageService');    
     if( SessionStorageService.get('USER_ROLES') ){
        return; 
     }
     
     var def = $.Deferred();
-
-    $.ajax({
-        url: '../api/me.json?fields=id,name,userCredentials[userRoles[id,authorities]]',
-        type: 'GET'
-    }).done(function(response) {
-        SessionStorageService.set('USER_ROLES', response);
-        def.resolve();
-    }).fail(function(){
-        def.resolve();
-    });
-
-    return def.promise();
+    var promise = def.promise();
+    promise = promise.then( getD2Object(null, 'USER_ROLES', '../api/me.json', 'fields=id,name,userCredentials[userRoles[id,authorities]]', 'sessionStorage') );
+    promise = promise.done(function(){});    
+    def.resolve();
 }
 
 function getCalendarSetting()
@@ -194,18 +182,10 @@
     }
     
     var def = $.Deferred();
-
-    $.ajax({
-        url: '../api/systemSettings?key=keyCalendar&key=keyDateFormat',
-        type: 'GET'
-    }).done(function(response) {
-        localStorage['CALENDAR_SETTING'] = JSON.stringify(response);
-        def.resolve();
-    }).fail(function(){
-        def.resolve();
-    });
-
-    return def.promise();
+    var promise = def.promise();
+    promise = promise.then( getD2Object(null, 'CALENDAR_SETTING', '../api/systemSettings', 'key=keyCalendar&key=keyDateFormat', 'localStorage') );
+    promise = promise.done(function(){});    
+    def.resolve();    
 }
 
 function getConstants()
@@ -214,7 +194,7 @@
         if(res.length > 0){
             return;
         }        
-        return getD2Objects('constants', 'constants', '../api/constants.json', 'paging=false&fields=id,name,displayName,value');        
+        return getD2Objects('constants', 'constants', '../api/constants.json', 'paging=false&fields=id,name,displayName,value', 'idb');        
     });    
 }
 
@@ -224,7 +204,7 @@
         if(res.length > 0){
             return;
         }
-        return getD2Objects('relationshipTypes', 'relationshipTypes', '../api/relationshipTypes.json', 'paging=false&fields=id,name,aIsToB,bIsToA,displayName');
+        return getD2Objects('relationshipTypes', 'relationshipTypes', '../api/relationshipTypes.json', 'paging=false&fields=id,name,aIsToB,bIsToA,displayName', 'idb');
     });    
 }
 
@@ -234,25 +214,13 @@
         if(res.length > 0){
             return;
         }        
-        return getD2Objects('trackedEntities', 'trackedEntities', '../api/trackedEntities.json', 'paging=false&fields=id,name');
+        return getD2Objects('trackedEntities', 'trackedEntities', '../api/trackedEntities.json', 'paging=false&fields=id,name', 'idb');
     });    
 }
 
 function getMetaPrograms()
 {
-    var def = $.Deferred();
-
-    $.ajax({
-        url: '../api/programs.json',
-        type: 'GET',
-        data:'filter=programType:eq:WITH_REGISTRATION&paging=false&fields=id,version,programTrackedEntityAttributes[trackedEntityAttribute[id,optionSet[id,version]]],programStages[id,name,version,minDaysFromStart,standardInterval,periodType,generatedByEnrollmentDate,reportDateDescription,repeatable,autoGenerateEvent,openAfterEnrollment,reportDateToUse,programStageDataElements[dataElement[optionSet[id,version]]]]'
-    }).done( function(response) {          
-        def.resolve( response.programs ? response.programs: [] );
-    }).fail(function(){
-        def.resolve( null );
-    });
-    
-    return def.promise(); 
+    return getD2Objects('programs', 'programs', '../api/programs.json', 'filter=programType:eq:WITH_REGISTRATION&paging=false&fields=id,version,programTrackedEntityAttributes[trackedEntityAttribute[id,optionSet[id,version]]],programStages[id,name,version,minDaysFromStart,standardInterval,periodType,generatedByEnrollmentDate,reportDateDescription,repeatable,autoGenerateEvent,openAfterEnrollment,reportDateToUse,programStageDataElements[dataElement[optionSet[id,version]]]]', 'temp');
 }
 
 function getPrograms( programs )
@@ -568,13 +536,11 @@
     dhis2.tc.store.getKeys( 'ouLevels').done(function(res){        
         if(res.length > 0){
             return;
-        }
-        
-        return getD2Objects('ouLevels', 'organisationUnitLevels', '../api/organisationUnitLevels.json', 'filter=level:gt:1&fields=id,name,level&paging=false');        
+        }        
+        return getD2Objects('ouLevels', 'organisationUnitLevels', '../api/organisationUnitLevels.json', 'filter=level:gt:1&fields=id,name,level&paging=false', 'idb');        
     }); 
 }
 
-
 function getMetaProgramValidations( programs )
 {    
     return getD2MetaObject(programs, 'programValidations', '../api/programValidations.json', 'paging=false&fields=id,program[id]');
@@ -701,7 +667,7 @@
     return mainPromise;
 }
 
-function getD2Objects(store, objs, url, filter)
+function getD2Objects(store, objs, url, filter, storage)
 {
     var def = $.Deferred();
 
@@ -710,10 +676,25 @@
         type: 'GET',
         data: filter
     }).done(function(response) {
-        if(response[objs]){
-            dhis2.tc.store.setAll( store, response[objs] );
-        }            
-        def.resolve();        
+        if(response[objs]){            
+            if(storage === 'idb'){
+                dhis2.tc.store.setAll( store, response[objs] );
+            }
+            if(storage === 'localStorage'){                
+                localStorage[store] = JSON.stringify(response[objs]);
+            }            
+            if(storage === 'sessionStorage'){
+                var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
+                SessionStorageService.set(store, response[objs]);
+            }
+        }
+        
+        if(storage === 'temp'){
+            def.resolve(response[objs] ? response[objs] : []);
+        }
+        else{
+            def.resolve();
+        }    
     }).fail(function(){
         def.resolve();
     });