import { Component, OnInit, OnDestroy } from '@angular/core'; // import { LocalDataSource } from 'ng2-smart-table'; import { ComponentsService } from '../../../services/components.service'; import { ActionOptions, ComponentOptions } from '../../../model/componets'; import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop'; import { NbWindowService, // NbWindowState, } from '@nebular/theme'; import { ActionFormComponent } from '../action-form/action-form.component'; import { GuardedComponent } from '../../../guards/confirm-deactivate-guard.guard'; @Component({ selector: 'ngx-actions-tables', templateUrl: './actions-tables.component.html', styleUrls: ['./actions-tables.component.scss'], }) export class ActionsTablesComponent implements OnInit, OnDestroy, GuardedComponent { component: ComponentOptions; data: ActionOptions[]; saved = true; ngOnInit(): void { this.component = this.componentsService.getCurrentComponent(); } ngOnDestroy() { } destroyMsg: string; canDestroy(): boolean { this.destroyMsg = '还没有保存,确定要离开么?'; return this.isSaved(); } constructor( private windowService: NbWindowService, private componentsService: ComponentsService, ) { this.data = this.componentsService.getAllActions(); } drop(event: CdkDragDrop) { moveItemInArray(this.data, event.previousIndex, event.currentIndex); } addAction() { this.saved = false; this.componentsService.addAction(); this.updateAction(this.componentsService.getCurrentAction(), 'add'); } updateAction(action: ActionOptions, formType = 'update') { this.saved = false; this.componentsService.changeCurrentAction(action); const component = ActionFormComponent; this.openWindowForm(action, component, formType); } openWindowForm(data: ActionOptions, component, formType: string) { this.windowService.open(component, { title: `Actions`, context: { data: data, formType: formType, }, }); } deleteAction(action: ActionOptions) { this.saved = false; if (window.confirm('Are you sure you want to delete')) { this.componentsService.deleteAction(action); this.save(); } else {} } save() { this.componentsService.updateCurrentComponent() .then((result) => { // 成功 this.saved = true; }).catch((err) => { // 失败 }); } /** * 保存过返回true,否则返回false */ private isSaved(): boolean { return this.saved; } }