213 lines
12 KiB
JavaScript
213 lines
12 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('changeModule')
|
|
.directive('changeProfilePlans', ['$q', 'attachmentService', 'events', 'systemAlertService',
|
|
function ($q, attachmentService, events, systemAlertService) {
|
|
return {
|
|
restrict: 'E',
|
|
replace: true,
|
|
templateUrl: 'views/change/change-profile-plans.html',
|
|
scope: {
|
|
changeRequest: '=context',
|
|
documentTypes: '=types',
|
|
editModeAllowed: '=',
|
|
fromCopyChange: '=?'
|
|
},
|
|
link: function (scope) {
|
|
var plansInitialState, plansByIdInitial;
|
|
scope.state = {
|
|
processing: false,
|
|
hideControlButtons: false
|
|
};
|
|
scope.isChild = false;
|
|
scope.editMode = false;
|
|
scope.showDocumentViewer = function (plan) {
|
|
scope.$parent.ticketActions.showAttachmentPreviewer(plan);
|
|
};
|
|
scope.editPlans = function () {
|
|
prepareDocumentsData();
|
|
scope.editMode = true;
|
|
scope.$emit(events.DISABLE_PARENT_EDITMODE, true);
|
|
scope.$emit(events.DISABLE_EDITMODE, true);
|
|
};
|
|
scope.cancelEdit = function () {
|
|
scope.changeRequest.plans = plansInitialState;
|
|
scope.editMode = false;
|
|
scope.types = scope.documentTypes;
|
|
scope.changeRequest.documents = [];
|
|
scope.$emit(events.DISABLE_PARENT_EDITMODE, false);
|
|
scope.$emit(events.DISABLE_EDITMODE, false);
|
|
};
|
|
scope.$on(events.DISABLE_EDITCHILD, function (event, childEditMode) {
|
|
scope.editModeAllowed = true;
|
|
scope.isChild = childEditMode;
|
|
});
|
|
/**
|
|
* 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, scope.$parent.type, 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,
|
|
type: scope.$parent.type
|
|
}, request));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
_.each(plansInitialState, function (initialPlan) {
|
|
var matchingPlanType = _.find(scope.changeRequest.documents, { index: initialPlan.workNote.workNoteTypeId }), matchingPlan;
|
|
if (matchingPlanType) {
|
|
matchingPlan = _.find(matchingPlanType.plans, { id: initialPlan.id });
|
|
}
|
|
if (!matchingPlanType || !matchingPlan) {
|
|
updatePromises.push(attachmentService.deletePlan(scope.changeRequest.id, scope.$parent.type, initialPlan.id));
|
|
_.remove(scope.changeRequest.plans, { id: initialPlan.id });
|
|
}
|
|
});
|
|
return $q.all(updatePromises)
|
|
.finally(function () {
|
|
scope.state.processing = false;
|
|
scope.editMode = false;
|
|
scope.$emit(events.DISABLE_PARENT_EDITMODE, true);
|
|
scope.$emit(events.DISABLE_EDITMODE, false);
|
|
})
|
|
.then(function (response) {
|
|
_.each(response, function (respData) {
|
|
var plan = respData && respData.data && respData.data[0];
|
|
if (respData && respData.action !== 'deleted') {
|
|
var docType = _.find(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 = _.find(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);
|
|
scope.$parent.refreshTicket();
|
|
}).catch(function (error) {
|
|
if (error) {
|
|
systemAlertService.error({
|
|
text: (error.data && error.data.error) || error,
|
|
clear: false
|
|
});
|
|
}
|
|
});
|
|
};
|
|
/**
|
|
* Creates documents object structure required for documents tab directive
|
|
* */
|
|
function prepareDocumentsData() {
|
|
scope.types = _.cloneDeep(scope.documentTypes);
|
|
plansInitialState = _.cloneDeep(scope.changeRequest.plans);
|
|
plansByIdInitial = _.keyBy(plansInitialState, 'id');
|
|
var documents = [], plans = _.cloneDeep(scope.changeRequest.plans);
|
|
_.each(plans, function (plan) {
|
|
var planType = _.find(scope.types, { index: plan.workNote.workNoteTypeId }), plansGroup = _.find(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;
|
|
scope.showSave = state.showSave;
|
|
});
|
|
//prepareDocumentsData();
|
|
}
|
|
};
|
|
}
|
|
]);
|
|
})();
|