nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / src / mouse.widget.coffee
1 ###
2 This widget does the same a the mouse widget in jqueryui.
3 ###
4
5 SimpleWidget = require './simple.widget'
6
7
8 $ = jQuery
9
10
11 class MouseWidget extends SimpleWidget
12     @is_mouse_handled = false
13
14     _init: ->
15         @$el.bind('mousedown.mousewidget', $.proxy(@_mouseDown, this))
16         @$el.bind('touchstart.mousewidget', $.proxy(@_touchStart, this))
17
18         @is_mouse_started = false
19         @mouse_delay = 0
20         @_mouse_delay_timer = null
21         @_is_mouse_delay_met = true
22         @mouse_down_info = null
23
24     _deinit: ->
25         @$el.unbind('mousedown.mousewidget')
26         @$el.unbind('touchstart.mousewidget')
27
28         $document = $(document)
29         $document.unbind('mousemove.mousewidget')
30         $document.unbind('mouseup.mousewidget')
31
32     _mouseDown: (e) ->
33         # Is left mouse button?
34         if e.which != 1
35             return
36
37         result = @_handleMouseDown(
38             e,
39             @_getPositionInfo(e)
40         )
41
42         if result
43             e.preventDefault()
44
45         return result
46
47     _handleMouseDown: (e, position_info) ->
48         # Don't let more than one widget handle mouseStart
49         if MouseWidget.is_mouse_handled
50             return
51
52         # We may have missed mouseup (out of window)
53         if @is_mouse_started
54             @_handleMouseUp(position_info)
55
56         @mouse_down_info = position_info
57
58         if not @_mouseCapture(position_info)
59             return
60
61         @_handleStartMouse()
62
63         @is_mouse_handled = true
64         return true
65
66     _handleStartMouse: ->
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))
72
73         if @mouse_delay
74             @_startMouseDelayTimer()
75
76     _startMouseDelayTimer: ->
77         if @_mouse_delay_timer
78             clearTimeout(@_mouse_delay_timer)
79
80         @_mouse_delay_timer = setTimeout(
81             =>
82                 @_is_mouse_delay_met = true
83             , @mouse_delay
84         )
85
86         @_is_mouse_delay_met = false
87
88     _mouseMove: (e) ->
89         return @_handleMouseMove(
90             e,
91             @_getPositionInfo(e)
92         )
93
94     _handleMouseMove: (e, position_info) ->
95         if @is_mouse_started
96             @_mouseDrag(position_info)
97             return e.preventDefault()
98
99         if @mouse_delay and not @_is_mouse_delay_met
100             return true
101
102         @is_mouse_started = @_mouseStart(@mouse_down_info) != false
103
104         if @is_mouse_started
105             @_mouseDrag(position_info)
106         else
107             @_handleMouseUp(position_info)
108
109         return not @is_mouse_started
110
111     _getPositionInfo: (e) ->
112         return {
113             page_x: e.pageX,
114             page_y: e.pageY,
115             target: e.target,
116             original_event: e
117         }
118
119     _mouseUp: (e) ->
120         return @_handleMouseUp(
121             @_getPositionInfo(e)
122         )
123
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')
130
131         if @is_mouse_started
132             @is_mouse_started = false
133             @_mouseStop(position_info)
134
135         return
136
137     _mouseCapture: (position_info) ->
138         return true
139
140     _mouseStart: (position_info) ->
141         null
142
143     _mouseDrag: (position_info) ->
144         null
145
146     _mouseStop: (position_info) ->
147         null
148
149     setMouseDelay: (mouse_delay) ->
150         @mouse_delay = mouse_delay
151
152     _touchStart: (e) ->
153         if e.originalEvent.touches.length > 1
154             return
155
156         touch = e.originalEvent.changedTouches[0]
157
158         return @_handleMouseDown(
159             e,
160             @_getPositionInfo(touch)
161         )
162
163     _touchMove: (e) ->
164         if e.originalEvent.touches.length > 1
165             return
166
167         touch = e.originalEvent.changedTouches[0]
168
169         return @_handleMouseMove(
170             e,
171             @_getPositionInfo(touch)
172         )
173
174     _touchEnd: (e) ->
175         if e.originalEvent.touches.length > 1
176             return
177
178         touch = e.originalEvent.changedTouches[0]
179
180         return @_handleMouseUp(
181             @_getPositionInfo(touch)
182         )
183
184 module.exports = MouseWidget