"use strict"; /** * Created by igor.samulenko on 6/20/2014. */ (function () { 'use strict'; angular.module('myitsmApp') .directive('customFieldContainer', ['events', 'customFieldAreaLinkFunction', 'screenConfigurationModel', function (events, customFieldAreaLinkFunction, screenConfigurationModel) { return { restrict: 'E', replace: true, scope: { fields: '=', ticket: '=', stacked: '=' }, templateUrl: 'views/field-customization/custom-field-area.html', link: function (scope) { var fieldWatcherUnregister; scope.editMode = true; scope.fieldsCountToShow = 5; if (scope.ticket.type === EntityVO.TYPE_ASSET) { fieldWatcherUnregister = scope.$watch('fields', function () { scope.visibleFields = scope.getFieldsListToShow(); }); scope.$on('$destroy', function () { fieldWatcherUnregister(); }); } customFieldAreaLinkFunction(scope); scope.visibleFields = scope.getFieldsListToShow(); if (scope.ticket.customFields) { _.forEach(scope.fields, function (field) { field.value = scope.ticket.customFields[field.name]; }); } /** * Handle field value change event * @param event * @param {FieldVO} field */ function handleFieldValueChange(event, field) { setTicketFieldValue(field.name, field.getValue()); field.valueField && setTicketFieldValue(field.valueField, field.getLinkedValue()); scope.$emit(events.FIELD_FORM_IS_DIRTY); } scope.$watch('ticket.customFields', function (newCustomFields) { if (newCustomFields && _.isObject(newCustomFields && scope.fields)) { screenConfigurationModel.initFieldValues(scope.fields, scope.ticket); scope.$broadcast(events.REFRESH_FIELD_VALUES); } }); scope.isEditable = function (field) { return scope.ticket.accessMappings ? field.isEditable(scope.ticket.accessMappings) : !field.isReadOnly; }; scope.$on(events.FIELD_VALUE_CHANGE, handleFieldValueChange); function handleDiscardChanges() { _.forEach(scope.fields, function (field) { field.clearValue(); }); } /** * Sets ticket's field value * @param {String} fieldName * @param {*} value */ function setTicketFieldValue(fieldName, value) { scope.ticket.customFields[fieldName] = value; } scope.$on(events.DISCARD_CHANGES, handleDiscardChanges); } }; }]); })();