← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21252: event-capture: file upload/download/delete for data elements of type file.

 

------------------------------------------------------------
revno: 21252
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-12-01 15:01:37 +0100
message:
  event-capture: file upload/download/delete for data elements of type file.
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/i18n/i18n_app.properties
  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/directives.js
  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/views/defaultForm.html
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/home.html


--
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/i18n/i18n_app.properties'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/i18n/i18n_app.properties	2015-11-26 16:57:49 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/i18n/i18n_app.properties	2015-12-01 14:01:37 +0000
@@ -25,6 +25,7 @@
 save_and_back=Save and go back
 upload=Upload
 delete=Delete
+delete_file=Delete file
 cancel=Cancel
 close=Close
 discard=Discard

=== 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-11-23 09:52:35 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-12-01 14:01:37 +0000
@@ -65,6 +65,7 @@
     $scope.proceedSelection = true;
     $scope.formUnsaved = false;
     $scope.fileNames = [];
+    $scope.currentFileNames = [];
     
     //notes
     $scope.note = {};
@@ -139,6 +140,7 @@
         $scope.currentEvent = {};
         $scope.currentEventOriginialValue = {};
         $scope.fileNames = [];
+        $scope.currentFileNames = [];
 
         $scope.eventRegistration = false;
         $scope.editGridColumns = false;
@@ -354,13 +356,10 @@
                                     if($scope.prStDes[dataValue.dataElement].dataElement.valueType === 'FILE_RESOURCE'){
                                         FileService.get(val).then(function(response){
                                             if(response && response.name){
-                                                if($scope.fileNames[event.event]){
-                                                    $scope.fileNames[event.event][dataValue.dataElement] = response.name;
+                                                if(!$scope.fileNames[event.event]){
+                                                    $scope.fileNames[event.event] = [];
                                                 } 
-                                                else{
-                                                    $scope.fileNames[event.event] = [];
-                                                    $scope.fileNames[event.event][dataValue.dataElement] = response.name;
-                                                }
+                                                $scope.fileNames[event.event][dataValue.dataElement] = response.name;
                                             }
                                         });
                                     }
@@ -673,7 +672,11 @@
             else {
                 
                 //add the new event to the grid                
-                newEvent.event = data.response.importSummaries[0].reference;                
+                newEvent.event = data.response.importSummaries[0].reference; 
+                $scope.currentEvent.event = newEvent.event;
+                
+                $scope.updateFileNames();
+                
                 if( !$scope.dhis2Events ){
                     $scope.dhis2Events = [];                   
                 }
@@ -758,9 +761,9 @@
         }
 
         DHIS2EventFactory.update(updatedEvent).then(function(data){            
-            
             //reflect the change in the gird            
-            $scope.resetEventValue($scope.currentEvent);            
+            $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);    
+            $scope.updateFileNames();
             $scope.outerForm.submitted = false;            
             $scope.editingEventInFull = false;
             $scope.currentEvent = {};
@@ -782,7 +785,7 @@
         if (!rawDate || !convertedDate || rawDate !== convertedDate) {
             $scope.invalidDate = true;
             $scope.currentEvent.eventDate = $scope.currentEventOriginialValue.eventDate;            
-            $scope.resetEventValue($scope.currentEvent);
+            $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
             $scope.currentElement.updated = false;
             return;
         }
@@ -793,7 +796,7 @@
         
         if ($scope.currentEvent.eventDate === '') {
             $scope.currentEvent.eventDate = oldValue;            
-            $scope.resetEventValue($scope.currentEvent);
+            $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
             $scope.currentElement.updated = false;
             return;
         }
@@ -808,7 +811,7 @@
 
             DHIS2EventFactory.updateForEventDate(e, updatedFullValueEvent).then(function () {
                 //reflect the new value in the grid
-                $scope.resetEventValue($scope.currentEvent);
+                $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
                 
                 //update original value
                 $scope.currentEventOriginialValue = angular.copy($scope.currentEvent);      
@@ -837,7 +840,7 @@
             $scope.currentElement.updated = false;            
             //reset value back to original
             $scope.currentEvent[dataElement] = oldValue;            
-            $scope.resetEventValue($scope.currentEvent);
+            $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
             return;            
         }
         
@@ -845,7 +848,7 @@
             $scope.currentElement.updated = false;                        
             //reset value back to original
             $scope.currentEvent[dataElement] = oldValue;            
-            $scope.resetEventValue($scope.currentEvent);
+            $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
             return;
         }        
                 
@@ -857,7 +860,7 @@
             DHIS2EventFactory.updateForSingleValue(updatedSingleValueEvent, updatedFullValueEvent).then(function(data){
                 
                 //reflect the new value in the grid
-                $scope.resetEventValue($scope.currentEvent);
+                $scope.dhis2Events = DHIS2EventService.refreshList($scope.dhis2Events, $scope.currentEvent);
                 
                 //update original value
                 $scope.currentEventOriginialValue = angular.copy($scope.currentEvent);      
@@ -868,16 +871,6 @@
         }
     };
     
-    $scope.resetEventValue = function(){
-        var continueLoop = true;
-        for(var i=0; i< $scope.dhis2Events.length && continueLoop; i++){
-            if($scope.dhis2Events[i].event === $scope.currentEvent.event ){
-                $scope.dhis2Events[i] = $scope.currentEvent;
-                continueLoop = false;
-            }
-        }
-    };
-    
     $scope.removeEvent = function(){
         
         var dhis2Event = ContextMenuSelectedItem.getSelectedItem();
@@ -893,6 +886,8 @@
             
             DHIS2EventFactory.delete(dhis2Event).then(function(data){
                 
+                $scope.currentFileNames = [];
+                delete $scope.fileNames[$scope.currentEvent.event];
                 var continueLoop = true, index = -1;
                 for(var i=0; i< $scope.dhis2Events.length && continueLoop; i++){
                     if($scope.dhis2Events[i].event === dhis2Event.event ){
@@ -1162,4 +1157,30 @@
             e.preventDefault();
         }
     };
+    
+    $scope.deleteFile = function(dataElement){
+        var modalOptions = {
+            closeButtonText: 'cancel',
+            actionButtonText: 'remove',
+            headerText: 'remove',
+            bodyText: 'are_you_sure_to_remove'
+        };
+
+        ModalService.showModal({}, modalOptions).then(function(result){            
+            $scope.fileNames[$scope.currentEvent.event][dataElement] = null;
+            $scope.currentEvent[dataElement] = null;
+            $scope.updateEventDataValue($scope.currentEvent, dataElement);
+        });
+    };
+    
+    $scope.updateFileNames = function(){        
+        for(var dataElement in $scope.currentFileNames){
+            if($scope.currentFileNames[dataElement]){
+                if(!$scope.fileNames[$scope.currentEvent.event]){
+                    $scope.fileNames[$scope.currentEvent.event] = [];
+                }                 
+                $scope.fileNames[$scope.currentEvent.event][dataElement] = $scope.currentFileNames[dataElement];
+            }
+        }
+    };
 });
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/directives.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/directives.js	2015-11-16 15:48:46 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/directives.js	2015-12-01 14:01:37 +0000
@@ -33,34 +33,43 @@
     };
 })
 
-.directive('d2FileInputField', function () {
-
-    return {
-        restrict: 'E',
-        templateUrl: 'views/file-input.html',
-        link: function (scope, element, attrs) {}
-    };
-})
-
-.directive('d2FileInput', function($parse, $timeout, FileService, DialogService){
+.directive('d2FileInput', function(DHIS2EventService, DHIS2EventFactory, FileService, DialogService){
     
     return {
         restrict: "A",
-        link: function (scope, element, attrs) { 
-            var modelGetter = $parse(attrs.d2FileInput);
-            var modelSetter = modelGetter.assign;
-            
-            var nameGetter = $parse(attrs.d2FileInputName);
-            var nameSetter = nameGetter.assign;
- 
+        scope: {
+            d2FileInputList: '=',
+            d2FileInput: '=',
+            d2FileInputName: '=',
+            d2FileInputCurrentName: '=',
+            d2FileInputPs: '='
+        },
+        link: function (scope, element, attrs) {
+            
+            var de = attrs.inputFieldId;
+            
             var updateModel = function () {
+                
+                var update = attrs.d2FileInputInstant;
+                
                 FileService.upload(element[0].files[0]).then(function(data){
-                    if(data && data.status === 'OK' && data.response && data.response.fileResource && data.response.fileResource.id){
-                        $timeout(function(){
-                            modelSetter(scope, data.response.fileResource.id);
-                            nameSetter(scope, element[0].files[0].name);
-                            scope.$apply();
-                        });
+                    
+                    if(data && data.status === 'OK' && data.response && data.response.fileResource && data.response.fileResource.id && data.response.fileResource.name){
+                                            
+                        scope.d2FileInput[de] = data.response.fileResource.id;   
+                        scope.d2FileInputCurrentName[de] = data.response.fileResource.name;
+                        if( update === 'true' ){                            
+                            if(!scope.d2FileInputName[scope.d2FileInput.event]){
+                                scope.d2FileInputName[scope.d2FileInput.event] = [];
+                            }                            
+                            scope.d2FileInputName[scope.d2FileInput.event][de] = data.response.fileResource.name;
+                            
+                            var updatedSingleValueEvent = {event: scope.d2FileInput.event, dataValues: [{value: data.response.fileResource.id, dataElement:  de}]};
+                            var updatedFullValueEvent = DHIS2EventService.reconstructEvent(scope.d2FileInput, scope.d2FileInputPs.programStageDataElements);
+                            DHIS2EventFactory.updateForSingleValue(updatedSingleValueEvent, updatedFullValueEvent).then(function(data){
+                                scope.d2FileInputList = DHIS2EventService.refreshList(scope.d2FileInputList, scope.d2FileInput);
+                            });
+                        }
                     }
                     else{
                         var dialogOptions = {
@@ -77,18 +86,17 @@
     };    
 })
 
-.directive('d2FileInputDelete', function($parse, $timeout, ModalService, FileService, DialogService){
+.directive('d2FileInputDelete', function($parse, $timeout, FileService, DialogService){
     
     return {
         restrict: "A",
         link: function (scope, element, attrs) {
-            var modelGetter = $parse(attrs.d2FileInputDelete);
-            var modelSetter = modelGetter.assign;
+            var valueGetter = $parse(attrs.d2FileInputDelete);
             var nameGetter = $parse(attrs.d2FileInputName);
             var nameSetter = nameGetter.assign;
             
-            if(modelGetter(scope)) {
-                FileService.get(modelGetter(scope)).then(function(data){
+            if(valueGetter(scope)) {
+                FileService.get(valueGetter(scope)).then(function(data){
                     if(data && data.name && data.id){
                         $timeout(function(){
                             nameSetter(scope, data.name);
@@ -103,26 +111,7 @@
                         DialogService.showDialog({}, dialogOptions);
                     }                    
                 });                 
-            }            
-            
-            var deleteFile = function () {
-                var modalOptions = {
-                    closeButtonText: 'cancel',
-                    actionButtonText: 'remove',
-                    headerText: 'remove',
-                    bodyText: 'are_you_sure_to_remove'
-                };
-
-                ModalService.showModal({}, modalOptions).then(function(result){
-                    $timeout(function(){
-                        modelSetter(scope, '');
-                        nameSetter(scope, '');
-                        scope.$apply();
-                    });
-                });
-            };
-             
-            element.bind('click', deleteFile);            
+            }
         }
     };    
 });

=== 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-11-17 15:28:34 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-12-01 14:01:37 +0000
@@ -27,6 +27,12 @@
             });
             return promise;
         },
+        delete: function (uid) {
+            var promise = $http.get('../api/fileResources/' + uid).then(function (response) {
+                return response.data;
+            });
+            return promise;
+        },
         download: function (fileName) {
             var promise = $http.get(fileName).then(function (response) {
                 return response.data;
@@ -595,6 +601,16 @@
             }
 
             return e;
-        }        
+        },
+        refreshList: function(eventList, currentEvent){
+            var continueLoop = true;
+            for(var i=0; i< eventList.length && continueLoop; i++){
+                if(eventList[i].event === currentEvent.event ){
+                    eventList[i] = currentEvent;
+                    continueLoop = false;
+                }
+            }            
+            return eventList;
+        }
     };
 });

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/defaultForm.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/defaultForm.html	2015-11-25 10:41:52 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/defaultForm.html	2015-12-01 14:01:37 +0000
@@ -209,12 +209,15 @@
                             <div ng-switch-when="FILE_RESOURCE">                                
                                 <div class="input-group">
                                     <div class="form-control">
-                                        <a href ng-click="downloadFile(currentEvent.event, eventGridColumn.id)">{{fileNames[eventGridColumn.id]}}</a>                                        
+                                        <a href ng-click="downloadFile(currentEvent.event, eventGridColumn.id)">{{fileNames[currentEvent.event][eventGridColumn.id]}}</a>                                        
                                     </div>
                                     <span class="input-group-btn">
                                         <span class="btn btn-primary btn-file">
-                                            <span ng-if="currentEvent[eventGridColumn.id]" title="{{'delete' | translate}}" d2-file-input-name="fileNames[eventGridColumn.id]" d2-file-input-delete="currentEvent[eventGridColumn.id]">
-                                                <i class="fa fa-trash"></i>
+                                            <span ng-if="currentEvent[eventGridColumn.id]" 
+                                                  title="{{'delete' | translate}}" 
+                                                  d2-file-input-name="fileNames[currentEvent.event][eventGridColumn.id]" 
+                                                  d2-file-input-delete="currentEvent[eventGridColumn.id]">
+                                                <a href ng-click="deleteFile(eventGridColumn.id)"><i class="fa fa-trash alert-danger"></i></a>
                                             </span>
                                             <span ng-if="!currentEvent[eventGridColumn.id]" title="{{'upload' | translate}}">
                                                 <i class="fa fa-upload"></i>
@@ -223,8 +226,10 @@
                                                        ng-disabled="assignedFields[eventGridColumn.id]"
                                                        name="foo"
                                                        input-field-id={{eventGridColumn.id}}
-                                                       d2-file-input="currentEvent[eventGridColumn.id]" 
-                                                       d2-file-input-name="fileNames[eventGridColumn.id]">
+                                                       d2-file-input-instant="false"
+                                                       d2-file-input="currentEvent" 
+                                                       d2-file-input-current-name="currentFileNames"
+                                                       d2-file-input-name="fileNames">
                                             </span>                                                
                                         </span>
                                     </span>                
@@ -247,12 +252,12 @@
                 </td>
             </tr>        
         </tbody>
-    </table>    
+    </table>
+    
 </div>
 
 <div ng-if='selectedProgramStage.programStageSections.length'>
     <div ng-include="'views/event-details.html'"></div>
-
     <div ng-repeat='section in selectedProgramStage.programStageSections' ng-if="section.id === selectedSection.id || selectedSection.id === 'ALL' && !hiddenSections[section.id] ">
         <div class="vertical-spacing section-label">
            {{section.name}}
@@ -476,12 +481,15 @@
                                 <div ng-switch-when="FILE_RESOURCE">
                                     <div class="input-group">
                                         <div class="form-control">
-                                            <a href ng-click="downloadFile(currentEvent.event, de.dataElement.id)">{{fileNames[de.dataElement.id]}}</a>                                        
+                                            <a href ng-click="downloadFile(currentEvent.event, de.dataElement.id)">{{fileNames[currentEvent.event][de.dataElement.id]}}</a>                                        
                                         </div>
                                         <span class="input-group-btn">
                                             <span class="btn btn-primary btn-file">
-                                                <span ng-if="currentEvent[de.dataElement.id]" title="{{'delete' | translate}}" d2-file-input-name="fileNames[de.dataElement.id]" d2-file-input-delete="currentEvent[de.dataElement.id]">
-                                                    <i class="fa fa-trash"></i>
+                                                <span ng-if="currentEvent[de.dataElement.id]" 
+                                                      title="{{'delete' | translate}}" 
+                                                      d2-file-input-name="fileNames[currentEvent.event][de.dataElement.id]" 
+                                                      d2-file-input-delete="currentEvent[de.dataElement.id]">
+                                                    <a href ng-click="deleteFile(de.dataElement.id)"><i class="fa fa-trash alert-danger"></i></a>
                                                 </span>
                                                 <span ng-if="!currentEvent[de.dataElement.id]" title="{{'upload' | translate}}">
                                                     <i class="fa fa-upload"></i>
@@ -490,8 +498,10 @@
                                                            ng-disabled="assignedFields[de.dataElement.id]"
                                                            name="foo"
                                                            input-field-id={{de.dataElement.id}}
-                                                           d2-file-input="currentEvent[de.dataElement.id]" 
-                                                           d2-file-input-name="fileNames[de.dataElement.id]">
+                                                           d2-file-input-instant="false"
+                                                           d2-file-input="currentEvent" 
+                                                           d2-file-input-current-name="currentFileNames"
+                                                           d2-file-input-name="fileNames">
                                                 </span>                                                
                                             </span>
                                         </span>                
@@ -519,5 +529,5 @@
             </tbody>
         </table>
     </div> 
-
+    
 </div>
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html	2015-11-27 11:04:21 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html	2015-12-01 14:01:37 +0000
@@ -349,12 +349,15 @@
                                                 <div ng-switch-when="FILE_RESOURCE">                                
                                                     <div class="input-group">
                                                         <div class="form-control">
-                                                            <a href ng-click="downloadFile(currentEvent.event, eventGridColumn.id, $event)">{{fileNames[eventGridColumn.id]}}</a>
+                                                            <a href ng-click="downloadFile(currentEvent.event, eventGridColumn.id, $event)">{{fileNames[currentEvent.event][eventGridColumn.id]}}</a>
                                                         </div>
                                                         <span class="input-group-btn">
                                                             <span class="btn btn-primary btn-file">
-                                                                <span ng-if="currentEvent[eventGridColumn.id]" title="{{'delete' | translate}}" d2-file-input-name="fileNames[eventGridColumn.id]" d2-file-input-delete="currentEvent[eventGridColumn.id]">
-                                                                    <i class="fa fa-trash"></i>
+                                                                <span ng-if="currentEvent[eventGridColumn.id]" 
+                                                                      title="{{'delete_file' | translate}}" 
+                                                                      d2-file-input-name="fileNames[currentEvent.event][eventGridColumn.id]" 
+                                                                      d2-file-input-delete="currentEvent[eventGridColumn.id]">
+                                                                    <a href ng-click="deleteFile(eventGridColumn.id)"><i class="fa fa-trash alert-danger"></i></a>
                                                                 </span>
                                                                 <span ng-if="!currentEvent[eventGridColumn.id]" title="{{'upload' | translate}}">
                                                                     <i class="fa fa-upload"></i>
@@ -363,8 +366,12 @@
                                                                            ng-disabled="assignedFields[eventGridColumn.id]"
                                                                            name="foo"
                                                                            input-field-id={{eventGridColumn.id}}
-                                                                           d2-file-input="currentEvent[eventGridColumn.id]" 
-                                                                           d2-file-input-name="fileNames[eventGridColumn.id]">
+                                                                           d2-file-input-list="dhis2Events"
+                                                                           d2-file-input-ps="selectedProgramStage"
+                                                                           d2-file-input-instant="true"
+                                                                           d2-file-input="currentEvent" 
+                                                                           d2-file-input-current-name="currentFileNames"
+                                                                           d2-file-input-name="fileNames">
                                                                 </span>                                                
                                                             </span>
                                                         </span>                

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/home.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/home.html	2015-11-18 15:09:30 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/home.html	2015-12-01 14:01:37 +0000
@@ -127,7 +127,7 @@
                         <div ng-include="'views/buttons-section.html'"></div>
                     </div>                    
                 </div>
-                <div class="col-sm-4"ng-if="allProgramRules.programIndicators.rules && allProgramRules.programIndicators.rules.length > 0">
+                <div class="col-sm-4" ng-if="allProgramRules.programIndicators.rules && allProgramRules.programIndicators.rules.length > 0">
                     <div class="bordered-div">
                         <div ng-if="allProgramRules.programIndicators.rules && allProgramRules.programIndicators.rules.length > 0" ng-include="'views/indicators.html'"></div>
                         <div ng-if="warningMessages.length > 0" ng-include="'views/warnings.html'"></div>