-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathshowErrors.js
More file actions
99 lines (95 loc) · 3.42 KB
/
showErrors.js
File metadata and controls
99 lines (95 loc) · 3.42 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
96
97
98
99
(function() {
var showErrorsModule;
showErrorsModule = angular.module('ui.bootstrap.showErrors', []);
showErrorsModule.directive('showErrors', [
'$timeout', 'showErrorsConfig', '$interpolate', function($timeout, showErrorsConfig, $interpolate) {
var getShowSuccess, getTrigger, linkFn;
getTrigger = function(options) {
var trigger;
trigger = showErrorsConfig.trigger;
if (options && (options.trigger != null)) {
trigger = options.trigger;
}
return trigger;
};
getShowSuccess = function(options) {
var showSuccess;
showSuccess = showErrorsConfig.showSuccess;
if (options && (options.showSuccess != null)) {
showSuccess = options.showSuccess;
}
return showSuccess;
};
linkFn = function(scope, el, attrs, formCtrl) {
var blurred, inputEl, inputName, inputNgEl, options, showSuccess, toggleClasses, trigger;
blurred = false;
options = scope.$eval(attrs.showErrors);
showSuccess = getShowSuccess(options);
trigger = getTrigger(options);
inputEl = el[0].querySelector('input[name],select[name],password[name],email[name],datetime[name],datetime-local[name],date[name],month[name],time[name],week[name],number[name],url[name],search[name],tel[name],color[name],textarea[name]');
inputNgEl = angular.element(inputEl);
inputName = $interpolate(inputNgEl.attr('name') || '')(scope);
if (!inputName) {
throw "show-errors element has no child input element with a 'name' attribute";
}
inputNgEl.bind(trigger, function() {
blurred = true;
return toggleClasses(formCtrl[inputName].$invalid);
});
scope.$watch(function() {
return formCtrl[inputName] && formCtrl[inputName].$invalid;
}, function(invalid) {
if (!blurred) {
return;
}
return toggleClasses(invalid);
});
scope.$on('show-errors-check-validity', function() {
return toggleClasses(formCtrl[inputName].$invalid);
});
scope.$on('show-errors-reset', function() {
return $timeout(function() {
el.removeClass('has-error');
el.removeClass('has-success');
return blurred = false;
}, 0, false);
});
return toggleClasses = function(invalid) {
el.toggleClass('has-error', invalid);
if (showSuccess) {
return el.toggleClass('has-success', !invalid);
}
};
};
return {
restrict: 'A',
require: '^form',
compile: function(elem, attrs) {
if (attrs['showErrors'].indexOf('skipFormGroupCheck') === -1) {
if (!(elem.hasClass('form-group') || elem.hasClass('input-group'))) {
throw "show-errors element does not have the 'form-group' or 'input-group' class";
}
}
return linkFn;
}
};
}
]);
showErrorsModule.provider('showErrorsConfig', function() {
var _showSuccess, _trigger;
_showSuccess = false;
_trigger = 'blur';
this.showSuccess = function(showSuccess) {
return _showSuccess = showSuccess;
};
this.trigger = function(trigger) {
return _trigger = trigger;
};
this.$get = function() {
return {
showSuccess: _showSuccess,
trigger: _trigger
};
};
});
}).call(this);