forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
159 lines (127 loc) · 3.93 KB
/
input.js
File metadata and controls
159 lines (127 loc) · 3.93 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* MUI Angular Input and Textarea Components
* @module angular/input
*/
import angular from 'angular';
const moduleName = 'mui.input',
emptyClass = 'mui--is-empty',
notEmptyClass = 'mui--is-not-empty',
dirtyClass = 'mui--is-dirty';
/**
* Handle empty/not-empty/dirty classes.
* @param {Element} elem - The angular-wrapped DOM element.
*/
function handleEmptyClasses(inputEl, value) {
if (value) inputEl.removeClass(emptyClass).addClass(notEmptyClass);
else inputEl.removeClass(notEmptyClass).addClass(emptyClass);
}
/**
* Build directive function.
* @param {Boolean} isTextArea
*/
function inputFactory(isTextArea) {
var emptyClass = 'mui--is-empty',
notEmptyClass = 'mui--is-not-empty',
dirtyClass = 'mui--is-dirty',
scopeArgs,
template;
// defaults
scopeArgs = {
floatLabel: '@',
hint: '@',
label: '@',
ngDisabled: '=',
ngModel: '='
};
template = '<div class="mui-textfield">';
// element-specific
if (!isTextArea) {
scopeArgs.type = '@';
template += '<input ' +
'placeholder={{hint}} ' +
'type={{type}} ' +
'ng-change="onChange()" ' +
'ng-disabled="ngDisabled" ' +
'ng-focus="onFocus()" ' +
'ng-model="ngModel" ' +
'>';
} else {
scopeArgs.rows = '@';
template += '<textarea ' +
'placeholder={{hint}} ' +
'rows={{rows}} ' +
'ng-change="onChange()" ' +
'ng-disabled="ngDisabled" ' +
'ng-focus="onFocus()" ' +
'ng-model="ngModel" ' +
'></textarea>';
}
// update template
template += '<label>{{label}}</label></div>';
// directive function
return ['$timeout', function($timeout) {
return {
restrict: 'AE',
require: ['ngModel'],
scope: scopeArgs,
replace: true,
template: template,
link: function(scope, element, attrs, controllers) {
var inputEl = element.find('input') || element.find('textarea'),
labelEl = element.find('label'),
ngModelCtrl = controllers[0],
formCtrl = controllers[1],
isUndef = angular.isUndefined,
el = inputEl[0];
// disable MUI js
if (el) el._muiTextfield = true;
// remove attributes from wrapper
element.removeAttr('ng-change');
element.removeAttr('ng-model');
// scope defaults
if (!isTextArea) scope.type = scope.type || 'text';
else scope.rows = scope.rows || 2;
// autofocus
if (!isUndef(attrs.autofocus)) inputEl[0].focus();
// required
if (!isUndef(attrs.required)) inputEl.prop('required', true);
// invalid
if (!isUndef(attrs.invalid)) inputEl.addClass('mui--is-invalid');
// set is-empty|is-no-empty
handleEmptyClasses(inputEl, scope.ngModel);
// float-label
if (!isUndef(scope.floatLabel)) {
element.addClass('mui-textfield--float-label');
$timeout(function() {
labelEl.css({
'transition': '.15s ease-out',
'-webkit-transition': '.15s ease-out',
'-moz-transition': '.15s ease-out',
'-o-transition': '.15s ease-out',
'-ms-transition': '.15s ease-out',
})
}, 150);
}
// handle changes
scope.onChange = function() {
var val = scope.ngModel;
// trigger ng-change
if (ngModelCtrl) ngModelCtrl.$setViewValue(val);
// set is-empty|is-no-empty
handleEmptyClasses(inputEl, val);
// add is-dirty
inputEl.addClass(dirtyClass);
}
// handle focus event
scope.onFocus = function() {
inputEl.addClass(dirtyClass);
}
}
};
}];
}
angular.module(moduleName, [])
.directive('muiInput', inputFactory(false))
.directive('muiTextarea', inputFactory(true));
/** Define module API */
export default moduleName;