kdf
Version:
145 lines (113 loc) • 4.38 kB
text/coffeescript
# example = new KDSliderBarView
# cssClass : 'my-cute-slider'
# minValue : 0
# maxValue : 200
# interval : 10
# width : 500
# snap : yes
# snapOnDrag : no
# drawBar : yes
# showLabels : yes or [0, 25, 50, 75, 100]
# handles : [100, 60]
KDCustomHTMLView = require './../../core/customhtmlview.coffee'
KDSliderBarHandleView = require './sliderbarhandleview.coffee'
module.exports = class KDSliderBarView extends KDCustomHTMLView
constructor:(options = {}, data = {})->
options.cssClass = KD.utils.curry "sliderbar-container", options.cssClass
options.minValue ?= 0
options.maxValue ?= 100
options.interval ?= no
options.drawBar ?= yes
options.showLabels ?= yes
options.snap ?= yes
options.snapOnDrag ?= no
options.width or= 300
options.drawOpposite ?= no
super options, data
= []
= []
createHandles:->
for value in "handles"
.push handle = new KDSliderBarHandleView {value}
sortRef = (a,b) ->
return -1 if a.options.value < b.options.value
return 1 if a.options.value > b.options.value
return 0
.sort(sortRef)
"labeled"
drawBar:->
positions = []
positions.push handle.getRelativeX() for handle in
len = positions.length
left = (parseInt(positions.first) if len > 1) or 0
right = parseInt(positions.last)
diff = right - left
unless
= new KDCustomHTMLView
cssClass : "bar"
.setWidth diff
.setX "#{left}px"
drawOppositeBar:->
positions = []
positions.push handle.getRelativeX() for handle in
right = parseInt(positions.last)
diff = - right
unless
= new KDCustomHTMLView
cssClass : "opposite bar"
.setWidth diff
.setX "#{right}px"
_createLabel : (value) =>
{maxValue, minValue, interval, showLabels} =
pos = ((value - minValue) * 100) / (maxValue - minValue)
.push label = new KDCustomHTMLView
cssClass : "sliderbar-label"
partial : "#{value}"
label.setX "#{pos}%"
addLabels:->
{maxValue, minValue, interval, showLabels} =
if Array.isArray showLabels
then value for value in showLabels
else value for value in [minValue..maxValue] by interval
getValues:-> handle.getOptions().value for handle in
setValue:(value, handle = .first, updateHandle = yes)->
handle.setValue value if updateHandle
if
if
"ValueIsChanging", handle.value
"ValueChanged", handle
setLimits:->
{maxValue, minValue, interval} =
if .length is 1
.first.options.leftLimit = minValue
.first.options.rightLimit = maxValue
else
for handle, i in
options = handle.getOptions()
options.leftLimit = [i-1]?.value + interval or minValue
options.rightLimit = [i+1]?.value - interval or maxValue
attachEvents:->
"click", (event) ->
{maxValue, minValue} =
sliderWidth =
clickedPos = event.pageX - .x
clickedValue = ((maxValue - minValue) * clickedPos) / sliderWidth + minValue
snappedValue = .first.getSnappedValue clickedValue
closestHandle = null
mindiff = null
for handle in
{value} = handle
diff = Math.abs(clickedValue - value)
if (diff < mindiff) or not mindiff
mindiff = diff
closestHandle = handle
closestHandle.setValue snappedValue
viewAppended:->
"width"
if
if
if