SmartIT_Extensions/BMC/smart-it-full/scripts/app/create/create-aqi-controller.js

363 lines
16 KiB
JavaScript

"use strict";
/**
* Created by andey on 14-09-2015.
*/
(function () {
'use strict';
angular.module('createTicketModule')
.controller('createAqiController', ['$scope', '$q', 'knowledgeArticleModel', 'metadataModel', 'searchModel', 'createTicketModel', 'i18nService', 'systemAlertService', function ($scope, $q, knowledgeArticleModel, metadataModel, searchModel, createTicketModel, i18nService, systemAlertService) {
$scope.state = {
isInitialPage: true,
isEditPage: false,
isClonePage: false,
dataLoading: false
};
$scope.questionSet = {
questionSetId: "",
name: "",
company: null,
organization: {}
};
$scope.selections = {};
$scope.questionList = {};
$scope.questionSetList = [];
$scope.sortableOptions = {
stop: function () {
// update field ordering
assignSequenceNo($scope.questionList);
}
};
$scope.knowledgeMetadata = {};
function assignSequenceNo(questionList) {
var deletedList = _.remove(questionList, function (question) {
if (question.delete) {
question.sequenceNo = 0;
return true;
}
});
questionList.forEach(function (question, index) {
question.sequenceNo = index + 1;
});
Array.prototype.push.apply(questionList, deletedList);
}
/**
* get supported locales
*
* @returns {Array} locales
*/
function getSupportedLocales() {
var temp = {
default: 'Default',
en: '(en) ',
es: '(es) ',
fr: '(fr) ',
he: '(he) ',
zh_CN: '(zh) ',
it: '(it) ',
ko: '(ko) ',
de: '(de) ',
ru: '(ru) ',
ja: '(ja) ',
pt_BR: '(pt) '
};
_.forEach($scope.knowledgeMetadata.languages, function (language) {
switch (language.name) {
case 'English':
temp.en = temp.en + language.label;
break;
case 'German':
temp.de = temp.de + language.label;
break;
case 'French':
temp.fr = temp.fr + language.label;
break;
case 'Hebrew':
temp.he = temp.he + language.label;
break;
case 'Italian':
temp.it = temp.it + language.label;
break;
case 'Japanese':
temp.ja = temp.ja + language.label;
break;
case 'Korean':
temp.ko = temp.ko + language.label;
break;
case 'Russian':
temp.ru = temp.ru + language.label;
break;
case 'Chinese':
temp.zh_CN = temp.zh_CN + language.label;
break;
case 'Spanish':
temp.es = temp.es + language.label;
break;
case 'Portuguese':
temp.pt_BR = temp.pt_BR + language.label;
break;
}
});
return temp;
}
$scope.openAction = function (questionObject) {
questionObject.isOpen = !questionObject.isOpen;
};
$scope.addNewQuestion = function () {
var newQuestion = new AssessmentQuestionVO();
newQuestion.textMap['default'] = "";
if (!$scope.questionList) {
$scope.questionList = [];
}
$scope.questionList.unshift(newQuestion);
assignSequenceNo($scope.questionList);
$scope.closeAllEditors();
$scope.openAction(newQuestion);
};
$scope.closeAllEditors = function () {
_.forEach($scope.questionList, function (item) {
item.closeItem();
});
};
$scope.deleteAction = function ($event, questionObj) {
$event.stopPropagation();
if (questionObj.questionId) {
questionObj.deleteItem();
}
else {
_.remove($scope.questionList, function (item) {
return item.sequenceNo === questionObj.sequenceNo;
});
}
assignSequenceNo($scope.questionList);
};
$scope.addLabel = function (question) {
for (var key in $scope.supportedLocales) {
if (!question.textMap.hasOwnProperty(key)) {
question.textMap[key] = '';
return;
}
}
};
$scope.removeLabel = function (question, label) {
question.removeLabel(label);
};
$scope.updateLabel = function (question, locale, selLocale) {
question.textMap[selLocale] = question.textMap[locale];
$scope.removeLabel(question, locale);
};
$scope.getCompaniesByName = function (name) {
return searchModel.getCompaniesByText(name).then(function (response) {
return response.companies;
});
};
$scope.editQuestionSet = function (questionSet) {
$scope.state.dataLoading = true;
$scope.state.isInitialPage = false;
$scope.state.isEditPage = true;
$scope.state.isClonePage = false;
$scope.questionSet.name = questionSet.name;
$scope.questionSet.company = questionSet.company;
$scope.questionSet.questionSetId = questionSet.id;
getQuestionListByID(questionSet.id);
editPageInit(questionSet);
};
$scope.cloneQuestionSet = function (questionSetId) {
$scope.state.dataLoading = true;
$scope.state.isInitialPage = false;
$scope.state.isEditPage = false;
$scope.state.isClonePage = true;
getQuestionListByID(questionSetId);
loadCompanies();
};
$scope.deleteQuestionSet = function (questionSetId) {
var modalInstance = systemAlertService.modal({
title: i18nService.getLocalizedString('common.notification.delete.aqi.title'),
text: i18nService.getLocalizedString('common.notification.delete.aqi.message'),
buttons: [
{
text: i18nService.getLocalizedString('common.labels.yes'),
data: {
questionSetId: questionSetId
}
},
{
text: i18nService.getLocalizedString('common.labels.no')
}
]
});
modalInstance.result.then(function (data) {
if (!_.isEmpty(data)) {
$scope.state.dataLoading = true;
var deleteQuestionSetPromise = knowledgeArticleModel.deleteQuestionSetById(data.questionSetId);
$q.when(deleteQuestionSetPromise).then(function () {
_.remove($scope.questionSetList, function (questionSet) {
return questionSet.id === questionSetId;
});
$scope.state.dataLoading = false;
}).catch(function (error) {
$scope.state.dataLoading = false;
systemAlertService.error({
text: error.data.detailedMessage || error.data.error,
clear: false
});
});
}
});
};
function createQuestionSet() {
$scope.questionSet.knowledgeQuestionList = $scope.questionList;
var postdata = _.cloneDeep($scope.questionSet);
delete postdata.questionSetId;
if ($scope.questionSet.organization) {
postdata.organization = {
organization: $scope.questionSet.organization.name
};
}
_.forEach(postdata.knowledgeQuestionList, function (questionObj) {
delete questionObj.text;
delete questionObj._persistence_shouldRefreshFetchGroup;
delete questionObj.isOpen;
delete questionObj.delete;
delete questionObj.sequenceNo;
});
knowledgeArticleModel.createQuestionSet(postdata).then(function () {
$scope.state.isEditPage = false;
$scope.state.isClonePage = false;
$scope.state.isInitialPage = true;
init();
}).catch(function (error) {
$scope.state.dataLoading = false;
systemAlertService.error({
text: error.data.detailedMessage || error.data.error,
clear: false
});
});
}
function updateQuestionSet() {
$scope.questionSet.knowledgeQuestionList = $scope.questionList;
var postdata = _.cloneDeep($scope.questionSet);
if ($scope.questionSet.organization) {
postdata.organization = {
organization: $scope.questionSet.organization.name
};
}
_.remove(postdata.knowledgeQuestionList, function (questionObj) {
if (questionObj.delete) {
return questionObj.delete;
}
for (var key in questionObj.originalTextMap) {
if (_.isUndefined(questionObj.textMap[key])) {
questionObj.textMap[key] = "";
}
}
delete questionObj.originalTextMap;
delete questionObj.text;
delete questionObj._persistence_shouldRefreshFetchGroup;
delete questionObj.isOpen;
delete questionObj.delete;
delete questionObj.sequenceNo;
});
_.forEach(postdata.knowledgeQuestionList, function () {
});
knowledgeArticleModel.updateQuestionSet($scope.questionSet.questionSetId, postdata).then(function () {
$scope.state.isEditPage = false;
$scope.state.isClonePage = false;
$scope.state.isInitialPage = true;
init();
}).catch(function (error) {
$scope.state.dataLoading = false;
systemAlertService.error({
text: error.data.detailedMessage || error.data.error,
clear: false
});
});
}
$scope.onSaveClick = function () {
$scope.state.dataLoading = true;
if ($scope.state.isEditPage) {
updateQuestionSet();
}
else if ($scope.state.isClonePage) {
createQuestionSet();
}
};
$scope.onCancelClick = function () {
$scope.state.dataLoading = false;
$scope.state.isEditPage = false;
$scope.state.isClonePage = false;
$scope.state.isInitialPage = true;
$scope.questionSet = {};
$scope.questionList = {};
var questionSetListPromise = knowledgeArticleModel.getQuestionSetList();
$q.all([questionSetListPromise]).then(function (results) {
$scope.questionSetList = results[0][0].items[0].objects;
$scope.state.dataLoading = false;
});
};
$scope.openConfigureQuestionsPage = function () {
var defaultSetId = "";
_.findIndex($scope.questionSetList, function (questionSet) {
if (questionSet.isDefault) {
defaultSetId = questionSet.id;
}
});
$scope.cloneQuestionSet(defaultSetId);
};
function getQuestionListByID(questionSetId) {
var questionsListPromise = knowledgeArticleModel.getQuestionSetById(questionSetId);
var knowledgeMetadataPromise = metadataModel.getMetadataByType('knowledge');
$q.all([questionsListPromise, knowledgeMetadataPromise]).then(function (response) {
$scope.questionList = response[0];
_.forEach($scope.questionList, function (questionObj) {
questionObj.originalTextMap = _.cloneDeep(questionObj.textMap);
});
assignSequenceNo($scope.questionList);
$scope.knowledgeMetadata = response[1];
$scope.supportedLocales = getSupportedLocales();
$scope.state.dataLoading = false;
});
}
$scope.loadOrganizations = function (company) {
return createTicketModel.getList(EntityVO.TYPE_ORGANIZATION, {
companyName: company.name
}).then(function (organizations) {
$scope.state.organizationsLoading = false;
$scope.selections.organizations = organizations.objects;
$scope.questionSet.organization = null;
});
};
function loadCompanies() {
searchModel.getOperatingCompanies(null, -1).then(function (response) {
$scope.selections.companies = _.cloneDeep(response.companies);
$scope.state.tooManyCompanies = response.exceedsChunkSize;
});
}
function editPageInit(questionSet) {
loadCompanies();
if (questionSet.company) {
$scope.loadOrganizations(questionSet.company).then(function () {
if (questionSet.organization && questionSet.organization.organization) {
_.some($scope.selections.organizations, function (organization) {
if (organization.name === questionSet.organization.organization) {
$scope.questionSet.organization = organization;
return true;
}
});
}
});
}
}
function init() {
$scope.state.dataLoading = true;
$scope.questionList = {};
$scope.postValidate = false;
var questionSetListPromise = knowledgeArticleModel.getQuestionSetList();
$q.all([questionSetListPromise]).then(function (results) {
$scope.questionSetList = results[0][0].items[0].objects;
$scope.state.dataLoading = false;
});
}
init();
}]);
})();