|
23 | 23 | return showSuccess;
|
24 | 24 | };
|
25 | 25 | linkFn = function(scope, el, attrs, formCtrl) {
|
26 |
| - var blurred, inputEl, inputName, inputNgEl, options, showSuccess, toggleClasses, trigger; |
27 |
| - blurred = false; |
| 26 | + var inputEl, inputName, inputNgEl, options, showSuccess, toggleClasses, trigger; |
28 | 27 | options = scope.$eval(attrs.showErrors);
|
29 | 28 | showSuccess = getShowSuccess(options);
|
30 | 29 | trigger = getTrigger(options);
|
|
35 | 34 | throw "show-errors element has no child input elements with a 'name' attribute and a 'form-control' class";
|
36 | 35 | }
|
37 | 36 | inputNgEl.bind(trigger, function() {
|
38 |
| - blurred = true; |
| 37 | + scope.$apply(function() { |
| 38 | + formCtrl[inputName].$blurred = true; |
| 39 | + }) |
39 | 40 | return toggleClasses(formCtrl[inputName].$invalid);
|
40 | 41 | });
|
41 | 42 | scope.$watch(function() {
|
42 | 43 | return formCtrl[inputName] && formCtrl[inputName].$invalid;
|
43 | 44 | }, function(invalid) {
|
44 |
| - if (!blurred) { |
| 45 | + if (!formCtrl[inputName].$blurred) { |
45 | 46 | return;
|
46 | 47 | }
|
47 | 48 | return toggleClasses(invalid);
|
|
53 | 54 | return $timeout(function() {
|
54 | 55 | el.removeClass('has-error');
|
55 | 56 | el.removeClass('has-success');
|
56 |
| - return blurred = false; |
| 57 | + return formCtrl[inputName].$blurred = false; |
57 | 58 | }, 0, false);
|
58 | 59 | });
|
59 | 60 | return toggleClasses = function(invalid) {
|
|
78 | 79 | }
|
79 | 80 | ]);
|
80 | 81 |
|
| 82 | + showErrorsModule.directive('showErrorIcons', [ |
| 83 | + '$timeout', 'showErrorsConfig', '$interpolate', function($timeout, showErrorsConfig, $interpolate) { |
| 84 | + var getShowSuccess, linkFn; |
| 85 | + getShowSuccess = function(options) { |
| 86 | + var showSuccess; |
| 87 | + showSuccess = showErrorsConfig.showSuccess; |
| 88 | + if (options && (options.showSuccess != null)) { |
| 89 | + showSuccess = options.showSuccess; |
| 90 | + } |
| 91 | + return showSuccess; |
| 92 | + }; |
| 93 | + |
| 94 | + linkFn = function(scope, el, attrs, formCtrl) { |
| 95 | + var inputEl, inputName, inputNgEl, options, showSuccess, toggleClasses, trigger; |
| 96 | + options = scope.$eval(attrs.showErrors); |
| 97 | + showSuccess = getShowSuccess(options); |
| 98 | + |
| 99 | + inputEl = el[0].parentNode.querySelector('.form-control[name]'); |
| 100 | + inputNgEl = angular.element(inputEl); |
| 101 | + inputName = $interpolate(inputNgEl.attr('name') || '')(scope); |
| 102 | + if (!inputName) { |
| 103 | + throw "show-error-icons element's parent has no child input elements with a 'name' attribute and a 'form-control' class"; |
| 104 | + } |
| 105 | + |
| 106 | + scope.$watch(function() { |
| 107 | + return formCtrl[inputName] ^ formCtrl[inputName].$invalid ^ formCtrl[inputName].$blurred; |
| 108 | + }, function() { |
| 109 | + return toggleClasses(formCtrl[inputName].$blurred, formCtrl[inputName].$invalid); |
| 110 | + }) |
| 111 | + |
| 112 | + scope.$on('show-errors-check-validity', function() { |
| 113 | + return toggleClasses(true, formCtrl[inputName].$invalid); |
| 114 | + }); |
| 115 | + |
| 116 | + scope.$on('show-errors-reset', function() { |
| 117 | + return $timeout(function() { |
| 118 | + toggleClasses(false) |
| 119 | + }, 0, false); |
| 120 | + }); |
| 121 | + return toggleClasses = function(show, invalid) { |
| 122 | + el.toggleClass('ng-hide', !show) |
| 123 | + el.toggleClass('glyphicon', true) |
| 124 | + el.toggleClass('glyphicon-remove', invalid); |
| 125 | + if (showSuccess) { |
| 126 | + return el.toggleClass('glyphicon-ok', !invalid); |
| 127 | + } |
| 128 | + }; |
| 129 | + }; |
| 130 | + return { |
| 131 | + restrict: 'A', |
| 132 | + require: '^form', |
| 133 | + compile: function(elem, attrs) { |
| 134 | + if (!elem.parent().children(".form-control[name]")) { |
| 135 | + throw "show-error-icons must be applied on an element whose parent contains an input[name] element!" |
| 136 | + } |
| 137 | + |
| 138 | + return linkFn; |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | + ]); |
| 143 | + |
| 144 | + showErrorsModule.directive('showErrorHint', [ |
| 145 | + '$timeout', 'showErrorsConfig', '$interpolate', function($timeout, showErrorsConfig, $interpolate) { |
| 146 | + var getShowSuccess, linkFn; |
| 147 | + getShowSuccess = function(options) { |
| 148 | + var showSuccess; |
| 149 | + showSuccess = showErrorsConfig.showSuccess; |
| 150 | + if (options && (options.showSuccess != null)) { |
| 151 | + showSuccess = options.showSuccess; |
| 152 | + } |
| 153 | + return showSuccess; |
| 154 | + }; |
| 155 | + |
| 156 | + linkFn = function(formgroup) { |
| 157 | + return function(scope, el, attrs, formCtrl) { |
| 158 | + var inputEl, inputName, inputNgEl, toggleClasses, validator; |
| 159 | + |
| 160 | + inputEl = formgroup[0].querySelector('input.form-control[name]'); |
| 161 | + inputNgEl = angular.element(inputEl) |
| 162 | + inputName = $interpolate(inputNgEl.attr('name') || '')(scope); |
| 163 | + validator = scope.showErrorHint |
| 164 | + |
| 165 | + if (!inputName) { |
| 166 | + throw "show-error-hint element's parent has no child input elements with a 'name' attribute and a 'form-control' class"; |
| 167 | + } |
| 168 | + |
| 169 | + var update = function(force) { |
| 170 | + return function() { |
| 171 | + if (validator) { |
| 172 | + return scope.toggleClasses((formCtrl[inputName].$blurred || force) && formCtrl[inputName].$error[validator]) |
| 173 | + } else { |
| 174 | + var errors = formCtrl[inputName].$error || {} |
| 175 | + var keys = Object.keys(errors) |
| 176 | + var errorCount = keys.filter(function(e) { return errors[e]; }).length |
| 177 | + |
| 178 | + return scope.toggleClasses((formCtrl[inputName].$blurred || force) && errorCount > 0) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (validator) { |
| 184 | + scope.$watch(function() { |
| 185 | + return formCtrl[inputName].$error[validator] |
| 186 | + }, update(false)) |
| 187 | + } else { |
| 188 | + scope.$watch(function() { |
| 189 | + return JSON.stringify(formCtrl[inputName].$error) |
| 190 | + }, update(false)) |
| 191 | + } |
| 192 | + |
| 193 | + scope.$watch(function() { |
| 194 | + return formCtrl[inputName].$blurred |
| 195 | + }, update(false)) |
| 196 | + |
| 197 | + scope.$on('show-errors-check-validity', function() { |
| 198 | + return update(true)() |
| 199 | + }); |
| 200 | + |
| 201 | + scope.$on('show-errors-reset', function() { |
| 202 | + return $timeout(function() { |
| 203 | + scope.toggleClasses(true) |
| 204 | + }, 0, false); |
| 205 | + }); |
| 206 | + |
| 207 | + scope.toggleClasses = function(show) { |
| 208 | + el.toggleClass('ng-hide', !show) |
| 209 | + }; |
| 210 | + |
| 211 | + return scope.toggleClasses |
| 212 | + }; |
| 213 | + } |
| 214 | + |
| 215 | + return { |
| 216 | + restrict: 'A', |
| 217 | + require: '^form', |
| 218 | + scope: { |
| 219 | + showErrorHint: '@' |
| 220 | + }, |
| 221 | + |
| 222 | + compile: function(elem, attrs) { |
| 223 | + |
| 224 | + //Find first ancestor with a form control |
| 225 | + while(elem && !elem[0].querySelector(".form-control[name]")) { |
| 226 | + elem = elem.parent() |
| 227 | + } |
| 228 | + |
| 229 | + if (!elem) { |
| 230 | + throw "show-error-hint must be applied on an element whose parent contains an input[name] element!" |
| 231 | + } |
| 232 | + |
| 233 | + return linkFn(elem); |
| 234 | + } |
| 235 | + }; |
| 236 | + } |
| 237 | + ]); |
| 238 | + |
81 | 239 | showErrorsModule.provider('showErrorsConfig', function() {
|
82 | 240 | var _showSuccess, _trigger;
|
83 | 241 | _showSuccess = false;
|
|
0 commit comments