import {Component, OnInit, EventEmitter} from '@angular/core'; import {DialogRef, ModalComponent, CloseGuard} from 'ngx-modialog'; import {BSModalContext} from 'ngx-modialog/plugins/bootstrap'; import UtilService from "@common/services/util/index"; import StorageService from "@common/services/storage"; import ViewService from "@common/modules/manage/services/view"; import SiteRouterService from "@common/modules/manage/services/siteRouter"; declare var $: any; export class ViewEditFormContext extends BSModalContext { constructor(private args: any = {}) { super(); /* * 点击背景是否隐藏模态框 * @value true 不隐藏 * @value false 隐藏 * */ this.isBlocking = false; /* * 模态框位置 * @value modal-top 居顶 * */ this.dialogClass = 'modal-dialog modal-top viewEditForm'; } } @Component({ selector: 'viewEditForm', templateUrl: './template.html', styleUrls: ['./style.less'] }) export default class ViewEditForm implements OnInit, ModalComponent { data: any = { sort: 0, type: 'default' }; otherData: any = {}; context: any = {}; constructor(public dialog: DialogRef, private storageService: StorageService, private viewService: ViewService, private siteRouterService: SiteRouterService, private utilService: UtilService) { this.context = dialog.context; } ngOnInit(): void { if (this.context.args.item) { this.data = $.extend(true, {}, this.context.args.item); } } submit() { const {parentItem} = this.context.args; if (!/^[a-zA-Z]+$/.test(this.data.className)) { this.utilService.toastrError('类名只能由英文字母构成'); return; } this.otherData.loading = true; const data: any = { className: this.data.className.substring(0, 1).toUpperCase() + this.data.className.substring(1), module: this.storageService.get('module'), title: this.data.title, icon: this.data.icon, params: this.data.params, redirect: this.data.redirect, sort: this.data.sort, type: this.data.type }; if (parentItem) { const paths = parentItem.name.split('.').slice(1); data.path = paths.join('.'); } else { data.type = 'root'; } if (this.context.args.item) { //修改视图 data.name = this.data.name; this.viewService.update(data).then((data: any) => { this.otherData.loading = false; if (data.status == 0) { this.utilService.toastrSuccess(); } else { this.utilService.toastrError(); } }) } else { //添加视图 this.viewService.add(data).then((data: any) => { this.otherData.loading = false; if (data.status == 0) { this.utilService.toastrSuccess(); if (!parentItem) { //添加顶级视图 this.siteRouterService.siteMainNav.push({ ...data.result, children: [] }); } else { //添加子视图 parentItem.children.push({ ...data.result, children: [] }) } this.dialog.close(); } else { this.utilService.toastrError(); } }); } } }