diff --git a/assets/js/comp-data.ts b/assets/js/comp-data.ts index c92f575..94bf90d 100644 --- a/assets/js/comp-data.ts +++ b/assets/js/comp-data.ts @@ -1,5 +1,6 @@ // comp-data.ts - definition and methods of the Data component +import { reactive } from 'petite-vue' import { sendMsg } from './common' // Data component @@ -8,23 +9,24 @@ import { sendMsg } from './common' export function Data(name: string, conf: any): object { const domain = conf.domain || this.conf.domain const comp = { - id: this.conf.itemid, + state: { + id: this.conf.itemid, + mode: 'view' + }, 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], {}) + this.components[name] = reactive(comp) + if (comp.state.id) { + sendMsg(this.conf, [domain, 'query', name, comp.state.id], {}) } return comp } @@ -32,16 +34,12 @@ export function Data(name: string, conf: any): object { // Data methods function handle(parent: any, domain, action, class_, item, payload: string) { - if (action == 'data') { + if (action == 'data') { // && this.state.id const data = JSON.parse(payload) Object.assign(this.data, data) - this.id = item - this.mode = 'view' - this.data_bak = {} + this.state.mode = 'view' } else if (action == 'response') { - this.id = item - this.mode = 'view' - this.data_bak = {} + this.state.id = item window.location.hash = item sendMsg(parent.conf, [this.domain, 'query', this.name, item], {}) } @@ -59,7 +57,7 @@ function exec(action: string) { console.log('exec:', value) const msgbase = [this.domain, action, this.name] if (this.mode != 'new') { - msgbase.push(this.id) + msgbase.push(this.state.id) } sendMsg(conf, msgbase, data) } @@ -73,12 +71,9 @@ function initField(name: string, meta: any) { } function chmode(action: string) { - if (this.mode == action) { + if (this.state.mode == action) { return } - if (this.mode == 'view') { - Object.assign(this.data_bak, this.data) - } switch(action) { case 'query': setQuery(this) @@ -86,15 +81,12 @@ function chmode(action: string) { case 'new': setNew(this) break - default: - Object.assign(this.data, this.data_bak) } - this.mode = action + this.state.mode = action } function copynew(action: string) { - Object.assign(this.data, this.data_bak) - this.mode = 'new' + this.state.mode = 'new' } // helper functions for data manipulation (used by Data component) diff --git a/assets/js/comp-list.ts b/assets/js/comp-list.ts index a886b13..194369b 100644 --- a/assets/js/comp-list.ts +++ b/assets/js/comp-list.ts @@ -8,7 +8,10 @@ import { sendMsg } from './common' export function List(name: string, conf: any): object { const domain = conf.domain || this.conf.domain const comp = { - id: this.conf.itemid, + state: { + id: this.conf.itemid, + mode: 'view' + }, 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 diff --git a/exampleSite/content/app/person.md b/exampleSite/content/app/person.md index 1bac950..8bbe1cc 100644 --- a/exampleSite/content/app/person.md +++ b/exampleSite/content/app/person.md @@ -27,11 +27,11 @@ cyberscopes example site - view / edit person (user) data. {{< pv/button mode="new" label="Create Item" >}}
- {{< pv/display linkto="id" + {{< pv/display linkto="state.id" expr="`${data.firstname} ${data.lastname}`.trim() || '???'" >}}
{{< pv/link >}} - Reload page with id {{< pv/display name="id" >}} + Reload page with id {{< pv/display expr="state.id" >}} {{< /pv/link >}} {{< /pv/fieldset >}} diff --git a/layouts/shortcodes/pv/button.html b/layouts/shortcodes/pv/button.html index 9ab3a01..a6fe623 100644 --- a/layouts/shortcodes/pv/button.html +++ b/layouts/shortcodes/pv/button.html @@ -5,6 +5,6 @@ {{- $action := .Get "action" | default "" -}} {{- $mode := .Get "mode" -}} diff --git a/layouts/shortcodes/pv/input-textline.html b/layouts/shortcodes/pv/input-textline.html index c97a502..0ec0a86 100644 --- a/layouts/shortcodes/pv/input-textline.html +++ b/layouts/shortcodes/pv/input-textline.html @@ -4,7 +4,7 @@
diff --git a/layouts/shortcodes/pv/link.html b/layouts/shortcodes/pv/link.html index 9f987d0..c2cec8e 100644 --- a/layouts/shortcodes/pv/link.html +++ b/layouts/shortcodes/pv/link.html @@ -1,6 +1,3 @@ -{{- $target := .Get "target" | default "id" -}} +{{- $target := .Get "target" | default "state.id" -}} {{- $prefix := .Get "prefix" | default "#" -}} - this.id = item - this.mode = 'view' - this.data_bak = {} {{ .Inner }} diff --git a/layouts/shortcodes/pv/tabs-mode.md b/layouts/shortcodes/pv/tabs-mode.md index f2bd3f2..4a5c879 100644 --- a/layouts/shortcodes/pv/tabs-mode.md +++ b/layouts/shortcodes/pv/tabs-mode.md @@ -1,15 +1,15 @@