"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .directive('editActivityDates', ['$rootScope', 'events', 'utilityFunctions', 'configurationModel', 'ticketModel', 'fieldValidationModel', 'editTicketDatesService', function ($rootScope, events, utilityFunctions, configurationModel, ticketModel, fieldValidationModel, editTicketDatesService) { return { restrict: 'E', templateUrl: 'views/common/edit-activity-dates.html', replace: true, scope: { ticketOriginal: '=ticket', dateForm: '=', isDraft: '=', updateIsHandledByParent: '=', onlyShowRequired: '=', ignoreAccessMapping: '=', makeScheduleDatesOptional: '=', makeActualDatesOptional: '=' }, link: function (scope) { scope.ticket = _.cloneDeep(scope.ticketOriginal); var datesCopy, parentGuid = scope.ticket ? scope.ticket.parentGuid : ''; if (scope.ticket) { makeCopy(); adjustTimezone(); } scope.$watch('ticketOriginal', function (value) { scope.ticket = _.cloneDeep(scope.ticketOriginal); if (scope.ticket) { adjustTimezone(); } }); var state = { pendingTicketDatesChange: false }; scope.state = state; scope.validator = editTicketDatesService; scope.datePickerOptions = { startingDay: configurationModel.getWeekStartingDay(), 'show-weeks': false, 'min-date': moment().year(1970).month(0).date(2), 'max-date': moment().year(2038).month(0).date(18) }; scope.showMeridian = window.showMeridian; scope.$watch('ticket.timing', function () { if (scope.ticket && scope.ticket.timing) { scope.ticket.scheduledStartDate = scope.isFieldDisabled('scheduledStartDate') ? null : scope.ticket.scheduledStartDate; scope.ticket.scheduledEndDate = scope.isFieldDisabled('scheduledEndDate') ? null : scope.ticket.scheduledEndDate; } }); /** * Collect changes * * @return {Object} change set */ function collectChanges() { if (state.pendingTicketDatesChange) { return { scheduledStartDate: getTimestamp(scope.ticket.scheduledStartDate), scheduledEndDate: getTimestamp(scope.ticket.scheduledEndDate), actualStartDate: getTimestamp(scope.ticket.actualStartDate), actualEndDate: getTimestamp(scope.ticket.actualEndDate) }; } else { return {}; } } function makeCopy() { datesCopy = _.clone({ scheduledStartDate: scope.ticket.scheduledStartDate, scheduledEndDate: scope.ticket.scheduledEndDate, actualStartDate: scope.ticket.actualStartDate, actualEndDate: scope.ticket.actualEndDate }); } function adjustTimezone() { if ($rootScope.timezone) { scope.ticket.scheduledStartDate = utilityFunctions.convertToLocalTimezone(scope.ticket.scheduledStartDate, $rootScope.timezone); scope.ticket.scheduledEndDate = utilityFunctions.convertToLocalTimezone(scope.ticket.scheduledEndDate, $rootScope.timezone); scope.ticket.actualStartDate = utilityFunctions.convertToLocalTimezone(scope.ticket.actualStartDate, $rootScope.timezone); scope.ticket.actualEndDate = utilityFunctions.convertToLocalTimezone(scope.ticket.actualEndDate, $rootScope.timezone); } } /** * Handle save changes */ scope.save = function () { var changes = collectChanges(); var isDraft = scope.isDraft; var singleMode = !scope.updateIsHandledByParent; scope.$emit(events.SAVE_CHANGES_REQUEST, changes, singleMode || isDraft); if (_.size(changes)) { if (isDraft) { closeEditor(); } else { if (singleMode) { updateTicketDates(changes) .then(function () { closeEditor(); }); } } } else { closeEditor(); } }; scope.cancel = function () { scope.ticket.scheduledStartDate = datesCopy.scheduledStartDate; scope.ticket.scheduledEndDate = datesCopy.scheduledEndDate; scope.ticket.actualStartDate = datesCopy.actualStartDate; scope.ticket.actualEndDate = datesCopy.actualEndDate; scope.updateDateTime('actual'); scope.updateDateTime('scheduled'); closeEditor(); }; /** * Handle user input * * @param {String} type - date type */ scope.updateDateTime = function (type) { state.pendingTicketDatesChange = true; editTicketDatesService.updateDateTime(scope.dateForm, scope.ticket, type); }; scope.isFieldRequired = function (field) { return scope.ticket && fieldValidationModel.isFieldRequired(scope.ticket.type, scope.ticket.status.value, scope.ticket.timing, field); }; scope.isFieldDisabled = function (field) { return scope.ticket && fieldValidationModel.isFieldDisabled(scope.ticket.type, scope.ticket.status.value, scope.ticket.timing, field); }; /** * Update ticket dates * * @param {Object} changes * @returns {*} */ function updateTicketDates(changes) { return ticketModel.update(scope.ticket.id, scope.ticket.type, changes); } function closeEditor() { if (!scope.updateIsHandledByParent || scope.isDraft) { scope.$emit(events.SAVE_CHANGES_COMPLETE); } clearPendingChangeFlag(); } function clearPendingChangeFlag() { state.pendingTicketDatesChange = false; } /** * Handle save all changes event * @param event * @param eventData */ function handleSaveAllChangesComplete() { if (!scope.ticket.parentGuid) { scope.ticket.parentGuid = parentGuid; } clearPendingChangeFlag(); } function handleToggleEditMode() { makeCopy(); } function handleSaveChanges() { console.log('handleSaveChanges in editTicketDates directive'); scope.save(); } function handleDiscardChanges() { scope.cancel(); } function getTimestamp(date, time) { date = $rootScope.timezone ? utilityFunctions.convertToUserPreferenceTimezone(date, $rootScope.timezone) : date; time = time ? moment(time) : moment(date); return moment(date).set('hours', time.get('hours')).set('minutes', time.get('minutes')).format('X') * 1000; } scope.$on(events.SAVE_CHANGES, handleSaveChanges); scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode); scope.$on(events.DISCARD_CHANGES, handleDiscardChanges); scope.$on(events.SAVE_ALL_CHANGES_COMPLETE, handleSaveAllChangesComplete); } }; }]); }());