forked from mbraak/jqTree
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.widget.coffee
105 lines (76 loc) · 2.41 KB
/
mouse.widget.coffee
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
###
This widget does the same a the mouse widget in jqueryui.
###
class MouseWidget extends SimpleWidget
@is_mouse_handled = false
_init: ->
@$el.bind('mousedown.mousewidget', $.proxy(@_mouseDown, this))
@is_mouse_started = false
@mouse_delay = 0
@_mouse_delay_timer = null
@_is_mouse_delay_met = true
_deinit: ->
@$el.unbind('mousedown.mousewidget')
$document = $(document)
$document.unbind('mousemove.mousewidget')
$document.unbind('mouseup.mousewidget')
_mouseDown: (e) ->
# Don't let more than one widget handle mouseStart
if MouseWidget.is_mouse_handled
return
# We may have missed mouseup (out of window)
if not @is_mouse_started
@_mouseUp(e)
# Is left mouse button?
if e.which != 1
return
if not @_mouseCapture(e)
return
@mouse_down_event = e
$document = $(document)
$document.bind('mousemove.mousewidget', $.proxy(@_mouseMove, this))
$document.bind('mouseup.mousewidget', $.proxy(@_mouseUp, this))
if @mouse_delay
@_startMouseDelayTimer()
e.preventDefault()
@is_mouse_handled = true
return true
_startMouseDelayTimer: ->
if @_mouse_delay_timer
clearTimeout(@_mouse_delay_timer)
@_mouse_delay_timer = setTimeout(
=>
@_is_mouse_delay_met = true
, @mouse_delay
)
@_is_mouse_delay_met = false
_mouseMove: (e) ->
if @is_mouse_started
@_mouseDrag(e)
return e.preventDefault()
if @mouse_delay and not @_is_mouse_delay_met
return true
@is_mouse_started = @_mouseStart(@mouse_down_event) != false
if @is_mouse_started
@_mouseDrag(e)
else
@_mouseUp(e)
return not @is_mouse_started
_mouseUp: (e) ->
$document = $(document)
$document.unbind('mousemove.mousewidget')
$document.unbind('mouseup.mousewidget')
if @is_mouse_started
@is_mouse_started = false
@_mouseStop(e)
return false
_mouseCapture: (e) ->
return true
_mouseStart: (e) ->
null
_mouseDrag: (e) ->
null
_mouseStop: (e) ->
null
setMouseDelay: (mouse_delay) ->
@mouse_delay = mouse_delay