15:52b632342209
Anton Shestakov <av6@dwimlabs.net>, Sun, 20 Mar 2016 11:48:13 +0800
validation: move to the new namespace, and move from ui/ up

next change 16:46619ef56f14
previous change 14:8f8eddb3b264

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:', conn.jid)
R.trigger 'v/start'
when Strophe.Status.REGISTER
console.debug('Got registration prompt')
R.trigger 'r/start'
when Strophe.Status.REGISTERED
console.debug('Registered!')
R.trigger 'r/done'
conn.authenticate()
when Strophe.Status.CONFLICT
console.debug('Contact already exists!')
R.trigger 'r/conflict'
when Strophe.Status.NOTACCEPTABLE
console.debug('Registration form not properly filled out.')
R.trigger 'r/failed'
when Strophe.Status.REGIFAIL
console.debug('The Server does not support In-Band Registration')
R.trigger 'r/closed'
savevCard = (data) ->
$vcard = $iq({type: 'set'}).c('vCard', {xmlns: Strophe.NS.VCARD})
if data.nickname
$vcard.c('NICKNAME').t(data.nickname).up()
if data.fullname
$vcard.c('FN').t(data.fullname).up()
if data.avatar
b = ';base64,'
d = 'data:'
dl = d.length
bl = b.length
bi = data.avatar.indexOf(b)
type = data.avatar.substr(dl, bi - dl)
binval = data.avatar.substr(bi + bl)
$vcard.c('PHOTO')
.c('TYPE').t(type).up()
.c('BINVAL').t(binval)
okcb = ->
console.debug('vcard is set!', arguments)
R.trigger 'v/done'
failcb = ->
console.warn('vcard is NOT set!', arguments)
R.trigger 'v/failed'
conn.sendIQ $vcard.tree(), okcb, failcb
window.R = _.clone Backbone.Events
_(R).extend
register: (username, password) ->
conn.register.fields.username = username
conn.register.fields.password = password
conn.register.submit()
savevCard: savevCard
conn = new Strophe.Connection "https://#{ Tram.config.host }/http-bind"
conn.register.connect Tram.config.domain, onConnect
$('#register').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
R.register $('#username').val().trim(), $('#password1').val()
$('#save').on 'click', ->
file = $('#avatar').get(0).files[0]
if file
reader = new FileReader()
reader.onloadend = ->
R.savevCard
fullname: $('#fullname').val()
nickname: $('#nickname').val()
avatar: @result
reader.readAsDataURL(file)
else
R.savevCard
fullname: $('#fullname').val()
nickname: $('#nickname').val()
R.on 'r/start', ->
$('[data-step="registration"]').removeClass 'uk-hidden'
R.on 'r/done', ->
$('[data-step="registration"]').addClass 'uk-hidden'
R.on 'r/closed', ->
$('[data-msg="r/closed"]').removeClass 'uk-hidden'
R.on 'r/conflict', ->
Tram.validation.setError $('#username'), 'A user with this username already exists.'
R.on 'r/failed', ->
$('[data-msg="r/failed"]').removeClass 'uk-hidden'
R.on 'v/start', ->
$('#nickname').val $('#username').val()
$('[data-step="vcard"]').removeClass 'uk-hidden'
R.on 'v/done', ->
$('[data-step="vcard"]').addClass 'uk-hidden'
location.href = '/'
R.on 'v/failed', ->
$('[data-msg="v/failed"]').removeClass 'uk-hidden'