=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/index.html' --- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/index.html 2014-09-23 10:48:24 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/index.html 2014-10-17 14:33:27 +0000 @@ -1,5 +1,6 @@ - + + Event Capture @@ -8,9 +9,23 @@ - - - + + + + + + + + + + + + + + + + + === 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 2014-10-17 13:22:28 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js 2014-10-17 14:33:27 +0000 @@ -43,9 +43,7 @@ //notes $scope.note = {}; - var today = moment(); - today = Date.parse(today); - today = $filter('date')(today, 'yyyy-MM-dd'); + $scope.today = DateUtils.getToday(); var loginDetails = storage.get('LOGIN_DETAILS'); var storedBy = ''; @@ -230,7 +228,7 @@ if(angular.isObject($scope.prStDes[dataValue.dataElement].dataElement)){ //converting int string value to integer for proper sorting. - if($scope.prStDes[dataValue.dataElement].dataElement.type == 'int'){ + if($scope.prStDes[dataValue.dataElement].dataElement.type === 'int'){ if( !isNaN(parseInt(val)) ){ val = parseInt(val); } @@ -238,12 +236,15 @@ val = ''; } } - if($scope.prStDes[dataValue.dataElement].dataElement.type == 'string'){ + if($scope.prStDes[dataValue.dataElement].dataElement.type === 'string'){ if($scope.prStDes[dataValue.dataElement].dataElement.optionSet && $scope.optionNamesByCode[ '"' + val + '"']){ val = $scope.optionNamesByCode[ '"' + val + '"']; } } - if( $scope.prStDes[dataValue.dataElement].dataElement.type == 'trueOnly'){ + if($scope.prStDes[dataValue.dataElement].dataElement.type === 'date'){ + val = DateUtils.formatFromApiToUser(val); + } + if( $scope.prStDes[dataValue.dataElement].dataElement.type === 'trueOnly'){ if(val == 'true'){ val = true; } @@ -258,7 +259,7 @@ }); $scope.dhis2Events[i]['uid'] = $scope.dhis2Events[i].event; - $scope.dhis2Events[i].eventDate = DateUtils.format($scope.dhis2Events[i].eventDate); + $scope.dhis2Events[i].eventDate = DateUtils.formatFromApiToUser($scope.dhis2Events[i].eventDate); $scope.dhis2Events[i]['event_date'] = $scope.dhis2Events[i].eventDate; delete $scope.dhis2Events[i].dataValues; @@ -437,13 +438,17 @@ var val = $scope.currentEvent[dataElement]; if(val){ valueExists = true; - if($scope.prStDes[dataElement].dataElement.type == 'string'){ + if($scope.prStDes[dataElement].dataElement.type === 'string'){ if($scope.prStDes[dataElement].dataElement.optionSet){ if($scope.optionCodesByName[ '"' + val + '"']){ val = $scope.optionCodesByName[ '"' + val + '"']; } } - } + } + + if($scope.prStDes[dataElement].dataElement.type === 'date'){ + val = DateUtils.formatFromUserToApi(val); + } } dataValues.push({dataElement: dataElement, value: val}); } @@ -466,7 +471,7 @@ programStage: $scope.selectedProgramStage.id, orgUnit: $scope.selectedOrgUnit.id, status: 'ACTIVE', - eventDate: $filter('date')(newEvent.eventDate, 'yyyy-MM-dd'), + eventDate: DateUtils.formatFromUserToApi(newEvent.eventDate), dataValues: dataValues }; @@ -477,7 +482,7 @@ if(!angular.isUndefined($scope.note.value) && $scope.note.value != ''){ dhis2Event.notes = [{value: $scope.note.value}]; - newEvent.notes = [{value: $scope.note.value, storedDate: today, storedBy: storedBy}]; + newEvent.notes = [{value: $scope.note.value, storedDate: $scope.today, storedBy: storedBy}]; $scope.noteExists = true; } @@ -486,7 +491,7 @@ dhis2Event.coordinate = {latitude: $scope.currentEvent.coordinate.latitude ? $scope.currentEvent.coordinate.latitude : '', longitude: $scope.currentEvent.coordinate.longitude ? $scope.currentEvent.coordinate.longitude : ''}; } - + //send the new event to server DHIS2EventFactory.create(dhis2Event).then(function(data) { if (data.importSummaries[0].status === 'ERROR') { @@ -505,7 +510,7 @@ $scope.dhis2Events = []; } newEvent['uid'] = newEvent.event; - newEvent['event_date'] = DateUtils.format(newEvent.eventDate); + newEvent['event_date'] = newEvent.eventDate; $scope.dhis2Events.splice(0,0,newEvent); $scope.eventLength++; @@ -548,13 +553,16 @@ for(var dataElement in $scope.prStDes){ var val = $scope.currentEvent[dataElement]; - if(val && $scope.prStDes[dataElement].dataElement.type == 'string'){ + if(val && $scope.prStDes[dataElement].dataElement.type === 'string'){ if($scope.prStDes[dataElement].dataElement.optionSet){ if($scope.optionCodesByName[ '"' + val + '"']){ val = $scope.optionCodesByName[ '"' + val + '"']; } } } + if(val && $scope.prStDes[dataElement].dataElement.type === 'date'){ + val = DateUtils.formatFromUserToApi(val); + } dataValues.push({dataElement: dataElement, value: val}); } @@ -563,12 +571,11 @@ programStage: $scope.currentEvent.programStage, orgUnit: $scope.currentEvent.orgUnit, status: 'ACTIVE', - eventDate: $scope.currentEvent.eventDate, + eventDate: DateUtils.formatFromUserToApi($scope.currentEvent.eventDate), event: $scope.currentEvent.event, dataValues: dataValues }; - updatedEvent.eventDate = DateUtils.format(updatedEvent.eventDate); if($scope.selectedProgramStage.captureCoordinates){ updatedEvent.coordinate = {latitude: $scope.currentEvent.coordinate.latitude ? $scope.currentEvent.coordinate.latitude : '', @@ -580,10 +587,10 @@ updatedEvent.notes = [{value: $scope.note.value}]; if($scope.currentEvent.notes){ - $scope.currentEvent.notes.splice(0,0,{value: $scope.note.value, storedDate: today, storedBy: storedBy}); + $scope.currentEvent.notes.splice(0,0,{value: $scope.note.value, storedDate: $scope.today, storedBy: storedBy}); } else{ - $scope.currentEvent.notes = [{value: $scope.note.value, storedDate: today, storedBy: storedBy}]; + $scope.currentEvent.notes = [{value: $scope.note.value, storedDate: $scope.today, storedBy: storedBy}]; } $scope.noteExists = true; @@ -640,6 +647,9 @@ newValue = $scope.optionCodesByName[ '"' + newValue + '"']; } } + } + if($scope.prStDes[dataElement].dataElement.type === 'date'){ + newValue = DateUtils.formatFromUserToApi(newValue); } var updatedSingleValueEvent = {event: currentEvent.event, dataValues: [{value: newValue, dataElement: dataElement}]}; === 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 2014-10-17 13:22:28 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/directives.js 2014-10-17 14:33:27 +0000 @@ -191,10 +191,62 @@ }; }) +.directive('d2Date', function(DateUtils, CalendarService, storage, $parse) { + return { + restrict: 'A', + require: 'ngModel', + link: function(scope, element, attrs, ctrl) { + + var calendarSetting = CalendarService.getSetting(); + var dateFormat = 'yyyy-mm-dd'; + if(calendarSetting.keyDateFormat === 'dd-MM-yyyy'){ + dateFormat = 'dd-mm-yyyy'; + } + + var minDate = $parse(attrs.minDate)(scope), + maxDate = $parse(attrs.maxDate)(scope), + calendar = $.calendars.instance(calendarSetting.keyCalendar); + + element.calendarsPicker({ + changeMonth: true, + dateFormat: dateFormat, + yearRange: '-120:+30', + minDate: minDate, + maxDate: maxDate, + calendar: calendar, + renderer: $.calendars.picker.themeRollerRenderer, + onSelect: function(date) { + //scope.date = date; + ctrl.$setViewValue(date); + $(this).change(); + scope.$apply(); + } + }) + .change(function() { + var rawDate = this.value; + var convertedDate = DateUtils.format(this.value); + + if(rawDate != convertedDate){ + scope.invalidDate = true; + ctrl.$setViewValue(this.value); + ctrl.$setValidity('foo', false); + scope.$apply(); + } + else{ + scope.invalidDate = false; + ctrl.$setViewValue(this.value); + ctrl.$setValidity('foo', true); + scope.$apply(); + } + }); + } + }; +}) + .directive('blurOrChange', function() { return function( scope, elem, attrs) { - elem.datepicker({ + elem.calendarsPicker({ onSelect: function() { scope.$apply(attrs.blurOrChange); $(this).change(); === modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js' --- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js 2014-10-01 08:08:35 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js 2014-10-17 14:33:27 +0000 @@ -60,6 +60,7 @@ promise = promise.then( dhis2.ec.store.open ); promise = promise.then( getUserProfile ); + promise = promise.then( getCalendarSetting ); promise = promise.then( getLoginDetails ); promise = promise.then( getMetaPrograms ); promise = promise.then( getPrograms ); @@ -166,6 +167,21 @@ return def.promise(); } +function getCalendarSetting() +{ + var def = $.Deferred(); + + $.ajax({ + url: '../api/systemSettings?key=keyCalendar&key=keyDateFormat', + type: 'GET' + }).done(function(response) { + localStorage['CALENDAR_SETTING'] = JSON.stringify(response); + def.resolve(); + }); + + return def.promise(); +} + function getLoginDetails() { var def = $.Deferred(); @@ -334,7 +350,7 @@ { return function() { return $.ajax( { - url: '../api/programStages.json?filter=id:eq:' + id +'&fields=id,name,version,description,reportDateDescription,captureCoordinates,dataEntryForm,minDaysFromStart,repeatable,preGenerateUID,programStageSections[id,name,programStageDataElements[dataElement[id]]],programStageDataElements[displayInReports,allowProvidedElsewhere,allowDateInFuture,compulsory,dataElement[id,name,type,formName,optionSet[id]]]', + url: '../api/programStages.json?filter=id:eq:' + id +'&fields=id,name,version,description,reportDateDescription,captureCoordinates,dataEntryForm,minDaysFromStart,repeatable,preGenerateUID,programStageSections[id,name,programStageDataElements[dataElement[id]]],programStageDataElements[displayInReports,sortOrder,allowProvidedElsewhere,allowFutureDate,compulsory,dataElement[id,name,type,formName,optionSet[id]]]', type: 'GET' }).done( function( response ){ _.each( _.values( response.programStages ), function( programStage ) { === 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 2014-10-17 13:22:28 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js 2014-10-17 14:33:27 +0000 @@ -16,16 +16,54 @@ }; }) -.service('DateUtils', function($filter){ +.service('DateUtils', function($filter, CalendarService){ return { format: function(dateValue) { + if(!dateValue){ + return; + } + var calendarSetting = CalendarService.getSetting(); + dateValue = $filter('date')(dateValue, calendarSetting.keyDateFormat); + return dateValue; + }, + formatToHrsMins: function(dateValue) { + var calendarSetting = CalendarService.getSetting(); + var dateFormat = 'YYYY-MM-DD @ hh:mm A'; + if(calendarSetting.keyDateFormat === 'dd-MM-yyyy'){ + dateFormat = 'DD-MM-YYYY @ hh:mm A'; + } + return moment(dateValue).format(dateFormat); + }, + getToday: function(){ + var calendarSetting = CalendarService.getSetting(); + var tdy = $.calendars.instance(calendarSetting.keyCalendar).newDate(); + var today = moment(tdy._year + '-' + tdy._month + '-' + tdy._day, 'YYYY-MM-DD')._d; + today = Date.parse(today); + today = $filter('date')(today, calendarSetting.keyDateFormat); + return today; + }, + formatFromUserToApi: function(dateValue){ + if(!dateValue){ + return; + } + var calendarSetting = CalendarService.getSetting(); + dateValue = moment(dateValue, calendarSetting.momentFormat)._d; + dateValue = Date.parse(dateValue); + dateValue = $filter('date')(dateValue, 'yyyy-MM-dd'); + return dateValue; + }, + formatFromApiToUser: function(dateValue){ + if(!dateValue){ + return; + } + var calendarSetting = CalendarService.getSetting(); dateValue = moment(dateValue, 'YYYY-MM-DD')._d; - dateValue = Date.parse(dateValue); - dateValue = $filter('date')(dateValue, 'yyyy-MM-dd'); + dateValue = Date.parse(dateValue); + dateValue = $filter('date')(dateValue, calendarSetting.keyDateFormat); return dateValue; } - }; + }; }) /* factory for loading logged in user profiles from DHIS2 */ @@ -286,6 +324,8 @@ attributes['name'] = deId; } + var maxDate = programStageDataElements[deId].allowFutureDate ? '' : 0; + //check data element type and generate corresponding angular input field if(programStageDataElements[deId].dataElement.type == "int"){ newInputField = ''; } if(programStageDataElements[deId].dataElement.type == "trueOnly"){ @@ -568,4 +609,30 @@ } }; +}) + +/* service for getting calendar setting */ +.service('CalendarService', function(storage, $rootScope){ + + return { + getSetting: function() { + + var dhis2CalendarFormat = {keyDateFormat: 'yyyy-MM-dd', keyCalendar: 'gregorian', momentFormat: 'YYYY-MM-DD'}; + var storedFormat = storage.get('CALENDAR_SETTING'); + if(angular.isObject(storedFormat) && storedFormat.keyDateFormat && storedFormat.keyCalendar){ + if(storedFormat.keyCalendar === 'iso8601'){ + storedFormat.keyCalendar = 'gregorian'; + } + + if(storedFormat.keyDateFormat === 'dd-MM-yyyy'){ + dhis2CalendarFormat.momentFormat = 'DD-MM-YYYY'; + } + + dhis2CalendarFormat.keyCalendar = storedFormat.keyCalendar; + dhis2CalendarFormat.keyDateFormat = storedFormat.keyDateFormat; + } + $rootScope.dhis2CalendarFormat = dhis2CalendarFormat; + return dhis2CalendarFormat; + } + }; }); === modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/customForm.html' --- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/customForm.html 2014-08-21 16:30:01 +0000 +++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/customForm.html 2014-10-17 14:33:27 +0000 @@ -13,8 +13,9 @@ + style="width:99%;">{{dhis2CalendarFormat.keyDateFormat}} {{'required'| translate}} @@ -133,8 +134,9 @@
- + - + @@ -213,8 +213,9 @@