import { Injectable, } from '@angular/core'; import { ActionOptions, ComponentOptions, ComponentOptionsClass, ActionOptionsClass, } from '../model/componets'; // import { // dataComponent, // } from '../mock-data/component'; import { DataSourceService } from './data-source.service'; // import { MessageToasterService } from './message-toaster.service'; // import { MessageDialogService } from './message-dialog.service'; import { MessageToasterService } from './message-toaster.service'; @Injectable({ providedIn: 'root', }) export class ComponentsService { private currentComponent: ComponentOptions; private data: ComponentOptions[]; private currentAction: ActionOptions; private configuration: any; private loading = false; constructor( private dataSource: DataSourceService, private messageService: MessageToasterService, // private messageDialogService: MessageDialogService, ) { this.data = []; this.initData(); } initData() { if (this.loading) return; this.loading = true; return new Promise((resolve, reject) => { this.dataSource.getAllComponents().subscribe(data => { data.forEach(component => { this.data.push(new ComponentOptionsClass(component)); }); if (this.data.length > 0) { this.currentComponent = this.data[0]; } this.loading = false; resolve(); }); }); } createMockData() { for (let j = 0; j < 10; j++) { const actions = []; const component: ComponentOptions = { id: (j + 1).toString(), createtimestamp: (new Date()).getTime(), updatetimestamp: (new Date()).getTime(), description: `Better than last week (${j})`, actions: actions, }; this.data.push(component); for (let i = 0; i < 10; i++) { actions.push({ description: j + ': description' + i, actionType: j + ': actionType' + i, actionParam: j + ': actionParam' + i, objectXpath: j + ': objectXpath' + i, ObjectParam: ['ObjectParam1' + i, 'ObjectParam2' + i, 'ObjectParam3' + i], frame: j + ': frame' + i, }); } } } getAllComponents(): Promise { return new Promise(async (resolve, reject) => { if (this.data.length === 0) { await this.initData(); } resolve(this.data); }); } getCurrentComponent() { return this.currentComponent; } changeCurrentComponent(item) { this.currentComponent = item; } updateCurrentComponent(): Promise { return new Promise((resolve, reject) => { this.dataSource.updateOneComponent(this.currentComponent).subscribe(result => { if (result.componentUpdateResult && result.componentUpdateResult.result === 'updated') { // 更新成功 // console.log('更新成功'); this.messageService.makeToast('component 更新', '更新成功', null, 5000); resolve(result); } else { // 更新失败 // console.log('更新失败'); reject(); } }); }); } addTmpComponent() { const newComponent = new ComponentOptionsClass(); this.data.push(newComponent); this.changeCurrentComponent(newComponent); } addComponent(): Promise { return new Promise((resolve, reject) => { this.dataSource.addComponent(this.getCurrentComponent()).subscribe(component => { if (component.id && component.id !== '') { // 更新成功 this.currentComponent.id = component.id; this.messageService.makeToast('component 保存', '保存成功', null, 5000); resolve(component); } else { // 更新失败 // console.log('增加失败'); reject(); } }); }); } deleteCurrentComponent() { this.deleteComponent(this.currentComponent); } deleteComponent(targetComponent: ComponentOptions) { this.deleteRemoteComponent(targetComponent) .then(deletedComponent => { this.deleteLocalComponent(targetComponent); }) .catch(component => alert('Error, 删除失败')); } deleteLocalComponent(targetComponent?: ComponentOptionsClass) { for (let index = 0; index < this.data.length; index++) { const component = this.data[index]; if (component === (targetComponent || this.getCurrentComponent())) { this.data.splice(index, 1); break; } } } deleteRemoteComponent(component: ComponentOptions): Promise { return new Promise((resolve, reject) => { this.dataSource.deleteOneComponentById(component.id).subscribe(result => { if (result.result.indexOf('deleted') !== -1) { this.messageService.makeToast('component 删除', '删除成功', null, 5000); resolve(result.deleteddata); } else { reject(result.deleteddata); } }, err => { this.messageService.makeToast('component 删除', '删除成功', null, 5000); reject(err); }); }); } getAllActions() { return this.currentComponent.actions; } getCurrentAction() { return this.currentAction; } changeCurrentAction(action: ActionOptions) { this.currentAction = action; } deleteCurrentAction() { this.deleteAction(this.currentAction); } deleteAction(target?: ActionOptions) { if (!target) { target = this.currentAction; } const actions = this.currentComponent.actions; for (let index = 0; index < actions.length; index++) { const action = actions[index]; if (action === target) { actions.splice(index, 1); break; } } } addAction() { const newAction = new ActionOptionsClass(); this.currentComponent.actions.push(newAction); this.changeCurrentAction(newAction); } getConfiguration(): Promise { return new Promise((resolve, reject) => { this.dataSource.getActionConfig().subscribe(data => { resolve(data); }); }); } getActionConfig(): Promise { return new Promise(async (resolve, reject) => { if (!this.configuration) { this.configuration = await this.getConfiguration(); } const {actionList, defalut, customized} = this.configuration; let configs = null; if (actionList.indexOf(this.currentAction.actionType) !== -1) { configs = customized.filter(conf => { return conf.actionType === this.currentAction.actionType; }); } resolve({ actionList: actionList, actionConfig: configs && configs.length > 0 ? configs[0].displayLayout : defalut, }); }); } }