1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 
 25 /**
 26  * 
 27  * @private
 28  */
 29 DwtDragTracker = function() {}
 30 
 31 /**
 32  * Initializes the tracker.
 33  * 
 34  * @param {DwtControl}	control        the control that can be moved/dragged
 35  * @param {number}	[threshX=1]        the minimum number of X pixels before we move
 36  * @param {number}	[threshY=1]        the minimum number of Y pixels before we move
 37  * @param {function}	callbackFunc   callback function
 38  * @param callbackObj    object for callback
 39  * @param	userData		the user data
 40  * 
 41  */
 42 DwtDragTracker.init = 
 43 function(control, style, threshX, threshY, callbackFunc, callbackObj, userData) {
 44 
 45     var ctxt = control._dragTrackerContext = {};
 46     var htmlElement = control.getHtmlElement();
 47     
 48     if (style) htmlElement.style.cursor = style;
 49     
 50    	ctxt.style = style;
 51 	ctxt.threshX = (threshX > 0) ? threshX : 1;
 52 	ctxt.threshY = (threshY > 0) ? threshY : 1;
 53 	ctxt.data = { delta: {}, userData: userData};
 54 
 55 	ctxt.captureObj = new DwtMouseEventCapture({
 56 		targetObj:control,
 57 		mouseOverHdlr:DwtDragTracker._mouseOverHdlr,
 58 		mouseDownHdlr:DwtDragTracker._mouseDownHdlr,
 59 		mouseMoveHdlr:DwtDragTracker._mouseMoveHdlr,
 60 		mouseUpHdlr:DwtDragTracker._mouseUpHdlr,
 61 		mouseOutHdlr:DwtDragTracker._mouseOutHdlr
 62 	});
 63 
 64 	control.setHandler(DwtEvent.ONMOUSEDOWN, DwtDragTracker._mouseDownHdlr);
 65 	control.setHandler(DwtEvent.ONMOUSEOVER, DwtDragTracker._mouseOverHdlr);
 66 	control.setHandler(DwtEvent.ONMOUSEOUT, DwtDragTracker._mouseOutHdlr);
 67 	ctxt.callbackFunc = callbackFunc;
 68 	ctxt.callbackObj = callbackObj;	
 69 }
 70 
 71 DwtDragTracker.STYLE_NONE = "auto";
 72 DwtDragTracker.STYLE_MOVE = "move";
 73 DwtDragTracker.STYLE_RESIZE_NORTHWEST = "nw-resize";
 74 DwtDragTracker.STYLE_RESIZE_NORTH = "n-resize";
 75 DwtDragTracker.STYLE_RESIZE_NORTHEAST = "ne-resize";
 76 DwtDragTracker.STYLE_RESIZE_WEST = "w-resize";
 77 DwtDragTracker.STYLE_RESIZE_EAST = "e-resize";
 78 DwtDragTracker.STYLE_RESIZE_SOUTHWEST = "sw-resize";
 79 DwtDragTracker.STYLE_RESIZE_SOUTH = "s-resize";
 80 DwtDragTracker.STYLE_RESIZE_SOUTHEAST = "se-resize";
 81 
 82 DwtDragTracker.STATE_START = 1;
 83 DwtDragTracker.STATE_DRAGGING = 2;
 84 DwtDragTracker.STATE_END = 3;
 85 
 86 DwtDragTracker._mouseOverHdlr =
 87 function(ev) {
 88 	var mouseEv = DwtShell.mouseEvent;
 89 	mouseEv.setFromDhtmlEvent(ev);
 90 	mouseEv._stopPropagation = true;
 91 	mouseEv._returnValue = false;
 92 	mouseEv.setToDhtmlEvent(ev);
 93 	return false;	
 94 }
 95 
 96 DwtDragTracker._mouseDownHdlr =
 97 function(ev) {
 98 	var mouseEv = DwtShell.mouseEvent;
 99 	mouseEv.setFromDhtmlEvent(ev, true);	
100 	if (mouseEv.button != DwtMouseEvent.LEFT) {
101 		DwtUiEvent.setBehaviour(ev, true, false);
102 		return false;
103 	}
104 	var control = mouseEv.dwtObj;
105 	if (control && control._dragTrackerContext) {
106         var ctxt = control._dragTrackerContext;
107         	if (ctxt.callbackFunc != null) {
108 				ctxt.oldCapture = DwtMouseEventCapture.getCaptureObj();
109 				if (ctxt.oldCapture) {
110 					ctxt.oldCapture.release();
111 				}
112         		ctxt.captureObj.capture();
113         		ctxt.data.startDoc = {x: mouseEv.docX, y: mouseEv.docY};
114         		ctxt.data.state = DwtDragTracker.STATE_START;
115              DwtDragTracker._doCallback(ctxt, mouseEv);
116         	}
117    	}
118 	mouseEv._stopPropagation = true;
119 	mouseEv._returnValue = false;
120 	mouseEv.setToDhtmlEvent(ev);
121 	return false;	
122 }
123 
124 DwtDragTracker._doCallback =
125 function(ctxt, mouseEv) {
126 	ctxt.data.mouseEv = mouseEv;
127 	if (ctxt.callbackObj != null)
128 		ctxt.callbackFunc.call(ctxt.callbackObj, ctxt.data);
129 	else 
130 		ctxt.callbackFunc(ctxt.data);
131 	ctxt.data.mouseEv = null;
132 }
133 
134 DwtDragTracker._mouseMoveHdlr =
135 function(ev) {
136 	var mouseEv = DwtShell.mouseEvent;
137 	mouseEv.setFromDhtmlEvent(ev);	
138 	
139 	var control = DwtMouseEventCapture.getTargetObj();
140     var ctxt = control._dragTrackerContext;
141     var data = ctxt.data;
142 	    
143 	data.delta.x = mouseEv.docX - data.startDoc.x;
144 	data.delta.y = mouseEv.docY - data.startDoc.y;
145 	
146 	if (Math.abs(data.delta.x) >= ctxt.threshX || Math.abs(data.delta.y) >= ctxt.threshY) {
147         data.prevState = data.state;
148         data.state = DwtDragTracker.STATE_DRAGGING;
149 	    DwtDragTracker._doCallback(ctxt, mouseEv);
150 	}
151 	mouseEv._stopPropagation = true;
152 	mouseEv._returnValue = false;
153 	mouseEv.setToDhtmlEvent(ev);
154 	return false;	
155 }
156 
157 DwtDragTracker._mouseUpHdlr =
158 function(ev) {
159 	var mouseEv = DwtShell.mouseEvent;
160 	mouseEv.setFromDhtmlEvent(ev);	
161 	if (mouseEv.button != DwtMouseEvent.LEFT) {
162 		DwtUiEvent.setBehaviour(ev, true, false);
163 		return false;
164 	}
165 	
166 	var ctxt = DwtMouseEventCapture.getTargetObj()._dragTrackerContext;
167 	if (ctxt) {
168         	if (ctxt.callbackFunc != null)
169         		DwtMouseEventCapture.getCaptureObj().release();
170 			if (ctxt.oldCapture) {
171 				ctxt.oldCapture.capture();
172 				ctxt.oldCapture = null;
173 			}
174         	ctxt.data.state = DwtDragTracker.STATE_END;
175         DwtDragTracker._doCallback(ctxt, mouseEv);
176 	}
177 	mouseEv._stopPropagation = true;
178 	mouseEv._returnValue = false;
179 	mouseEv.setToDhtmlEvent(ev);
180 	return false;	
181 }
182 
183 DwtDragTracker._mouseOutHdlr =
184 function(ev) {
185 	var mouseEv = DwtShell.mouseEvent;
186 	
187 	mouseEv.setFromDhtmlEvent(ev);
188 	mouseEv._stopPropagation = true;
189 	mouseEv._returnValue = false;
190 	mouseEv.setToDhtmlEvent(ev);
191 	return false;	
192 }
193