2017-02-28 23:37:15 +00:00
|
|
|
const {EventEmitter} = require('events')
|
|
|
|
|
|
|
|
let itemIdIncrementor = 1
|
|
|
|
|
|
|
|
class TouchBar extends EventEmitter {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (items) {
|
2017-02-28 23:37:15 +00:00
|
|
|
super()
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
if (!Array.isArray(items)) {
|
|
|
|
throw new Error('The items object provided has to be an array')
|
|
|
|
}
|
2017-02-28 23:37:15 +00:00
|
|
|
|
|
|
|
this.items = {}
|
|
|
|
this.ordereredItems = []
|
|
|
|
const registerItem = (item) => {
|
|
|
|
this.items[item.id] = item
|
2017-03-01 00:08:12 +00:00
|
|
|
item.on('change', () => {
|
|
|
|
this.emit('change', item.id, item.type)
|
|
|
|
})
|
2017-02-28 23:37:15 +00:00
|
|
|
if (item.child instanceof TouchBar) {
|
|
|
|
item.child.ordereredItems.forEach(registerItem)
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
items.forEach((item) => {
|
2017-02-28 23:37:15 +00:00
|
|
|
this.ordereredItems.push(item)
|
|
|
|
if (!(item instanceof TouchBarItem)) {
|
2016-11-27 11:54:12 +00:00
|
|
|
throw new Error('Each item must be an instance of a TouchBarItem')
|
|
|
|
}
|
2017-02-28 23:37:15 +00:00
|
|
|
registerItem(item)
|
2016-11-27 11:54:12 +00:00
|
|
|
})
|
|
|
|
|
2017-02-28 23:37:15 +00:00
|
|
|
this.on('interaction', (itemID, details) => {
|
|
|
|
const item = this.items[itemID]
|
|
|
|
if (item != null && item.onInteraction != null) {
|
|
|
|
item.onInteraction(details)
|
|
|
|
}
|
|
|
|
})
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:08:12 +00:00
|
|
|
class TouchBarItem extends EventEmitter {
|
2016-11-27 11:54:12 +00:00
|
|
|
constructor (config) {
|
2017-03-01 00:08:12 +00:00
|
|
|
super()
|
2017-02-28 23:37:15 +00:00
|
|
|
this.id = `${itemIdIncrementor++}`
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'button'
|
|
|
|
this.label = config.label
|
|
|
|
this.backgroundColor = config.backgroundColor
|
|
|
|
this.labelColor = config.labelColor
|
|
|
|
this.onInteraction = config.click
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'colorpicker'
|
|
|
|
|
|
|
|
const {change} = config
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2017-02-28 23:37:15 +00:00
|
|
|
this.onInteraction = (details) => {
|
|
|
|
change(details.color)
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:36:57 +00:00
|
|
|
TouchBar.Group = class TouchBarGroup extends TouchBarItem {
|
2016-11-28 07:24:48 +00:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'group'
|
|
|
|
this.child = config.items
|
|
|
|
if (!(this.child instanceof TouchBar)) {
|
|
|
|
this.child = new TouchBar(this.items)
|
|
|
|
}
|
2016-11-28 07:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2016-11-29 07:36:57 +00:00
|
|
|
TouchBar.Label = class TouchBarLabel extends TouchBarItem {
|
2016-11-28 10:43:39 +00:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'label'
|
2017-03-01 00:08:12 +00:00
|
|
|
this._label = config.label
|
|
|
|
}
|
|
|
|
|
|
|
|
set label (newLabel) {
|
|
|
|
this._label = newLabel
|
|
|
|
this.emit('change')
|
|
|
|
}
|
|
|
|
|
|
|
|
get label () {
|
|
|
|
return this._label
|
2016-11-28 10:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
|
2016-11-29 07:00:08 +00:00
|
|
|
TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'popover'
|
|
|
|
this.label = config.label
|
|
|
|
this.showCloseButton = config.showCloseButton
|
|
|
|
this.child = config.items
|
|
|
|
if (!(this.child instanceof TouchBar)) {
|
|
|
|
this.child = new TouchBar(this.items)
|
|
|
|
}
|
2016-11-29 07:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:54:12 +00:00
|
|
|
TouchBar.Slider = class TouchBarSlider extends TouchBarItem {
|
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
2017-02-28 23:37:15 +00:00
|
|
|
this.type = 'slider'
|
|
|
|
this.minValue = config.minValue
|
|
|
|
this.maxValue = config.maxValue
|
|
|
|
this.initialValue = config.initialValue
|
|
|
|
|
|
|
|
const {change} = config
|
2016-11-27 11:54:12 +00:00
|
|
|
if (typeof change === 'function') {
|
2017-02-28 23:37:15 +00:00
|
|
|
this.onInteraction = (details) => {
|
|
|
|
change(details.value)
|
|
|
|
}
|
2016-11-27 11:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:55:07 +00:00
|
|
|
module.exports = TouchBar
|