diff --git a/assets/js/common.ts b/assets/js/common.ts index 3d85570..7e0cc6c 100644 --- a/assets/js/common.ts +++ b/assets/js/common.ts @@ -50,6 +50,7 @@ const appdata = { } // components +// (move to separate module(s)) function Data(name, action, domain: string): object { domain = domain || this.conf.domain @@ -58,9 +59,13 @@ function Data(name, action, domain: string): object { action: 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', - exec, // default function to execute upon button click - chmode + mounted, + chmode, + copynew, + exec // default function to execute upon button click } this.components[name] = comp return comp @@ -68,10 +73,38 @@ function Data(name, action, domain: string): object { // appdata and component method definitions +function mounted(name: string, meta: any) { + console.log('name: ', name, ', meta: ', meta) + this.data[name] = meta.default || eval(meta.defexpr) || "" + 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.data.id = '' + this.mode = 'new' +} + function exec(action: string) { action = action || this.action const data = this.data @@ -102,6 +135,25 @@ function poll() { dopoll(this) } +// helper functions for data manipulation + +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 || '' + } + } + obj.data.id = '' +} + // basic functions - move to api.ts async function sendMsg(conf: Config, basemsg: string[], data: any) { diff --git a/exampleSite/content/app/person.md b/exampleSite/content/app/person.md index f5872ab..a96a96f 100644 --- a/exampleSite/content/app/person.md +++ b/exampleSite/content/app/person.md @@ -17,14 +17,14 @@ cyberscopes example site - view / edit person (user) data. {{< pv/tabs-mode >}} - Id: {{< pv/input-textline name="id" defexpr="conf.itemid" >}} + Id: {{< pv/input-textline name="id" defexpr="this.conf.itemid" >}} First Name: {{< pv/input-textline name="firstname" attrs="autofocus" >}} Last Name: {{< pv/input-textline name="lastname" >}} Email: {{< pv/input-textline name="email" default="hm@cy55.de" >}} {{< pv/button mode="query" action="query" label="Execute Query" >}} {{< pv/button mode="edit" label="Save Changes" >}} - {{< pv/button mode="edit" action="copy" label="Copy Data" >}} + {{< pv/button mode="edit" exec="copynew" label="Copy Data" >}} {{< pv/button mode="edit" action="delete" label="Delete Item" >}} {{< pv/button mode="new" label="Create Item" >}} diff --git a/layouts/shortcodes/pv/button.html b/layouts/shortcodes/pv/button.html index 2b9e670..9ab3a01 100644 --- a/layouts/shortcodes/pv/button.html +++ b/layouts/shortcodes/pv/button.html @@ -3,10 +3,8 @@ {{- $label := .Get "label" | default (title $name) -}} {{- $exec := .Get "exec" | default "exec" -}} {{- $action := .Get "action" | default "" -}} -{{- $mode := .Get "mode" | default "view" -}} +{{- $mode := .Get "mode" -}} diff --git a/layouts/shortcodes/pv/input-textline.html b/layouts/shortcodes/pv/input-textline.html index a5d1346..4dea309 100644 --- a/layouts/shortcodes/pv/input-textline.html +++ b/layouts/shortcodes/pv/input-textline.html @@ -2,11 +2,12 @@ {{- $name := .Get "name" | default "textline" -}} {{- $default := .Get "default" | default "" -}} {{- $defexpr := .Get "defexpr" | default (printf "'%s'" $default) -}} +{{- $meta := $.Params | jsonify -}}