"use strict"; /** * Created by viktor.shevchenko on 7/9/15. */ /* * Recommended KA directive loads related KA to a ticket and Similar KA + displays them in a list with the related on top. * */ angular.module('resourceModule') .directive('rsRecommendedKa', ['searchService', 'relationModel', '$q', 'configurationModel', 'events', '$filter', function (searchService, relationModel, $q, configurationModel, events, $filter) { return { require: '^rs', restrict: 'E', scope: {}, templateUrl: 'views/resource/rs-recommended-ka.html', link: function (scope, element, attrs, rsController) { angular.extend(scope, rsController); var context = scope.context, state = { isKnowledgeInstalled: configurationModel.isServerApplicationEnabled(EntityVO.TYPE_KNOWLEDGE), itemLimit: 4, isDataLoading: false, watchersOn: false }, similarItems, searchSimilarItemsParams = { query: { chunk_size: 15, need_totalcount: false }, post: { types: ['knowledge'], search_text: (context.summary || context.title) + (context.desc ? ' ' + context.desc : '') } }; scope.recommendedArticles = []; scope.titleRegExp = new RegExp(/.{1,5}/g); function init() { if (context.type !== EntityVO.TYPE_PROBLEM && context.type !== EntityVO.TYPE_KNOWNERROR) { searchSimilarItemsParams.post.searchCompanies = fetchContextCompany(); searchSimilarItemsParams.post.sites = (context.customer && context.customer.site) ? [context.customer.site] : []; } state.isDataLoading = true; $q.all([relationModel.getRelations(context.id, context.type), searchService.getGlobalSearchResults(searchSimilarItemsParams.query, searchSimilarItemsParams.post)]).then(function (responce) { scope.contextRelations = angular.copy(relationModel.cache[context.id]); if (!_.isEmpty(responce[1].items)) { similarItems = _.reject(responce[1].items[0].results, { id: context.id }); } if (!state.watchersOn) { initWatchers(); } else { populateRecommendedArticles(scope.contextRelations); } }).finally(function () { state.isDataLoading = false; }); } function populateRecommendedArticles(relatedItems) { if (relationModel.unlinked.length && !rsController.isKaSearchEnabled()) { //Case : Article is unlinked and it is recommended articles screen relationModel.unlinked.length = 0; state.isDataLoading = true; searchService.getGlobalSearchResults(searchSimilarItemsParams.query, searchSimilarItemsParams.post).then(function (response) { if (!_.isEmpty(response.items)) { similarItems = _.reject(response.items[0].results, { id: context.id }); } mergeRelatedAndSimilarArticles(relatedItems, similarItems); }).finally(function () { state.isDataLoading = false; }); return; } mergeRelatedAndSimilarArticles(relatedItems, similarItems); } function mergeRelatedAndSimilarArticles(relatedItems, similarItems) { var relatedArticles = _.filter(relatedItems, { type: EntityVO.TYPE_KNOWLEDGE }), similarNotRelatedArticles = _.reject(similarItems, function (similarItem) { return _.find(relatedArticles, { id: similarItem.id }); }), resolvedArticle = _.remove(relatedArticles, { relationshipType: EntityVO.TYPE_RESOLVED }); if (resolvedArticle && resolvedArticle.length) { relatedArticles = resolvedArticle.concat(relatedArticles); } scope.recommendedArticles = relatedArticles.concat(similarNotRelatedArticles); scope.$emit(events.RESOURCES_FOUND, { data: scope.recommendedArticles, type: 'articles' }); } function initWatchers() { state.watchersOn = true; scope.$watchCollection('contextRelations', function (relatedItems) { populateRecommendedArticles(relatedItems); }, true); scope.$watchCollection('context', function (newValue, oldValue) { var oldContextSummary = oldValue.summary || oldValue.title; var newContextSummary = newValue.summary || newValue.title; if (newContextSummary !== oldContextSummary || newValue.desc !== oldValue.desc) { searchSimilarItemsParams.post.search_text = (newValue.summary || newValue.title) + (newValue.desc ? ' ' + newValue.desc : ''); init(); } }); } function fetchContextCompany() { if (context.type === EntityVO.TYPE_KNOWLEDGE) { return [context.company]; } return context.customer ? [context.customer.company] : [{ name: context.company }]; } scope.context = context; scope.state = state; //Knowledge status is not getting populated when using filter in html in case of browser refresh because metadataModel.cache doesn't fetch value at that time. scope.localizedStatus = function (status) { return $filter('localizeLabel')(status, 'status', 'knowledge'); }; init(); scope.$on(events.REFRESH_RESOURCE_FEED, function () { init(); }); scope.$on(events.REFRESH_RECOMMENDED_KNOWLEDGE, function () { scope.contextRelations = angular.copy(relationModel.cache[context.id]); populateRecommendedArticles(scope.contextRelations); }); } }; }]);