-
Notifications
You must be signed in to change notification settings - Fork 4
/
input-clear-icon.js
39 lines (34 loc) · 1.49 KB
/
input-clear-icon.js
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
angular.module('controls', [])
.directive('clearText', function () {
return {
restrict: "A",
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
ctrl.$render = function () {
element.val(ctrl.$viewValue);
};
element.bind('input', function (event) {
ctrl.$setViewValue(element.val());
tog(!ctrl.$isEmpty(ctrl.$viewValue), 'x');
});
element.bind('click', function (event) {
if ((event.target.offsetWidth - 24) < (event.clientX - event.target.getBoundingClientRect().left)
&& !ctrl.$isEmpty(ctrl.$viewValue)) {
reset();
}
});
element.bind('mousemove', function (event) {
tog((event.target.offsetWidth - 24) < (event.clientX - event.target.getBoundingClientRect().left)
&& !ctrl.$isEmpty(ctrl.$viewValue), 'onX');
});
function tog(cond, cls) {
cond ? angular.element(element).addClass(cls) : angular.element(element).removeClass(cls);
}
function reset() {
element.removeClass('x');
ctrl.$setViewValue(null);
ctrl.$render();
}
}
};
});