1
0
mirror of https://github.com/fazo96/telecommander.git synced 2025-01-10 11:34:20 +01:00
telecommander/lib/ui.js

248 lines
6.6 KiB
JavaScript
Raw Normal View History

2015-08-31 14:04:32 +02:00
var blessed = require('blessed')
var ChatBox = require('./ui-widgets/chatbox.js')
2015-08-31 14:04:32 +02:00
module.exports = function(data){
data.statusWindow = "Status"
data.selectedWindow = data.statusWindow // the currently selected window
// Get msgBox for given group/user NAME, create if not exists
data.getMsgBox = function(chat){
2015-08-31 14:04:32 +02:00
if(chat === undefined){
data.log('ERROR: asked for box for "undefined"!!')
2015-08-31 14:04:32 +02:00
return data.msgBox[statusWindow]
}
if(!data.msgBox[chat]){
//log('Generating window: "'+chat+'"')
data.msgBox[chat] = data.mkBox(chat)
data.screen.insertBefore(data.msgBox[chat],data.loader)
//data.getMessages(chat,data.msgBox[chat])
2015-08-31 14:04:32 +02:00
} // else log('Getting window','"'+chat+'"')
return data.msgBox[chat]
}
data.switchToBox = function(boxname){
// Hide current window
2015-08-31 14:04:32 +02:00
if(data.selectedWindow && data.msgBox[data.selectedWindow])
data.msgBox[data.selectedWindow].hide()
if(boxname === undefined){
// Leave the clear
} else {
// Switch window
data.selectedWindow = boxname;
if(data.selectedWindow != data.statusWindow){
data.cmdline.setLabel('to '+data.selectedWindow)
data.markAsRead(data.selectedWindow)
} else {
data.cmdline.setLabel('Command for Telecommander')
}
var newb = data.getMsgBox(data.selectedWindow)
newb.show()
newb.emit('scroll')
}
data.screen.render()
2015-08-31 14:04:32 +02:00
}
data.screen = blessed.screen({
smartCSR: true,
dockBorders: true
})
data.screen.title = "Telecommander"
2015-08-31 15:32:39 +02:00
data.getDefaultStyle = function(){
return {
fg: 'white',
border: { fg: 'grey' },
focus: {
border: { fg: 'white' },
scrollbar: {
fg: 'white',
bg: 'white'
}
2015-08-31 15:32:39 +02:00
},
selected: { bold: true, fg: 'white' },
2015-08-31 15:32:39 +02:00
scrollbar: {
fg: 'white', bg: 'white',
track: { fg: 'grey', bg: 'grey' }
2015-08-31 15:32:39 +02:00
}
2015-08-31 14:04:32 +02:00
}
}
// Contact list window
data.chats = blessed.list({
keys: true,
tags: true,
2015-08-31 14:04:32 +02:00
label: 'Conversations',
left: 0,
top:0,
height: data.screen.height-3,
width: '20%',
border: { type: 'line' },
mouse: true,
scrollbar: {
ch: ' ',
track : {
ch: ' '
}
},
2015-08-31 18:37:32 +02:00
//scrollbar: false, // disabled cause can't change track style when focused
invertSelected: false,
2015-08-31 15:32:39 +02:00
style: data.getDefaultStyle(),
2015-08-31 14:04:32 +02:00
})
data.chats.key('tab',function(){
if(data.msgBox[data.selectedWindow])
data.msgBox[data.selectedWindow].focus()
})
data.screen.append(data.chats)
2015-08-31 14:04:32 +02:00
// Command line prompt
data.cmdline = blessed.textbox({
keys: false,
mouse: true,
label: 'Command for Telecommander',
bottom: 0,
left: 'center',
width: '100%',
height: 3,
border: { type: 'line' },
style: data.getDefaultStyle()
})
data.screen.append(data.cmdline);
2015-08-31 14:04:32 +02:00
// Function to create a log box
2015-08-31 18:37:32 +02:00
data.mkBox = function(txt){
var b = ChatBox({
2015-08-31 14:04:32 +02:00
keys: true,
tags: true,
mouse: true,
2015-08-31 14:04:32 +02:00
right: 0,
2015-08-31 18:37:32 +02:00
label: { text: txt, side: 'left' },
2015-08-31 14:04:32 +02:00
width: '80%',
2015-08-31 14:35:08 +02:00
hidden: true,
2015-08-31 14:04:32 +02:00
height: data.screen.height - data.cmdline.height,
border: { type: 'line' },
scrollable: true,
autoscroll: true,
scrollbar: {
ch: ' ',
fg: 'white',
track: {
ch: ' ', fg: 'grey', bg: 'grey'
}
},
2015-08-31 15:32:39 +02:00
style: data.getDefaultStyle()
2015-08-31 14:04:32 +02:00
})
b.data.downloadedHistoryTimes = 0
b.key('enter',function(){
this.setScrollPerc(100)
data.cmdline.focus()
})
b.on('scroll',function(){
// The functions might not yet exist if this is the first window
if(b.getScrollPerc() === 100 && data.markAsRead)
data.markAsRead(data.selectedWindow)
else if(b.getScrollPerc() === 0 && data.getMessages){
data.getMessages(txt,b)
}
})
return b
2015-08-31 14:04:32 +02:00
}
data.getDefaultPopupStyle = function(){
return {
width: '30%',
key: true,
height: 'shrink',
left: 'center',
top: 'center',
align: 'center',
valign: 'center',
border: { type: 'line' },
style: data.getDefaultStyle()
}
}
2015-08-31 14:04:32 +02:00
// Widget used to show loading windows
data.loader = blessed.Loading(data.getDefaultPopupStyle())
data.screen.append(data.loader)
data.load = function(msg){
data.loader.stop()
data.loader.load(msg)
}
// Widget used to ask for phone number and code
data.promptBox = blessed.Prompt(data.getDefaultPopupStyle())
data.screen.append(data.promptBox)
// Widget used to show pop up read only messages
data.popup = blessed.Message(data.getDefaultPopupStyle())
data.screen.append(data.popup)
data.popup.hide()
// mgsBox holds the chat window instance for every chat
2015-08-31 14:04:32 +02:00
data.msgBox = { }
// Add the status window but don't show it
data.msgBox[data.statusWindow] = data.mkBox(data.statusWindow)
data.screen.append(data.msgBox[data.statusWindow])
data.switchToBox()
2015-08-31 14:04:32 +02:00
data.screen.on('resize',function(){
for(i in data.msgBox){
item = data.msgBox[i]
2015-08-31 14:35:08 +02:00
item.height = data.screen.height - data.cmdline.height
2015-08-31 14:04:32 +02:00
}
data.chats.height = data.screen.height - data.cmdline.height
data.screen.render()
})
data.screen.key('tab',function(){
data.screen.focusPush(data.chats)
})
data.screen.key('0',function(){
data.switchToBox(data.statusWindow)
})
2015-08-31 14:04:32 +02:00
data.command = function(cmd){
data.log('Commands are not implemented... sorry!')
2015-08-31 14:04:32 +02:00
}
// What happens when a different window is selected
data.chats.on('select',function(selected){
2015-08-31 14:35:08 +02:00
//data.log('SELECT:',selected.content)
2015-08-31 18:37:32 +02:00
if(selected === undefined) return
var sel = data.escapeFromList(selected)
data.switchToBox(sel)
data.msgBox[data.selectedWindow].focus()
2015-08-31 14:04:32 +02:00
})
2015-08-31 15:32:39 +02:00
/*
data.cmdline.on('click',function(){
data.cmdline.focus()
data.screen.render()
})
*/
2015-08-31 14:04:32 +02:00
// Catch ctrl-c or escape event and close program
data.screen.key(['escape','C-c'], function(ch,key){
data.quit()
});
2015-08-31 15:32:39 +02:00
data.cmdline.on('focus',function(){
data.cmdline.readInput()
})
2015-08-31 14:04:32 +02:00
// What happens when the user submits a command in the prompt
data.cmdline.on('submit',function(value){
data.getMsgBox(data.statusWindow).add('< '+value)
2015-08-31 14:04:32 +02:00
if(data.selectedWindow === data.statusWindow || data.nameToObj(data.selectedWindow) === undefined){
//log('Window:',selectedWindow,'Eval cmd:',value)
data.command(value)
} else if(value.indexOf('//') === 0){
data.sendMsg(selectedWindow,value.substring(1))
} else if(value.indexOf('/') === 0){
data.command(value.substring(1))
} else {
data.sendMsg(data.selectedWindow,value)
}
data.cmdline.clearValue()
data.cmdline.focus()
})
}