25:bab5df239765
Anton Shestakov <av6@dwimlabs.net>, Mon, 21 Mar 2016 20:10:19 +0800
index: get version info from server on connect, show it in a message log

next change 26:8d301732c5cc
previous change 23:98a9a4b85104

coffee/index.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
window.messages ?= new Tram.Messages()
window.logApp ?= new Tram.LogApp(el: $('[data-app="log"]'), collection: messages)
onConnected = ->
getVersion()
getText = (el) ->
if not el
return null
str = ''
if el.childNodes.length is 0 and el.nodeType is Strophe.ElementType.TEXT
str += el.nodeValue
for node in el.childNodes
if node.nodeType is Strophe.ElementType.TEXT
str += node.nodeValue
return str
getVersion = ->
iq = $iq(type: 'get', id: X.conn.getUniqueId('version'), to: Tram.config.domain).c('query', xmlns: Strophe.NS.VERSION)
okcb = (stanza) ->
name = getText stanza.getElementsByTagName('name')[0]
version = getText stanza.getElementsByTagName('version')[0]
os = getText stanza.getElementsByTagName('os')[0]
delay = stanza.getElementsByTagName('delay')[0]
stamp = if delay then new Date(delay.getAttribute('stamp')) else new Date()
messages.add
id: stanza.getAttribute('id')
type: stanza.getAttribute('type')
from: stanza.getAttribute('from')
stamp: stamp
cls: 'info'
name: name
version: version
os: os
text: "connected to #{ name } #{ version } on #{ os }"
failcb = (stanza) ->
console.error("couldn't get version", stanza?.innerHTML)
X.conn.sendIQ(iq.tree(), okcb, failcb, 5000)
window.X = new Tram.XMPPInterface()
X.on 'connecting', ->
$('[data-msg^="a/"]').addClass('uk-hidden')
X.on 'authfail', ->
$('[data-msg="a/fail"]').removeClass('uk-hidden')
X.on 'disconnected', ->
$('[data-step="login"]').removeClass('uk-hidden')
$('[data-step="main"]').addClass('uk-hidden')
X.on 'connected attached', ->
$('[data-step="login"]').addClass('uk-hidden')
$('[data-step="main"]').removeClass('uk-hidden')
onConnected()
$('button[data-disconnect]').on 'click', ->
X.disconnect('Logged out')
$form = $('[data-form="connect"]')
$form.find('input').on 'keydown', (e) ->
if (not @required or @value isnt '') and e.keyCode is 13
e.preventDefault()
index = $form.find('input').index(@)
$next = $form.find('input').eq(index + 1)
if $next.length isnt 0
$next.focus()
else
$form.find('button').trigger('click')
$form.find('button').on 'click', (e) ->
e.preventDefault()
ok = true
Tram.validation.unsetError($('#username, #password'))
ok &= Tram.validation.validateRequired($('#username'), true)
ok &= Tram.validation.validateRequired($('#password'))
if ok
X.connect($('#username').val().trim(), $('#password').val())