SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/knowledge-article/ka-metadata-directive.js

359 lines
21 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('knowledgeArticleModule')
.directive('kaMetadataDirective', function () {
return {
restrict: 'E',
templateUrl: 'views/knowledge-article/knowledge-article-metadata.html',
scope: {
article: '=',
editMode: '='
},
controller: ['$scope', '$rootScope', 'knowledgeArticleModel', 'ticketActionService', 'searchService', 'events', 'metadataModel', 'categoriesService', '$q', '$timeout', 'userModel', 'systemAlertService', '$filter', 'searchModel', 'ticketModel', 'utilityFunctions',
function ($scope, $rootScope, knowledgeArticleModel, ticketActionService, searchService, events, metadataModel, categoriesService, $q, $timeout, userModel, systemAlertService, $filter, searchModel, ticketModel, utilityFunctions) {
var state = {
dataIsLoading: true,
loadSite: false,
loadOrganization: false,
loadService: false,
tooManyCompanies: false,
exceedsChunkSizeService: false,
isTooltipOpenService: false,
tag: '',
site: '',
organization: '',
service: '',
primaryCompany: $scope.article.company,
selectedLanguage: {}
}, metadata, serializationMap = ['allSites', 'allOrganizations', 'allServices', 'categoriesSet'], allCompanies, availiableCompanies;
$scope.loggedInUserId = userModel.userId;
$scope.reviewDate = {
opened: false
};
$scope.currentDate = new Date();
$scope.foundationOptions = {
site: {
company: {
visible: false
},
region: {
connected: 'company'
}
},
organization: {
company: {
visible: false
},
organization: {
connected: 'company'
}
}
};
if ($scope.article.supportGroup) {
$scope.article.assigneeGroup = $scope.article.supportGroup;
}
$q.all([userModel.getUserDataForCurrentSession(), metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE)]).then(function (result) {
metadata = result[1];
$scope.languages = metadata.languages;
//Company handling
return populateCompanies();
}).finally(function () {
state.dataIsLoading = false;
});
$scope.selectLanguage = function (item) {
$scope.article.language = item.name;
};
$scope.assign = function ($event) {
handleAssignment($event);
};
$scope.assignToMe = function ($event, role) {
var assigneeRole = ''; // Removed role for knowledge 'knowledgeassignee';
var assigneeSupportGroup = $scope.article.supportGroup;
var ownerCompany = $scope.article.owner.company ? $scope.article.owner.company.name : '', authorCompany = $scope.article.author.company ? $scope.article.author.company.name : '', primaryCompany = getPrimaryCompany($scope.article);
primaryCompany = primaryCompany ? primaryCompany.name : '';
ticketModel.getTicketAssigneePersonsBySearchText(assigneeRole, null, null, ownerCompany, primaryCompany, authorCompany).then(function (persons) {
persons = _.uniqBy(persons.results, function (person) {
return person.loginId && person.supportGroupId;
});
var person = persons.length === 1 ? persons[0] : _.find(persons, { supportGroupId: assigneeSupportGroup.id });
if (person && person.loginId) {
updateAssignment(person);
}
else {
handleAssignment($event, role);
}
});
};
function getPrimaryCompany(ticket) {
if (ticket.allCompanies) {
return _.find(ticket.allCompanies, function (company) {
return company.primary;
});
}
else {
return ticket.company;
}
}
function handleAssignment($event, role) {
var article = $scope.article, currentAssignment = angular.extend({}, { ghostEntity: true }, article); // ghostEntity: true means that this knowledge doesn't exist yet, even in draft state
currentAssignment.customer = article.author;
ticketActionService.showAssignDialog(currentAssignment, true, role).result.then(function (result) {
if (result.autoAssign) {
article.autoAssign = true;
}
else {
article.autoAssign = false;
if (result.assignee) {
article.assignee = result.assignee;
}
if (result.group) {
article.assigneeGroup = result.group;
}
}
$event.currentTarget.focus();
}, function () {
$event.currentTarget.focus();
});
}
function updateAssignment(person) {
$scope.article.assignee = { id: person.id, loginId: person.loginId, fullName: person.fullName, thumbnailMime: person.thumbnailMime, thumbnail: person.thumbnail, organization: person.organization };
$scope.article.assigneeGroup = { id: person.supportGroupId, name: person.supportGroup, organization: person.organization, company: person.company };
}
$scope.addTag = function (item, array) {
if (!_.find(array, item)) {
array.push(item);
if (item.product && item.product.categorizations && item.product.categorizations[0] && !(_.isEmpty(item.product.categorizations[0].tiers))
&& $scope.article.accessMappings && $scope.article.accessMappings.categorizationEditAllowed) {
$scope.$emit(events.AFFECTED_SERVICE_UPDATED, item);
}
if (array.length === 1 && ((item.company.name === 'All') || (item.company.name === $scope.state.primaryCompany.name))) {
$scope.setPrimaryBusinessService(item);
}
}
else {
console.warn('should not be 2 similar tags');
}
};
$scope.addKeyword = function () {
_.forEach(state.tag.split(','), function (tag) {
if (tag.trim() && _.indexOf($scope.article.tags, tag.trim()) === -1) {
$scope.article.tags.push(tag.trim());
}
});
state.tag = '';
};
$scope.removeTag = function (tag, array) {
_.pull(array, tag);
};
$scope.setPrimaryTag = function (newPrimaryTag, array) {
if ((newPrimaryTag.company && newPrimaryTag.company.name === $scope.state.primaryCompany.name)
|| newPrimaryTag.companyName === $scope.state.primaryCompany.name) {
var oldPrimaryTag = _.find(array, 'primary');
newPrimaryTag.primary = true;
if (oldPrimaryTag) {
oldPrimaryTag.primary = !oldPrimaryTag.primary;
}
}
};
$scope.onServiceFocusBlur = function () {
$scope.state.isTooltipOpenService = false;
};
$scope.setPrimaryBusinessService = function (newPrimaryService) {
_.forEach($scope.article.allServices, function (service) {
service.primary = service.reconciliationId === newPrimaryService.reconciliationId;
});
};
$scope.$on(events.SAVE_CHANGES, function () {
$scope.$emit(events.SAVE_CHANGES_REQUEST, null, true);
$scope.article.allCategories = categoriesService.collectValues($scope.article.categoriesSet, $scope.article);
categoriesService.clearDirtyValues($scope.article.categoriesSet);
if ($rootScope.timezone && $scope.article.nextReviewDate) {
$scope.article.nextReviewDate = utilityFunctions.convertToUserPreferenceTimezone($scope.article.nextReviewDate, $rootScope.timezone);
}
knowledgeArticleModel.updateArticleMetadata($scope.article, $scope.articleCopy).then(function (result) {
angular.extend($scope.article, result);
$scope.$emit(events.TICKET_PROFILE_RESEND_EVENT, { eventName: events.UPDATE_CONTEXT_ON_REFRESH, eventData: $scope.article });
metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE).then(function (metadata) {
$scope.article.categoriesControls = _.cloneDeep(categoriesService.populateCategories($scope.article.defaultCategorization, metadata));
$scope.article.categoriesSet = _.cloneDeep(categoriesService.populateMultipleCategories($scope.article.allCategories, metadata));
});
}).catch(function (error) {
systemAlertService.error({
text: (error.data.defaultMessage) || (error.data.errorCode ? $filter('i18n')('error.unknown') : error.data.error)
});
}).finally(function () {
$scope.$emit(events.SAVE_CHANGES_COMPLETE);
});
});
$scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode);
$scope.$on(events.DISCARD_CHANGES, handleDiscardChanges);
!$scope.article.isDraft && $scope.$on('$destroy', function () {
if ($scope.editMode) {
handleDiscardChanges();
}
});
$scope.state = state;
$scope.searchService = searchService;
$scope.getCompaniesByName = function (name) {
return searchModel.getCompaniesByText(name)
.then(function (response) {
var filteredCompanies;
filteredCompanies = _.filter(response.companies, function (company) {
return !_.find(allCompanies, { name: company.name });
});
return { list: filteredCompanies, exceedsChunkSize: response.exceedsChunkSize };
});
};
$scope.getServicesByText = function (searchText, companyName) {
return searchService.getListObjOfServiceByText(searchText, companyName).then(function (response) {
$scope.state.exceedsChunkSizeService = response.exceedsChunkSize;
$scope.state.isTooltipOpenService = response.exceedsChunkSize;
if ($scope.state.isTooltipOpenService) {
$timeout(function () {
$scope.state.isTooltipOpenService = false;
}, 10000);
}
return response.list;
});
};
$scope.addCompany = function (company) {
allCompanies.push(company);
setAvailiableCompanies();
};
$scope.$on(events.FOUNDATION_ADD, function (event, item) {
if ((item.department || item.organization) && $scope.article.allOrganizations.length === 1) {
$scope.setPrimaryTag(item, $scope.article.allOrganizations);
}
else if (item.siteId && $scope.article.allSites.length === 1) {
$scope.setPrimaryTag(item, $scope.article.allSites);
}
});
$scope.removeCompany = function (company, $event) {
_.remove(allCompanies, { name: company.name });
setAvailiableCompanies();
$event.stopPropagation();
};
$scope.setPrimaryCompany = function (newPrimaryCompany) {
_.forEach(allCompanies, function (company) {
company.primary = company.name === newPrimaryCompany.name;
});
if ($scope.state.primaryCompany.name !== newPrimaryCompany.name) {
$scope.state.primaryCompany = newPrimaryCompany;
resetPrimaryFields();
populatePotentialPrimary();
}
};
$scope.buildSiteTag = function (site) {
var siteGroup = site.siteGroup ? ' > ' + site.siteGroup : '', siteName = site.name ? ' > ' + site.name : '', siteRegion = site.region ? site.region : '';
return siteRegion + siteGroup + siteName;
};
$scope.buildOrganizationTag = function (organization) {
var department = organization.department ? ' > ' + organization.department : '';
return organization.organization + department;
};
function resetPrimaryFields() {
_.forEach(serializationMap, function (field) {
_.forEach($scope.article[field], function (item) {
item.primary = false;
});
});
}
$scope.prepareOrganization = function (organization) {
return { organization: organization.name };
};
$scope.openNextReviewDate = function ($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.reviewDate.opened = true;
};
function handleToggleEditMode() {
populateCompanies();
$scope.articleCopy = _.cloneDeep($scope.article);
if ($rootScope.timezone && $scope.article.nextReviewDate) {
$scope.article.nextReviewDate = utilityFunctions.convertToLocalTimezone($scope.article.nextReviewDate, $rootScope.timezone);
}
state.selectedLanguage = _.find($scope.languages, { name: $scope.article.language });
$scope.$emit(events.TOGGLE_KNOWLEDGE_ARTICLE_EDIT_MODE);
populatePotentialPrimary();
}
function handleDiscardChanges() {
angular.extend($scope.article, $scope.articleCopy);
}
function setAvailiableCompanies() {
$scope.availiableCompanies = _.filter(availiableCompanies, function (company) {
return !_.find(allCompanies, { name: company.name });
});
}
function populateCompanies() {
allCompanies = $scope.article.allCompanies;
$scope.userCompany = userModel.userFullData.company;
return searchModel.getOperatingCompanies(null, -1).then(function (response) {
availiableCompanies = [{ name: 'All' }].concat(_.cloneDeep(response.companies));
state.tooManyCompanies = response.exceedsChunkSize;
setAvailiableCompanies();
});
}
function populatePotentialPrimary() {
if ($scope.article.isDraft) {
return;
}
state.dataIsLoading = true;
var requestObject = {
company: $scope.state.primaryCompany,
site: _.reject($scope.article.allSites, 'companyName'),
organization: _.reject($scope.article.allOrganizations, 'companyName'),
categories: _.reject(categoriesService.collectValues($scope.article.categoriesSet, $scope.article), function (category) {
return category.company && category.company.name === 'All';
})
};
if (!requestObject.site.length && !requestObject.organization.length && !requestObject.categories.length) {
state.dataIsLoading = false;
return;
}
knowledgeArticleModel.getPotentialPrimary(requestObject).then(function (responce) {
var foundationFields = {
site: responce.site,
organization: responce.organization
};
_.forIn(foundationFields, function (metadataItems, key) {
_.forEach(metadataItems, function (metadataItem, index) {
if (metadataItem.isPrimary) {
requestObject[key][index].companyName = requestObject.company.name;
}
});
});
_.forEach(responce.categories, function (category, index) {
if (category.isPrimary) {
$scope.article.categoriesSet[index].company = { name: requestObject.company.name };
}
});
}).finally(function () {
state.dataIsLoading = false;
});
}
var refreshResourceFeed = false;
$scope.$on(events.REFRESH_RESOURCE_FEED, function () {
state.dataIsLoading = true;
refreshResourceFeed = true;
});
$scope.$on(events.REFRESH_KA_META, function (event, data) {
if (refreshResourceFeed) {
refreshResourceFeed = false;
$scope.article = data.article;
$q.all([userModel.getUserDataForCurrentSession(), metadataModel.getMetadataByType(EntityVO.TYPE_KNOWLEDGE)]).then(function (result) {
metadata = result[1];
$scope.languages = metadata.languages;
//Company handling
return populateCompanies();
}).finally(function () {
state.dataIsLoading = false;
});
}
});
}
]
};
});
})();