-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhas-feedback.coffee
More file actions
35 lines (25 loc) · 1.22 KB
/
has-feedback.coffee
File metadata and controls
35 lines (25 loc) · 1.22 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
toArray = (arrayish) -> Array.prototype.slice.call arrayish
angular.module("ng-form-group")
.directive "hasFeedback", ->
restrict: "C"
compile: (el, attrs) ->
toArray(el[0].querySelectorAll(".form-control")).forEach (input) ->
input.setAttribute("has-feedback-watcher", "")
.directive "hasFeedbackWatcher", ($templateCache) ->
require: "ngModel",
link: (scope, input, attrs, ctrl) ->
validIcon = attrs.validIcon || "glyphicon-ok"
invalidIcon = attrs.invalidIcon || "glyphicon-remove"
feedbackTemplate = $templateCache.get(attrs.feedbackTemplate) || "<span class=\"glyphicon {{feedbackIcon}} form-control-feedback\"></span>";
feedbackIcon = (isGood = false) ->
icon = if isGood then validIcon else invalidIcon
feedbackTemplate.replace /{{feedbackIcon}}/, icon
unref = scope.$watch ->
return unless ctrl.$dirty
# Strip the existing state
toArray(input[0].parentElement.querySelectorAll(".form-control-feedback")).forEach (span) ->
span.parentElement.removeChild(span)
# Add any relevant icons
if ctrl.$valid then input.after(feedbackIcon(true))
else if ctrl.$invalid then input.after(feedbackIcon(false))
scope.$on "$destroy", unref