SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/custom-widgets/email-field-directive.js

56 lines
2.7 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('customWidgetsModule').directive('emailField', ['customFieldLinkFunction', 'events', 'objectValueMapperService',
function (customFieldLinkFunction, events, objectValueMapperService) {
return {
restrict: 'E',
replace: true,
scope: {
data: '=',
isEditable: '=',
ticket: '='
},
templateUrl: 'views/custom-widgets/email-field.html',
link: function (scope) {
customFieldLinkFunction(scope);
var ootbKey = scope.data.members.length ? scope.data.members[0].name : null;
if (angular.isDefined(scope.data.value)) {
scope.data.ootbValue = scope.data.value[ootbKey];
}
scope.$watch('data.setValueFlag', function (value) {
if (value && value !== '#$#') {
if (_.isObject(value) && value.hasOwnProperty(ootbKey)) {
scope.data.ootbValue = value[ootbKey];
}
else {
scope.data.ootbValue = value;
}
scope.onFieldValueChange();
scope.data.setValueFlag = '#$#';
}
});
scope.getEmailSubject = function () {
return encodeURIComponent(scope.ticket.displayId + ' : ' + scope.ticket.summary);
};
scope.onFieldValueChange = function () {
var emailField = objectValueMapperService.getFieldByName(scope.data.name);
emailField.value[ootbKey] = scope.data.ootbValue;
var members = [];
if (scope.data.members) {
_.forEach(scope.data.members, function (member) {
members.push(member.name);
});
}
scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: members });
};
scope.$on(events.DISCARD_CHANGES, function () {
scope.data.ootbValue = scope.data.value[ootbKey];
var emailField = objectValueMapperService.getFieldByName(scope.data.name);
emailField.value[ootbKey] = scope.data.ootbValue;
});
}
};
}]);
}());