forked from mnmly/select-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (113 loc) · 3.25 KB
/
index.js
File metadata and controls
150 lines (113 loc) · 3.25 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
/**
* Module dependencies.
*/
var o = require( 'jquery' ),
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 = o( '<div class="select-switch"/>' );
this.optionList = o( '<ul class="option-list"/>' );
this.selectbox = o( selectbox );
this.optionCount = this.selectbox.find( 'option' ).length;
this.indexCount = 0;
this.el.css( 'height', this.height );
this.el.append( '<div class="list-wrap"/>' );
this.el.find( '.list-wrap' ).append( this.optionList );
this.el.insertAfter( this.selectbox );
this.renderItems();
this.selectbox.hide();
this.presetValue();
this.bind();
}
/**
* Render initial option item list
*/
SelectSwitch.prototype.renderItems = function() {
var _this = this,
_itemHTML = '';
this.selectbox.find( 'option' ).each( function( i, el ){
_this.addItem( '<li>' + o( el ).text() + '</li>' );
} );
};
/**
* Add list item
*/
SelectSwitch.prototype.addItem = function( item ) {
this.optionList.append( item );
this.optionList.find('li:last-child').css('line-height', this.height + 'px');
}
/**
* Bind events
*/
SelectSwitch.prototype.bind = function() {
this.events = events(this.el.get(0), 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.find( ':selected' ).index() + 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 ) {
if( e ){ e.preventDefault(); }
var _this = this,
count = this.indexCount++,
shiftY = -this.height * count;
_newItem = this.optionList.find('li').eq( count ).clone();
this.addItem( _newItem );
this.selectbox.prop('selectedIndex', count % this.optionCount ).trigger('change');
this.shift( shiftY );
}
/**
* Shift labels by `shiftY`
*/
SelectSwitch.prototype.shift = function( shiftY ) {
this.optionList.css( {
'-webkit-transform': 'translate3d( 0, ' + shiftY + 'px, 0 )',
'-moz-transform': 'translate3d( 0, ' + shiftY + 'px, 0 )',
'transform': 'translate3d( 0, ' + shiftY + 'px, 0 )'
} );
};
/**
* Setter and getter for value
*/
SelectSwitch.prototype.value = function( value ) {
if ( typeof value !== 'undefined' ){
var _index = this.selectbox.find( '[value="' + value + '"]' ).index(),
_currentIndex = this.selectbox.find( ':selected' ).index();
if( _index === -1 ){ return; }
_index -= _currentIndex;
if ( _index < 0 ){
_index += this.optionCount;
}
while( _index-- ){
this.onclick();
}
} else {
return this.selectbox.find( 'option:selected' ).val();
}
}