From 26611565b4a0da0c13e7d2da2bf7360f89392632 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 25 Apr 2019 10:20:19 +0200 Subject: [PATCH] widgets: new state widget (#509) --- src/client/app/widgets/index.js | 3 +- src/client/app/widgets/switchers/state.js | 136 ++++++++++++++++++++++ src/scss/widgets/switch.scss | 10 +- 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/client/app/widgets/switchers/state.js diff --git a/src/client/app/widgets/index.js b/src/client/app/widgets/index.js index c1059aba..b5942b4e 100644 --- a/src/client/app/widgets/index.js +++ b/src/client/app/widgets/index.js @@ -47,6 +47,7 @@ module.exports.widgets = { // state switchers switcher: require('./switchers/switcher'), crossfader: require('./switchers/crossfader'), + state: require('./switchers/state'), // inputs input: require('./inputs/input'), @@ -64,6 +65,6 @@ module.exports.categories = { 'Matrices':['matrix', 'keyboard', 'patchbay'], 'Plots':['plot','eq','visualizer','led', 'rgbled','meter','text', 'image', 'svg', 'frame'], 'Containers':['panel','strip','modal', 'clone'], - 'Switchers':['switcher','crossfader'], + 'Switchers':['state', 'switcher','crossfader'], 'Inputs':['input', 'keys', 'script', 'gyroscope', 'file'] } diff --git a/src/client/app/widgets/switchers/state.js b/src/client/app/widgets/switchers/state.js new file mode 100644 index 00000000..b83e596a --- /dev/null +++ b/src/client/app/widgets/switchers/state.js @@ -0,0 +1,136 @@ +var Widget = require('../common/widget'), + Push = require('../buttons/push'), + pushDefaults = Push.defaults()._props(), + html = require('nanohtml'), + stateMananer = require('../../managers/state'), + widgetManager = require('../../managers/widgets'), + {deepCopy} = require('../../utils'), + osc = require('../../osc') + + +class State extends Widget { + + static description() { + + return 'Save and recall the state of other widgets.' + + } + + static defaults() { + + return super.defaults({ + + _state: 'state', + + filter: {type: 'string|array', value: '', help: [ + 'Only save state from widgets contained in widgets designated by id. If empty, the whole session\'s state is saved. Switchers are always ignored.', + '- widget id `string`', + '- `array` of widget id strings' + ]}, + + }, ['_value', 'default', 'value']) + + } + + + constructor(options) { + + super({...options, html: html`
`}) + + this._isSwitcher = true + + this.saveButton = new Push({props: {...pushDefaults, type: 'push', label: 'save'}, parent: this, container: true}) + this.loadButton = new Push({props: {...pushDefaults, type: 'push', label: 'load'}, parent: this, container: true}) + + this.saveButton.container.classList.add('not-editable') + this.loadButton.container.classList.add('not-editable') + this.loadButton.container.classList.add('disabled') + + this.widget.appendChild(this.saveButton.container) + this.widget.appendChild(this.loadButton.container) + + this.on('change', (e)=>{ + + var {widget} = e + if (widget === this) return + + if (widget.active) this.setValue(widget === this.saveButton ? 'save' : 'load', {sync: true, send: true}) + + }) + + this.filter = this.getProp('filter') ? Array.isArray(this.getProp('filter')) ? this.getProp('filter') : [this.getProp('filter')] : false + this.value = null + + } + + save() { + + var filter + if (this.filter) { + var containers = this.filter.map(x=>widgetManager.getWidgetById(x)).reduce((a,b)=>a.concat(b), []) + if (!containers.length) return + filter = (widget)=>{ + return !widget.isSwitcher && containers.some(x=>x.contains(widget) || x === widget) + } + } + + if (!this.value) this.loadButton.container.classList.remove('disabled') + + this.value = deepCopy(stateMananer.get(filter)) + + } + + load(send) { + + stateMananer.set(this.value, send) + + } + + setValue(v,options={}) { + + var valid + + if (v === 'save') { + + this.save() + valid = true + + } else if (v === 'load') { + + this.load(options.send) + valid = true + + } else if (typeof v === 'object' && v !== null && !Array.isArray(v)) { + + if (!this.value) this.loadButton.container.classList.remove('disabled') + this.value = v + valid = true + + } + + if (valid) { + if (options.sync) this.changed(options) + if (options.send) this.sendValue() + } + + } + + sendValue() { + + osc.sync({ + h:this.hash, + v:this.value + }) + + } + + onRemove() { + + this.saveButton.onRemove() + this.loadButton.onRemove() + super.onRemove() + + } +} + +module.exports = State diff --git a/src/scss/widgets/switch.scss b/src/scss/widgets/switch.scss index 21f5ee3b..dd281913 100644 --- a/src/scss/widgets/switch.scss +++ b/src/scss/widgets/switch.scss @@ -1,4 +1,4 @@ -.switch-container, .switcher-container, .crossfader-container { +.state-container, .switch-container, .switcher-container, .crossfader-container { z-index: 1; width:100rem; height:100rem; @@ -206,3 +206,11 @@ } } + +.state-container { + .disabled { + opacity: 0.65; + pointer-events: none; + cursor: default; + } +}