151:0178573216e3
Anton Shestakov <av6@dwimlabs.net>, Thu, 14 Apr 2016 02:05:24 +0800
index: remove offline contacts only when they change to being offline Since new contacts are currently added while they still have the default presence of 'unavailable', add event sees them as offline and throws them away immediately, before their presence could change to something else.

next change 155:89f02b4d8280
previous change 123:8ef1d512ccf8

coffee/register.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
window.clientState = new Tram.ClientState()
window.progressApp = new Tram.ProgressApp(el: $('[data-app="progress"]'), model: clientState)
class RegistrationData extends Backbone.Model
defaults:
username: ''
password1: ''
password2: ''
validate: (attrs, options) ->
@unset('username-errors')
@unset('password1-errors')
@unset('password2-errors')
if (attrs.username ? '').trim() is ''
@set('username-errors', ['This field is required.'])
if (attrs.password1 ? '') is ''
@set('password1-errors', ['This field is required.'])
if (attrs.password1 ? '') isnt (attrs.password2 ? '')
@set('password2-errors', ['Passwords must match.'])
else if (attrs.password2 ? '') is ''
@set('password2-errors', ['This field is required.'])
return @has('username-errors') or @has('password1-errors') or @has('password2-errors')
class ProfileData extends Backbone.Model
defaults:
fullname: ''
nickname: ''
avatar: ''
window.regData = new RegistrationData()
window.vcData = new ProfileData()
registerfn = ->
if regData.isValid()
clientState.set('progress', 0)
X.register(regData.get('username').trim(), regData.get('password1'))
savefn = ->
data =
fullname: vcData.get('fullname').trim()
nickname: vcData.get('nickname').trim()
okcb = ->
clientState.set('progress', 100)
console.debug('vcard is set!', arguments)
$('[data-step="vcard"]').addClass('uk-hidden')
location.href = '/'
failcb = ->
clientState.unset('progress')
console.warn('vcard is NOT set!', arguments)
$('[data-msg="save-failed"]').removeClass('uk-hidden')
file = $('#avatar').get(0).files[0]
if file?
reader = new FileReader()
reader.onloadend = ->
_(data).extend(avatar: reader.result)
clientState.set('progress', 0)
X.savevCard(data, okcb, failcb)
reader.readAsDataURL(file)
else
clientState.set('progress', 0)
X.savevCard(data, okcb, failcb)
cancelfn = ->
X.unregister()
window.regRivet = rivets.bind($('[data-form="registration"]'), data: regData, register: registerfn)
window.vcRivet = rivets.bind($('[data-form="vcard"]'), data: vcData, save: savefn, cancel: cancelfn)
window.X = new Tram.XMPPInterface()
X.on 'register', ->
$('[data-step="registration"]').removeClass('uk-hidden')
X.on 'registered', ->
X.finishRegistration()
$('[data-step="registration"]').addClass('uk-hidden')
X.on 'regifail', ->
$('[data-msg="registration-closed"]').removeClass('uk-hidden')
X.on 'conflict', ->
regData.set('username-errors', ['A user with this username already exists.'])
X.on 'notacceptable', ->
$('[data-msg="registration-failed"]').removeClass('uk-hidden')
X.on 'connected', ->
vcData.set('nickname', regData.get('username').trim())
$('[data-step="vcard"]').removeClass('uk-hidden')
$('#fullname').focus()
X.on 'disconnected', ->
location.reload()
X.on 'status', (status) ->
switch status
when Strophe.Status.REGISTERED
clientState.set('progress', 50)
when Strophe.Status.CONNECTED
clientState.set('progress', 100)
else
clientState.unset('progress')
X.startRegistration()
$('[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')