From 47340cb5eb9a650a4d52ceb91aa672ae199e9dcb Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 4 May 2018 17:36:07 +0200 Subject: [PATCH] linkId: add support for master/slave modes with >> and << prefixes (#251) --- CHANGELOG.md | 3 ++- resources/docs/widgets/widgets.md | 6 ++++-- src/browser/js/app/managers/widgets.js | 15 +++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49160640..98879c02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## next - widgets - - `target` properties are no longer coerced to arrays and can be simple strings when there's only one value to set + - `target` and `preArgs` properties are no longer coerced to arrays and can be written as single string values + - `linkId` supports master and slave mode with `>>` and `>>` prefixes ## 0.29.2 diff --git a/resources/docs/widgets/widgets.md b/resources/docs/widgets/widgets.md index fa1edbee..2cc13554 100644 --- a/resources/docs/widgets/widgets.md +++ b/resources/docs/widgets/widgets.md @@ -16,8 +16,10 @@ All widgets share a set of generic properties described below. ### `linkId` - type: `string|array` - default: `empty` -- usage: widgets sharing the same `linkId` update each other's value(s) AND send their respective osc messages. - +- usage: + - widgets sharing the same `linkId` update each other's value(s) AND send their respective osc messages. + - when prefixed with `>>`, the linkId will make the widget act as a master (sending but not receiving) + - when prefixed with `<<`, the linkId will make the widget act as a slave (receiving but not sending) ## Style diff --git a/src/browser/js/app/managers/widgets.js b/src/browser/js/app/managers/widgets.js index 67cdaa4f..b7342577 100644 --- a/src/browser/js/app/managers/widgets.js +++ b/src/browser/js/app/managers/widgets.js @@ -29,8 +29,14 @@ var WidgetManager = class WidgetManager extends EventEmitter { onChange(e) { - var {id, widget, linkId, options} = e, - widgetsById = this.getWidgetById(id), + var {id, widget, linkId, options} = e + + if (linkId) { + if (!Array.isArray(linkId)) linkId = [linkId] + linkId = linkId.map(x => x.replace(/^>>\s*/, '')).filter(x => x.indexOf('<<') < 0) + } + + var widgetsById = this.getWidgetById(id), widgetsByLinkId = linkId ? this.getWidgetByLinkId(linkId) : [] // Widget that share the same id will update each other @@ -89,8 +95,9 @@ var WidgetManager = class WidgetManager extends EventEmitter { } if (!Array.isArray(linkId)) linkId = [linkId] + linkId = linkId.map(x => x.replace(/^<<\s*/, '')) for (var i in linkId) { - if (linkId[i]) { + if (linkId[i] && linkId[i].indexOf('>>') < 0) { if (!this.linkIdRoute[linkId[i]]) this.linkIdRoute[linkId[i]] = [] this.linkIdRoute[linkId[i]].push(hash) } @@ -157,7 +164,7 @@ var WidgetManager = class WidgetManager extends EventEmitter { if (!Array.isArray(linkId)) linkId = [linkId] for (var i in linkId) { - if (linkId[i] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1) + if (linkId[i] && this.linkIdRoute[linkId[i]] && this.linkIdRoute[linkId[i]].indexOf(hash) != -1) this.linkIdRoute[linkId[i]].splice(this.linkIdRoute[linkId[i]].indexOf(hash), 1) } }