← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18975: event-capture: if applicable, remember previously selected program when switching orgunit

 

------------------------------------------------------------
revno: 18975
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-04-22 09:45:28 +0200
message:
  event-capture: if applicable, remember previously selected program when switching orgunit
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.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-event-capture/scripts/controllers.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-04-17 13:14:21 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-04-22 07:45:28 +0000
@@ -98,7 +98,6 @@
     $scope.loadPrograms = function() {
         
         $scope.resetOu = false;
-        $scope.selectedProgram = null;
         $scope.selectedProgramStage = null;
         $scope.programValidations = [];
         $scope.dhis2Events = [];
@@ -115,13 +114,10 @@
         
         if (angular.isObject($scope.selectedOrgUnit)) {    
             
-            ProgramFactory.getAll().then(function(programs){
-                $scope.programs = programs;                
-                
-                if(angular.isObject($scope.programs) && $scope.programs.length === 1){
-                    $scope.selectedProgram = $scope.programs[0];
-                    $scope.getProgramDetails();
-                }                
+            ProgramFactory.getProgramsByOu($scope.selectedOrgUnit, $scope.selectedProgram).then(function(response){
+                $scope.programs = response.programs;
+                $scope.selectedProgram = response.selectedProgram;
+                $scope.getProgramDetails();
             });
         }    
     };

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-03-28 09:24:30 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-04-22 07:45:28 +0000
@@ -179,28 +179,49 @@
     };
     
     return {
-        
-        getAll: function(){
+        getProgramsByOu: function(ou, selectedProgram){
             var roles = SessionStorageService.get('USER_ROLES');
             var userRoles = roles && roles.userCredentials && roles.userCredentials.userRoles ? roles.userCredentials.userRoles : [];
-            var ou = SessionStorageService.get('SELECTED_OU');
             var def = $q.defer();
             
             ECStorageService.currentStore.open().done(function(){
                 ECStorageService.currentStore.getAll('programs').done(function(prs){
-                    var programs = [];                    
-                    angular.forEach(prs, function(pr){
+                    var programs = [];
+                    angular.forEach(prs, function(pr){                            
                         if(pr.organisationUnits.hasOwnProperty( ou.id ) && userHasValidRole(pr, userRoles)){
                             programs.push(pr);
                         }
                     });
+                    
+                    if(programs.length === 0){
+                        selectedProgram = null;
+                    }
+                    else if(programs.length === 1){
+                        selectedProgram = programs[0];
+                    } 
+                    else{
+                        if(selectedProgram){
+                            var continueLoop = true;
+                            for(var i=0; i<programs.length && continueLoop; i++){
+                                if(programs[i].id === selectedProgram.id){                                
+                                    selectedProgram = programs[i];
+                                    continueLoop = false;
+                                }
+                            }
+                            if(continueLoop){
+                                selectedProgram = null;
+                            }
+                        }
+                    }
+                    
                     $rootScope.$apply(function(){
-                        def.resolve(programs);
-                    });                    
+                        def.resolve({programs: programs, selectedProgram: selectedProgram});
+                    });                      
                 });
             });
+            
             return def.promise;
-        }        
+        }
     };
 })