"use strict"; (function () { 'use strict'; angular.module('customWidgetsModule').directive('organizationField', ['customFieldLinkFunction', 'events', 'searchModel', 'objectValueMapperService', '$timeout', function (customFieldLinkFunction, events, searchModel, objectValueMapperService, $timeout) { return { restrict: 'E', replace: true, scope: { data: '=', isEditable: '=' }, templateUrl: 'views/custom-widgets/organization-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.getList = function (term) { var customer = objectValueMapperService.getValueByFieldName('customer') || {}, company; if (_.isEmpty(customer)) { company = objectValueMapperService.getValueByFieldName('company'); } else { company = customer.company.name; } if (company) { scope.dataLoading = true; return searchModel.getOrganizationsByTextAndCompany(term, company) .then(function (response) { scope.isTooltipOpenOrg = scope.exceedsChunkSizeOrg = response.exceedsChunkSize; $timeout(function () { scope.isTooltipOpenOrg = false; }, 10000); var organizations = []; if (response.items.length) { _.forEach(response.items, function (org) { organizations.push(org.name); }); } return organizations; }) .finally(function () { scope.dataLoading = false; }); } }; scope.onInputFocusBlur = function () { scope.isTooltipOpenOrg = false; }; scope.$watch('data.setValueFlag', function (value) { if (value && value !== '#$#') { scope.data.ootbValue = value; scope.onFieldValueChange(); scope.data.setValueFlag = '#$#'; } }); scope.onFieldValueChange = function () { var orgField = objectValueMapperService.getFieldByName(scope.data.name); orgField.value[ootbKey] = scope.data.ootbValue; scope.$emit(events.WIDGET_VALUE_CHANGE, { fieldName: scope.data.name, memberName: scope.data.name }); }; scope.clearValue = function () { scope.data.ootbValue = ''; scope.onFieldValueChange(); }; scope.$on(events.DISCARD_CHANGES, function () { scope.data.ootbValue = scope.data.value[ootbKey]; var orgField = objectValueMapperService.getFieldByName(scope.data.name); orgField.value[ootbKey] = scope.data.ootbValue; }); } }; }]); }());