"use strict"; /** * Created by viktor.shevchenko on 7/15/15. */ (function () { 'use strict'; angular.module('resourceModule') .directive('rsResourcePreview', ['events', 'relationModel', 'objectValueMapperService', '$sce', function (events, relationModel, objectValueMapperService, $sce) { 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) { var invalidKAStatusForPreview = ["Cancelled", "Retire Approval", "Retired", "Closed Version"]; scope.saveable = angular.isDefined(attr.saveable) ? scope.$parent.$eval(attr.saveable) : false; scope.editMode = false; scope.isTitleMultiline = false; if (scope.context.id === 'eschat') { scope.openKALinkInNewTab = true; } var directiveLookupTable = { knowledge: 'preview-knowledge-article' }, container = element.parents('.app__content-frame'), 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 () { var isPathChanged = scope.context.isDraft ? false : true; scope.closePreview(isPathChanged); }); 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.getPreviewSource = function () { function checkMidtierUrl() { var url = localStorage.getItem('midtierUrl'); var lastSlashIndex = url.lastIndexOf('/'); if (lastSlashIndex === url.length - 1) { url = url.substr(0, lastSlashIndex); } if (url.search('/arsys') > -1) { return url; } return url + '/arsys'; } var midtierUrl = localStorage.getItem('midtierUrl') ? checkMidtierUrl() : null, path = 'goto', formName = '', context; var formView = 'SV_View'; var homeServer = localStorage.getItem('homeServer'); var origin = window.location.origin; var guid = scope.resourcePreviewItem.id; if (scope.resourcePreviewItem.type === EntityVO.TYPE_INCIDENT) { formName = 'SHR:SV_TicketDisplay/SV_TicketDisplay'; context = 'Incident'; } else if (scope.resourcePreviewItem.type === EntityVO.TYPE_WORKORDER) { formName = 'SHR:SV_TicketDisplay/SV_TicketDisplay'; context = 'Work Order'; } else if (scope.resourcePreviewItem.type === EntityVO.TYPE_PROBLEM) { formName = 'SHR:SV_TicketDisplay/SV_TicketDisplay'; context = 'Problem Investigation'; } else if (scope.resourcePreviewItem.type === EntityVO.TYPE_KNOWNERROR) { formName = 'SHR:SV_TicketDisplay/SV_TicketDisplay'; context = 'Known Error'; } if (homeServer) { path = 'forms/' + homeServer; } var finalUrl = midtierUrl + '/pwa/#/' + path + '/' + formName + '?origin=' + origin + '&mode=GET&context=PREVIEW'; if (context) { finalUrl += '&targetForm=' + context + '&targetView=' + formView + '&targetId=' + guid; } else { finalUrl += '&guid=' + guid; } return $sce.trustAsResourceUrl(finalUrl); }; scope.closePreview = function (isPathChanged) { if (!objectValueMapperService.isShelvedParentEmpty() && !isPathChanged) { 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(); disableOptionsForKAPreview(resourcePreviewItem); 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 disableOptionsForKAPreview(resourcePreviewItem) { scope.showKAOptions = true; if (resourcePreviewItem.type === EntityVO.TYPE_KNOWLEDGE) { var kaStatus = resourcePreviewItem.additionalInformation && resourcePreviewItem.additionalInformation.status && (_.isObject(resourcePreviewItem.additionalInformation.status) ? resourcePreviewItem.additionalInformation.status.value : resourcePreviewItem.additionalInformation.status.value); if (!kaStatus && resourcePreviewItem.status) { kaStatus = resourcePreviewItem.status; } if (invalidKAStatusForPreview.indexOf(kaStatus) !== -1) { scope.showKAOptions = false; } } } 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.STATUS_RESOLVED) || (scope.context.status.value.toLowerCase() === EntityVO.STATUS_CLOSED)) { 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); profileSublayerClickHandler(); //clear all the shelving object if user moves to another screen }); scope.$on(events.CLOSE_TICKET_PREVIEW, function () { scope.closePreview(); scope.$apply(); }); } }; } ]); }());