60 lines
2.6 KiB
JavaScript
60 lines
2.6 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('myitsmApp')
|
|
.directive('dynamicFieldContainer', ['events', 'customFieldAreaLinkFunction', function (events, customFieldAreaLinkFunction) {
|
|
return {
|
|
restrict: 'E',
|
|
replace: true,
|
|
scope: {
|
|
fields: '=',
|
|
ticket: '=',
|
|
panelId: '='
|
|
},
|
|
templateUrl: 'views/field-customization/custom-field-area.html',
|
|
link: function (scope) {
|
|
scope.editMode = true;
|
|
scope.isDynamicArea = true;
|
|
scope.fieldsCountToShow = 10;
|
|
customFieldAreaLinkFunction(scope);
|
|
// Dynamic fields don't have $scope.stacked,
|
|
// so limit logic will never be applied to them. So visibleFields should be calculated only during init
|
|
scope.visibleFields = scope.getFieldsListToShow();
|
|
scope.$on(events.FIELD_VALUE_CHANGE, handleFieldValueChange);
|
|
initFieldValues(scope.fields);
|
|
scope.isEditable = function (field) {
|
|
return field.isEditable(scope.ticket.accessMappings);
|
|
};
|
|
function initFieldValues(fields) {
|
|
fields.forEach(function (field) {
|
|
if (isValid(field.value) && field.value.toString().length) {
|
|
field.setValue(field.value);
|
|
}
|
|
});
|
|
}
|
|
function isValid(value) {
|
|
return angular.isDefined(value) && value !== null;
|
|
}
|
|
/**
|
|
* Handle field value change event
|
|
* @param event
|
|
* @param {FieldVO} field
|
|
*/
|
|
function handleFieldValueChange(event, field) {
|
|
var newValue = field.getValue();
|
|
setTicketFieldValue(field, newValue);
|
|
scope.$emit(events.FIELD_FORM_IS_DIRTY);
|
|
}
|
|
/**
|
|
* Sets ticket's field value
|
|
* @param {FieldVO} field
|
|
* @param {*} value
|
|
*/
|
|
function setTicketFieldValue(field, value) {
|
|
_.find(scope.ticket.dynamicFields, { name: field.name }).value = value;
|
|
}
|
|
}
|
|
};
|
|
}]);
|
|
})();
|