"use strict"; /** * Created by viktor.shevchenko on 7/15/15. */ (function () { 'use strict'; angular.module('resourceModule') .directive('rsResourcePreview', ['events', 'relationModel', 'objectValueMapperService', function (events, relationModel, objectValueMapperService) { return { restrict: 'EA', replace: true, templateUrl: 'views/resource/rs-resource-preview.html', scope: { savedTemplate: '=', saveToTicket: '&', linkAsDuplicateOf: '&', saveAndResolve: '&', deleteFromTicket: '&', removeLinkedItem: '&', context: '=', resourcePreviewItem: '=', hideEditButton: '=' }, link: function (scope, element, attr) { scope.saveable = angular.isDefined(attr.saveable) ? scope.$parent.$eval(attr.saveable) : false; scope.editMode = false; scope.isTitleMultiline = false; var directiveLookupTable = { knowledge: 'preview-knowledge-article' }, container = element.parents('.app__content'), profileSublayer = container.find('.resource-slice__fade-out'), resourcePreviewOpenedClass = 'resource-preview-opened', tabs = container.find('.nav-tabs li'), body = angular.element(document.body); scope.$watch('$location.path', function () { scope.closePreview(); }); function profileSublayerClickHandler() { scope.closePreview(); scope.$apply(); } function tabsClickHandler() { if (element.is(':visible')) { return; } scope.closePreview(); scope.$apply(); } function rsResourcePreviewKeydownHandler() { var escKeyNumber = 27; if (event.which === escKeyNumber) { scope.closePreview(); scope.$apply(); } } profileSublayer.on('click', profileSublayerClickHandler); tabs.on('click', tabsClickHandler); body.on('keydown.rsResourcePreview', rsResourcePreviewKeydownHandler); scope.closePreview = function () { if (!objectValueMapperService.isShelvedParentEmpty()) { objectValueMapperService.unshelveParent(scope.context && scope.context.type); } if (!scope.resourcePreviewItem) { return; } if (scope.resourcePreviewItem.type === EntityVO.TYPE_KNOWLEDGE) { scope.$emit(events.KNOWLEDGE_ARTICLE_GETTING_EDIT_STATUS, false); } scope.resourcePreviewItem = null; }; scope.edit = function (type) { var editDirectiveElement = element.find('div[' + directiveLookupTable[type] + ']'); if (editDirectiveElement) { editDirectiveElement.trigger(events.CHANGE_TO_EDIT_MODE); } }; scope.$on(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE, function () { scope.editMode = !scope.editMode; }); scope.$on(events.TICKET_PROFILE_RESEND_EVENT, function (event, resend) { event.stopPropagation(); scope.$broadcast(resend.eventName, resend.eventData); }); scope.$on(events.KNOWLEDGE_ARTICLE_PREVIEW_LOADED, function (event, data) { scope.articleFlagged = data.article.flagged; scope.currentArticle = data.article; }); scope.$on(events.MULTILINE_CONTENT_CHANGED, function (event, data) { scope.isTitleMultiline = data.isContentMultiline; }); scope.addComment = function () { scope.$broadcast(events.SET_FOCUS_TO_ACTIVITY_INPUT); }; scope.setFlag = function (flag) { if ((scope.currentArticle.accessMappings.flagEditAllowed && flag) || (scope.currentArticle.accessMappings.unflagEditAllowed && !flag)) { scope.$broadcast(events.ADD_FLAG_NOTE, { flag: flag }); } }; scope.$watch('resourcePreviewItem', function (resourcePreviewItem) { scope.editMode = false; if (!resourcePreviewItem) { container.removeClass(resourcePreviewOpenedClass); } else { scope.contextRelations = relationModel.cache[scope.context.id]; setActionDeleteFlag(); setSaveAnsDeleteFlag(); setSaveAndResolveFlag(); container.addClass(resourcePreviewOpenedClass); } }); scope.$watchCollection('contextRelations', function () { setActionDeleteFlag(); }, true); function setActionDeleteFlag() { if (!scope.resourcePreviewItem) { return; } scope.resourcePreviewActionDelete = !!(_.find(scope.contextRelations, { id: scope.resourcePreviewItem.id })); } function setSaveAnsDeleteFlag() { if (scope.context.type === EntityVO.TYPE_KNOWLEDGE) { scope.showSaveAndDelete = false; } else { scope.showSaveAndDelete = scope.context.accessMappings.relationsEditAllowed; } } function setSaveAndResolveFlag() { scope.showSaveAndResolve = true; if (scope.context.type === EntityVO.TYPE_INCIDENT) { if (scope.context.status) { if ((scope.context.status.value.toLowerCase() === EntityVO.TYPE_RESOLVED)) { scope.showSaveAndResolve = false; } } } } element.on('$destroy', function () { profileSublayer.off('click', profileSublayerClickHandler); body.off('keydown.rsResourcePreview'); container.removeClass(resourcePreviewOpenedClass); }); scope.$on('$destroy', function () { profileSublayer.off('click', profileSublayerClickHandler); tabs.off('click', tabsClickHandler); body.off('keydown.rsResourcePreview', rsResourcePreviewKeydownHandler); }); scope.$on(events.CLOSE_TICKET_PREVIEW, function () { scope.closePreview(); scope.$apply(); }); } }; } ]); }());