-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (76 loc) · 2.26 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
var events = require('events')
, event = require('event')
, position = require('position')
, query = require('query')
, Tip = require('tip')
, _ = require('lodash')
, template = require('./template')
module.exports = Slider
function domify(txt) {
var d = document.createElement('div')
d.innerHTML = txt
return d.firstChild
}
function Slider(opts) {
Tip.call(this, template)
this.sliding = false
opts = opts || {}
this.initial = this.value = opts.value || 0
if ('undefined' !== typeof opts.min &&
'undefined' !== typeof opts.max && !opts.scale) {
opts.scale = (opts.max - opts.min) / 200
}
opts.scale = opts.scale || 1
this.opts = opts
this.sliderContainer = query('.slider-container', this.el)
this.sliderInner = query('.slider-inner', this.el)
this.sliderHandle = query('.slider-handle', this.el)
event.bind(this.sliderHandle, 'mousedown', this.slideDown.bind(this))
this.winEvents.bind('mousemove', 'slideMove')
this.winEvents.bind('mouseup', 'slideUp')
}
Slider.prototype = new Tip()
_.extend(Slider.prototype, {
set: function (value, silent) {
this.value = this.initial = value
if (!silent) this.emit('change', value)
},
slideMove: function (e) {
if (!this.sliding) return
e.preventDefault()
e.stopPropagation()
var pos = position(this.sliderContainer)
, num = (e.pageX - pos.left - 15)
, val = parseInt(this.initial + num * this.opts.scale, 10)
if ('undefined' !== typeof this.opts.min && this.opts.min > val) {
val = this.opts.min
num = (val - this.initial)/this.opts.scale
}
if ('undefined' !== typeof this.opts.max && this.opts.max < val) {
val = this.opts.max
num = (val - this.initial)/this.opts.scale
}
if (val !== this.value) {
this.sliderInner.style.marginLeft = num + 'px'
this.value = val
this.emit('change', this.value)
}
return false
},
slideDown: function (e) {
this.sliding = true
this.value = this.initial
e.preventDefault()
e.stopPropagation()
return false
},
slideUp: function (e) {
if (!this.sliding) return
this.sliding = false
this.initial = this.value
this.sliderInner.style.marginLeft = '0px'
e.preventDefault()
e.stopPropagation()
return false
}
})