-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
178 lines (132 loc) · 3.67 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
/**
* Module dependencies.
*/
var find = require('select')
, events = require('events');
/**
* Expose `SelectionSwitch`.
*/
module.exports = SelectSwitch;
/**
* Initialize a new `SelectionSwitch`
*
* Create an `SelectionSwitch` against `selectbox`.
* `height` is optional, but should be required...
*
*/
function SelectSwitch(selectbox, height){
if(!(this instanceof SelectSwitch)) return new SelectSwitch(selectbox);
this.height = height || 21;
this.el = this._createElement('div', 'select-switch');
this.optionList = this._createElement('ul', 'option-list');
this.selectbox = selectbox;
this.optionCount = this.selectbox.options.length;
this.indexCount = 0;
this.el.style.height = this.height + 'px';
this.listWrap = this._createElement('div', 'list-wrap');
this.listWrap.appendChild(this.optionList);
this.el.appendChild(this.listWrap);
this.selectbox.parentElement.appendChild(this.el);
this.renderItems();
this.selectbox.style.display = 'none';
this.presetValue();
this.bind();
}
/**
* Render initial option item list
*/
SelectSwitch.prototype.renderItems = function() {
for ( var i = 0; i < this.selectbox.options.length; i += 1 ) {
var option = this.selectbox.options[i]
, li = document.createElement('li');
li.textContent = option.textContent;
this.addItem(li);
}
};
/**
* Add list item
*/
SelectSwitch.prototype.addItem = function(item) {
item.style.lineHeight = this.height + 'px';
this.optionList.appendChild(item);
}
/**
* Bind events
*/
SelectSwitch.prototype.bind = function() {
this.events = events(this.el, this);
this.events.bind(('ontouchstart' in window) ? 'touchstart' : 'click');
}
/**
* Preset the value by getting `selected` state from `selectbox` options
*/
SelectSwitch.prototype.presetValue = function() {
var _selectedIndex = this.selectbox.selectedIndex + 1;
while(_selectedIndex--){
this.onclick();
}
}
/**
* onclick event
*
* Change the selectbox value, and shift labels.
*/
SelectSwitch.prototype.ontouchstart = function( e ) {
this.onclick(e);
}
SelectSwitch.prototype.onclick = function( e ) {
e && e.preventDefault();
var count = this.indexCount++,
shiftY = -this.height * count,
_newItem = this.optionList.children[count].cloneNode(true);
this.addItem(_newItem);
var newValue = this.selectbox.options[count % this.optionCount].value;
if ("fireEvent" in this.selectbox){
this.selectbox.fireEvent("onchange");
} else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
this.selectbox.dispatchEvent(evt);
}
this.shift(shiftY);
}
/**
* Shift labels by `shiftY`
*/
SelectSwitch.prototype.shift = function(shiftY) {
this.optionList.style.webkitTransform =
this.optionList.style.mozTransform =
this.optionList.style.transform = 'translate3d(0, ' + shiftY + 'px, 0)';
};
/**
* Setter and getter for value
*/
SelectSwitch.prototype.value = function(value) {
if (typeof value !== 'undefined'){
var _index = -1
, option = find(this.selectbox.options, function(item, i) {
var condition = value === item.value;
if (condition) _index = i;
return condition
})
, _currentIndex = this.selectbox.selectedIndex;
if( _index === -1 ){ return; }
_index -= _currentIndex;
if (_index < 0){
_index += this.optionCount;
}
while( _index-- ){
this.onclick();
}
} else {
return this.selectbox.selectedOptions[0];
}
}
/**
* Create element helper
*/
SelectSwitch.prototype._createElement = function(tagName, className) {
var el = document.createElement(tagName);
el.className = className;
return el;
};