forked from chemzqm/scroll-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (140 loc) · 3.42 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
var Emitter = require('emitter')
var domify = require('domify')
var _ = require('dom')
var Iscroll = require('iscroll')
var hasTouch = require('has-touch')
/**
* Init select with `el` and optional option
*
* @constructor
* @param {Element} el
* @param {Object} opt [optional]
* @returns {undefined}
*/
function Select(el, opt) {
if (!(this instanceof Select)) return new Select(el)
var container = this.container = create('div', 'scroll-select-container')
el.appendChild(this.container)
this.rowHeight = opt.rowHeight || 30
var data = opt.data || []
var main = create('div')
container.appendChild(main)
container.appendChild(create('div', 'scroll-select-top'))
container.appendChild(create('div', 'scroll-select-bottom'))
var iscroll = this.iscroll = new Iscroll(container);
this.el = create('ul', 'scroll-select')
main.appendChild(this.el)
this.setData(data)
this._onscrollend = this.onscrollend.bind(this)
iscroll.on('scrollend', this._onscrollend)
}
Emitter(Select.prototype)
/**
* Set internal data
*
* @public
* @param {Array} data
*/
Select.prototype.setData = function (data) {
var v = this.value()
var parentNode = this.el
var is = this.iscroll
this.data = data
_(parentNode).clean('.scroll-select-item')
var fragment = document.createDocumentFragment()
for (var i = 0, l = data.length; i < l; i++) {
var o = data[i]
var el = domify('<li class="scroll-select-item" data-index="' +
i + '">' + o.text + '</li>')
fragment.appendChild(el)
}
parentNode.appendChild(fragment)
//no scroll refresh
is.refresh(true)
var vals = data.map(function (o) {
return String(o.id)
})
var idx = vals.indexOf(v)
if (v == null || idx === -1) return is.scrollTo(0, 0)
is.scrollTo(- idx*this.rowHeight, 0)
}
/**
* Unbind all event listeners
*
* @public
*/
Select.prototype.unbind = function () {
this.iscroll.unbind()
this.off()
_(this.container).remove()
}
/**
* Select previous item
*
* @public
*/
Select.prototype.prev = function () {
var is = this.iscroll
var y = is.y
is.scrollTo(y + this.rowHeight, 350)
}
/**
* Select next item
*
* @public
*/
Select.prototype.next = function () {
var is = this.iscroll
var y = is.y
is.scrollTo(y - this.rowHeight, 350)
}
/**
* Scroll end event handler
*
* @private
*/
Select.prototype.onscrollend = function () {
var is = this.iscroll
var y = is.y
if (y > 0) return is.scrollTo(0, 300)
if (y < is.minY) return is.scrollTo(is.minY, 300)
if (y%this.rowHeight === 0) {
var v = this.value()
if (this._value !== v) {
this.emit('change', v, this._value)
}
this._value = v
return
}
var m = is.direction == 1 ? Math.floor : Math.ceil
m = hasTouch? Math.round : m
var dest = this.rowHeight * m(y/this.rowHeight)
is.scrollTo(dest, 300)
}
/**
* Get/Set the value
*
* @public
* @param {String|Number} value
*/
Select.prototype.value = function (value) {
if (arguments.length === 0) {
if (!this.data) return
var n = Math.round(- this.iscroll.y/this.rowHeight)
return this.data[n].id
}
for (var i = 0, l = this.data.length; i < l; i++) {
var o = this.data[i]
if (o.id == value) {
this.iscroll.scrollTo(- i*this.rowHeight, 0)
return
}
}
throw new Error('value: ' + value + ' not found on data')
}
function create(tag, className) {
var el = document.createElement(tag)
if (className) el.className = className
return el
}
module.exports = Select