117 lines
2.7 KiB
TypeScript
117 lines
2.7 KiB
TypeScript
// comp-data.ts - definition and methods of the Data component
|
|
|
|
import { sendMsg } from './common'
|
|
|
|
// Data component
|
|
|
|
//export function Data(name, action, domain: string): object {
|
|
export function Data(name: string, conf: any): object {
|
|
const domain = conf.domain || this.conf.domain
|
|
const comp = {
|
|
id: this.conf.itemid,
|
|
name: name, // also used as class of incoming/outgoing message
|
|
action: conf.action, // (default) action of outgoing message
|
|
domain: domain, // domain of incoming/outgoing message
|
|
data: {}, // model (2-way data store)
|
|
data_bak: {}, // backup copy when leaving view or edit mode
|
|
meta: {}, // metadata (params) for each data field
|
|
mode: 'view',
|
|
handle, // handle incoming messages
|
|
exec, // default function to execute upon button click
|
|
initField, // initialize field (= @mounted)
|
|
chmode,
|
|
copynew
|
|
}
|
|
this.components[name] = comp
|
|
if (comp.id) {
|
|
sendMsg(this.conf, [domain, 'query', name, comp.id], {})
|
|
}
|
|
return comp
|
|
}
|
|
|
|
// Data methods
|
|
|
|
function handle(parent: any, domain, action, class_, item, payload: string) {
|
|
if (action == 'data') {
|
|
const data = JSON.parse(payload)
|
|
Object.assign(this.data, data)
|
|
this.id = item
|
|
this.mode = 'view'
|
|
this.data_bak = {}
|
|
} else if (action == 'response') {
|
|
this.id = item
|
|
this.mode = 'view'
|
|
this.data_bak = {}
|
|
window.location.hash = item
|
|
sendMsg(parent.conf, [this.domain, 'query', this.name, item], {})
|
|
}
|
|
}
|
|
|
|
function exec(action: string) {
|
|
action = action || this.action
|
|
const data = this.data
|
|
const conf = this.conf
|
|
let value = ''
|
|
for (const k of Object.keys(data)) {
|
|
value += `${k}: ${data[k]}, `
|
|
}
|
|
this.output += '\n' + value
|
|
console.log('exec:', value)
|
|
const msgbase = [this.domain, action, this.name]
|
|
if (this.mode != 'new') {
|
|
msgbase.push(this.id)
|
|
}
|
|
sendMsg(conf, msgbase, data)
|
|
}
|
|
|
|
function initField(name: string, meta: any) {
|
|
if (meta.defexpr) {
|
|
meta.default = eval(meta.defexpr)
|
|
}
|
|
this.data[name] = meta.default || ""
|
|
this.meta[name] = meta
|
|
}
|
|
|
|
function chmode(action: string) {
|
|
if (this.mode == action) {
|
|
return
|
|
}
|
|
if (this.mode == 'view') {
|
|
Object.assign(this.data_bak, this.data)
|
|
}
|
|
switch(action) {
|
|
case 'query':
|
|
setQuery(this)
|
|
break
|
|
case 'new':
|
|
setNew(this)
|
|
break
|
|
default:
|
|
Object.assign(this.data, this.data_bak)
|
|
}
|
|
this.mode = action
|
|
}
|
|
|
|
function copynew(action: string) {
|
|
Object.assign(this.data, this.data_bak)
|
|
this.mode = 'new'
|
|
}
|
|
|
|
// helper functions for data manipulation (used by Data component)
|
|
|
|
function setQuery(obj) {
|
|
for (const key in obj.data) {
|
|
obj.data[key] = ''
|
|
}
|
|
}
|
|
|
|
function setNew(obj) {
|
|
for (const key in obj.data) {
|
|
obj.data[key] = ''
|
|
const meta = obj.meta[key]
|
|
if (meta) {
|
|
obj.data[key] = meta.default || ''
|
|
}
|
|
}
|
|
}
|
|
|