2016-01-21 17:41:45 -05:00
|
|
|
module.exports.formToObject = ($el, options) ->
|
|
|
|
options = _.extend({ trim: true, ignoreEmptyString: true }, options)
|
2014-01-03 13:32:13 -05:00
|
|
|
obj = {}
|
|
|
|
|
2016-01-21 17:41:45 -05:00
|
|
|
inputs = $('input, textarea, select', $el)
|
2014-01-03 13:32:13 -05:00
|
|
|
for input in inputs
|
|
|
|
input = $(input)
|
2014-05-22 14:24:35 -04:00
|
|
|
continue unless name = input.attr('name')
|
2016-01-21 17:41:45 -05:00
|
|
|
if input.attr('type') is 'checkbox'
|
|
|
|
obj[name] ?= []
|
|
|
|
if input.is(':checked')
|
|
|
|
obj[name].push(input.val())
|
|
|
|
else if input.attr('type') is 'radio'
|
|
|
|
continue unless input.is('checked')
|
|
|
|
obj[name] = input.val()
|
|
|
|
else
|
|
|
|
value = input.val() or ''
|
|
|
|
value = _.string.trim(value) if options.trim
|
|
|
|
if value or (not options.ignoreEmptyString)
|
|
|
|
obj[name] = value
|
2014-01-03 13:32:13 -05:00
|
|
|
obj
|
|
|
|
|
2014-07-10 14:50:16 -04:00
|
|
|
module.exports.applyErrorsToForm = (el, errors, warning=false) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
errors = [errors] if not $.isArray(errors)
|
2014-08-04 13:57:13 -04:00
|
|
|
missingErrors = []
|
2014-01-03 13:32:13 -05:00
|
|
|
for error in errors
|
2016-01-21 17:41:45 -05:00
|
|
|
if error.code is tv4.errorCodes.OBJECT_REQUIRED
|
|
|
|
prop = _.last(_.string.words(error.message)) # hack
|
|
|
|
message = 'Required field'
|
|
|
|
|
|
|
|
else if error.dataPath
|
2014-01-03 13:32:13 -05:00
|
|
|
prop = error.dataPath[1..]
|
|
|
|
message = error.message
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
else
|
|
|
|
message = "#{error.property} #{error.message}."
|
|
|
|
message = message[0].toUpperCase() + message[1..]
|
|
|
|
message = error.message if error.formatted
|
|
|
|
prop = error.property
|
2014-06-30 22:16:26 -04:00
|
|
|
|
2014-08-04 13:57:13 -04:00
|
|
|
missingErrors.push error unless setErrorToProperty el, prop, message, warning
|
|
|
|
missingErrors
|
2014-07-10 14:50:16 -04:00
|
|
|
|
2014-08-04 13:57:13 -04:00
|
|
|
# Returns the jQuery form group element in case of success, otherwise undefined
|
2014-07-10 14:50:16 -04:00
|
|
|
module.exports.setErrorToField = setErrorToField = (el, message, warning=false) ->
|
|
|
|
formGroup = el.closest('.form-group')
|
|
|
|
unless formGroup.length
|
2014-09-01 12:11:10 -04:00
|
|
|
return console.error el, " did not contain a form group, so couldn't show message:", message
|
2014-07-10 14:50:16 -04:00
|
|
|
|
|
|
|
kind = if warning then 'warning' else 'error'
|
2016-01-21 17:41:45 -05:00
|
|
|
afterEl = $(formGroup.find('.help-block, .form-control, input, select, textarea')[0])
|
2014-07-10 14:50:16 -04:00
|
|
|
formGroup.addClass "has-#{kind}"
|
2016-01-21 17:41:45 -05:00
|
|
|
helpBlock = $("<span class='help-block #{kind}-help-block'>#{message}</span>")
|
|
|
|
if afterEl.length
|
|
|
|
afterEl.before helpBlock
|
|
|
|
else
|
|
|
|
formGroup.append helpBlock
|
2014-07-10 14:50:16 -04:00
|
|
|
|
|
|
|
module.exports.setErrorToProperty = setErrorToProperty = (el, property, message, warning=false) ->
|
|
|
|
input = $("[name='#{property}']", el)
|
|
|
|
unless input.length
|
2014-09-01 12:11:10 -04:00
|
|
|
return console.error "#{property} not found in", el, "so couldn't show message:", message
|
2014-07-10 14:50:16 -04:00
|
|
|
|
|
|
|
setErrorToField input, message, warning
|
2016-02-01 18:51:29 -05:00
|
|
|
|
|
|
|
module.exports.scrollToFirstError = ($el=$('body')) ->
|
|
|
|
$first = $el.find('.has-error, .alert-danger, .error-help-block, .has-warning, .alert-warning, .warning-help-block').first()
|
|
|
|
$('body').nanoScroller({scroll: 'top'}) # normalizes offset().top value
|
|
|
|
$('body').nanoScroller({scrollTop: $first.offset().top - 20})
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports.clearFormAlerts = (el) ->
|
2014-01-27 14:30:22 -05:00
|
|
|
$('.has-error', el).removeClass('has-error')
|
2014-07-10 14:50:16 -04:00
|
|
|
$('.has-warning', el).removeClass('has-warning')
|
2014-05-30 16:57:21 -04:00
|
|
|
$('.alert.alert-danger', el).remove()
|
2014-07-10 14:50:16 -04:00
|
|
|
$('.alert.alert-warning', el).remove()
|
2014-06-30 22:16:26 -04:00
|
|
|
el.find('.help-block.error-help-block').remove()
|
2014-07-10 14:50:16 -04:00
|
|
|
el.find('.help-block.warning-help-block').remove()
|