SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/custom-widgets/dates-widget-directive.js

194 lines
10 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('customWidgetsModule')
.directive('dateWidget', ['customFieldLinkFunction', 'events', 'editTicketDatesService', '$state', 'tabIds', '$rootScope', 'objectValueMapperService', 'expressionEvaluatorService', 'configurationModel',
function (customFieldLinkFunction, events, editTicketDatesService, $state, tabIds, $rootScope, objectValueMapperService, expressionEvaluatorService, configurationModel) {
return {
restrict: 'E',
replace: true,
scope: {
data: '=',
isEditable: '=',
isDatesPanel: '=',
context: '=',
editMode: "="
},
templateUrl: 'views/custom-widgets/dates-widget.html',
link: function (scope) {
if (scope.isDatesPanel) {
if (scope.data.requiredCondition) {
scope.data.isRequired = expressionEvaluatorService.evaluate(scope.data.requiredCondition);
}
if (scope.data.readOnlyCondition) {
scope.data.isReadOnly = expressionEvaluatorService.evaluate(scope.data.readOnlyCondition);
}
}
scope.tabIds = tabIds;
scope.disabled = false;
scope.datePickerOptions = {
'show-weeks': false,
startingDay: configurationModel.getWeekStartingDay(),
minDate: moment().year(1970).month(0).date(2),
maxDate: moment().year(2038).month(0).date(18)
};
var originaldata = _.cloneDeep(scope.data.value);
scope.targetDate = "targetDate";
customFieldLinkFunction(scope);
if (scope.context.timing && (scope.context.timing === 'Latent'
|| (scope.context.timing.name && scope.context.timing.name === 'Latent'))) {
if (scope.data.name === 'scheduledDates') {
scope.data.isReadOnly = true;
}
if (scope.data.name === 'actualDates') {
scope.data.isRequired = true;
}
}
if (scope.context.earliestStartDate) {
scope.earliestStartDate = new Date(scope.context.earliestStartDate);
}
scope.status = {
openedStart: false,
openedEnd: false,
openedTimeStart: false,
openedTimeEnd: false,
};
scope.openStart = function ($event) {
$event.preventDefault();
$event.stopPropagation();
scope.status.openedStart = true;
scope.status.openedEnd = false;
scope.status.openedTimeStart = false;
scope.status.openedTimeEnd = false;
};
scope.openEnd = function ($event) {
$event.preventDefault();
$event.stopPropagation();
scope.status.openedEnd = true;
scope.status.openedStart = false;
scope.status.openedTimeStart = false;
scope.status.openedTimeEnd = false;
};
scope.editDatesView = function () {
$state.go('changeEditDates', {
id: $state.params.id
});
};
scope.validator = function (name) {
if (name === 'scheduledStartDate') {
return editTicketDatesService.scheduledStartDateDisabled(scope.context, scope.editMode);
}
else if (name === 'scheduledEndDate') {
return editTicketDatesService.scheduledEndDateDisabled(scope.context, scope.editMode);
}
else if (name === 'actualStartDate') {
return editTicketDatesService.actualStartDateDisabled(scope.context, scope.editMode);
}
else if (name === 'actualEndDate') {
return editTicketDatesService.actualEndDateDisabled(scope.context, scope.editMode);
}
else if (name === 'targetDate') {
return scope.context.timing
&& (scope.context.timing === 'Latent');
}
};
scope.onFieldValueChange = function (data, fieldName, identifier) {
_.forEach(data.members, function (member) {
scope.context[member.name] = data.value[member.name];
});
if (identifier === 'start') {
data.initDate = angular.copy(data.value[data.members[0].name]);
}
updateDateFormValidation(data);
//In create screen this is outside layout renderer so we need to broadcast via root scope
$rootScope.$broadcast(events.WIDGET_VALUE_CHANGE, { fieldName: data.name, processed: true, memberName: fieldName });
};
function updateDateFormValidation(data) {
var type;
switch (data.name) {
case 'scheduledDates':
type = 'scheduled';
break;
case 'actualDates':
type = 'actual';
break;
case 'targetDate':
type = 'target';
break;
}
if (type === 'scheduled' || type === 'actual') {
editTicketDatesService.updateDateTime(scope.dateForm, data.value, type);
}
else if (type === 'target') {
if (!scope.dateForm) {
return;
}
editTicketDatesService.updateTargetDateTime(scope.dateForm, scope.context);
}
}
scope.targetDateErrorMessageVisible = function () {
return (scope.editMode || scope.context.useTargetDate) && scope.dates && scope.dates.targetDate && scope.dates.targetDate.$invalid;
};
var unbindRootScopeListener = $rootScope.$on(events.WIDGET_VALUE_CHANGE, function (event, field) {
if (field && field.fieldName === 'status') {
scope.showEarliestDate = objectValueMapperService.getValueByFieldName(field.fieldName) === 'Scheduled For Review';
}
});
scope.$on('$destroy', function () {
unbindRootScopeListener();
});
scope.$on(events.AFTER_SAVED_CHANGES, function () {
originaldata = _.cloneDeep(scope.data.value);
});
scope.$on(events.DISCARD_CHANGES, function () {
scope.data.value = _.cloneDeep(originaldata);
_.forEach(scope.data.members, function (member) {
scope.context[member.name] = scope.data.value[member.name];
});
});
scope.$on(events.CLEAR_CHANGE_DATES, function (event, targetDateChecked) {
if (scope.data.name === 'scheduledDates') {
scope.data.value.scheduledEndDate = null;
scope.data.value.scheduledStartDate = null;
}
else if (scope.data.name === 'actualDates') {
scope.data.value.actualStartDate = null;
scope.data.value.actualEndDate = null;
}
else if (scope.data.name === 'targetDate') {
scope.data.value.targetDate = null;
}
scope.disabled = targetDateChecked;
});
scope.$on(events.UPDATE_SCHEDULE_DATE, function (event, date) {
var modelDate, contextDate;
modelDate = scope.data.value[date.name] ? scope.data.value[date.name].valueOf() : scope.data.value[date.name];
contextDate = date.value ? date.value.valueOf() : date.value;
if (modelDate !== contextDate) {
scope.data.value[date.name] = date.value;
updateDateFormValidation(scope.data);
}
});
scope.$watch('data.setValueFlag', function (value) {
if (value !== '#$#' && value) {
var obj = {}, nonEmptyValuesObj = _.omitBy(value, function (item) {
return _.isEmpty(item) && !_.isNumber(item) || _.isNumber(item) && item < 0;
});
_.forEach(nonEmptyValuesObj, function (n, key) {
obj[key] = new Date(parseInt(n));
});
scope.data.value = obj;
scope.data.setValueFlag = '#$#';
}
});
scope.$watch('data.isHidden', function (newValue, oldValue) {
if (newValue !== oldValue) {
scope.$emit(events.UPDATE_DATE_LABEL_VISIBILITY, { name: scope.data.name, isHidden: newValue });
}
});
}
};
}
]);
})();