filo_caspar/app/main/header.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-04-14 04:01:51 +00:00
const m = require('mithril')
2018-06-26 18:35:12 +00:00
const createModule = require('./common/module')
const socket = require('../shared/socket')
2016-04-14 04:01:51 +00:00
const Header = createModule({
init: function() {
2018-06-26 18:35:12 +00:00
this.currentLength = 0
this.updateMargin = false
this.connected = socket.connected
this.monitor('list', 'content.list', [], null, () => this.checkChanged())
socket.on('connect', () => {
this.connected = true
m.redraw()
})
socket.on('disconnect', () => {
this.connected = false
m.redraw()
})
2016-04-14 04:01:51 +00:00
},
hide: function(item) {
socket.emit('content.hide', {
name: item.name,
})
},
2018-06-26 18:35:12 +00:00
onupdate: function() {
if (!this.updateMargin) return
this.updateMargin = false
let header = document.getElementById('header')
let container = document.getElementById('container')
container.style.marginTop = `${ header.clientHeight - 1}px`
},
checkChanged: function() {
if (this.currentLength === this.list.length) return
this.currentLength = this.list.length
this.updateMargin = true
},
}, function() {
return [
this.list.length > 0 && [
m('h4', 'Active graphics'),
this.list.map(item =>
m('div.item', { key: `header-${item.id}` }, [
m('h3', `${item.name} - ${item.display}`),
m('button.red', {
onclick: () => this.hide(item),
2016-04-14 04:01:51 +00:00
}, 'Hide'),
])
),
2018-06-26 18:35:12 +00:00
] || null,
!this.connected && m('div.disconnected', `
Lost connection with server, Attempting to reconnect
`) || null,
]
2016-04-14 04:01:51 +00:00
})
module.exports = Header