2 This widget does the same a the mouse widget in jqueryui.
5 SimpleWidget = require './simple.widget'
11 class MouseWidget extends SimpleWidget
12 @is_mouse_handled = false
15 @$el.bind('mousedown.mousewidget', $.proxy(@_mouseDown, this))
16 @$el.bind('touchstart.mousewidget', $.proxy(@_touchStart, this))
18 @is_mouse_started = false
20 @_mouse_delay_timer = null
21 @_is_mouse_delay_met = true
22 @mouse_down_info = null
25 @$el.unbind('mousedown.mousewidget')
26 @$el.unbind('touchstart.mousewidget')
28 $document = $(document)
29 $document.unbind('mousemove.mousewidget')
30 $document.unbind('mouseup.mousewidget')
33 # Is left mouse button?
37 result = @_handleMouseDown(
47 _handleMouseDown: (e, position_info) ->
48 # Don't let more than one widget handle mouseStart
49 if MouseWidget.is_mouse_handled
52 # We may have missed mouseup (out of window)
54 @_handleMouseUp(position_info)
56 @mouse_down_info = position_info
58 if not @_mouseCapture(position_info)
63 @is_mouse_handled = true
67 $document = $(document)
68 $document.bind('mousemove.mousewidget', $.proxy(@_mouseMove, this))
69 $document.bind('touchmove.mousewidget', $.proxy(@_touchMove, this))
70 $document.bind('mouseup.mousewidget', $.proxy(@_mouseUp, this))
71 $document.bind('touchend.mousewidget', $.proxy(@_touchEnd, this))
74 @_startMouseDelayTimer()
76 _startMouseDelayTimer: ->
77 if @_mouse_delay_timer
78 clearTimeout(@_mouse_delay_timer)
80 @_mouse_delay_timer = setTimeout(
82 @_is_mouse_delay_met = true
86 @_is_mouse_delay_met = false
89 return @_handleMouseMove(
94 _handleMouseMove: (e, position_info) ->
96 @_mouseDrag(position_info)
97 return e.preventDefault()
99 if @mouse_delay and not @_is_mouse_delay_met
102 @is_mouse_started = @_mouseStart(@mouse_down_info) != false
105 @_mouseDrag(position_info)
107 @_handleMouseUp(position_info)
109 return not @is_mouse_started
111 _getPositionInfo: (e) ->
120 return @_handleMouseUp(
124 _handleMouseUp: (position_info) ->
125 $document = $(document)
126 $document.unbind('mousemove.mousewidget')
127 $document.unbind('touchmove.mousewidget')
128 $document.unbind('mouseup.mousewidget')
129 $document.unbind('touchend.mousewidget')
132 @is_mouse_started = false
133 @_mouseStop(position_info)
137 _mouseCapture: (position_info) ->
140 _mouseStart: (position_info) ->
143 _mouseDrag: (position_info) ->
146 _mouseStop: (position_info) ->
149 setMouseDelay: (mouse_delay) ->
150 @mouse_delay = mouse_delay
153 if e.originalEvent.touches.length > 1
156 touch = e.originalEvent.changedTouches[0]
158 return @_handleMouseDown(
160 @_getPositionInfo(touch)
164 if e.originalEvent.touches.length > 1
167 touch = e.originalEvent.changedTouches[0]
169 return @_handleMouseMove(
171 @_getPositionInfo(touch)
175 if e.originalEvent.touches.length > 1
178 touch = e.originalEvent.changedTouches[0]
180 return @_handleMouseUp(
181 @_getPositionInfo(touch)
184 module.exports = MouseWidget