-
Notifications
You must be signed in to change notification settings - Fork 1
/
material-number-select.js
114 lines (98 loc) · 4.26 KB
/
material-number-select.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
/* Material Number Select -- v0.4.1 -- https://github.com/jsejcksn/material-number-select.js/ */
(function() {
'use strict';
var activeElements = document.getElementsByClassName('mdl-number-select'),
bodyClassName = document.body.className,
firstChildClass,
scrollDist = 0,
selectedElement,
wrapper;
// Cancels number select input view
function cancelSelect() {
console.log('Selection canceled');
// this.removeEventListener('click', cancelSelect); // Not needed?
restoreClass();
window.scrollTo(0, scrollDist);
document.getElementById('mdl-number-select-input').remove();
}
// Primary function: Construct number select input view
function numberSelect(element) {
selectedElement = element;
var selectedOptions = selectedElement.options;
scrollDist = document.body.scrollTop;
selectedElement.blur(); // Prevents default iOS number wheel
selectedElement.setAttribute('disabled', 'disabled'); // Safeguard
wrapper.className += ' mdl-no-scroll'; // Keep body from scrolling underneath input view
document.body.className += ' mdl-expand'; // Make body bg-color match input view and expand height to full viewport
window.scrollTo(0, 0);
// Create view
var numberInput = document.createElement('div');
numberInput.setAttribute('id', 'mdl-number-select-input');
numberInput.className = 'mdl-number-select-input';
document.body.appendChild(numberInput);
// Create cancel button
var btn = document.createElement('button');
var txtNode = document.createTextNode('✕');
btn.appendChild(txtNode);
btn.className = 'mdl-button mdl-js-button mdl-js-ripple-effect mdl-number-select-input-cancel';
componentHandler.upgradeElement(btn);
document.getElementById('mdl-number-select-input').appendChild(btn);
btn.addEventListener('click', cancelSelect);
// Populate with options from select element
for (var i = 0; i < selectedOptions.length; i++) {
var button = document.createElement('button');
var textNode = document.createTextNode(selectedOptions[i].text);
button.value = selectedOptions[i].value;
button.appendChild(textNode);
button.className = 'mdl-button mdl-js-button mdl-js-ripple-effect mdl-number-select-input-option';
componentHandler.upgradeElement(button);
numberInput.appendChild(button);
button.addEventListener('click', returnNum);
}
selectedElement.removeAttribute('disabled'); // Remove safeguard
}
// Restore scrolling to body
function restoreClass() {
document.body.className = bodyClassName;
wrapper.className = firstChildClass;
}
// Return selection from number input view
function returnNum() {
console.log(this.value + ' selected');
// var btns = document.querySelectorAll('#mdl-number-select-input > button'); // Not needed?
// for (var i = 0; i < btns.length; i++) {
// btns[i].removeEventListener('click', returnNum);
// }
selectedElement.value = this.value;
restoreClass();
window.scrollTo(0, scrollDist);
document.getElementById('mdl-number-select-input').remove();
}
// Add event listener to each select element having class="mdl-number-select"
for (var i = 0; i < activeElements.length; i++) {
activeElements[i].addEventListener('mousedown', function(e) {
e.preventDefault();
numberSelect(this);
});
}
// Wrap body contents into containing <div>
(function() {
var div = document.createElement("div");
// Move the body's children into this wrapper
while (document.body.firstChild) {
div.appendChild(document.body.firstChild);
}
// Append the wrapper to the body
document.body.appendChild(div);
wrapper = div;
firstChildClass = wrapper.className;
}());
// Add CSS to <head>
(function() {
var head = document.head,
style = document.createElement('style'),
css = '.mdl-expand {background-color: #efefef;height: 100%;} .mdl-no-scroll {height: 0;overflow: hidden;width: 0;} .mdl-number-select-input {background-color: #efefef;height: 100%;margin: 0;padding: 0;width: 100%;} .mdl-number-select-input-cancel {background-color: #cc0000;color: #efefef;height: 64px;} .mdl-number-select-input-option {height: 64px;}';
style.innerHTML = css;
head.appendChild(style);
}());
}());