import { DocumentLinkRestService, IDocumentLink } from 'isogd-document-link-rest'; import { SolarRestService } from 'isogd-solar-rest'; import * as _ from 'lodash'; import { SearchResultDocumentEx } from './search-candidates'; import { ILinkWithDocument } from './linked-documents-edit'; import { LinkedDocumentService } from '../../services/LinkedDocumentService'; import { DocumentIsogd, DocumentRestService } from 'isogd-document-rest'; import { NsiRestService } from 'oasi-nsi-rest'; class LinkedDocumentsController { static $inject = ['$filter', 'solarRestService', 'linkedDocumentService', '$timeout', 'toastr', 'blockUI', 'documentLinkRestService', 'documentRestService', '$q', 'nsiRestService']; private document: DocumentIsogd; private linksWithDocument: ILinkWithDocument[]; private chapters: any[]; private linkdocTypes: any[]; private linkdocStatuses: any[]; private myBlockUI: any; private type: string; private selectAll: boolean = false; private disabledAll: boolean = true; private allLinkType: string = ""; private updateAndFind: () => void; private linkedDocumentEditorClass: string; private linkedDocumentEditorBlockUIName: string; constructor(private $filter: ng.IFilterService, private solarService: SolarRestService, private linkedDocumentService: LinkedDocumentService, private $timeout: ng.ITimeoutService, private toastr: Toastr, private blockUI: any, private documentLinkRestService: DocumentLinkRestService, private documentRestService: DocumentRestService, private $q: ng.IQService, private nsiRestService: NsiRestService) { } clear() { this.selectAll = false; this.disabledAll = true; this.allLinkType = ""; } $onInit() { switch (this.type) { case "approve": this.linkedDocumentEditorClass = "linkedDocumentApproveEditor"; this.linkedDocumentEditorBlockUIName = "linkedDocumentApproveEditor"; break; case "review": this.linkedDocumentEditorClass = "linkedDocumentReviewEditor"; this.linkedDocumentEditorBlockUIName = "linkedDocumentReviewEditor"; break; case "reject": this.linkedDocumentEditorClass = "linkedDocumentRejectditor"; this.linkedDocumentEditorBlockUIName = "linkedDocumentRejectditor"; break; } this.myBlockUI = this.blockUI.instances.get(this.linkedDocumentEditorBlockUIName); } $onChanges(changes: any) { if(changes.linksWithDocument && changes.linksWithDocument.currentValue) { this.clear(); } } $onDestroy() { this.myBlockUI.reset(); } selectAllLinks() { this.linksWithDocument.forEach(l => { l.checked = this.selectAll; }); this.disabledAll = !this.selectAll; } changeSelection() { let selectAll = true; let anyChecked = false; this.linksWithDocument.forEach(l => { if (l.checked === false) { selectAll = false; } if (l.checked === true) { anyChecked = true; } }); this.selectAll = selectAll; this.disabledAll = !anyChecked; } changeLinkType(doc: ILinkWithDocument) { this.myBlockUI.start(); this.linkedDocumentService.changeLink(doc.link).then(response => { this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Тип связи успешно изменён!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при изменении типа связи!", "Ошибка"); }); } changeSelectedLinkType() { let promises: ng.IPromise[] = []; this.linksWithDocument.forEach(l => { if (l.checked === true) { l.link.linkType = this.allLinkType; promises.push(this.linkedDocumentService.changeLink(l.link)); } }); this.myBlockUI.start(); this.$q.all(promises).then(response => { this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Тип выделенных связей успешно изменён!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при изменении типа выделенных связей!", "Ошибка"); }); } deleteLink(doc: ILinkWithDocument, index: number) { this.myBlockUI.start(); this.linkedDocumentService.deleteLink(doc.link).then(response => { this.linksWithDocument.splice(index, 1); this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Связь успешно удалена!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при удалении связи!", "Ошибка"); }); } deleteSelectedLink() { let promises: ng.IPromise[] = []; this.linksWithDocument.forEach(l => { if (l.checked === true) { promises.push(this.linkedDocumentService.deleteLink(l.link)); } }); this.myBlockUI.start(); this.$q.all(promises).then(response => { this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Выделенные связи успешно удалены!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при удалении выделенных связей!", "Ошибка"); }); } changeLinkStatus(doc: ILinkWithDocument, status: string) { this.myBlockUI.start(); doc.link.status = status; this.linkedDocumentService.changeLink(doc.link).then(response => { this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Изменение статуса связи прошло успешно!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при изменении статуса связи!", "Ошибка"); }); } changeLinkStatusToApprove(doc: ILinkWithDocument) { this.changeLinkStatus(doc, "approve"); } changeLinkStatusToReview(doc: ILinkWithDocument) { this.changeLinkStatus(doc, "review"); } changeLinkStatusToReject(doc: ILinkWithDocument) { this.changeLinkStatus(doc, "reject"); } changeSelectedLinkStatus(status: string) { let promises: ng.IPromise[] = []; this.linksWithDocument.forEach(l => { if (l.checked === true) { l.link.status = status; promises.push(this.linkedDocumentService.changeLink(l.link)); } }); this.myBlockUI.start(); this.$q.all(promises).then(response => { this.myBlockUI.stop(); this.updateAndFind(); this.toastr.success("Изменение статуса выделенных связей прошло успешно!", "Успех"); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при изменении статуса выделенных связей!", "Ошибка"); }); } changeSelectedLinkStatusToApprove() { this.changeSelectedLinkStatus("approve"); } changeSelectedLinkStatusToReview() { this.changeSelectedLinkStatus("review"); } changeSelectedLinkStatusToReject() { this.changeSelectedLinkStatus("reject"); } } export const LinkedDocumentsComponent: angular.IComponentOptions = { controller: LinkedDocumentsController, template: require('./linked-documents.html'), bindings: { document: "<", linksWithDocument: "<", chapters: "<", linkdocTypes: "<", linkdocStatuses: "<", updateAndFind: "&", type: "<" } };