-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
329 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { DATA_DIR as dataDir } from '../../../../shared/common/constant'; | ||
import { ConfigurationInterface, ConfigurationValueInterface } from '../types'; | ||
import * as path from 'path'; | ||
import { fileExists, readJson, writeJson } from '../../../../shared/node/file'; | ||
|
||
export class Configuration implements ConfigurationInterface { | ||
|
||
/** | ||
* 配置文件地址 | ||
*/ | ||
private readonly configPath: string; | ||
|
||
constructor() { | ||
this.configPath = path.join(dataDir, 'config.json'); | ||
this.checkConfig(); | ||
} | ||
|
||
/** | ||
* 检查配置文件,不存在创建 | ||
*/ | ||
private checkConfig() { | ||
if (!fileExists(this.configPath)) { | ||
this.saveConfig({ settings: {} }); | ||
} | ||
} | ||
|
||
/** | ||
* 读取配置文件 | ||
*/ | ||
private loadConfig(): ConfigurationValueInterface { | ||
const data = readJson(this.configPath) || { settings: {} }; | ||
return data; | ||
} | ||
|
||
/** | ||
* 保存配置文件 | ||
*/ | ||
private saveConfig(data: ConfigurationValueInterface): boolean { | ||
return writeJson(this.configPath, data); | ||
} | ||
|
||
/** | ||
* 保存全局配置 | ||
*/ | ||
saveSettings(settings: ConfigurationValueInterface): boolean { | ||
let data = this.loadConfig(); | ||
data.settings = settings; | ||
return this.saveConfig(data); | ||
} | ||
|
||
/** | ||
* 保存模块配置 | ||
* @param moduleID | ||
* @param settings | ||
*/ | ||
saveModuleSettings(moduleID: string, settings: ConfigurationValueInterface): boolean { | ||
let data = this.loadConfig(); | ||
if (!data.settings) { | ||
data.settings = {}; | ||
} | ||
data.settings[moduleID] = settings; | ||
return this.saveConfig(data); | ||
} | ||
|
||
/** | ||
* 删除模块配置 | ||
* @param moduleID | ||
* @returns | ||
*/ | ||
deleteModuleSettings(moduleID: string): boolean { | ||
let data = this.loadConfig(); | ||
if (data.settings && data.settings[moduleID]) { | ||
delete(data.settings[moduleID]); | ||
return this.saveConfig(data); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 获取全局配置 | ||
* @returns | ||
*/ | ||
getSettings(): ConfigurationValueInterface { | ||
const data = this.loadConfig(); | ||
return data.settings; | ||
} | ||
|
||
/** | ||
* 获取模块配置 | ||
* @param moduleID | ||
* @returns | ||
*/ | ||
getModuleSettings(moduleID: string): ConfigurationValueInterface { | ||
const settings = this.getSettings(); | ||
return settings[moduleID] || {}; | ||
} | ||
|
||
} | ||
|
||
export default () => new Configuration(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* 配置管理 | ||
*/ | ||
export interface ConfigurationInterface { | ||
saveSettings: (settings: ConfigurationValueInterface) => boolean; | ||
saveModuleSettings: (moduleID: string, settings: ConfigurationValueInterface) => boolean; | ||
deleteModuleSettings: (moduleID: string) => boolean; | ||
getSettings: () => ConfigurationValueInterface; | ||
getModuleSettings: (moduleID: string) => ConfigurationValueInterface; | ||
} | ||
|
||
export interface ConfigurationValueInterface { | ||
[propName: string]: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/workbench/browser/src/app/pages/setting/setting.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<a class="sidebar_item sidebar_setting" title="设置" (click)="handleShowModal()" *ngIf="isVisible"> | ||
<i nz-icon nzType="setting"></i> | ||
</a> | ||
|
||
<nz-modal [(nzVisible)]="isVisible && isModal" nzWidth="70%" nzTitle="配置" (nzOnCancel)="handleCancel()"> | ||
<section *nzModalContent class="flex"> | ||
<form nz-form [nzLayout]="'vertical'" [formGroup]="validateForm" (ngSubmit)="handleSave()"> | ||
<nz-tabset [nzTabPosition]="position"> | ||
<nz-tab *ngFor="let module of modules" [nzTitle]="module.title"> | ||
<nz-form-item nz-col class="fg1" *ngFor="let field of module.fields"> | ||
<nz-form-label nzFor="{{ field.name}}">{{ field.label }} </nz-form-label> | ||
<nz-form-control nzErrorTip="请输入 {{ field.label }}"> | ||
<input type="text" nz-input id="{{ field.name }}" formControlName="{{ field.name }}" [(ngModel)]="settings[module.key][field.key]" /> | ||
</nz-form-control> | ||
</nz-form-item> | ||
</nz-tab> | ||
</nz-tabset> | ||
</form> | ||
</section> | ||
<div *nzModalFooter class="footer"> | ||
<button nz-button nzType="primary" (click)="handleSave()">保存</button> | ||
<button nz-button nzType="default" (click)="handleCancel()">取消</button> | ||
</div> | ||
</nz-modal> |
16 changes: 16 additions & 0 deletions
16
src/workbench/browser/src/app/pages/setting/setting.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.sidebar_setting { | ||
font-size: 20px; | ||
position: absolute; | ||
padding: 15px; | ||
bottom: 0; | ||
} | ||
|
||
.flex { | ||
display: flex; | ||
width: 100%; | ||
height: 60vh; | ||
} | ||
|
||
.footer { | ||
text-align: right; | ||
} |
101 changes: 101 additions & 0 deletions
101
src/workbench/browser/src/app/pages/setting/setting.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { NzTabPosition } from 'ng-zorro-antd/tabs'; | ||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'eo-setting', | ||
templateUrl: './setting.component.html', | ||
styleUrls: ['./setting.component.scss'], | ||
}) | ||
export class SettingComponent implements OnInit { | ||
isVisible = false; | ||
isModal = false; | ||
position: NzTabPosition = 'left'; | ||
modules = []; | ||
settings = {}; | ||
validateForm!: FormGroup; | ||
constructor( | ||
private fb: FormBuilder, | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.init(); | ||
} | ||
|
||
private init() { | ||
if (window.eo && window.eo.getFeature) { | ||
this.isVisible = true; | ||
this.settings = window.eo.getSettings(); | ||
const featureList = window.eo.getFeature('configuration'); | ||
const controls = {}; | ||
featureList?.forEach((feature: object, key: string) => { | ||
if (!feature['title'] || !feature['properties'] || typeof feature['properties'] !== 'object') { | ||
return true; | ||
} | ||
if (!this.settings[key] || typeof this.settings[key] !== 'object') { | ||
this.settings[key] = {}; | ||
} | ||
const fields = []; | ||
for (let field_key in feature['properties']) { | ||
let field = feature['properties'][field_key]; | ||
// 加入允许的type限制 | ||
if (!field['type'] || !field['label']) { | ||
continue; | ||
} | ||
if ('select' === field['type'] && !field['options']) { | ||
continue; | ||
} | ||
const name = key + '_' + field_key; | ||
field = Object.assign({ | ||
name: name, | ||
key: field_key, | ||
required: false, | ||
default: '', | ||
description: '' | ||
}, field); | ||
fields.push(field); | ||
if (!this.settings[key][field_key]) { | ||
this.settings[key][field_key] = field['default']; | ||
} | ||
// 可扩展加入更多默认校验 | ||
if (field.required) { | ||
controls[name] = [null, [Validators.required]]; | ||
} else { | ||
controls[name] = [null]; | ||
} | ||
} | ||
this.modules.push({ | ||
key: key, | ||
title: feature['title'], | ||
fields: fields, | ||
}); | ||
}); | ||
this.validateForm = this.fb.group(controls); | ||
} | ||
} | ||
|
||
handleShowModal() { | ||
this.isModal = true; | ||
} | ||
|
||
handleSave(): void { | ||
for (const i in this.validateForm.controls) { | ||
if (this.validateForm.controls.hasOwnProperty(i)) { | ||
this.validateForm.controls[i].markAsDirty(); | ||
this.validateForm.controls[i].updateValueAndValidity(); | ||
} | ||
} | ||
if (this.validateForm.status === 'INVALID') { | ||
return; | ||
} | ||
// 加入根据返回显示提示消息 | ||
const saved = window.eo.saveSettings(this.settings); | ||
if (saved) { | ||
this.handleCancel(); | ||
} | ||
} | ||
|
||
handleCancel(): void { | ||
this.isModal = false; | ||
} | ||
} |
Oops, something went wrong.