2012-11-18 20:02:46 +01:00
|
|
|
Morris.Hover =
|
|
|
|
hoverConfigure: (options) ->
|
|
|
|
@hoverOptions = $.extend {}, @hoverDefaults, options ? {}
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverInit: ->
|
|
|
|
if @hoverOptions.enableHover
|
|
|
|
@hover = @hoverBuild()
|
|
|
|
@hoverBindEvents()
|
|
|
|
@hoverShow(if @hoverOptions.hideHover then null else @data.length - 1)
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverDefaults:
|
|
|
|
enableHover: true
|
|
|
|
popupClass: "morris-popup"
|
|
|
|
hideHover: false
|
|
|
|
allowOverflow: false
|
|
|
|
pointMargin: 10
|
|
|
|
hoverFill: (index, row) -> @hoverFill(index, row)
|
|
|
|
|
|
|
|
hoverBindEvents: ->
|
|
|
|
@el.mousemove (evt) =>
|
|
|
|
@hoverUpdate evt.pageX
|
|
|
|
if @hoverOptions.hideHover
|
|
|
|
@el.mouseout (evt) =>
|
|
|
|
@hoverShow null
|
|
|
|
touchHandler = (evt) =>
|
|
|
|
touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0]
|
|
|
|
@hoverUpdate touch.pageX
|
|
|
|
return touch
|
|
|
|
@el.bind 'touchstart', touchHandler
|
|
|
|
@el.bind 'touchmove', touchHandler
|
|
|
|
@el.bind 'touchend', touchHandler
|
|
|
|
|
|
|
|
@hover.mousemove (evt) -> evt.stopPropagation()
|
|
|
|
@hover.mouseout (evt) -> evt.stopPropagation()
|
|
|
|
@hover.bind 'touchstart', (evt) -> evt.stopPropagation()
|
|
|
|
@hover.bind 'touchmove', (evt) -> evt.stopPropagation()
|
|
|
|
@hover.bind 'touchend', (evt) -> evt.stopPropagation()
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverCalculateMargins: ->
|
|
|
|
@hoverMargins = for i in [1...@data.length]
|
|
|
|
@left + i * @width / @data.length
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverBuild: ->
|
|
|
|
hover = $ "<div/>"
|
|
|
|
hover.addClass "#{@hoverOptions.popupClass} js-morris-popup"
|
|
|
|
hover.appendTo @el
|
|
|
|
hover.hide()
|
|
|
|
hover
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverUpdate: (x) ->
|
|
|
|
x -= @el.offset().left
|
|
|
|
for hoverIndex in [0...@hoverMargins.length]
|
|
|
|
break if @hoverMargins[hoverIndex] > x
|
|
|
|
@hoverShow hoverIndex
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverShow: (index) ->
|
|
|
|
if index isnt null
|
|
|
|
@hover.html("")
|
|
|
|
@hoverOptions.hoverFill.call(@, index, @data[index])
|
|
|
|
@hoverPosition(index)
|
|
|
|
@fire "hover.show", index
|
|
|
|
@hover.show()
|
|
|
|
if not index?
|
|
|
|
@hoverHide()
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverHide: ->
|
|
|
|
@hover.hide()
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
colorFor: (row, i, type) -> "inherit"
|
|
|
|
yLabelFormat: (label) -> Morris.commas(label)
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverPosition: (index) ->
|
|
|
|
[x, y] = @hoverGetPosition index
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
@hover.css
|
|
|
|
top: "#{@el.offset().top + y}px"
|
|
|
|
left: "#{@el.offset().left + x}px"
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverGetPosition: (index) ->
|
|
|
|
row = @data[index]
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
@hoverWidth = @hover.outerWidth(true)
|
|
|
|
@hoverHeight = @hover.outerHeight(true)
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
miny = y = Math.min.apply(null, (y for y in row._y when y isnt null).concat(@bottom))
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
x = row._x - @hoverWidth/2
|
|
|
|
y = miny
|
|
|
|
y = y - @hoverHeight - @hoverOptions.pointMargin
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
unless @hoverOptions.allowOverflow
|
|
|
|
if x < @left
|
|
|
|
x = row._x + @hoverOptions.pointMargin
|
|
|
|
else if x > @right - @hoverWidth
|
|
|
|
x = row._x - @hoverWidth - @hoverOptions.pointMargin
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
y = Math.max y, @top
|
|
|
|
y = Math.min y, (@bottom - @hoverHeight - @hoverOptions.pointMargin)
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
if y - miny < @hoverWidth + @hoverOptions.pointMargin
|
|
|
|
y = miny + @hoverOptions.pointMargin
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
[x, y]
|
2012-12-02 23:23:59 +01:00
|
|
|
|
2012-11-18 20:02:46 +01:00
|
|
|
hoverFill: (index, row) ->
|
|
|
|
xLabel = $ "<h4/>"
|
|
|
|
xLabel.text row.label
|
|
|
|
xLabel.appendTo @hover
|
|
|
|
for y, i in row.y
|
|
|
|
yLabel = $ "<p/>"
|
|
|
|
yLabel.css "color", @colorFor(row, i, "hover")
|
|
|
|
yLabel.text "#{@options.labels[i]}: #{@yLabelFormat(y)}"
|
2012-12-02 23:23:59 +01:00
|
|
|
yLabel.appendTo @hover
|