From 90a3a7880a03ab1281d8bfcf409bdf0351e204d8 Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 31 Oct 2018 11:51:07 +0100 Subject: [PATCH] session: unwrap root widget; parser: don't parse arrays --- src/client/app/editor/index.js | 2 +- src/client/app/managers/session.js | 10 +-- src/client/app/parser.js | 92 +++++++++++------------ src/client/app/widgets/containers/root.js | 2 +- tests/test.js | 4 +- 5 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/client/app/editor/index.js b/src/client/app/editor/index.js index 3a3e1a94..38a0ecb5 100644 --- a/src/client/app/editor/index.js +++ b/src/client/app/editor/index.js @@ -818,7 +818,7 @@ var Editor = class Editor { updateWidgetFromPatch(patch, indexes) { - var [widget, subpatch] = diffToWidget(widgetManager.getWidgetById('root')[0], patch[0]), + var [widget, subpatch] = diffToWidget(widgetManager.getWidgetById('root')[0], patch), options = {} if (indexes) { diff --git a/src/client/app/managers/session.js b/src/client/app/managers/session.js index 2bd8e0c7..9318be80 100644 --- a/src/client/app/managers/session.js +++ b/src/client/app/managers/session.js @@ -40,11 +40,9 @@ var SessionManager = class SessionManager { try { - if (session[0].type != 'root') { - this.session = [{type:'root', tabs:session}] - } else if (session[0].type == 'root'){ - this.session = session - } + if (Array.isArray(session)) session = session[0] + if (session.type !== 'root') throw '' + this.session = session } catch(err) { @@ -180,7 +178,7 @@ var SessionManager = class SessionManager { } create() { - this.load([{}],function(){ + this.load({type: 'root'},function(){ require('../ui/sidepanel').open() editor.enable() editor.select(widgetManager.getWidgetById('root')) diff --git a/src/client/app/parser.js b/src/client/app/parser.js index 35b89978..65053398 100644 --- a/src/client/app/parser.js +++ b/src/client/app/parser.js @@ -36,74 +36,68 @@ var Parser = class Parser { index } = options - if (!Array.isArray(data)) data = [data] + var props = data - for (let i in data) { + // Set default widget type + props.type = tab ? 'tab' : props.type || 'fader' - var props = data[i] + // Safe copy widget's options + let defaults = this.widgets[props.type].defaults()._props() - // Set default widget type - props.type = tab ? 'tab' : props.type || 'fader' + // Set widget's undefined options to default + for (let i in defaults) { + if (i.indexOf('_')!=0 && props[i]===undefined) props[i] = defaults[i] + } - // Safe copy widget's options - let defaults = this.widgets[props.type].defaults()._props() - - // Set widget's undefined options to default - for (let i in defaults) { - if (i.indexOf('_')!=0 && props[i]===undefined) props[i] = defaults[i] + // Genrate widget's id, based on its type + if (props.id=='auto' || !props.id ) { + var id + while (!id || widgetManager.getWidgetById(id).length) { + id=props.type+'_'+this.getIterator(props.type) } + props.id = id + } - // Genrate widget's id, based on its type - if (props.id=='auto' || !props.id ) { - var id - while (!id || widgetManager.getWidgetById(id).length) { - id=props.type+'_'+this.getIterator(props.type) - } - props.id = id - } + // Generate default address + props.address = props.address == 'auto' ? '/' + props.id : props.address - // Generate default address - props.address = props.address == 'auto' ? '/' + props.id : props.address + for (let j in props) { + if (defaults[j] === undefined || j[0] === '_') delete props[j] + } - for (let j in props) { - if (defaults[j] === undefined || j[0] === '_') delete props[j] - } + // create widget + var widget = new this.widgets[props.type]({ + container:true, + props, + parent, + parentNode, + reCreateOptions, + children + }) - // create widget - var widget = new this.widgets[props.type]({ - container:true, - props, - parent, - parentNode, - reCreateOptions, - children - }) + widgetManager.addWidget(widget) - widgetManager.addWidget(widget) + widget.created(index) - widget.created(index) + // set widget's initial state + var defaultValue = widget.getProp('default'), + currentValue = widget.getProp('value') - // set widget's initial state - var defaultValue = widget.getProp('default'), - currentValue = widget.getProp('value') + if (currentValue !== '' && currentValue !== undefined) { - if (currentValue !== '' && currentValue !== undefined) { + stateManager.pushValueNewProp(widget.getProp('id'), currentValue) - stateManager.pushValueNewProp(widget.getProp('id'), currentValue) + } else if (defaultValue !== '' && defaultValue !== undefined) { - } else if (defaultValue !== '' && defaultValue !== undefined) { - - widget.setValue(defaultValue) - - } - - - parentNode.appendChild(widget.container) + widget.setValue(defaultValue) } + + parentNode.appendChild(widget.container) + // Editor needs to get the container object - if (data && data.length==1) return widget + return widget } diff --git a/src/client/app/widgets/containers/root.js b/src/client/app/widgets/containers/root.js index 548ceac7..3985525c 100644 --- a/src/client/app/widgets/containers/root.js +++ b/src/client/app/widgets/containers/root.js @@ -15,7 +15,7 @@ class Root extends Panel { traversing: {type: 'boolean', value: false, help: 'Set to `true` to enable traversing gestures in this widget. Set to `smart` or `auto` to limit affected widgets by the type of the first touched widget'}, variables: {type: '*', value: {}, help: 'Defines one or more arbitrary variables that can be inherited by children widgets'}, - tabs: {type: 'array', value: [], help: 'Each element of the array must be a tab object'}, + tabs: {type: 'array', value: [{}], help: 'Each element of the array must be a tab object'}, id: {type: 'string', value: 'root', help: 'Widgets sharing the same id will act as clones and update each other\'s value(s) without sending extra osc messages' }, value: {type: 'integer', value: '', help: [ diff --git a/tests/test.js b/tests/test.js index 5faab557..93f08f09 100644 --- a/tests/test.js +++ b/tests/test.js @@ -3,14 +3,14 @@ var fs = require('fs') // create tmp session file -var session = [{tabs:[{widgets:[]}]}], +var session = {type: 'root', tabs:[{widgets:[]}]}, sessionPath = 'test-session.json' require('../scripts/client-shim') var {widgets} = require('../src/client/app/widgets') for (let type in widgets) { - session[0].tabs[0].widgets.push({type: type}) + session.tabs[0].widgets.push({type: type}) } fs.writeFileSync(sessionPath, JSON.stringify(session))