422 lines
21 KiB
JavaScript
422 lines
21 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('knowledgeArticleModule')
|
|
.controller('KnowledgeArticleController', ['$scope', 'knowledgeArticleModel', '$stateParams', '$state', 'followService', 'favoriteService', 'ticketActionService', 'events',
|
|
'metadataModel', 'categoriesService', '$q', '$interval', '$filter', 'systemAlertService', '$modal', '$timeout', 'i18nService', 'configurationModel',
|
|
'knowledgeArticleService', '$window',
|
|
function ($scope, knowledgeArticleModel, $stateParams, $state, followService, favoriteService, ticketActionService, events, metadataModel, categoriesService, $q, $interval, $filter, systemAlertService, $modal, $timeout, i18nService, configurationModel, knowledgeArticleService, $window) {
|
|
$scope.defaultArticle = $stateParams.articleId;
|
|
$scope.rxDecisionTreeConfig = { decisionTree: {} };
|
|
$scope.isFullVersion = true;
|
|
$scope.editMode = false;
|
|
$scope.isUnflagEditAllowed = false;
|
|
$scope.article = {};
|
|
$scope.dropDownOptions = {};
|
|
$scope.errorMessage = $filter('i18n')('ka.notFound');
|
|
$scope.isTitleMultiline = false;
|
|
$scope.titleMaxLength = 4000;
|
|
$scope.titleRegExp = new RegExp(/.{1,5}/g);
|
|
var state = {
|
|
dataIsLoading: false,
|
|
currentRevision: null,
|
|
gettingEditStatus: false,
|
|
updatingFollowingFlag: false,
|
|
updatingFavoriteFlag: false
|
|
}, editState = {
|
|
isMinorEdit: true,
|
|
autoSavedContent: [],
|
|
autoSavingInterval: null,
|
|
styles: []
|
|
};
|
|
$scope.viewFullArticle = function (e) {
|
|
e.preventDefault();
|
|
if ($scope.$parent.$parent.newTab) {
|
|
var url = $state.href('knowledge', {
|
|
id: $scope.article.id,
|
|
preventIncrement: true
|
|
});
|
|
$window.open(url, '_blank');
|
|
}
|
|
else {
|
|
$state.go('knowledge', {
|
|
id: $scope.article.id,
|
|
preventIncrement: true
|
|
});
|
|
}
|
|
};
|
|
$scope.getArticle = function (articleId, ignoreCache, preventIncrement) {
|
|
if (!articleId) {
|
|
return;
|
|
}
|
|
if ($scope.getLatestVersion) {
|
|
state.dataIsLoading = true;
|
|
getLatestKAVersionById();
|
|
return;
|
|
}
|
|
var kaPromise = knowledgeArticleModel.getKnowledgeArticleById(articleId, ignoreCache, preventIncrement), revisionPromise = knowledgeArticleModel.getArticleRevisionsById(articleId, true), autoSavePromise = _.isEmpty(editState.autoSavedContent) ? knowledgeArticleModel.getAutoSavedCopy(articleId) : $q.when(editState.autoSavedContent), metaDataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE), globalMetadataPromise = metadataModel.getMetadataByType(EntityVO.TYPE_GLOBAL);
|
|
state.dataIsLoading = true;
|
|
$q.all([kaPromise, revisionPromise, autoSavePromise, metaDataPromise, globalMetadataPromise]).then(function (result) {
|
|
var metadata = result[3], globalMetadata = result[4];
|
|
$scope.article = result[0];
|
|
$scope.article.revisions = result[1];
|
|
editState.autoSavedContent = result[2];
|
|
$scope.knowledgeAnchorParser = globalMetadata.configurationParameters.knowledgeAnchorParser;
|
|
state.currentRevision = _.find($scope.article.revisions, { id: $scope.article.id });
|
|
$scope.article.categoriesControls = _.cloneDeep(categoriesService.populateCategories($scope.article.defaultCategorization, metadata));
|
|
$scope.article.categoriesSet = _.cloneDeep(categoriesService.populateMultipleCategories($scope.article.allCategories, metadata));
|
|
if ($state.params.editMode) {
|
|
return edit();
|
|
}
|
|
if ($scope.article.perfAssessment) {
|
|
configureMoreActions();
|
|
}
|
|
if ($scope.article.isDecisionTree()) {
|
|
$scope.rxDecisionTreeConfig.decisionTree = $scope.article.decisionTree;
|
|
}
|
|
showAlerts();
|
|
if ($scope.isFullVersion) {
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_LOADED, { article: $scope.article });
|
|
}
|
|
else {
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_PREVIEW_LOADED, { article: $scope.article });
|
|
}
|
|
})
|
|
.catch(function (error) {
|
|
if (error && error.data && error.data.defaultMessage) {
|
|
$scope.errorMessage = error.data.defaultMessage;
|
|
}
|
|
else {
|
|
$scope.errorMessage = $filter('i18n')('ka.notFound');
|
|
}
|
|
})
|
|
.finally(function () {
|
|
state.dataIsLoading = false;
|
|
});
|
|
};
|
|
$scope.refreshTicket = function () {
|
|
$scope.getArticle($scope.article.id, true, true);
|
|
$scope.$emit(events.REFRESH_TICKET, { isRefreshTicket: true });
|
|
};
|
|
$scope.openArticleRevision = function (revision) {
|
|
$state.go('knowledge', { id: revision.id, editMode: false });
|
|
};
|
|
$scope.share = function ($event) {
|
|
ticketActionService.showShareDialog($scope.article).result.finally(function () {
|
|
$event.currentTarget.focus();
|
|
});
|
|
};
|
|
$scope.toggleFollowingFlag = function () {
|
|
state.updatingFollowingFlag = true;
|
|
var toggleFollowingFunction = $scope.article.following ? followService.unfollow : followService.follow;
|
|
toggleFollowingFunction($scope.article)
|
|
.then(function () {
|
|
$scope.article.following = !$scope.article.following;
|
|
})
|
|
.finally(function () {
|
|
state.updatingFollowingFlag = false;
|
|
});
|
|
};
|
|
$scope.toggleFavoriteFlag = function () {
|
|
state.updatingFavoriteFlag = true;
|
|
var toggleFavoriteFunction = $scope.article.favorite ? favoriteService.removeFavorite : favoriteService.addFavorite;
|
|
toggleFavoriteFunction($scope.article)
|
|
.then(function () {
|
|
$scope.article.favorite = !$scope.article.favorite;
|
|
})
|
|
.finally(function () {
|
|
state.updatingFavoriteFlag = false;
|
|
});
|
|
};
|
|
$scope.handleEditClick = function () {
|
|
if (!$scope.article.accessMappings.detailsEditAllowed) {
|
|
systemAlertService.warning({
|
|
text: $filter('i18n')('knowledge.edit.reject.no.permission'),
|
|
clear: true,
|
|
hide: 10000
|
|
});
|
|
return false;
|
|
}
|
|
if ($scope.article.isLastVersion || $scope.article.status.value !== 'Published') {
|
|
if (editState.autoSavedContent.length) {
|
|
showAutoSaveModal().result.then(edit);
|
|
}
|
|
else {
|
|
edit();
|
|
}
|
|
}
|
|
else {
|
|
systemAlertService.warning({
|
|
title: $filter('i18n')('knowledge.edit.reject.modal.title'),
|
|
text: $filter('i18n')('knowledge.edit.reject.modal.draft.text'),
|
|
hide: 10000
|
|
});
|
|
}
|
|
};
|
|
$scope.moreDropDownActionsCallbackFuncns = {
|
|
'startAssessment': function () {
|
|
$scope.setAssessFromFullArticle(true);
|
|
$scope.setKcsAssessMode(true);
|
|
}
|
|
};
|
|
function configureMoreActions() {
|
|
var moreDropDownActionItems = configurationModel.get('moreActions.knowledge');
|
|
$scope.dropDownOptions = {
|
|
'actions': moreDropDownActionItems,
|
|
'registeredCallbacks': $scope.moreDropDownActionsCallbackFuncns
|
|
};
|
|
}
|
|
function showAlerts() {
|
|
$scope.alertDetails = {
|
|
alertItems: []
|
|
};
|
|
var alertHeading = [];
|
|
if ($scope.article.flagged || ($scope.isFullVersion && $scope.article.isInApproval)) {
|
|
$scope.hasAlerts = true;
|
|
if ($scope.isFullVersion && $scope.article.isInApproval) {
|
|
$scope.alertDetails.alertItems.push("approval_banner");
|
|
alertHeading.push(i18nService.getLocalizedString('alert.heading.approvalBanner'));
|
|
}
|
|
if ($scope.article.flagged) {
|
|
$scope.alertDetails.alertItems.push("flagged_article");
|
|
alertHeading.push(i18nService.getLocalizedString('alert.heading.flaggedArticle'));
|
|
}
|
|
}
|
|
else {
|
|
$scope.hasAlerts = false;
|
|
}
|
|
$scope.alertDetails.alertHeading = alertHeading.join(', ');
|
|
}
|
|
function showAutoSaveModal() {
|
|
var autoSavedDate = editState.autoSavedContent[0].modifiedDate;
|
|
return systemAlertService.modal({
|
|
type: 'info',
|
|
title: $filter('i18n')('knowledge.autoSaveCopy.modal.title'),
|
|
text: $filter('i18n')('knowledge.autoSaveCopy.modal.text', [$filter('date')(autoSavedDate, 'shortTime'), $filter('date')(autoSavedDate, 'MMMM d')]),
|
|
buttons: [
|
|
{
|
|
text: $filter('i18n')('knowledge.autoSaveCopy.modal.continue'),
|
|
data: true
|
|
},
|
|
{
|
|
text: $filter('i18n')('knowledge.autoSaveCopy.modal.discard'),
|
|
data: false
|
|
}
|
|
]
|
|
});
|
|
}
|
|
function edit(useCopy) {
|
|
var editStatusPromise = knowledgeArticleModel.checkEditStatus($scope.articleId), templatesPromise = knowledgeArticleModel.getKnowledgeArticleTemplates();
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_GETTING_EDIT_STATUS, $scope.state.gettingEditStatus = true);
|
|
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
|
|
return $q.all([editStatusPromise, templatesPromise]).then(function (result) {
|
|
var editStatus = result[0], templates = result[1];
|
|
editState.styles = _.find(templates, { name: $scope.article.templateName }).templateObject.styles;
|
|
if (!editStatus.inEdit) {
|
|
switchToEdit(useCopy);
|
|
}
|
|
else {
|
|
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
|
|
systemAlertService.info({
|
|
title: $filter('i18n')('knowledge.edit.reject.modal.title'),
|
|
text: $filter('i18n')('knowledge.edit.reject.modal.text', editStatus.user.firstName + ' ' + editStatus.user.lastName),
|
|
hide: 10000
|
|
});
|
|
}
|
|
}).catch(function () {
|
|
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
|
|
}).finally(function () {
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_GETTING_EDIT_STATUS, $scope.state.gettingEditStatus = false);
|
|
if ($scope.isFullVersion) {
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_LOADED, { article: $scope.article, isEditMode: true });
|
|
}
|
|
else {
|
|
$scope.$emit(events.KNOWLEDGE_ARTICLE_PREVIEW_LOADED, { article: $scope.article });
|
|
}
|
|
});
|
|
}
|
|
$scope.editExistingDraft = function () {
|
|
$state.go('knowledge', { id: _.last($scope.article.revisions).id, editMode: true });
|
|
};
|
|
function switchToEdit(useCopy) {
|
|
$scope.editMode = true;
|
|
$scope.editArticle = _.cloneDeep($scope.article);
|
|
useCopy && _.merge($scope.editArticle.content, editState.autoSavedContent);
|
|
$state.params.content && angular.extend($scope.editArticle.content, $state.params.content);
|
|
autoSave();
|
|
editState.autoSavingInterval = $interval(autoSave, 60000);
|
|
}
|
|
function autoSave() {
|
|
/*knowledgeArticleModel.isContentDifferent($scope.editArticle.content, editState.autoSavedContent) && */
|
|
knowledgeArticleModel.autoSave($scope.editArticle).then(function (result) {
|
|
editState.autoSavedContent = result;
|
|
});
|
|
}
|
|
$scope.cancelEditing = function () {
|
|
$interval.cancel(editState.autoSavingInterval);
|
|
var cancelEditPromise = editState.autoSavedContent.length ? confirmAutoSavedCopyDelete() : $q.when(1);
|
|
cancelEditPromise.finally(function () {
|
|
$scope.editMode = false;
|
|
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
|
|
});
|
|
};
|
|
function confirmAutoSavedCopyDelete() {
|
|
return systemAlertService.modal({
|
|
type: 'info',
|
|
title: $filter('i18n')('knowledge.deleteAutoSaveCopy.modal.title'),
|
|
text: $filter('i18n')('knowledge.deleteAutoSaveCopy.modal.text'),
|
|
buttons: [
|
|
{
|
|
text: $filter('i18n')('common.button.yes'),
|
|
data: true
|
|
},
|
|
{
|
|
text: $filter('i18n')('common.button.no'),
|
|
data: false
|
|
}
|
|
]
|
|
}).result.then(function (data) {
|
|
if (data) {
|
|
autoSave();
|
|
}
|
|
else {
|
|
removeSavedCopy();
|
|
}
|
|
});
|
|
}
|
|
function confirmUpdatePreviousCancelledVersion() {
|
|
return systemAlertService.modal({
|
|
type: 'info',
|
|
title: $filter('i18n')('knowledge.deleteAutoSaveCopy.modal.title'),
|
|
text: $filter('i18n')('knowledge.updateCancelledVersion.modal.text'),
|
|
buttons: [
|
|
{
|
|
text: $filter('i18n')('common.button.yes'),
|
|
data: true
|
|
},
|
|
{
|
|
text: $filter('i18n')('common.button.no'),
|
|
data: false
|
|
}
|
|
]
|
|
}).result.then(function (data) {
|
|
if (data) {
|
|
updateArtlcle();
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
function removeSavedCopy() {
|
|
knowledgeArticleModel.removeAutoSavedCopy($scope.article.uuid);
|
|
editState.autoSavedContent.length = 0;
|
|
}
|
|
$scope.update = function () {
|
|
$interval.cancel(editState.autoSavingInterval);
|
|
editState.autoSavedContent.length && removeSavedCopy();
|
|
$scope.editArticle.title = $scope.editArticle.title.replace(/\u00a0/g, ' ');
|
|
if (editState.isMinorEdit === false && $scope.editArticle.revisions[$scope.editArticle.revisions.length - 1].status === 'Cancelled') {
|
|
confirmUpdatePreviousCancelledVersion();
|
|
}
|
|
else {
|
|
setTimeout(updateArtlcle(), 200);
|
|
}
|
|
};
|
|
function updateArtlcle() {
|
|
state.dataIsLoading = true;
|
|
knowledgeArticleModel.update($scope.editArticle, editState.isMinorEdit, $scope.article.revisions).then(function (article) {
|
|
$scope.editMode = false;
|
|
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
|
|
if (!editState.isMinorEdit) {
|
|
$state.go('knowledge', { id: article.uuid, editMode: false });
|
|
}
|
|
else if (article.content) { //Need to reload content as JSOUP filter applied by backend on the content
|
|
$scope.article.content = article.content;
|
|
}
|
|
}).catch(function (error) {
|
|
systemAlertService.error({
|
|
text: (error.data.defaultMessage) || (error.data.errorCode ? $filter('i18n')('error.unknown') : error.data.error)
|
|
});
|
|
}).finally(function () {
|
|
state.dataIsLoading = false;
|
|
$scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, { eventName: events.REFRESH_ACTIVITY_FEED });
|
|
});
|
|
}
|
|
$scope.editStatus = function ($event) {
|
|
ticketActionService.showEditStatusDialog($scope.article, false, false).result.then(function (statusData) {
|
|
$event.currentTarget.focus();
|
|
return knowledgeArticleModel.refreshStatus($scope.article.id, statusData);
|
|
}).then(function () {
|
|
$scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, { eventName: events.REFRESH_ACTIVITY_FEED });
|
|
$scope.getArticle($scope.articleId, true, true);
|
|
}).catch(function () {
|
|
$event.currentTarget.focus();
|
|
});
|
|
};
|
|
$scope.state = state;
|
|
$scope.editState = editState;
|
|
$scope.$on('$destroy', function () {
|
|
$interval.cancel(editState.autoSavingInterval);
|
|
});
|
|
$scope.$on(events.REFRESH_KNOWLEDGE_ARTICLE, function () {
|
|
$scope.getArticle($scope.articleId, true, true);
|
|
});
|
|
$scope.$on(events.APPROVE_UPDATING_COMPLETE, function () {
|
|
$scope.getArticle($scope.articleId, true, true);
|
|
});
|
|
$scope.getArticle($scope.articleId, false, $scope.preventIncrement);
|
|
$scope.showPrintDialog = function ($event) {
|
|
var articlePrintDialog = $modal.open({
|
|
templateUrl: 'views/common/print-action-blade.html',
|
|
controller: 'PrintController',
|
|
windowClass: 'action-blade',
|
|
size: 'extra-lg',
|
|
resolve: {
|
|
params: function () {
|
|
return {
|
|
entity: $scope.article,
|
|
feed: $scope.article.feed,
|
|
knowledgeAnchorParser: $scope.knowledgeAnchorParser
|
|
};
|
|
}
|
|
}
|
|
});
|
|
articlePrintDialog.result.finally(function () {
|
|
$event.currentTarget.focus();
|
|
});
|
|
};
|
|
$scope.setFlag = function (flag) {
|
|
if (($scope.article.accessMappings.flagEditAllowed && flag) || ($scope.article.accessMappings.unflagEditAllowed && !flag)) {
|
|
$scope.$emit(events.SELECT_ACTIVITY_TAB);
|
|
$timeout(function () {
|
|
$scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, {
|
|
eventName: events.ADD_FLAG_NOTE,
|
|
eventData: { flag: flag }
|
|
});
|
|
}, 0);
|
|
}
|
|
};
|
|
function getLatestKAVersionById() {
|
|
var articleId = $scope.articleId;
|
|
knowledgeArticleService.getLatestVersionId($scope.articleId).then(function (data) {
|
|
if (data && data.id) {
|
|
articleId = data.id;
|
|
}
|
|
$state.go('knowledge', {
|
|
id: articleId
|
|
}, { location: 'replace' });
|
|
});
|
|
}
|
|
function showTicketLoader() {
|
|
$scope.state.dataIsLoading = true;
|
|
}
|
|
function hideTicketLoader() {
|
|
$scope.state.dataIsLoading = false;
|
|
}
|
|
$scope.$on(events.TICKET_SHOW_LOADER, showTicketLoader);
|
|
$scope.$on(events.TICKET_HIDE_LOADER, hideTicketLoader);
|
|
}
|
|
]);
|
|
})();
|