summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-FE/client/bower_components/jqTree/src/mouse.widget.coffee
blob: f76ff7e6a31468b0396ddda9ce078fe3467f0ece (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
###
This widget does the same a the mouse widget in jqueryui.
###

SimpleWidget = require './simple.widget'


$ = jQuery


class MouseWidget extends SimpleWidget
    @is_mouse_handled = false

    _init: ->
        @$el.bind('mousedown.mousewidget', $.proxy(@_mouseDown, this))
        @$el.bind('touchstart.mousewidget', $.proxy(@_touchStart, this))

        @is_mouse_started = false
        @mouse_delay = 0
        @_mouse_delay_timer = null
        @_is_mouse_delay_met = true
        @mouse_down_info = null

    _deinit: ->
        @$el.unbind('mousedown.mousewidget')
        @$el.unbind('touchstart.mousewidget')

        $document = $(document)
        $document.unbind('mousemove.mousewidget')
        $document.unbind('mouseup.mousewidget')

    _mouseDown: (e) ->
        # Is left mouse button?
        if e.which != 1
            return

        result = @_handleMouseDown(
            e,
            @_getPositionInfo(e)
        )

        if result
            e.preventDefault()

        return result

    _handleMouseDown: (e, position_info) ->
        # Don't let more than one widget handle mouseStart
        if MouseWidget.is_mouse_handled
            return

        # We may have missed mouseup (out of window)
        if @is_mouse_started
            @_handleMouseUp(position_info)

        @mouse_down_info = position_info

        if not @_mouseCapture(position_info)
            return

        @_handleStartMouse()

        @is_mouse_handled = true
        return true

    _handleStartMouse: ->
        $document = $(document)
        $document.bind('mousemove.mousewidget', $.proxy(@_mouseMove, this))
        $document.bind('touchmove.mousewidget', $.proxy(@_touchMove, this))
        $document.bind('mouseup.mousewidget', $.proxy(@_mouseUp, this))
        $document.bind('touchend.mousewidget', $.proxy(@_touchEnd, this))

        if @mouse_delay
            @_startMouseDelayTimer()

    _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) ->
        return @_handleMouseMove(
            e,
            @_getPositionInfo(e)
        )

    _handleMouseMove: (e, position_info) ->
        if @is_mouse_started
            @_mouseDrag(position_info)
            return e.preventDefault()

        if @mouse_delay and not @_is_mouse_delay_met
            return true

        @is_mouse_started = @_mouseStart(@mouse_down_info) != false

        if @is_mouse_started
            @_mouseDrag(position_info)
        else
            @_handleMouseUp(position_info)

        return not @is_mouse_started

    _getPositionInfo: (e) ->
        return {
            page_x: e.pageX,
            page_y: e.pageY,
            target: e.target,
            original_event: e
        }

    _mouseUp: (e) ->
        return @_handleMouseUp(
            @_getPositionInfo(e)
        )

    _handleMouseUp: (position_info) ->
        $document = $(document)
        $document.unbind('mousemove.mousewidget')
        $document.unbind('touchmove.mousewidget')
        $document.unbind('mouseup.mousewidget')
        $document.unbind('touchend.mousewidget')

        if @is_mouse_started
            @is_mouse_started = false
            @_mouseStop(position_info)

        return

    _mouseCapture: (position_info) ->
        return true

    _mouseStart: (position_info) ->
        null

    _mouseDrag: (position_info) ->
        null

    _mouseStop: (position_info) ->
        null

    setMouseDelay: (mouse_delay) ->
        @mouse_delay = mouse_delay

    _touchStart: (e) ->
        if e.originalEvent.touches.length > 1
            return

        touch = e.originalEvent.changedTouches[0]

        return @_handleMouseDown(
            e,
            @_getPositionInfo(touch)
        )

    _touchMove: (e) ->
        if e.originalEvent.touches.length > 1
            return

        touch = e.originalEvent.changedTouches[0]

        return @_handleMouseMove(
            e,
            @_getPositionInfo(touch)
        )

    _touchEnd: (e) ->
        if e.originalEvent.touches.length > 1
            return

        touch = e.originalEvent.changedTouches[0]

        return @_handleMouseUp(
            @_getPositionInfo(touch)
        )

module.exports = MouseWidget