nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / lib / mouse.widget.js
1
2 /*
3 This widget does the same a the mouse widget in jqueryui.
4  */
5 var $, MouseWidget, SimpleWidget,
6   extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
7   hasProp = {}.hasOwnProperty;
8
9 SimpleWidget = require('./simple.widget');
10
11 $ = jQuery;
12
13 MouseWidget = (function(superClass) {
14   extend(MouseWidget, superClass);
15
16   function MouseWidget() {
17     return MouseWidget.__super__.constructor.apply(this, arguments);
18   }
19
20   MouseWidget.is_mouse_handled = false;
21
22   MouseWidget.prototype._init = function() {
23     this.$el.bind('mousedown.mousewidget', $.proxy(this._mouseDown, this));
24     this.$el.bind('touchstart.mousewidget', $.proxy(this._touchStart, this));
25     this.is_mouse_started = false;
26     this.mouse_delay = 0;
27     this._mouse_delay_timer = null;
28     this._is_mouse_delay_met = true;
29     return this.mouse_down_info = null;
30   };
31
32   MouseWidget.prototype._deinit = function() {
33     var $document;
34     this.$el.unbind('mousedown.mousewidget');
35     this.$el.unbind('touchstart.mousewidget');
36     $document = $(document);
37     $document.unbind('mousemove.mousewidget');
38     return $document.unbind('mouseup.mousewidget');
39   };
40
41   MouseWidget.prototype._mouseDown = function(e) {
42     var result;
43     if (e.which !== 1) {
44       return;
45     }
46     result = this._handleMouseDown(e, this._getPositionInfo(e));
47     if (result) {
48       e.preventDefault();
49     }
50     return result;
51   };
52
53   MouseWidget.prototype._handleMouseDown = function(e, position_info) {
54     if (MouseWidget.is_mouse_handled) {
55       return;
56     }
57     if (this.is_mouse_started) {
58       this._handleMouseUp(position_info);
59     }
60     this.mouse_down_info = position_info;
61     if (!this._mouseCapture(position_info)) {
62       return;
63     }
64     this._handleStartMouse();
65     this.is_mouse_handled = true;
66     return true;
67   };
68
69   MouseWidget.prototype._handleStartMouse = function() {
70     var $document;
71     $document = $(document);
72     $document.bind('mousemove.mousewidget', $.proxy(this._mouseMove, this));
73     $document.bind('touchmove.mousewidget', $.proxy(this._touchMove, this));
74     $document.bind('mouseup.mousewidget', $.proxy(this._mouseUp, this));
75     $document.bind('touchend.mousewidget', $.proxy(this._touchEnd, this));
76     if (this.mouse_delay) {
77       return this._startMouseDelayTimer();
78     }
79   };
80
81   MouseWidget.prototype._startMouseDelayTimer = function() {
82     if (this._mouse_delay_timer) {
83       clearTimeout(this._mouse_delay_timer);
84     }
85     this._mouse_delay_timer = setTimeout((function(_this) {
86       return function() {
87         return _this._is_mouse_delay_met = true;
88       };
89     })(this), this.mouse_delay);
90     return this._is_mouse_delay_met = false;
91   };
92
93   MouseWidget.prototype._mouseMove = function(e) {
94     return this._handleMouseMove(e, this._getPositionInfo(e));
95   };
96
97   MouseWidget.prototype._handleMouseMove = function(e, position_info) {
98     if (this.is_mouse_started) {
99       this._mouseDrag(position_info);
100       return e.preventDefault();
101     }
102     if (this.mouse_delay && !this._is_mouse_delay_met) {
103       return true;
104     }
105     this.is_mouse_started = this._mouseStart(this.mouse_down_info) !== false;
106     if (this.is_mouse_started) {
107       this._mouseDrag(position_info);
108     } else {
109       this._handleMouseUp(position_info);
110     }
111     return !this.is_mouse_started;
112   };
113
114   MouseWidget.prototype._getPositionInfo = function(e) {
115     return {
116       page_x: e.pageX,
117       page_y: e.pageY,
118       target: e.target,
119       original_event: e
120     };
121   };
122
123   MouseWidget.prototype._mouseUp = function(e) {
124     return this._handleMouseUp(this._getPositionInfo(e));
125   };
126
127   MouseWidget.prototype._handleMouseUp = function(position_info) {
128     var $document;
129     $document = $(document);
130     $document.unbind('mousemove.mousewidget');
131     $document.unbind('touchmove.mousewidget');
132     $document.unbind('mouseup.mousewidget');
133     $document.unbind('touchend.mousewidget');
134     if (this.is_mouse_started) {
135       this.is_mouse_started = false;
136       this._mouseStop(position_info);
137     }
138   };
139
140   MouseWidget.prototype._mouseCapture = function(position_info) {
141     return true;
142   };
143
144   MouseWidget.prototype._mouseStart = function(position_info) {
145     return null;
146   };
147
148   MouseWidget.prototype._mouseDrag = function(position_info) {
149     return null;
150   };
151
152   MouseWidget.prototype._mouseStop = function(position_info) {
153     return null;
154   };
155
156   MouseWidget.prototype.setMouseDelay = function(mouse_delay) {
157     return this.mouse_delay = mouse_delay;
158   };
159
160   MouseWidget.prototype._touchStart = function(e) {
161     var touch;
162     if (e.originalEvent.touches.length > 1) {
163       return;
164     }
165     touch = e.originalEvent.changedTouches[0];
166     return this._handleMouseDown(e, this._getPositionInfo(touch));
167   };
168
169   MouseWidget.prototype._touchMove = function(e) {
170     var touch;
171     if (e.originalEvent.touches.length > 1) {
172       return;
173     }
174     touch = e.originalEvent.changedTouches[0];
175     return this._handleMouseMove(e, this._getPositionInfo(touch));
176   };
177
178   MouseWidget.prototype._touchEnd = function(e) {
179     var touch;
180     if (e.originalEvent.touches.length > 1) {
181       return;
182     }
183     touch = e.originalEvent.changedTouches[0];
184     return this._handleMouseUp(this._getPositionInfo(touch));
185   };
186
187   return MouseWidget;
188
189 })(SimpleWidget);
190
191 module.exports = MouseWidget;