ldx-widgets
Version:
widgets
133 lines (95 loc) • 2.92 kB
text/coffeescript
{Flux} = require 'delorean'
_ = require 'lodash'
Validation = Flux.createStore
actions:
'add-error': 'addError'
'clear-error': 'clearError'
'toggle-error': 'toggleError'
'show-all': 'showAll'
'force-show-all': 'forceShowAll'
'hide-all': 'hideAll'
'clear-all': 'clearAll'
'raise-validation': 'raiseValidation'
'lower-validation': 'lowerValidation'
scheme:
errors:
default: {}
errorsActive:
default: no
forceShowAllErrors:
default: no
zIndex:
default: 11
###
Error Object Specs
Each object represents a field, and contains all the active validation issues for that field
{
show: yes/no - whether or not the message displaying in the UI
autoHideAfter: Int - ms to hide the error after
direction: 'left/right/above/below' - defaults to below - direction of the message pvr
messages: [] - array of error message strings
}
###
addError: (options) ->
{groupId, error, anchor} = options
{errors} = @state
errors[groupId] = error
errors[groupId].anchor = anchor
errors[groupId].direction = 'below' unless error.direction?
@set
errorsActive: yes
if error.autoHideAfter then @autoHide error, groupId
clearError: (id) ->
{errors} = @state
if errors[id]?
delete errors[id]
@set
errorsActive: Object.keys(errors).length > 0
clearAll: ->
@set
errors: {}
errorsActive: no
forceShowAllErrors: no
zIndex: 11
toggleError: (options) ->
{groupId, status, isMouseOver} = options
{errors} = @state
error = errors[groupId]
if not error? then return
else
error.show = if status? then status else yes
@emit 'change'
@autoHide error, groupId if not isMouseOver and error.autoHideAfter
showAll: (autoHideAfter = no) ->
{errors} = @state
error.show = yes for groupId, error of errors
@emit 'change'
@autoHideAll() if autoHideAfter
forceShowAll: (autoHideAfter = no) ->
{errors} = @state
error.show = yes for groupId, error of errors
@set
forceShowAllErrors: yes
@autoHideAll() if autoHideAfter
hideAll: () ->
{errors} = @state
error.show = no for groupId, error of errors
@emit 'change'
hideTimers: {}
autoHide: (error, groupId) ->
clearInterval @hideTimers[groupId]
{errors} = @state
@hideTimers[groupId] = setTimeout =>
errors[groupId]?.show = no
@emit 'change'
, error.autoHideAfter or 1500
autoHideAll: () ->
clearInterval @hideAllTimer
@hideAllTimer = setTimeout =>
@hideAll()
, 1500
# By default the validaiton layer is below popovers and modals
# Setting this will allow it to be used for forms within popovers or modals
raiseValidation: -> @set 'zIndex', 14
lowerValidation: -> @set 'zIndex', 11
module.exports = Validation