18:be40f584efc1
Anton Shestakov <av6@dwimlabs.net>, Sun, 20 Mar 2016 23:30:16 +0800
register: improve scripting, move XMPP interface to a separate file

next change 20:aa1eeed14fff
previous change 16:46619ef56f14

coffee/register.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
onConnect = (status, error) ->
switch status
when Strophe.Status.CONNECTING
console.debug('Strophe is connecting.')
when Strophe.Status.AUTHENTICATING
console.debug('Strophe is authenticating.')
when Strophe.Status.AUTHFAIL
console.debug('Strophe failed to authenticate:', error)
when Strophe.Status.ERROR
console.debug('Strophe received an error:', error)
when Strophe.Status.CONNFAIL
console.debug('Strophe failed to connect:', error)
when Strophe.Status.DISCONNECTING
console.debug('Strophe is disconnecting.')
when Strophe.Status.DISCONNECTED
console.debug('Strophe is disconnected.')
when Strophe.Status.CONNECTED
console.debug('Strophe is connected.')
console.info('My jid:', X.conn.jid)
X.trigger('v/start')
when Strophe.Status.REGISTER
console.debug('Got registration prompt.')
X.trigger('r/start')
when Strophe.Status.REGISTERED
console.debug('Registered!')
X.trigger('r/done')
X.conn.authenticate()
when Strophe.Status.CONFLICT
console.debug('Contact already exists!')
X.trigger('r/conflict')
when Strophe.Status.NOTACCEPTABLE
console.debug('Registration form not properly filled out.')
X.trigger('r/failed')
when Strophe.Status.REGIFAIL
console.debug('The server does not support In-Band Registration.')
X.trigger('r/closed')
window.X = new Tram.XMPPInterface()
X.startRegistration(onConnect)
$('[data-form="registration"], [data-form="vcard"]').each ->
$form = $(@)
$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')
$('[data-form="registration"] button').on 'click', ->
ok = true
Tram.validation.unsetError($('#username, #password1, #password2'))
ok &= Tram.validation.validateRequired($('#username'), true)
ok &= Tram.validation.validatePasswords($('#password1'), $('#password2'))
ok &= Tram.validation.validateRequired($('#password1'))
ok &= Tram.validation.validateRequired($('#password2'))
if ok
X.register($('#username').val().trim(), $('#password1').val())
$('[data-form="vcard"] button').on 'click', ->
file = $('#avatar').get(0).files[0]
if file
reader = new FileReader()
reader.onloadend = ->
X.savevCard
fullname: $('#fullname').val()
nickname: $('#nickname').val()
avatar: @result
reader.readAsDataURL(file)
else
X.savevCard
fullname: $('#fullname').val()
nickname: $('#nickname').val()
X.on 'r/start', ->
$('[data-step="registration"]').removeClass('uk-hidden')
X.on 'r/done', ->
$('[data-step="registration"]').addClass('uk-hidden')
X.on 'r/closed', ->
$('[data-msg="r/closed"]').removeClass('uk-hidden')
X.on 'r/conflict', ->
Tram.validation.setError($('#username'), 'A user with this username already exists.')
X.on 'r/failed', ->
$('[data-msg="r/failed"]').removeClass('uk-hidden')
X.on 'v/start', ->
$('#nickname').val($('#username').val())
$('[data-step="vcard"]').removeClass('uk-hidden')
$('#fullname').focus()
X.on 'v/done', ->
$('[data-step="vcard"]').addClass('uk-hidden')
location.href = '/'
X.on 'v/failed', ->
$('[data-msg="v/failed"]').removeClass('uk-hidden')