mutants-appfx
Version:
appfx module
84 lines (77 loc) • 2.14 kB
JavaScript
import {
i18n
} from 'mutants-microfx';
const t = i18n.getFixedT(null, 'appfx');
// 抽象UI交互控制,只支持用户输入和反馈两种形式
export default class UIController {
viewModel;
static defaultHandlers = [];
static reginsterHandler(obj) {
UIController.defaultHandlers.push(obj);
}
_init(viewModel) {
this.viewModel = viewModel;
}
// 需要用户录入字段
async input(fieldNames, context, ...args) {
if (!Array.isArray(fieldNames)) {
fieldNames = [fieldNames];
}
if (!context) {
console.error(t('未能获取用户输入,输入对象未知'));
}
let ret;
for (const it of this.viewModel.items) {
if (typeof it.triggerFieldInput === 'function' && await it.triggerFieldInput(fieldNames, context, args)) {
ret = true;
break;
}
}
if (!ret) {
for (const it of UIController.defaultHandlers) {
if (typeof it.triggerFieldInput === 'function' && await it.triggerFieldInput(fieldNames, context, args)) {
ret = true;
break;
}
}
}
if (ret) {
console.warn(t('未能获取用户输入,编辑器未定义'));
}
}
// 向用户反馈信息
async feedback(message, type = 'info') {
if (!message) {
console.error(t('未向用户反馈,反馈内容不能为空'));
}
if (!type) {
console.error(t('未向用户反馈,反馈类型不能为空'));
}
let fbObj;
for (const it of this.viewModel.items) {
if (typeof it.triggerFeedback === 'function') {
fbObj = await it.triggerFeedback(message, type);
if (fbObj){
break;
}
}
}
if (!fbObj) {
for (const it of UIController.defaultHandlers) {
if (typeof it.triggerFeedback === 'function') {
fbObj = await it.triggerFeedback(message, type);
if (fbObj){
break;
}
}
}
}
if (!fbObj) {
console.warn(t('未向用户反馈,反馈形式未定义'));
}else{
if (typeof fbObj === 'object'){
return fbObj.result;
}
}
}
}