forked from mhartington/ion-md-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathion-md-input.js
87 lines (74 loc) · 2.49 KB
/
ion-md-input.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
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
angular.module('ionMdInput', [])
.directive('ionMdInput', function() {
return {
restrict: 'E',
replace: true,
require: '?ngModel',
template: '<label class="item item-input item-md-label">' +
'<input type="text" class="md-input">' +
'<span class="input-label"></span>' +
'<div class="highlight"></div>' +
'</label>',
compile: function(element, attr) {
var highlight = element[0].querySelector('.highlight');
var highlightColor;
if (!attr.highlightColor) {
highlightColor = 'calm';
} else {
highlightColor = attr.highlightColor;
}
highlight.className += ' highlight-' + highlightColor;
var label = element[0].querySelector('.input-label');
label.innerHTML = attr.placeholder;
/*Start From here*/
var input = element.find('input');
angular.forEach({
'name': attr.name,
'type': attr.type,
'ng-value': attr.ngValue,
'ng-model': attr.ngModel,
'required': attr.required,
'ng-required': attr.ngRequired,
'ng-minlength': attr.ngMinlength,
'ng-maxlength': attr.ngMaxlength,
'ng-pattern': attr.ngPattern,
'ng-change': attr.ngChange,
'ng-trim': attr.trim,
'ng-blur': attr.ngBlur,
'ng-focus': attr.ngFocus,
}, function(value, name) {
if (angular.isDefined(value)) {
input.attr(name, value);
}
});
var cleanUp = function() {
ionic.off('$destroy', cleanUp, element[0]);
};
// add listener
ionic.on('$destroy', cleanUp, element[0]);
return function LinkingFunction($scope, $element) {
var mdInput = $element[0].querySelector('.md-input');
var dirtyClass = 'used';
var reg = new RegExp('(\\s|^)' + dirtyClass + '(\\s|$)');
//Here is our toggle function
var toggleClass = function() {
if (this.value === '') {
this.className = mdInput.className.replace(reg, ' ');
} else {
this.classList.add(dirtyClass);
}
};
//Lets check if there is a value on load
ionic.DomUtil.ready(function() {
if (mdInput.value === '') {
mdInput.className = mdInput.className.replace(reg, ' ');
} else {
mdInput.classList.add(dirtyClass);
}
});
// Here we are saying, on 'blur', call toggleClass, on mdInput
ionic.on('blur', toggleClass, mdInput);
};
}
};
});