This repository was archived by the owner on Apr 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
290 lines (256 loc) · 7.93 KB
/
index.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'use strict';
/**
* Create a ripple.
* @param {object} rect - the size of an element and its position relative to
* the viewport.
* @param {string} rippleClass - class name of the ripple ot be created.
* @return {number} a DOM node.
*/
var createRipple = function createRipple(rect, rippleClass) {
var ripple = document.createElement('span');
ripple.classList.add(rippleClass);
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px';
return ripple;
};
/**
* Positionate a ripple.
* @param {object} ripple - DOM node.
* @param {string|number} top - top position of the node.
* @param {string|number} left - left position of the node.
* @example
* const node = document.getElementById('id')
* positionateRipple(node, 100, 200)
*/
var positionateRipple = function positionateRipple(ripple, top, left) {
ripple.style.top = top + 'px';
ripple.style.left = left + 'px';
ripple.classList.add('is-active');
};
/**
* Get half of a number.
* @param {number} n - a number.
* @return {number} half of the n.
*/
var getHalf = function getHalf(n) {
return n / 2;
};
/**
* Main function of button component that adds the ripple effect.
* @param {string} rippleClass - class name of the ripple.
* @param {object} e - DOM event.
*/
var button = (function (rippleClass) {
return function (e) {
var button = e.target;
var buttonRect = button.getBoundingClientRect();
var ripple = button.querySelector('.' + rippleClass);
if (!ripple) {
ripple = createRipple(buttonRect, rippleClass);
button.appendChild(ripple);
}
ripple.classList.remove('is-active');
var left = e.pageX - buttonRect.left - getHalf(ripple.offsetWidth) - document.body.scrollLeft;
var top = e.pageY - buttonRect.top - getHalf(ripple.offsetHeight) - document.body.scrollTop;
positionateRipple(ripple, top, left);
return false;
};
});
/**
* Check if browser is firefox.
* @param {string} userAgent - userAgent of the browser.
* @return {boolean} if userAgent contains Firefox return true.
* @example
* isFirefox(navigator.userAgent)
*/
var isFirefox = function isFirefox(userAgent) {
return userAgent.search('Firefox') > -1;
};
/**
* Toggle a attribute from a DOM node.
* @param {Object} node - DOM node.
* @param {attr} attr - attribute to be toggled.
* @example
* const node = document.getElementById('id')
* toggleAttribute(node, 'checked')
*/
var toggleAttribute = function toggleAttribute(node, attr) {
if (node.hasAttribute(attr)) {
node.removeAttribute(attr);
} else {
node.setAttribute(attr, '');
}
};
/**
* Checks if a DOM node is disabled.
* @param {Object} node - DOM node.
* @return {boolean} if disabled return true otherwise return false.
* @example
* const node = document.getElementById('id')
* isDisabled(node)
*/
var isDisabled = function isDisabled(node) {
var hasDisabledAttribute = node.hasAttribute('disabled') && node.getAttribute('disabled') !== 'false';
if (node.classList.contains('is-disabled')) {
return true;
} else if (hasDisabledAttribute) {
return true;
}
return false;
};
/**
* Simulate the default click behaviour of a standard checkbox.
* @param {object} e - DOM event.
* const node = document.getElementById('id')
* node.addEventListener('click', checkboxBehaviour)
*/
var checkboxBehaviour = function checkboxBehaviour(e) {
var node = e.target;
if (!isDisabled(node)) {
node.classList.toggle('is-checked');
toggleAttribute(node.nextSibling, 'checked');
}
};
/**
* Hide a node by setting the display to none.
* @param {object} node - DOM node.
* @example
* const node = document.getElementById('id')
* hide(node)
*/
var hide = function hide(node) {
node.style.display = 'none';
};
/**
* Create a DOM node that shadow a standard checkbox.
* @param {string} className - class name of the checkbox.
* @param {boolean} checked - checked state of the checkbox.
* @param {boolean} disabled - disabled state of the checkbox.
* @return {object} DOM node that shadow a standard chckbox.
* @example
* createCheckbox('checkbox', false, true)
*/
var createCheckbox = function createCheckbox(className, checked, disabled) {
var checkbox = document.createElement('span');
checkbox.classList.add(className);
if (checked) {
checkbox.classList.add('is-checked');
}
if (disabled) {
checkbox.classList.add('is-disabled');
} else {
checkbox.tabIndex = 0;
}
return checkbox;
};
/**
* Deactivate the checkboxes and add a DOM that simulate their behaviour.
* @param {string} checkboxClass - class name of the checkboxes.
* @example
* firefoxCompat('checkbox')
*/
var firefoxCompat = function firefoxCompat(checkboxClass) {
[].slice.call(document.querySelectorAll('.' + checkboxClass)).forEach(function (node) {
hide(node);
var checkbox = createCheckbox(checkboxClass, node.checked, node.disabled);
checkbox.addEventListener('click', checkboxBehaviour);
node.parentNode.insertBefore(checkbox, node);
});
};
/**
* Check if browser is firefox, if true add compatible checkboxes to the
* document.
* @param {string} userAgent - user agent of the browser.
* @param {string} checkboxClass - class name of the checkboxes.
* @example
* checkbox(navigator.userAgent, 'checkbox')
*/
var checkbox = function checkbox(userAgent, checkboxClass) {
if (isFirefox(userAgent)) {
firefoxCompat(checkboxClass);
}
};
/**
* Check validity of a text-field.
* Given a class, add this class to nodeToChange if nodeToValidate is valid,
* otherwise remove a this class from nodeTo Validate.
* @param {object} nodeToValidate - DOM node.
* @param {object} nodeToChange - DOM node.
* @param {string} validClassName - class name of valid nodes.
*/
var validate = function validate(nodeToValidate) {
return function (nodeToChange, validClassName) {
var isValid = nodeToValidate.checkValidity();
if (isValid) {
nodeToChange.classList.add(validClassName);
} else {
nodeToChange.classList.remove(validClassName);
}
};
};
/**
* On blur function for text-field.
* Remove class is-active of parent node and if value is not empty add class
* is-closed
* @param {object} e - DOM event.
* @example
* const node = document.getElementById('id')
* node.addEventListener('blur', leavingInput, true)
*/
var leavingInput = function leavingInput(e) {
var target = e.target;
target.parentNode.classList.remove('is-active');
if (!target.value && !target.placeholder) {
target.parentNode.classList.add('is-closed');
}
};
/**
* On click function for text-field.
* Remove class is-closed and add is-active.
* @param {object} e - DOM event.
* @example
* const node = document.getElementById('id')
* node.addEventListener('click', focusingInput)
*/
var focusingInput = function focusingInput(e) {
var target = e.target;
target.parentNode.classList.add('is-active');
target.parentNode.classList.remove('is-closed');
};
/**
* On input function for text-field.
* If input is valid add class is-valid to the parent node.
* @param {object} e - DOM event.
* @example
* const node = document.getElementById('id')
* node.addEventListener('input', typingInput)
*/
var typingInput = function typingInput(e) {
var target = e.target;
var toggleValidClass = validate(target);
toggleValidClass(target.parentNode, 'is-valid');
};
/**
* Main function.
* Add class is-closed do the parent node and attach the events.
* @param {object} node - DOM node.
* @example
* const node = document.getElementById('id')
* textField(node)
*/
var textField = (function (node) {
if (!node.placeholder) {
node.parentNode.classList.add('is-closed');
}
node.addEventListener('blur', leavingInput, true);
node.addEventListener('focus', focusingInput);
node.addEventListener('input', typingInput);
});
var potamus = function potamus(opts) {
return function (style) {
style.import('./components/*/*.styl');
};
};
potamus.button = button;
potamus.checkbox = checkbox;
potamus.textField = textField;
module.exports = potamus;