-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathshowErrors.coffee
More file actions
95 lines (77 loc) · 2.88 KB
/
showErrors.coffee
File metadata and controls
95 lines (77 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
showErrorsModule = angular.module('ui.bootstrap.showErrors', [])
showErrorsModule.directive 'showErrors',
['$timeout', 'showErrorsConfig', '$interpolate', ($timeout, showErrorsConfig, $interpolate) ->
getTrigger = (options) ->
trigger = showErrorsConfig.trigger
if options && options.trigger?
trigger = options.trigger
trigger
getShowSuccess = (options) ->
showSuccess = showErrorsConfig.showSuccess
if options && options.showSuccess?
showSuccess = options.showSuccess
showSuccess
getIgnorePristine = (options) ->
ignorePristine = showErrorsConfig.ignorePristine
if options && options.ignorePristine?
ignorePristine = options.ignorePristine
ignorePristine
linkFn = (scope, el, attrs, formCtrl) ->
blurred = false
options = scope.$eval attrs.showErrors
showSuccess = getShowSuccess options
ignorePristine = getIgnorePristine options
trigger = getTrigger options
inputEl = el[0].querySelector '[name]'
inputNgEl = angular.element inputEl
inputName = $interpolate(inputNgEl.attr('name') || '')(scope)
unless inputName
throw "show-errors element has no child input elements with a 'name' attribute"
inputNgEl.bind trigger, ->
return if ignorePristine && formCtrl[inputName].$pristine
blurred = true
toggleClasses formCtrl[inputName].$invalid
scope.$watch ->
formCtrl[inputName] && formCtrl[inputName].$invalid
, (invalid) ->
return if !blurred
toggleClasses invalid
scope.$on 'show-errors-check-validity', (event, name) ->
if angular.isUndefined(name) || formCtrl['$name'] == name
toggleClasses formCtrl[inputName].$invalid
scope.$on 'show-errors-reset', ->
$timeout ->
# want to run this after the current digest cycle
el.removeClass 'has-error'
el.removeClass 'has-success'
blurred = false
, 0, false
toggleClasses = (invalid) ->
el.toggleClass 'has-error', invalid
if showSuccess
el.toggleClass 'has-success', !invalid
{
restrict: 'A'
require: '^form'
compile: (elem, attrs) ->
if attrs['showErrors'].indexOf('skipFormGroupCheck') == -1
unless elem.hasClass('form-group') or elem.hasClass('input-group')
throw "show-errors element does not have the 'form-group' or 'input-group' class"
linkFn
}
]
showErrorsModule.provider 'showErrorsConfig', ->
_showSuccess = false
_trigger = 'blur'
_ignorePristine = false
@showSuccess = (showSuccess) ->
_showSuccess = showSuccess
@trigger = (trigger) ->
_trigger = trigger
@ignorePristine = (ignorePristine) ->
_ignorePristine = ignorePristine
@$get = ->
showSuccess: _showSuccess
trigger: _trigger
ignorePristine: _ignorePristine
return