big clean, multi point touchstate, auto update check toggle, rm switchers, add state fn to scripts
This commit is contained in:
parent
d776922b24
commit
f797988c64
51 changed files with 735 additions and 3591 deletions
|
|
@ -3,7 +3,7 @@ var Widget = require('../common/widget'),
|
|||
html = require('nanohtml'),
|
||||
{deepEqual, isJSON} = require('../../utils')
|
||||
|
||||
class Toggle extends Widget {
|
||||
class Button extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ class Toggle extends Widget {
|
|||
return super.defaults({
|
||||
|
||||
|
||||
_toggle: 'toggle',
|
||||
_button: 'button',
|
||||
|
||||
on: {type: '*', value: 1, help: [
|
||||
'Set to `null` to send send no argument in the osc message',
|
||||
|
|
@ -177,14 +177,14 @@ class Toggle extends Widget {
|
|||
|
||||
}
|
||||
|
||||
Toggle.cssVariables = Toggle.prototype.constructor.cssVariables.concat(
|
||||
Button.cssVariables = Button.prototype.constructor.cssVariables.concat(
|
||||
{js: 'colorTextOn', css: '--color-text-on'}
|
||||
)
|
||||
|
||||
Toggle.dynamicProps = Toggle.prototype.constructor.dynamicProps.concat(
|
||||
Button.dynamicProps = Button.prototype.constructor.dynamicProps.concat(
|
||||
'on',
|
||||
'off',
|
||||
'norelease'
|
||||
)
|
||||
|
||||
module.exports = Toggle
|
||||
module.exports = Button
|
||||
|
|
@ -18,6 +18,7 @@ class Dropdown extends Widget {
|
|||
|
||||
_dropdown:'dropdown',
|
||||
|
||||
align: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change text alignment (otherwise center)'},
|
||||
values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [
|
||||
'`Array` of possible values to switch between : `[1,2,3]`',
|
||||
'`Object` of label:value pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
|
||||
|
|
@ -36,6 +37,9 @@ class Dropdown extends Widget {
|
|||
</inner>
|
||||
`})
|
||||
|
||||
if (this.getProp('align') === 'left') this.widget.classList.add('left')
|
||||
if (this.getProp('align') === 'right') this.widget.classList.add('right')
|
||||
|
||||
this.select = this.widget.appendChild(html`<select class="no-keybinding"></select>`)
|
||||
this.text = DOM.get(this.widget, '.text')[0]
|
||||
|
||||
|
|
@ -18,6 +18,7 @@ module.exports = class File extends Widget {
|
|||
|
||||
_input:'input',
|
||||
|
||||
align: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change text alignment (otherwise center)'},
|
||||
directory: {type: 'string', value: 'auto', help: 'Default browsing directory'},
|
||||
extension: {type: 'string', value: '*', help: 'Only display files with this extension'},
|
||||
hidePath: {type: 'boolean', value: false, help: 'Set to `true` to only display the filename (the whole path will still be used as value)'},
|
||||
|
|
@ -30,10 +31,16 @@ module.exports = class File extends Widget {
|
|||
constructor(options) {
|
||||
|
||||
super({...options, html: html`
|
||||
<div class="file btn">${raw(icon('folder-open'))}<span></span></div>
|
||||
<inner>
|
||||
<div class="text"></div>
|
||||
<div class="icon"></div>
|
||||
</inner>
|
||||
`})
|
||||
|
||||
this.text = DOM.get(this.widget, 'span')[0]
|
||||
if (this.getProp('align') === 'left') this.widget.classList.add('left')
|
||||
if (this.getProp('align') === 'right') this.widget.classList.add('right')
|
||||
|
||||
this.text = DOM.get(this.widget, '.text')[0]
|
||||
this.widget.addEventListener('fast-click', (e)=>{
|
||||
|
||||
if (e.capturedByEditor === true) return
|
||||
|
|
@ -22,6 +22,7 @@ class Menu extends Widget {
|
|||
'- If `layout` is `circular`: diameter (in px)',
|
||||
'- Else: square size or `[width, height]` array',
|
||||
]},
|
||||
align: {type: 'string', value: 'center', choices: ['center', 'left', 'right'], help: 'Set to `left` or `right` to change text alignment (otherwise center)'},
|
||||
layout: {type: 'string', value: 'circular', choices: ['circular', 'horizontal', 'vertical', 'grid'], help: [
|
||||
'Defines whether the menu\'s layout should be rendered in a circle or in a box'
|
||||
]},
|
||||
|
|
@ -51,6 +52,9 @@ class Menu extends Widget {
|
|||
|
||||
`})
|
||||
|
||||
if (this.getProp('align') === 'left') this.widget.classList.add('left')
|
||||
if (this.getProp('align') === 'right') this.widget.classList.add('right')
|
||||
|
||||
this.menu = html`<menu></menu>`
|
||||
this.text = DOM.get(this.widget, '.text')[0]
|
||||
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
var _matrix_base = require('./_matrix_base'),
|
||||
Panel = require('../containers/panel'),
|
||||
var Panel = require('./panel'),
|
||||
Widget = require('../common/widget'),
|
||||
parser = require('../../parser')
|
||||
|
||||
|
|
@ -1,39 +1,23 @@
|
|||
module.exports.widgets = {
|
||||
// tabs
|
||||
tab: require('./containers/tab'),
|
||||
|
||||
// basics
|
||||
button: require('./basics/button'),
|
||||
switch: require('./basics/switch'),
|
||||
dropdown: require('./basics/dropdown'),
|
||||
menu: require('./basics/menu'),
|
||||
input: require('./basics/input'),
|
||||
file: require('./basics/file'),
|
||||
|
||||
// containers
|
||||
panel: require('./containers/panel'),
|
||||
root: require('./containers/root'),
|
||||
|
||||
// sliders
|
||||
fader: require('./sliders/fader'),
|
||||
knob: require('./sliders/knob'),
|
||||
encoder: require('./sliders/encoder'),
|
||||
range: require('./sliders/range'),
|
||||
|
||||
// buttons
|
||||
toggle: require('./buttons/toggle'),
|
||||
switch: require('./buttons/switch'),
|
||||
dropdown: require('./buttons/dropdown'), // redo as menu derivate ?
|
||||
menu: require('./buttons/menu'), // redo using canvas
|
||||
|
||||
// pads
|
||||
xy: require('./pads/xy'),
|
||||
rgb: require('./pads/rgb'),
|
||||
multixy: require('./pads/multixy'),
|
||||
canvas: require('./pads/canvas'), // implement...
|
||||
|
||||
// matrices
|
||||
keyboard: require('./matrices/keyboard'),
|
||||
patchbay: require('./matrices/patchbay').PatchBay,
|
||||
patchbaynode: require('./matrices/patchbay').PatchBayNode,
|
||||
|
||||
// indicators
|
||||
led: require('./indicators/led'),
|
||||
text: require('./indicators/text'),
|
||||
|
||||
// graphs
|
||||
plot: require('./graphs/plot'),
|
||||
eq: require('./graphs/eq'),
|
||||
visualizer: require('./graphs/visualizer'),
|
||||
tab: require('./containers/tab'),
|
||||
modal: require('./containers/modal'),
|
||||
clone: require('./containers/clone'),
|
||||
matrix: require('./containers/matrix'),
|
||||
keyboard: require('./containers/keyboard'),
|
||||
patchbay: require('./containers/patchbay').PatchBay,
|
||||
patchbaynode: require('./containers/patchbay').PatchBayNode,
|
||||
|
||||
// frames
|
||||
image: require('./frames/image'),
|
||||
|
|
@ -41,37 +25,41 @@ module.exports.widgets = {
|
|||
html: require('./frames/html'),
|
||||
frame: require('./frames/frame'),
|
||||
|
||||
// containers
|
||||
panel: require('./containers/panel'),
|
||||
modal: require('./containers/modal'),
|
||||
clone: require('./containers/clone'),
|
||||
matrix: require('./containers/matrix'),
|
||||
// graphs
|
||||
plot: require('./graphs/plot'),
|
||||
eq: require('./graphs/eq'),
|
||||
visualizer: require('./graphs/visualizer'),
|
||||
|
||||
// state switchers
|
||||
switcher: require('./switchers/switcher'),
|
||||
crossfader: require('./switchers/crossfader'),
|
||||
state: require('./switchers/state'),
|
||||
// indicators
|
||||
led: require('./indicators/led'),
|
||||
text: require('./indicators/text'),
|
||||
|
||||
// inputs
|
||||
input: require('./inputs/input'),
|
||||
keys: require('./inputs/keys'),
|
||||
script: require('./inputs/script'),
|
||||
gyroscope: require('./inputs/gyroscope'),
|
||||
file: require('./inputs/file'),
|
||||
// pads
|
||||
xy: require('./pads/xy'),
|
||||
rgb: require('./pads/rgb'),
|
||||
multixy: require('./pads/multixy'),
|
||||
canvas: require('./pads/canvas'),
|
||||
|
||||
// sliders
|
||||
fader: require('./sliders/fader'),
|
||||
knob: require('./sliders/knob'),
|
||||
encoder: require('./sliders/encoder'),
|
||||
range: require('./sliders/range'),
|
||||
|
||||
// scripts
|
||||
script: require('./scripts/script'),
|
||||
|
||||
}
|
||||
|
||||
module.exports.categories = {
|
||||
'Buttons':['toggle', 'switch', 'dropdown', 'menu'],
|
||||
'Containers':['panel', 'modal', 'clone', 'matrix'],
|
||||
'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'file'],
|
||||
'Containers':['panel', 'modal', 'clone', 'matrix', 'keyboard', 'patchbay'],
|
||||
'Frames':['frame', 'svg', 'html', 'image'],
|
||||
'Graphs':['plot','eq','visualizer'],
|
||||
'Indicators':['led', 'text'],
|
||||
'Inputs':['input', 'keys', 'script', 'gyroscope', 'file'],
|
||||
'Matrices':['keyboard', 'patchbay'],
|
||||
'Pads':['xy','rgb','multixy', 'canvas'],
|
||||
'Sliders':['fader','knob', 'encoder', 'range'],
|
||||
'Switchers':['state', 'switcher','crossfader'],
|
||||
'Scripts':['script'],
|
||||
}
|
||||
|
||||
var defaults = {}
|
||||
|
|
|
|||
|
|
@ -1,157 +0,0 @@
|
|||
var Widget = require('../common/widget'),
|
||||
{icon} = require('../../ui/utils'),
|
||||
keyboardJS = require('keyboardjs'),
|
||||
html = require('nanohtml'),
|
||||
raw = require('nanohtml/raw')
|
||||
|
||||
class Keys extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Keyboard binding.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
return super.defaults({
|
||||
|
||||
_keys:'keys',
|
||||
|
||||
binding: {type: 'string|array', value: '', help: 'Key combo `string` or `array` of strings (see <a href="https://github.com/RobertWHurst/KeyboardJS">KeyboardJS</a> documentation)'},
|
||||
keydown: {type: 'string', value: '', help: [
|
||||
'This property is evaluated each time the key combo is pressed and defines the widget\'s own value. Formulas are given extras variables in this context:',
|
||||
'- `key`: pressed key name (usefull for handling multiple keys with a single keys widget, lowercased when referencing a character key)',
|
||||
'- `code`: pressed key code name (<a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#Code_values">full list</a>)',
|
||||
'- `ctrl`: control key state',
|
||||
'- `alt`: alt key state',
|
||||
'- `shift`: shift key state',
|
||||
'- `meta`: command/windows key state'
|
||||
]},
|
||||
keyup: {type: 'string', value: '', help: 'Same as `keydown`, but evaluated when releasing the key combo'},
|
||||
repeat: {type: 'boolean', value: true, help: 'Set to `false` to prevent keydown repeats when holding the key combo pressed'},
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: null})
|
||||
|
||||
if (this.getProp('binding')) {
|
||||
|
||||
this.keyDownHandler = this.keyDown.bind(this)
|
||||
this.keyUpHandler = this.keyUp.bind(this)
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.bind(this.getProp('binding'), this.keyDownHandler, this.keyUpHandler)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onRemove() {
|
||||
|
||||
super.onRemove()
|
||||
|
||||
if (this.getProp('binding')) {
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.unbind(this.getProp('binding'), this.keyDownHandler, this.keyUpHandler)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
keyDown(e) {
|
||||
|
||||
if (e.target && (e.target.tagName === 'INPUT' || e.target.tabName === 'TEXTAREA' || e.target.tabName === 'SELECT')) return
|
||||
|
||||
if (!this.getProp('repeat') && e) e.preventRepeat()
|
||||
|
||||
e.preventDefault()
|
||||
|
||||
if (this.getProp('keydown') !== '') {
|
||||
|
||||
var context = {
|
||||
key: e.key,
|
||||
code: e.code,
|
||||
ctrl: e.ctrlKey,
|
||||
shift: e.shiftKey,
|
||||
alt: e.altKey,
|
||||
meta: e.metaKey
|
||||
}
|
||||
|
||||
if (context.key.length === 1) {
|
||||
context.key = context.key.toLowerCase()
|
||||
}
|
||||
|
||||
this.setValue(this.resolveProp('keydown', undefined, false, false, false, context), {sync: true, send: true})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
keyUp(e) {
|
||||
|
||||
if (e.target && (e.target.tagName === 'INPUT' || e.target.tabName === 'TEXTAREA' || e.target.tabName === 'SELECT')) return
|
||||
|
||||
e.preventDefault()
|
||||
|
||||
if (this.getProp('keyup') !== '') {
|
||||
|
||||
var context = {
|
||||
key: e.key,
|
||||
code: e.code,
|
||||
ctrl: e.ctrlKey,
|
||||
shift: e.shiftKey,
|
||||
alt: e.altKey,
|
||||
meta: e.metaKey
|
||||
}
|
||||
|
||||
if (context.key.length === 1) {
|
||||
context.key = context.key.toLowerCase()
|
||||
}
|
||||
|
||||
this.setValue(this.resolveProp('keyup', undefined, false, false, false, context), {sync: true, send: true})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options = {}) {
|
||||
|
||||
this.value = v
|
||||
|
||||
// if (options.send) this.sendValue()
|
||||
if (options.sync) this.changed(options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Keys.parsersContexts.keydown = Keys.parsersContexts.keyup = {
|
||||
key: 'x',
|
||||
code: 'x',
|
||||
ctrl: false,
|
||||
alt: false,
|
||||
shift: false,
|
||||
meta: false
|
||||
}
|
||||
|
||||
Keys.dynamicProps = Keys.prototype.constructor.dynamicProps.concat(
|
||||
'keydown',
|
||||
'keyup',
|
||||
'repeat'
|
||||
)
|
||||
|
||||
module.exports = Keys
|
||||
|
|
@ -1,179 +0,0 @@
|
|||
var Widget = require('../common/widget'),
|
||||
widgetManager = require('../../managers/widgets'),
|
||||
{icon} = require('../../ui/utils'),
|
||||
html = require('nanohtml'),
|
||||
raw = require('nanohtml/raw'),
|
||||
{urlParser} = require('../utils')
|
||||
|
||||
|
||||
class Script extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Evaluates a script each time it receives a value.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
return super.defaults({
|
||||
|
||||
_script:'script',
|
||||
|
||||
condition: {type: 'string', value: 1, help: [
|
||||
'When the widget receives a value, if this property return a falsy value, the script property won\'t be evaluated. If it\'s non-falsy, it will be evaluated normally. Formulas are given one extra variable in this context:',
|
||||
'- `value`: the value received by the widget'
|
||||
]},
|
||||
script: {type: 'string', value: '', help: [
|
||||
'This property is evaluated each time the widget receives a value* if condition is non-falsy. Formulas are given extras variables in this context:',
|
||||
'- `value`: value received by the widget',
|
||||
'- `id`: id of the widget that triggered the script',
|
||||
'- `send(target, address, arg1, arg2, ...)`: function for sending osc messages (ignores the script\'s targets and the server\'s defaults unless `target` is `false`; ignores the script\'s `preArgs`)',
|
||||
'- `set(id, value)`: function for setting a widget\'s value',
|
||||
'- `get(id)`: function for getting a widget\'s value (dynamic equivalent of @{id})',
|
||||
'- `getProp(id, property)`: function for getting a widget\'s property value ((dynamic equivalent of @{id.property})',
|
||||
'- `httpGet(url, callback)`: function for making http requests (asynchronous "GET" request and local urls only)',
|
||||
'',
|
||||
'* Note: `value` or `linkId` properties can be used to receive other widgets\' values. The `value` property must actually change to trigger the execution, where linked widgets via `linkId` can submit the same value over and over and trigger the execution',
|
||||
]}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: html`
|
||||
<div class="script">
|
||||
${raw(icon('code'))}
|
||||
</div>
|
||||
`})
|
||||
|
||||
this.scriptLock = false
|
||||
|
||||
}
|
||||
|
||||
|
||||
static scriptSet(id, value, options) {
|
||||
|
||||
if (id == options.id) options.sync = false
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
return widgets[i].setValue(value, options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static scriptGet(id) {
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
if (widgets[i].getValue) {
|
||||
|
||||
var v = widgets[i].getValue()
|
||||
if (v !== undefined) return v
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static scriptGetProp(id, prop) {
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
var v = widgets[i].getProp(prop)
|
||||
if (v !== undefined) return v
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
scriptSend(target, address, ...args) {
|
||||
|
||||
var overrides = {
|
||||
address,
|
||||
v: args,
|
||||
preArgs: []
|
||||
}
|
||||
|
||||
if (target) overrides.target = Array.isArray(target) ? target : [target]
|
||||
|
||||
this.sendValue(overrides)
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={} ) {
|
||||
|
||||
if (this.scriptLock) return
|
||||
|
||||
var context = {
|
||||
value: v,
|
||||
id: options.id,
|
||||
send: options.send ? this.scriptSend.bind(this) : ()=>{},
|
||||
set: (id, value)=>{Script.scriptSet(id, value, options)},
|
||||
get: (id)=>{return Script.scriptGet(id)},
|
||||
getProp: (id, prop)=>{return Script.scriptGetProp(id, prop)},
|
||||
httpGet: (url, callback)=>{
|
||||
var parser = urlParser(url),
|
||||
err = (e)=>{console.error(e)}
|
||||
|
||||
if (!parser.isLocal()) return err('httpGet error (' + url + '): non-local url')
|
||||
|
||||
var xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', url, true)
|
||||
xhr.onload = (e)=>{
|
||||
if (xhr.readyState === 4 && xhr.status === 200 && callback) {
|
||||
callback(xhr.responseText)
|
||||
}
|
||||
}
|
||||
xhr.onerror = (e)=>err('httpGet error (' + url + '): ' + xhr.statusText)
|
||||
xhr.send(null)
|
||||
}
|
||||
}
|
||||
|
||||
var condition = this.resolveProp('condition', undefined, false, false, false, {value: v})
|
||||
|
||||
if (condition) {
|
||||
this.scriptLock = true
|
||||
this.resolveProp('script', undefined, false, false, false, context)
|
||||
this.scriptLock = false
|
||||
}
|
||||
|
||||
|
||||
// if (options.send) this.sendValue()
|
||||
if (options.sync) this.changed(options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Script.parsersContexts.condition = {
|
||||
value: ''
|
||||
}
|
||||
|
||||
Script.parsersContexts.script = {
|
||||
value: '',
|
||||
id: '',
|
||||
send: ()=>{},
|
||||
set: ()=>{},
|
||||
get: ()=>{},
|
||||
getProp: ()=>{},
|
||||
httpGet: ()=>{}
|
||||
}
|
||||
|
||||
Script.dynamicProps = Script.prototype.constructor.dynamicProps.concat(
|
||||
'condition',
|
||||
'script'
|
||||
)
|
||||
|
||||
module.exports = Script
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
var Container = require('../common/container'),
|
||||
{enableTraversingGestures} = require('../../events/drag'),
|
||||
html = require('nanohtml')
|
||||
|
||||
|
||||
module.exports = class _matrix_base extends Container() {
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: html`<div class="matrix"></div>`})
|
||||
|
||||
this.value = []
|
||||
|
||||
if (this.getProp('traversing')) enableTraversingGestures(this.widget)
|
||||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
||||
switch (propName) {
|
||||
|
||||
case 'colorWidget':
|
||||
case 'colorFill':
|
||||
case 'colorStroke':
|
||||
case 'alphaStroke':
|
||||
case 'alphaFillOff':
|
||||
case 'alphaFillOn':
|
||||
for (var w of this.children) {
|
||||
if (w) w.onPropChanged(propName)
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -81,7 +81,6 @@ module.exports = class MultiXy extends Pad {
|
|||
|
||||
}
|
||||
|
||||
|
||||
this.value = []
|
||||
|
||||
this.padsCoords = []
|
||||
|
|
@ -119,6 +118,13 @@ module.exports = class MultiXy extends Pad {
|
|||
|
||||
e.stopPropagation = true
|
||||
|
||||
if (this.getProp('touchAddress')) {
|
||||
this.sendValue({
|
||||
address: this.getProp('touchAddress'),
|
||||
v: [parseInt(id), 1],
|
||||
})
|
||||
}
|
||||
|
||||
this.pads[id].trigger('draginit', e)
|
||||
|
||||
}, {element: this.widget, multitouch: true})
|
||||
|
|
@ -143,6 +149,13 @@ module.exports = class MultiXy extends Pad {
|
|||
|
||||
this.pads[i].trigger('dragend', e)
|
||||
|
||||
if (this.getProp('touchAddress')) {
|
||||
this.sendValue({
|
||||
address: this.getProp('touchAddress'),
|
||||
v: [parseInt(this.touchMap[e.pointerId]), 0],
|
||||
})
|
||||
}
|
||||
|
||||
delete this.touchMap[e.pointerId]
|
||||
|
||||
}, {element: this.widget, multitouch: true})
|
||||
|
|
@ -153,7 +166,6 @@ module.exports = class MultiXy extends Pad {
|
|||
this.setValue(this.getValue(), e.options)
|
||||
})
|
||||
|
||||
|
||||
var v = []
|
||||
for (let i = 0; i < this.npoints * 2; i = i + 2) {
|
||||
[v[i],v[i+1]] = this.pads[i/2].getValue()
|
||||
|
|
|
|||
|
|
@ -80,7 +80,10 @@ module.exports = class Xy extends Pad {
|
|||
e.stopPropagation = true
|
||||
})
|
||||
|
||||
this.value = []
|
||||
|
||||
touchstate(this, {element: this.canvas})
|
||||
|
||||
this.active = false
|
||||
|
||||
this.on('draginit',(e)=>{
|
||||
|
|
|
|||
174
src/client/widgets/scripts/script-vm.js
Normal file
174
src/client/widgets/scripts/script-vm.js
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
var widgetManager = require('../../managers/widgets'),
|
||||
stateManager = require('../../managers/state'),
|
||||
{deepCopy} = require('../../utils'),
|
||||
{urlParser} = require('../utils'),
|
||||
Vm = require('../vm')
|
||||
|
||||
|
||||
class ScriptVm extends Vm {
|
||||
|
||||
constructor() {
|
||||
|
||||
super()
|
||||
|
||||
this.valueOptions = []
|
||||
this.sendWidget = []
|
||||
|
||||
}
|
||||
|
||||
getValueOptions() {
|
||||
|
||||
return this.valueOptions[this.valueOptions.length - 1] || {send: true, sync: true}
|
||||
|
||||
}
|
||||
|
||||
setValueOptions(options) {
|
||||
|
||||
if (!options) this.valueOptions.pop()
|
||||
else this.valueOptions.push(options)
|
||||
|
||||
}
|
||||
|
||||
setSendWidget(widget) {
|
||||
|
||||
if (!widget) this.sendWidget.pop()
|
||||
else this.sendWidget.push(widget)
|
||||
|
||||
}
|
||||
|
||||
getSendWidget() {
|
||||
|
||||
return this.sendWidget[this.sendWidget.length - 1]
|
||||
|
||||
}
|
||||
|
||||
registerGlobals() {
|
||||
|
||||
super.registerGlobals()
|
||||
|
||||
this.sandbox.contentWindow.set = (id, value)=>{
|
||||
|
||||
var options = this.getValueOptions()
|
||||
|
||||
if (id === options.id) options.sync = false // loop stop
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
return widgets[i].setValue(value, options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.send = (target, address, ...args)=>{
|
||||
|
||||
var options = this.getValueOptions()
|
||||
|
||||
if (!options.send) return
|
||||
|
||||
var overrides = {
|
||||
address,
|
||||
v: args,
|
||||
preArgs: []
|
||||
}
|
||||
|
||||
if (target) overrides.target = Array.isArray(target) ? target : [target]
|
||||
|
||||
this.getSendWidget().sendValue(overrides)
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.get = (id)=>{
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
if (widgets[i].getValue) {
|
||||
|
||||
var v = widgets[i].getValue()
|
||||
if (v !== undefined) return v
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.getProp = (id, prop)=>{
|
||||
|
||||
var widgets = widgetManager.getWidgetById(id)
|
||||
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
|
||||
var v = widgets[i].getProp(prop)
|
||||
if (v !== undefined) return v
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.httpGet = (url, callback)=>{
|
||||
|
||||
var parser = urlParser(url),
|
||||
err = (e)=>{console.error(e)}
|
||||
|
||||
if (!parser.isLocal()) return err('httpGet error (' + url + '): non-local url')
|
||||
|
||||
var xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', url, true)
|
||||
xhr.onload = (e)=>{
|
||||
if (xhr.readyState === 4 && xhr.status === 200 && callback) {
|
||||
callback(xhr.responseText)
|
||||
}
|
||||
}
|
||||
xhr.onerror = (e)=>err('httpGet error (' + url + '): ' + xhr.statusText)
|
||||
xhr.send(null)
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.stateGet = (filter)=>{
|
||||
|
||||
if (filter) {
|
||||
|
||||
var filter = Array.isArray(filter) ? filter : [filter],
|
||||
containers = this.filter.map(x=>widgetManager.getWidgetById(x)).reduce((a,b)=>a.concat(b), [])
|
||||
if (!containers.length) return
|
||||
filter = (widget)=>{
|
||||
return containers.some(x=>x.contains(widget) || x === widget)
|
||||
}
|
||||
}
|
||||
|
||||
return deepCopy(stateManager.get(filter))
|
||||
|
||||
}
|
||||
|
||||
this.sandbox.contentWindow.stateSet = (state)=>{
|
||||
|
||||
var options = this.getValueOptions()
|
||||
stateManager.set(state, options.send)
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.sandbox.contentWindow.storage = {
|
||||
|
||||
setItem: (k, v)=>localStorage.setItem(k, JSON.stringify(v)),
|
||||
getItem: (k)=>JSON.parse(localStorage.getItem(k)),
|
||||
removeItem: (k)=>localStorage.removeItem(k),
|
||||
clear: ()=>localStorage.clear()
|
||||
|
||||
}
|
||||
|
||||
|
||||
for (var imports of ['set', 'get', 'getProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage']) {
|
||||
this.sanitize(this.sandbox.contentWindow[imports])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ScriptVm
|
||||
|
|
@ -1,20 +1,172 @@
|
|||
var {mapToScale} = require('../utils'),
|
||||
Widget = require('../common/widget'),
|
||||
StaticProperties = require('../mixins/static_properties')
|
||||
var Widget = require('../common/widget'),
|
||||
{icon} = require('../../ui/utils'),
|
||||
html = require('nanohtml'),
|
||||
raw = require('nanohtml/raw'),
|
||||
keyboardJS = require('keyboardjs'),
|
||||
ScriptVm = require('./script-vm'),
|
||||
scriptVm = new ScriptVm()
|
||||
|
||||
module.exports = class Script extends StaticProperties(Widget, {visible: false}) {
|
||||
|
||||
class Script extends Widget {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Evaluates a script each time it receives a value.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
return super.defaults({
|
||||
|
||||
_script: 'script',
|
||||
|
||||
event: {type: 'string', value: 'value', choices: ['value', 'keyboard']},
|
||||
|
||||
script: {type: 'string', value: '', help: [
|
||||
'This property is evaluated each time the widget receives a value*. Formulas are given extras variables in this context:',
|
||||
'- `value`: value received by the widget',
|
||||
'- `id`: id of the widget that triggered the script',
|
||||
'- `send(target, address, arg1, arg2, ...)`: function for sending osc messages (ignores the script\'s targets and the server\'s defaults unless `target` is `false`; ignores the script\'s `preArgs`)',
|
||||
'- `set(id, value)`: function for setting a widget\'s value',
|
||||
'- `get(id)`: function for getting a widget\'s value (dynamic equivalent of @{id})',
|
||||
'- `getProp(id, property)`: function for getting a widget\'s property value ((dynamic equivalent of @{id.property})',
|
||||
'- `httpGet(url, callback)`: function for making http requests (asynchronous "GET" request and local urls only)',
|
||||
'',
|
||||
'* Note: `value` or `linkId` properties can be used to receive other widgets\' values. The `value` property must actually change to trigger the execution, where linked widgets via `linkId` can submit the same value over and over and trigger the execution',
|
||||
]},
|
||||
|
||||
|
||||
_eventKey: 'event: keyboard',
|
||||
|
||||
keyBinding: {type: 'string|array', value: '', help: 'Key combo `string` or `array` of strings (see <a href="https://github.com/RobertWHurst/KeyboardJS">KeyboardJS</a> documentation)'},
|
||||
keyRepeat: {type: 'boolean', value: true, help: 'Set to `false` to prevent keydown repeats when holding the key combo pressed'},
|
||||
keyType: {type: 'string', value: 'keyDown', choices: ['keydown', 'keyup', 'both'], help: 'Determines which key event trigger the script\'s execution'},
|
||||
|
||||
|
||||
|
||||
}, [
|
||||
|
||||
'visible', 'label', 'interaction',
|
||||
'_geometry', 'left', 'top', 'width', 'height', 'expand',
|
||||
'_style', 'colorText', 'colorWidget', 'colorFill', 'colorStroke', 'alphaStroke', 'alphaFillOn', 'alphaFillOff', 'padding', 'css'
|
||||
|
||||
])
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super(options)
|
||||
super({...options, html: null})
|
||||
|
||||
// compile script
|
||||
this.scriptLock = false
|
||||
|
||||
|
||||
if (this.getProp('event') === 'value') {
|
||||
|
||||
this.script = scriptVm.compile(this.getProp('script'), {
|
||||
id: '',
|
||||
value: 0
|
||||
})
|
||||
|
||||
} else if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) {
|
||||
|
||||
this.keyCb = this.keyboardCallback.bind(this)
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.bind(this.getProp('keyBinding'), this.getProp('keyType') !== 'keyup' ? this.keyCb : undefined, this.getProp('keyType') !== 'keydown' ? this.keyCb : undefined)
|
||||
|
||||
})
|
||||
|
||||
this.script = scriptVm.compile(this.getProp('script'), {
|
||||
type: '',
|
||||
key: '',
|
||||
code: 0,
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
meta: false,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
runScript() {
|
||||
run(context) {
|
||||
|
||||
if (this.scriptLock) return
|
||||
|
||||
scriptVm.setSendWidget(this)
|
||||
|
||||
this.scriptLock = true
|
||||
try {
|
||||
this.script(context, this.parsersLocalScope)
|
||||
} catch(e) {
|
||||
console.log(e)
|
||||
}
|
||||
this.scriptLock = false
|
||||
|
||||
scriptVm.setSendWidget()
|
||||
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
if (this.getProp('event') === 'value') {
|
||||
|
||||
scriptVm.setValueOptions(options)
|
||||
|
||||
this.run({
|
||||
value: v,
|
||||
id: options.id,
|
||||
})
|
||||
|
||||
scriptVm.setValueOptions(options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
keyboardCallback(e) {
|
||||
|
||||
if (e.target && (e.target.tagName === 'INPUT' || e.target.tabName === 'TEXTAREA' || e.target.tabName === 'SELECT')) return
|
||||
|
||||
if (e && e.type === 'keydown' && !this.getProp('keyRepeat')) e.preventRepeat()
|
||||
|
||||
e.preventDefault()
|
||||
|
||||
this.run({
|
||||
type: e.type,
|
||||
key: e.key,
|
||||
code: e.code,
|
||||
ctrl: e.ctrlKey,
|
||||
shift: e.shiftKey,
|
||||
alt: e.altKey,
|
||||
meta: e.metaKey
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
onRemove() {
|
||||
|
||||
|
||||
if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) {
|
||||
|
||||
keyboardJS.withContext('global', ()=>{
|
||||
|
||||
keyboardJS.unbind(this.getProp('keyBinding'), this.keyCb, this.keyCb)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
super.onRemove()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Script
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
var {clip} = require('../utils'),
|
||||
Canvas = require('../common/canvas'),
|
||||
Fader = require('./fader'),
|
||||
Input = require('../inputs/input'),
|
||||
touchstate = require('../mixins/touch_state'),
|
||||
html = require('nanohtml')
|
||||
|
||||
|
|
@ -144,6 +143,13 @@ class Range extends Fader {
|
|||
|
||||
if (!i) return
|
||||
|
||||
if (this.getProp('touchAddress')) {
|
||||
this.sendValue({
|
||||
address: this.getProp('touchAddress'),
|
||||
v: [parseInt(i), 1],
|
||||
})
|
||||
}
|
||||
|
||||
if (e.shiftKey) {
|
||||
this.faders[0].trigger('drag', e)
|
||||
this.faders[1].trigger('drag', e)
|
||||
|
|
@ -161,6 +167,15 @@ class Range extends Fader {
|
|||
|
||||
e.stopPropagation = true
|
||||
this.faders[i].trigger('dragend', e)
|
||||
|
||||
|
||||
if (this.getProp('touchAddress')) {
|
||||
this.sendValue({
|
||||
address: this.getProp('touchAddress'),
|
||||
v: [parseInt(i), 0],
|
||||
})
|
||||
}
|
||||
|
||||
delete this.touchMap[e.pointerId]
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,12 +66,12 @@ class Slider extends Canvas {
|
|||
|
||||
this.widget.addEventListener('mousewheel',this.mousewheelHandleProxy.bind(this))
|
||||
|
||||
touchstate(this, {element: this.widget, multitouch: options.multitouch})
|
||||
|
||||
this.on('draginit', this.draginitHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
|
||||
this.on('drag', this.dragHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
|
||||
this.on('dragend', this.dragendHandleProxy.bind(this), {element:this.widget, multitouch: options.multitouch})
|
||||
|
||||
touchstate(this, {element: this.widget, multitouch: options.multitouch})
|
||||
|
||||
this.setSteps()
|
||||
|
||||
this.setValue(this.getSpringValue())
|
||||
|
|
|
|||
|
|
@ -1,115 +0,0 @@
|
|||
var Widget = require('../common/widget'),
|
||||
widgetManager = require('../../managers/widgets'),
|
||||
osc = require('../../osc')
|
||||
|
||||
module.exports = class _switchers_base extends Widget {
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super(options)
|
||||
|
||||
this._isSwitcher = true
|
||||
this.value = {_selected:false}
|
||||
this.linkedWidgets = typeof this.getProp('linkedWidgets') == 'object' ? this.getProp('linkedWidgets') : [this.getProp('linkedWidgets')]
|
||||
|
||||
for (var i in this.getProp('values')) {
|
||||
|
||||
this.value[this.getProp('values')[i]] = {}
|
||||
|
||||
}
|
||||
|
||||
widgetManager.on('change', this.syncHandler.bind(this), {context: this})
|
||||
|
||||
|
||||
}
|
||||
|
||||
syncHandler(e) {
|
||||
|
||||
var {id, widget} = e
|
||||
|
||||
if (widget && this.value._selected !== false && this.value[this.value._selected] && widget._isSwitcher !== true && this.isWatching(id, widget)) {
|
||||
|
||||
this.value[this.value._selected][id] = widget.getValue()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
isWatching(id, widget) {
|
||||
|
||||
if (this.linkedWidgets.indexOf(id) != -1) return true
|
||||
|
||||
for (var k in this.linkedWidgets) {
|
||||
|
||||
let widgets = widgetManager.getWidgetById(this.linkedWidgets[k])
|
||||
|
||||
for (var i in widgets) {
|
||||
|
||||
if (widgets[i].contains(widget) || widgets[i] === widget) return true
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
let apply = false
|
||||
|
||||
if (typeof v == 'object') {
|
||||
|
||||
for (var k in this.value) {
|
||||
|
||||
if (v.hasOwnProperty(k)) {
|
||||
|
||||
this.value[k] = v[k]
|
||||
apply = true
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else if (this.value[v] && v != '_selected') {
|
||||
|
||||
this.value._selected = v
|
||||
apply = true
|
||||
|
||||
}
|
||||
|
||||
if (apply) this.applyState(this.value[this.value._selected], options)
|
||||
|
||||
}
|
||||
|
||||
|
||||
applyState(state, options) {
|
||||
|
||||
// extra sync routine to ensure linked widgets' props update before osc is sent
|
||||
if (options.sync) this.changed({...options, send:false, sync:false})
|
||||
//
|
||||
|
||||
for (var id in state) {
|
||||
var value = state[id],
|
||||
widgets = widgetManager.getWidgetById(id)
|
||||
for (var i = widgets.length - 1; i >= 0; i--) {
|
||||
if (widgets[i].setValue) {
|
||||
widgets[i].setValue(value,{sync:options.sync, send:options.send})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options.sync) this.changed(options)
|
||||
if (options.send) this.sendValue()
|
||||
|
||||
}
|
||||
|
||||
sendValue() {
|
||||
|
||||
osc.sync({
|
||||
h:this.hash,
|
||||
v:this.value
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,158 +0,0 @@
|
|||
var Switcher = require('./switcher'),
|
||||
Widget = require('../common/widget'),
|
||||
Fader = require('../sliders/fader'),
|
||||
{mapToScale} = require('../utils')
|
||||
|
||||
var faderDefaults = Fader.defaults()._props()
|
||||
|
||||
|
||||
module.exports = class Crossfader extends Switcher {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Switcher with linear interpolation slider.'
|
||||
|
||||
}
|
||||
|
||||
static defaults(){
|
||||
|
||||
return Widget.defaults({
|
||||
|
||||
_crossfader: 'crossfader',
|
||||
|
||||
linkedWidgets: {type: 'string|array', value: '', help: [
|
||||
'- `String`: a widget\'s `id` whose state changes will be stored',
|
||||
'- `Array`: a list of widget `id` string'
|
||||
]},
|
||||
horizontal: {type: 'boolean', value: false, help: 'Set to `true` to display values horizontally'}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
options.props.values = options.props.horizontal ? ['A', 'B'] : ['B', 'A']
|
||||
|
||||
super(options)
|
||||
|
||||
this.fader = new Fader({props:{
|
||||
...faderDefaults,
|
||||
horizontal: this.getProp('horizontal'),
|
||||
range: {min:{'A':0}, '50%':{' ':0.5},max:{'B':1}},
|
||||
input: false
|
||||
}, parent: this})
|
||||
|
||||
this.fader.sendValue = ()=>{}
|
||||
|
||||
this.widget.appendChild(this.fader.widget)
|
||||
|
||||
if (this.getProp('horizontal')) {
|
||||
this.widget.classList.add('horizontal')
|
||||
} else {
|
||||
this.widget.classList.add('vertical')
|
||||
|
||||
}
|
||||
|
||||
this.fader.on('change', (e)=>{
|
||||
|
||||
e.stopPropagation = true
|
||||
|
||||
this.setValue(this.fader.getValue(), e.options)
|
||||
|
||||
})
|
||||
|
||||
this.value._fader = 0
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
v = v <= 0 || v == 'A' ?
|
||||
'A' :
|
||||
v >= 1 || v == 'B' ?
|
||||
'B' :
|
||||
v
|
||||
|
||||
super.setValue(v, options)
|
||||
|
||||
if (typeof v == 'object') {
|
||||
|
||||
this.fader.setValue(this.value._fader, {dragged:options.dragged})
|
||||
|
||||
}
|
||||
|
||||
if (v == 'A') {
|
||||
|
||||
this.fader.setValue(0, {dragged:options.dragged})
|
||||
this.value._fader = 0
|
||||
|
||||
|
||||
} else if (v == 'B') {
|
||||
|
||||
this.fader.setValue(1, {dragged:options.dragged})
|
||||
this.value._fader = 1
|
||||
|
||||
|
||||
} else if (!isNaN(v)) {
|
||||
|
||||
this.fader.setValue(v, {dragged:options.dragged})
|
||||
this.value._fader = this.fader.getValue()
|
||||
this.value._selected = false
|
||||
this.switch.setValue()
|
||||
|
||||
var s = this.getValue(),
|
||||
a = {}
|
||||
|
||||
for (var i in s['A']) {
|
||||
|
||||
if (s['B'][i] === undefined) continue
|
||||
|
||||
if (Array.isArray(s['A'][i])) {
|
||||
|
||||
a[i] = []
|
||||
|
||||
for (var k in s['A'][i]) {
|
||||
|
||||
if (!isNaN(s['A'][i][k])) {
|
||||
|
||||
a[i][k] = mapToScale(v, [0,1], [s['A'][i][k], s['B'][i][k]], -1)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else if (!isNaN(s['A'][i])) {
|
||||
|
||||
a[i] = mapToScale(v, [0,1], [s['A'][i], s['B'][i]], -1)
|
||||
}
|
||||
}
|
||||
|
||||
this.applyState(a, options)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
||||
switch (propName) {
|
||||
|
||||
case 'color':
|
||||
for (var w of [this.fader]) {
|
||||
w.onPropChanged('color')
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onRemove() {
|
||||
this.fader.onRemove()
|
||||
super.onRemove()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,161 +0,0 @@
|
|||
var Widget = require('../common/widget'),
|
||||
Toggle = require('../buttons/toggle'),
|
||||
pushDefaults = Toggle.defaults()._props(),
|
||||
html = require('nanohtml'),
|
||||
stateManager = 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'
|
||||
]},
|
||||
saveLabel: {type: 'string', value: 'Save', help: 'Label for save button'},
|
||||
loadLabel: {type: 'string', value: 'Load', help: 'Label for load button'},
|
||||
horizontal: {type: 'boolean', value: false, help: 'Set to `true` to display buttons horizontally'},
|
||||
|
||||
}, [], {
|
||||
|
||||
value: {type: 'object|string', value: '', help: [
|
||||
'- `object`: `"widget_id": VALUE` pairs',
|
||||
'- `string`: `"save"` (trigger save action), `"load"` (trigger load action) or `""` (reset value)',
|
||||
'',
|
||||
'Note: the widget\'s actual value is never `"save"` or `"load"`, it\'s either an empty string or an object.'
|
||||
]}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: html`<div class="switch"></div>`})
|
||||
|
||||
this._isSwitcher = true
|
||||
|
||||
if (this.getProp('horizontal')) this.widget.classList.add('horizontal')
|
||||
|
||||
this.saveButton = new Toggle({props: {...pushDefaults, type: 'toggle', mode: 'push', label: this.getProp('saveLabel')}, parent: this, container: true})
|
||||
this.loadButton = new Toggle({props: {...pushDefaults, type: 'toggle', mode: 'push', label: this.getProp('loadLabel')}, parent: this, container: true})
|
||||
|
||||
this.saveButton.container.classList.add('not-editable', 'value')
|
||||
this.loadButton.container.classList.add('not-editable', 'value', 'disabled')
|
||||
|
||||
this.saveButton.sendValue = this.loadButton.sendValue = ()=>{}
|
||||
|
||||
this.widget.appendChild(this.saveButton.container)
|
||||
this.widget.appendChild(this.loadButton.container)
|
||||
|
||||
this.on('change', (e)=>{
|
||||
|
||||
var {widget} = e
|
||||
if (widget === this) return
|
||||
|
||||
e.stopPropagation = true
|
||||
|
||||
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 = ''
|
||||
|
||||
}
|
||||
|
||||
save() {
|
||||
|
||||
var filter = (widget)=>{
|
||||
return !widget._isSwitcher
|
||||
}
|
||||
|
||||
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(stateManager.get(filter))
|
||||
|
||||
}
|
||||
|
||||
load(send) {
|
||||
|
||||
if (this.value) stateManager.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 (v === '') {
|
||||
|
||||
if (this.value) this.loadButton.container.classList.add('disabled')
|
||||
this.value = ''
|
||||
|
||||
} else if (typeof v === 'object' && !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
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
var _switchers_base = require('./_switchers_base'),
|
||||
Switch = require('../buttons/switch'),
|
||||
html = require('nanohtml')
|
||||
|
||||
var switchDefaults = Switch.defaults()._props()
|
||||
|
||||
module.exports = class Switcher extends _switchers_base {
|
||||
|
||||
static description() {
|
||||
|
||||
return 'Store other widgets\' values and recall them.'
|
||||
|
||||
}
|
||||
|
||||
static defaults() {
|
||||
|
||||
return super.defaults({
|
||||
|
||||
_switcher: 'switcher',
|
||||
|
||||
linkedWidgets: {type: 'string|array', value: '', help: [
|
||||
'- `String`: a widget\'s `id` whose state changes will be stored',
|
||||
'- `Array`: a list of widget `id` string'
|
||||
]},
|
||||
horizontal: {type: 'boolean', value: false, help: 'Set to `true` to display values horizontally'},
|
||||
values: {type: 'array|object', value: {'Value 1':1,'Value 2':2}, help: [
|
||||
'Each item represents a bank that stores the values from the widgets listed in `linkedWidgets`',
|
||||
'- `Array` of possible values to switch between : `[1,2,3]`',
|
||||
'- `Object` of `"label":value` pairs. Numeric labels must be prepended or appended with a white space (or any other non-numeric character) otherwise the order of the values won\'t be kept',
|
||||
]}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
constructor(options) {
|
||||
|
||||
super({...options, html: html`<div class="switcher"></div>`})
|
||||
|
||||
if (this.getProp('horizontal')) this.widget.classList.add('horizontal')
|
||||
|
||||
this.switch = new Switch({props:{
|
||||
...switchDefaults,
|
||||
label:false,
|
||||
values:this.getProp('values'),
|
||||
value: this.getProp('value'),
|
||||
horizontal:this.getProp('horizontal')
|
||||
}, parent: this})
|
||||
|
||||
this.switch.sendValue = ()=>{}
|
||||
|
||||
this.widget.appendChild(this.switch.widget)
|
||||
|
||||
this.switch.on('change', (e)=>{
|
||||
|
||||
e.stopPropagation = true
|
||||
e.options.fromSelf = true
|
||||
|
||||
this.setValue(this.switch.getValue(), e.options)
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
setValue(v, options={}) {
|
||||
|
||||
super.setValue(v, options)
|
||||
|
||||
if (!options.fromSelf) this.switch.setValue(this.value._selected)
|
||||
|
||||
}
|
||||
|
||||
onPropChanged(propName, options, oldPropValue) {
|
||||
|
||||
if (super.onPropChanged(...arguments)) return true
|
||||
|
||||
switch (propName) {
|
||||
|
||||
case 'color':
|
||||
for (var w of [this.switch]) {
|
||||
w.onPropChanged('color')
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
onRemove() {
|
||||
this.switch.onRemove()
|
||||
super.onRemove()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -28,22 +28,32 @@ class Vm {
|
|||
this.sandbox.contentWindow.__protect = loopProtect
|
||||
|
||||
// global context
|
||||
this.registerGlobals()
|
||||
|
||||
// detach sandbox
|
||||
document.documentElement.appendChild(this.sandbox)
|
||||
|
||||
}
|
||||
|
||||
registerGlobals() {
|
||||
|
||||
this.sandbox.contentWindow.console = console
|
||||
this.sandbox.contentWindow.setTimeout =
|
||||
this.sandbox.contentWindow.setInterval = ()=>{
|
||||
throw 'setTimeout and setInterval can\'t be used in the JS sandbox'
|
||||
}
|
||||
this.sandbox.contentWindow.globals = {}
|
||||
|
||||
this.sandbox.contentWindow.globals = {
|
||||
screen: {width: screen.width, height: screen.height},
|
||||
env: deepCopy(ENV),
|
||||
url: document.location.host,
|
||||
platform: navigator.platform
|
||||
}
|
||||
|
||||
// sanitize globals
|
||||
for (var imports of ['__protect', 'console', 'setTimeout', 'setInterval', 'globals']) {
|
||||
this.sanitize(this.sandbox.contentWindow[imports])
|
||||
}
|
||||
|
||||
// detach sandbox
|
||||
document.documentElement.appendChild(this.sandbox)
|
||||
|
||||
}
|
||||
|
||||
sanitize(object) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue