309:f8c525ff01cd
Anton Shestakov <av6@dwimlabs.net>, Mon, 02 Jul 2018 13:33:50 +0800
provision: slight rewording

previous change 169:4cebef28d40b

coffee/forms.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.RegistrationForm 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 Tram.ProfileForm extends Backbone.Model
defaults:
fullname: ''
nickname: ''
avatar: ''
validate: (attrs, options) ->
@unset('avatar-errors')
ae = []
file = $('#avatar').get(0).files[0]
if file?
if not file.type.match('image/.*')?
ae.push("File doesn't look like an image.")
if file.size > 64 * 1024
ae.push('File is too big.')
if ae.length > 0
@set('avatar-errors', ae)
return @has('avatar-errors')
class Tram.ConnectionForm extends Backbone.Model
defaults:
username: ''
password: ''
validate: (attrs, options) ->
@unset('username-errors')
@unset('password-errors')
@unset('auth-errors')
if (attrs.username ? '').trim() is ''
@set('username-errors', ['This field is required.'])
if (attrs.password ? '') is ''
@set('password-errors', ['This field is required.'])
return @has('username-errors') or @has('password-errors')
$.fn.streamline = ->
@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')