Skip to content

Commit

Permalink
Add on play options to playable items
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Boberg <[email protected]>
  • Loading branch information
axelboberg committed Mar 24, 2024
1 parent 9d80533 commit 40af571
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
2 changes: 1 addition & 1 deletion plugins/caspar/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function init (htmlPath) {
id: 'bridge.caspar.amcp',
name: 'AMCP',
category: 'Caspar',
inherits: 'bridge.types.root',
inherits: 'bridge.types.playable',
properties: {
'caspar.server': {
name: 'Server',
Expand Down
5 changes: 5 additions & 0 deletions plugins/rundown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const manifest = require('./package.json')

const Accumulator = require('./lib/Accumulator')

/*
Bootstrap play handlers
*/
require('./lib/handlers')

async function initWidget () {
const cssPath = `${assets.hash}.${manifest.name}.bundle.css`
const jsPath = `${assets.hash}.${manifest.name}.bundle.js`
Expand Down
20 changes: 20 additions & 0 deletions plugins/rundown/lib/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 Sveriges Television AB
//
// SPDX-License-Identifier: MIT

const bridge = require('bridge')

/**
* Get the id of the next sibling
* from the specified item within
* its parent item
* @param { String } parentId
* @param { String } itemId
* @returns { Promise.<String | undefined> }
*/
async function getNextSibling (parentId, itemId) {
const siblings = await bridge.state.get(`items.${parentId}.children`) || []
const index = siblings.indexOf(itemId)
return siblings[index + 1]
}
exports.getNextSibling = getNextSibling
85 changes: 85 additions & 0 deletions plugins/rundown/lib/handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2024 Sveriges Television AB
//
// SPDX-License-Identifier: MIT

const bridge = require('bridge')
const commands = require('./commands')

const ON_PLAY_OPTIONS = {
playNextSibling: 1,
selectNextSibling: 2
}

const MAIN_ROLE_ID = 1

const HANDLERS = [
/*
Handle the on play option
*/
{
condition: item => item.data?.onPlay,
fn: async item => {
const value = parseInt(item?.data?.onPlay)

if (value === ON_PLAY_OPTIONS.playNextSibling) {
const sibling = await commands.getNextSibling(item?.parent, item?.id)
if (!sibling) {
return
}
bridge.items.playItem(sibling)
return
}

if (value === ON_PLAY_OPTIONS.selectNextSibling) {
const sibling = await commands.getNextSibling(item?.parent, item?.id)
if (!sibling) {
return
}
selectItem(sibling)
}
}
}
]

/**
* Get the main client's id
* @returns { Promise.<String | undefined> }
*/
async function getMainClientId () {
const connections = await bridge.state.get('_connections')
const main = Object.entries(connections)
.filter(([_, value]) => value?.role === MAIN_ROLE_ID)
.map(([key]) => {
return key
})

return main[0]
}

/**
* Select the specified item
* in the main client
* @param { String } id
* @returns
*/
async function selectItem (id) {
const mainClientId = await getMainClientId()
if (mainClientId == null) {
return
}
bridge.state.apply({
_connections: {
[mainClientId]: {
selection: { $replace: [id] }
}
}
})
}

bridge.events.on('item.play', item => {
for (const handler of HANDLERS) {
if (handler.condition(item)) {
handler.fn(item)
}
}
})
19 changes: 16 additions & 3 deletions plugins/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@
}
}
},
{
"id": "bridge.types.playable",
"inherits": "bridge.types.root",
"properties": {
"onPlay": {
"name": "On play",
"type": "enum",
"default": 0,
"enum": ["Do nothing", "Play next sibling", "Select next sibling (main client)"],
"ui.group": "Timing"
}
}
},
{
"id": "bridge.types.group",
"name": "Group",
"inherits": "bridge.types.root",
"inherits": "bridge.types.playable",
"properties": {
"playMode": {
"name": "Play mode",
Expand All @@ -50,7 +63,7 @@
},
{
"id": "bridge.types.delayable",
"inherits": "bridge.types.root",
"inherits": "bridge.types.playable",
"properties": {
"delay": {
"name": "Delay",
Expand All @@ -63,7 +76,7 @@
},
{
"id": "bridge.types.media",
"inherits": "bridge.types.root",
"inherits": "bridge.types.playable",
"properties": {
"duration": {
"name": "Duration",
Expand Down
2 changes: 1 addition & 1 deletion plugins/variables/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
"id": "bridge.variables.variable",
"name": "Variable",
"inherits": "bridge.types.root",
"inherits": "bridge.types.playable",
"properties": {
"variable.key": {
"name": "Variable name",
Expand Down

0 comments on commit 40af571

Please sign in to comment.