SmartIT_Extensions/BMC/smart-it-full/scripts/app/release/release-profile-plans-direc...

191 lines
11 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('releaseModule')
.directive('releaseProfilePlans', ['$q', 'attachmentService', 'events',
function ($q, attachmentService, events) {
return {
restrict: 'E',
replace: true,
templateUrl: 'views/release/release-profile-plans.html',
scope: {
changeRequest: '=context',
documentTypes: '=types',
editModeAllowed: '='
},
link: function (scope) {
var plansInitialState, plansByIdInitial;
scope.state = {
processing: false,
hideControlButtons: false
};
scope.editMode = false;
scope.showDocumentViewer = function (plan) {
scope.$parent.ticketActions.showAttachmentPreviewer(plan);
};
scope.editPlans = function () {
prepareDocumentsData();
scope.editMode = true;
};
scope.cancelEdit = function () {
scope.changeRequest.plans = plansInitialState;
scope.editMode = false;
scope.types = scope.documentTypes;
scope.changeRequest.documents = [];
};
/**
* Handles documents save for
* */
scope.updatePlans = function () {
var updatePromises = [];
scope.state.processing = true;
_.each(scope.changeRequest.documents, function (plansGroup) {
_.each(plansGroup.plans, function (plan) {
if (!plan.desc && !plan.attachments.length) {
return;
}
var request = {
text: plan.desc,
type: plansGroup.index,
locked: false,
access: true,
attachments: [],
attachmentInfos: []
};
_.each(plan.attachments, function (attach) {
if (!attach.id) {
request.attachments.push(attach);
request.attachmentInfos.push({ name: attach.name, action: 'add' });
}
});
if (!plan.id) {
//No id means the plan is new
updatePromises.push(attachmentService.createChangePlan(scope.changeRequest.id, request));
}
else {
//Checking whether update is required
var initialPlanData = plansByIdInitial[plan.id];
_.each(initialPlanData.attachmentInfos, function (attachment) {
var matchingAttach = _.find(plan.attachments, function (newAttach) {
if (!newAttach.attachmentReference) {
return false;
}
return newAttach.attachmentReference.attachmentId === attachment.attachmentReference.attachmentId;
});
if (!matchingAttach) {
request.attachmentInfos.push({
name: attachment.name,
attachmentId: attachment.attachmentReference.attachmentId,
action: 'delete'
});
}
});
if (plan.workNote.notes !== request.text || request.attachmentInfos.length > 0) {
updatePromises.push(attachmentService.updateChangePlan({
parentId: scope.changeRequest.id,
id: plan.id
}, request));
}
}
});
});
_.each(plansInitialState, function (initialPlan) {
var matchingPlanType = _.findWhere(scope.changeRequest.documents, { index: initialPlan.workNote.workNoteTypeId }), matchingPlan;
if (matchingPlanType) {
matchingPlan = _.findWhere(matchingPlanType.plans, { id: initialPlan.id });
}
if (!matchingPlanType || !matchingPlan) {
updatePromises.push(attachmentService.deleteChangePlan(scope.changeRequest.id, initialPlan.id));
_.remove(scope.changeRequest.plans, { id: initialPlan.id });
}
});
return $q.all(updatePromises)
.finally(function () {
scope.state.processing = false;
scope.editMode = false;
})
.then(function (response) {
_.each(response, function (respData) {
var plan = respData.data[0];
if (respData.action !== 'deleted') {
var docType = _.findWhere(scope.documentTypes, { index: plan.workNote.workNoteTypeId });
plan.workNote.documentType = docType;
if (respData.action === 'created') {
scope.changeRequest.plans.push(respData.data[0]);
}
else if (respData.action === 'updated') {
var updatedItem = _.findWhere(scope.changeRequest.plans, { id: respData.data[0].id }), updatedItemIndex = scope.changeRequest.plans.indexOf(updatedItem);
respData.data[0].workNote.documentType = docType;
if (updatedItemIndex >= 0) {
scope.changeRequest.plans[updatedItemIndex] = respData.data[0];
}
else {
_.remove(scope.changeRequest.plans, { id: respData.data[0].id });
scope.changeRequest.plans.push(respData.data[0]);
}
}
}
});
scope.changeRequest.plans = updatePlanIndexes(scope.changeRequest.plans);
});
};
/**
* Creates documents object structure required for documents tab directive
* */
function prepareDocumentsData() {
scope.types = _.cloneDeep(scope.documentTypes);
plansInitialState = _.cloneDeep(scope.changeRequest.plans);
plansByIdInitial = _.indexBy(plansInitialState, 'id');
var documents = [], plans = _.cloneDeep(scope.changeRequest.plans);
_.each(plans, function (plan) {
var planType = _.findWhere(scope.types, { index: plan.workNote.workNoteTypeId }), plansGroup = _.findWhere(documents, { index: planType.index });
plan.attachments = attachmentService.normalizeAttachments(plan);
plan.desc = plan.workNote.notes;
plan.label = planType.label;
_.each(plan.attachments, function (attach) {
attach.pendingSave = true;
});
if (!plansGroup) {
planType.active = true;
planType.activePlanIndex = 0;
planType.plans = [plan];
if (plan.workNote && plan.workNote.locked) {
planType.isLocked = true;
}
documents.push(planType);
}
else {
plansGroup.plans.push(plan);
}
return plan;
});
scope.changeRequest.documents = documents;
scope.state.hideControlButtons = !documents.length;
}
function updatePlanIndexes(plans) {
var planGroupIndexing = {}, groupedPlans = _.groupBy(plans, function (item) {
return item.workNote.workNoteTypeId;
});
_.each(plans, function (plan) {
if (groupedPlans[plan.workNote.workNoteTypeId].length > 1) {
planGroupIndexing[plan.workNote.workNoteTypeId] ? planGroupIndexing[plan.workNote.workNoteTypeId]++ : (planGroupIndexing[plan.workNote.workNoteTypeId] = 1);
plan.typeIndex = planGroupIndexing[plan.workNote.workNoteTypeId];
}
else if (groupedPlans[plan.workNote.workNoteTypeId].length === 1 && plan.typeIndex) {
plan.typeIndex = null;
}
});
return _.sortBy(plans, function (plan) {
return plan.workNote.workNoteTypeId;
});
}
scope.$on(events.CHANGE_REQUEST_PLANS_EDIT_STATUS, function ($evt, state) {
scope.state.hideControlButtons = state.hideButtons;
});
//prepareDocumentsData();
}
};
}
]);
})();