179 lines
9.3 KiB
JavaScript
179 lines
9.3 KiB
JavaScript
"use strict";
|
|
angular.module('resourceModule')
|
|
.directive('rsDuplicateKaSearch', ['metadataModel', '$q', 'knowledgeArticleModel', 'categoriesService', 'roles', 'events', 'relationModel',
|
|
function (metadataModel, $q, knowledgeArticleModel, categoriesService, roles, events, relationModel) {
|
|
return {
|
|
require: '^rs',
|
|
restrict: 'E',
|
|
templateUrl: 'views/resource/rs-duplicate-ka-search.html',
|
|
link: function (scope, element, attrs, rsController) {
|
|
angular.extend(scope, rsController);
|
|
var context = scope.context, state = {
|
|
processing: false,
|
|
searching: false,
|
|
searchNotFound: false,
|
|
optionIsCollapsed: true,
|
|
watcherOn: false
|
|
}, similarItems;
|
|
scope.articlePlaceholder = {
|
|
type: EntityVO.TYPE_KNOWLEDGE,
|
|
allCategories: [],
|
|
customer: {},
|
|
summary: ''
|
|
};
|
|
scope.articlePlaceholder.customer.company = context.customer;
|
|
if (!relationModel.cache[context.id]) {
|
|
relationModel.cache[context.id] = [];
|
|
}
|
|
scope.contextRelations = relationModel.cache[context.id];
|
|
scope.toggleOptions = function () {
|
|
state.optionIsCollapsed = !state.optionIsCollapsed;
|
|
};
|
|
scope.setSortOption = function (option) {
|
|
scope.selectedSortOption = option;
|
|
scope.searchArticle();
|
|
};
|
|
scope.changeStatusOption = function (status) {
|
|
scope.statusOptions = status;
|
|
};
|
|
scope.changeSourceOption = function (source) {
|
|
scope.sourceOptions = source;
|
|
};
|
|
scope.searchArticle = function (fromContext) {
|
|
if (state.searching) {
|
|
return;
|
|
}
|
|
state.searching = true;
|
|
scope.searchResults = [];
|
|
state.searchNotFound = false;
|
|
var searchData = {
|
|
search_text: scope.articlePlaceholder.summary,
|
|
suggest_search: true
|
|
}, searchOptions = {}, categoryList = scope.articlePlaceholder.allCategories;
|
|
if (fromContext) {
|
|
searchData.search_text = context.title + (context.desc ? ' ' + context.desc : '');
|
|
}
|
|
if (scope.statusOptions) {
|
|
searchOptions.statuses = [{ value: scope.statusOptions.name }];
|
|
}
|
|
if (scope.sourceOptions) {
|
|
searchOptions.source = scope.sourceOptions.name;
|
|
}
|
|
searchOptions.category = [];
|
|
_.each(categoryList, function (category) {
|
|
if (category.listOfTiers[0] && category.listOfTiers[0].selectedValue) {
|
|
var categoryData = {};
|
|
categoryData.name = category.name;
|
|
categoryData.tiers = {};
|
|
_.each(category.listOfTiers, function (tier) {
|
|
categoryData.tiers[tier.name] = tier.selectedValue ? tier.selectedValue : '';
|
|
});
|
|
searchOptions.category.push(categoryData);
|
|
}
|
|
});
|
|
$q.all([knowledgeArticleModel.findArticleByText(searchData, searchOptions, scope.selectedSortOption)]).then(function (response) {
|
|
if (!_.isEmpty(response[0])) {
|
|
similarItems = _.reject(response[0], { id: context.id });
|
|
}
|
|
populateSimilarArticles(scope.contextRelations);
|
|
!state.watcherOn && initWatch();
|
|
}).finally(function () {
|
|
state.searching = false;
|
|
});
|
|
};
|
|
scope.clearSearch = function () {
|
|
scope.articlePlaceholder.summary = '';
|
|
scope.searchResults = [];
|
|
state.searchNotFound = false;
|
|
};
|
|
function populateSimilarArticles(relatedItems) {
|
|
scope.searchResults = [];
|
|
var relatedArticles = _.filter(relatedItems, { type: EntityVO.TYPE_KNOWLEDGE }), duplicateArticles = _.filter(relatedArticles, function (relatedArticle) {
|
|
if (relatedArticle.relationshipType === RelationItemVO.TYPE_DUPLICATEOF
|
|
|| relatedArticle.relationshipType === RelationItemVO.TYPE_RELATEDTO || relatedArticle.relationshipType === RelationItemVO.TYPE_REFERENCES) {
|
|
return true;
|
|
}
|
|
}), similarNotRelatedArticles = _.reject(similarItems, function (similarItem) {
|
|
return _.find(relatedArticles, { id: similarItem.id });
|
|
});
|
|
if (duplicateArticles.length) {
|
|
scope.$emit(events.DUPLICATE_ARTICLE_FOUND, duplicateArticles);
|
|
if (scope.previewItem && scope.compareFullArticle) { //if in preview duplicate article mode
|
|
scope.previewItem = _.find(duplicateArticles, { id: scope.previewItem.id }) || _.find(similarNotRelatedArticles, { id: scope.previewItem.id });
|
|
}
|
|
}
|
|
else {
|
|
scope.$emit(events.DUPLICATE_ARTICLE_FOUND, []);
|
|
}
|
|
scope.searchResults = duplicateArticles.concat(similarNotRelatedArticles);
|
|
state.searchNotFound = !scope.searchResults || !scope.searchResults.length;
|
|
}
|
|
function init() {
|
|
scope.clearSearch();
|
|
scope.state = state;
|
|
scope.itemsLimit = 4;
|
|
scope.sortOptions = [
|
|
{ name: 'mostRelevant', sortFieldName: '', sortFieldOrder: '' },
|
|
{ name: 'mostFavorite', sortFieldName: 'favorite', sortFieldOrder: 'des' },
|
|
{ name: 'mostRecent', sortFieldName: 'modifiedDate', sortFieldOrder: 'des' }
|
|
];
|
|
scope.selectedSortOption = scope.sortOptions[0];
|
|
state.processing = true;
|
|
$q.all([metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE), knowledgeArticleModel.getKnowledgeArticleTemplates()])
|
|
.then(function (result) {
|
|
var metaData = result[0];
|
|
var categorizations = [
|
|
{
|
|
name: 'operational',
|
|
tiers: {}
|
|
},
|
|
{
|
|
name: 'product',
|
|
tiers: {}
|
|
}
|
|
];
|
|
scope.availableStatuses = metaData.statuses ? metaData.statuses.slice() : [];
|
|
// remove work in progress status since it is not supported by backend MTF search
|
|
_.remove(scope.availableStatuses, { name: 'Work In Progress' });
|
|
scope.articlePlaceholder.allCategories = _.cloneDeep(categoriesService.populateCategories(categorizations, metaData));
|
|
scope.availableSources = result[1];
|
|
scope.searchArticle(true);
|
|
})
|
|
.finally(function () {
|
|
state.processing = false;
|
|
});
|
|
}
|
|
function initWatch() {
|
|
scope.$watchCollection('contextRelations', function (relatedItems) {
|
|
if (!state.watcherOn) {
|
|
state.watcherOn = true;
|
|
return; //Code below shouldn't run when initializing the watch
|
|
}
|
|
populateSimilarArticles(relatedItems);
|
|
}, true);
|
|
}
|
|
scope.toggleDuplicateMode = function () {
|
|
scope.$emit(events.TOGGLE_CHECK_DUPLICATE_MODE, false);
|
|
};
|
|
scope.viewFullArticle = function (resourceItem, display) {
|
|
scope.compareFullArticle = display;
|
|
if (display) {
|
|
scope.previewItem = resourceItem;
|
|
scope.articleId = resourceItem.id;
|
|
}
|
|
};
|
|
scope.$on(events.ASSESSMENT_REMOVE_DUPLICATES, function () {
|
|
var resourceItems = [];
|
|
_.forEach(scope.searchResults, function (item) {
|
|
if (item instanceof RelationItemVO && item.relationshipType === RelationItemVO.TYPE_DUPLICATEOF) {
|
|
resourceItems.push(item);
|
|
}
|
|
});
|
|
scope.deleteMultipleFromTicket(resourceItems);
|
|
});
|
|
init();
|
|
}
|
|
};
|
|
}
|
|
]);
|