SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/release/relate-change-controller.js

142 lines
6.3 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('releaseModule').controller('RelateChangeController', ['$scope', '$modalInstance', 'linkParams', 'searchService', '$q', 'relationModel', 'metadataModel',
'systemAlertService', 'i18nService',
function ($scope, $modalInstance, linkParams, searchService, $q, relationModel, metadataModel, systemAlertService, i18nService) {
var type = EntityVO.TYPE_RELEASE, selected = {}, state = {
searching: false,
processing: true,
isDirty: false
}, ticket = {};
$scope.state = state;
$scope.selected = selected;
$scope.availableEntities = null;
$scope.ticketType = type;
relationModel.getRelationsByTag(linkParams.selectedItems[0].id, EntityVO.TYPE_RELEASE, 'releasePlan').then(function (response) {
ticket.relations = response;
state.processing = false;
});
$scope.searchEntities = function () {
var params = {}, jsonParams = {};
// if there is no searchText, the request fails on the backend
// with an error 'The Search Text field cannot be null.'
if (!selected.searchText) {
return false;
}
if (state.showSuggestedItemsTooltip) {
state.showSuggestedItemsTooltip = false;
}
state.searching = true;
selected.entities = [];
params = {
search_text: selected.searchText,
types: EntityVO.TYPE_CHANGE,
chunk_index: 0,
chunk_size: 50
};
jsonParams = {
allowSpecialCharacters: true
};
function postSearch(data) {
var searchResults = data.items.length > 0 ? data.items[0].results : [];
//Remove already related items from search results
_.forEach(ticket.relations, function (relatedItem) {
if (relatedItem.type === EntityVO.TYPE_CHANGE) {
searchResults = _.reject(searchResults, { id: relatedItem.id });
}
});
$scope.availableEntities = searchResults;
}
var promarr = [searchService.getGlobalSearchResults(params, jsonParams)];
if (!metadataModel.cache[EntityVO.TYPE_CHANGE]) {
promarr.push(metadataModel.getMetadataByType(EntityVO.TYPE_CHANGE));
}
return $q.all(promarr).then(function (data) {
postSearch(data[0]);
}).finally(function () {
state.searching = false;
});
};
$scope.selectEntity = function (entity) {
state.isDirty = true;
if (entity.isSelected) {
selected.entities.push(entity);
}
else {
_.remove(selected.entities, { id: entity.id });
}
};
$scope.link = function () {
state.processing = true;
var sequence = linkParams.sequence;
var actionItems = _.map(linkParams.selectedItems, function (item) {
var actionObj = {
uuid: item.id,
type: item.type
};
return actionObj;
}), relateItems = _.map(selected.entities, function (entity) {
var relatedObj = {
id: entity.id,
displayId: entity.displayId,
relationshipType: RelationItemVO.TYPE_CONSISTSOF,
tag: RelationItemVO.TAG_LINKEDITEM,
type: entity.type,
desc: entity.title,
parentDesc: linkParams.selectedItems[0].desc,
milestone: linkParams.selectedItems[0].milestone.name,
details: { sequence: sequence }
};
sequence++;
if (linkParams.selectedItems[0].displayId) {
relatedObj.parentDisplayId = linkParams.selectedItems[0].displayId;
}
return relatedObj;
});
relationModel.bulkAddRelation(actionItems, relateItems).then(function () {
$modalInstance.close();
state.processing = false;
}, function (response) {
systemAlertService.error({
text: response.data.results.error,
clear: false
});
$modalInstance.dismiss();
}).catch(function (response) {
systemAlertService.error({
text: response.data.results.error,
clear: false
});
$modalInstance.dismiss();
});
};
$scope.close = function () {
if (state.isDirty) {
var modalInstance = systemAlertService.modal({
title: i18nService.getLocalizedString('common.notification.dirty.title'),
text: i18nService.getLocalizedString('common.notification.dirty.message'),
buttons: [
{
text: i18nService.getLocalizedString('common.labels.yes'),
data: true
},
{
text: i18nService.getLocalizedString('common.labels.no'),
data: false
}
]
});
modalInstance.result.then(function (data) {
if (data) {
$modalInstance.dismiss();
}
});
}
else {
$modalInstance.dismiss();
}
};
}]);
})();