1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2011, 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) 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @constructor
 26  * @class
 27  * A drag box is registered with a control to indicate that the control supports the
 28  * presence of an elastic box created by clicking and dragging, and typically used to
 29  * select visual objects within a space.
 30  * <p>
 31  * Application developers instantiate {@link DwtDragBox} and register it with the control
 32  * which is to be draggable (via {@link DwtControl.setDragBox}). The
 33  * application should then register a listener with the {@link DwtDragBox}. This way
 34  * when drag events occur the application will be notified and may act on them 
 35  * accordingly.
 36  * </p>
 37  * 
 38  * @author Conrad Damon
 39  * 
 40  * @see DwtDragEvent
 41  * @see DwtControl
 42  * @see DwtControl#setDragBox
 43  */
 44 DwtDragBox = function() {
 45 	this.__evtMgr = new AjxEventMgr();
 46 };
 47 
 48 /** @private */
 49 DwtDragBox.__DRAG_LISTENER = "DwtDragBox.__DRAG_LISTENER";
 50 
 51 /** @private */
 52 DwtDragBox.__dragEvent = new DwtDragEvent();
 53 
 54 /**
 55  * Returns a string representation of this object.
 56  * 
 57  * @return {string}	a string representation of this object
 58  */
 59 DwtDragBox.prototype.toString = 
 60 function() {
 61 	return "DwtDragBox";
 62 };
 63 
 64 
 65 /**
 66  * Registers a listener for <i>DwtDragEvent</i> events.
 67  *
 68  * @param {AjxListener} dragBoxListener Listener to be registered
 69  * 
 70  * @see DwtDragEvent
 71  * @see AjxListener
 72  * @see #removeDragListener
 73  */
 74 DwtDragBox.prototype.addDragListener =
 75 function(dragBoxListener) {
 76 	this.__evtMgr.addListener(DwtDragBox.__DRAG_LISTENER, dragBoxListener);
 77 };
 78 
 79 /**
 80  * Removes a registered event listener.
 81  * 
 82  * @param {AjxListener} dragBoxListener Listener to be removed
 83  * 
 84  * @see AjxListener
 85  * @see #addDragListener
 86  */
 87 DwtDragBox.prototype.removeDragListener =
 88 function(dragBoxListener) {
 89 	this.__evtMgr.removeListener(DwtDragBox.__DRAG_LISTENER, dragBoxListener);
 90 };
 91 
 92 // The following methods are called by DwtControl during the drag lifecycle 
 93 
 94 DwtDragBox.prototype._setStart =
 95 function(mouseEv, srcControl) {
 96 
 97 	this._startX = mouseEv.docX;
 98 	this._startY = mouseEv.docY;
 99 	this._dragObj = DwtDragBox.__dragEvent.srcControl = srcControl;
100 	DwtDragBox.__dragEvent.action = DwtDragEvent.DRAG_INIT;
101 	DwtDragBox.__dragEvent.target = mouseEv.target;
102 	return (this.__evtMgr.notifyListeners(DwtDragBox.__DRAG_LISTENER, DwtDragBox.__dragEvent) !== false);
103 };
104 
105 DwtDragBox.prototype._beginDrag =
106 function(srcControl) {
107 
108 	srcControl._dragging = DwtControl._DRAGGING;
109 	DwtDragBox.__dragEvent.srcControl = srcControl;
110 	DwtDragBox.__dragEvent.action = DwtDragEvent.DRAG_START;
111 	this.__evtMgr.notifyListeners(DwtDragBox.__DRAG_LISTENER, DwtDragBox.__dragEvent);
112 };
113 
114 DwtDragBox.prototype._dragMove =
115 function(mouseEv, srcControl) {
116 
117 	var deltaX = mouseEv.docX - this._startX;
118 	var deltaY = mouseEv.docY - this._startY;
119 	var locX = (deltaX > 0) ? this._startX : mouseEv.docX;
120 	var locY = (deltaY > 0) ? this._startY : mouseEv.docY;
121 
122 	var box = srcControl.getDragSelectionBox();
123 	Dwt.setLocation(box, locX, locY);
124 	Dwt.setSize(box, Math.abs(deltaX), Math.abs(deltaY));
125 
126 	DwtDragBox.__dragEvent.srcControl = srcControl;
127 	DwtDragBox.__dragEvent.action = DwtDragEvent.DRAG_MOVE;
128 	this.__evtMgr.notifyListeners(DwtDragBox.__DRAG_LISTENER, DwtDragBox.__dragEvent);
129 };
130 
131 DwtDragBox.prototype._endDrag =
132 function(srcControl) {
133 
134 	srcControl._dragging = DwtControl._NO_DRAG;
135 	DwtDragBox.__dragEvent.action = DwtDragEvent.DRAG_END;
136 	if (!this.__evtMgr.notifyListeners(DwtDragBox.__DRAG_LISTENER, DwtDragBox.__dragEvent)) {
137 		srcControl.destroyDragSelectionBox();
138 	}
139 	this._dragObj = null;
140 };
141 
142 /*
143  *  return starting X position
144  *  @return {int} starting X position
145  */
146 DwtDragBox.prototype.getStartX =
147 function() {
148     return this._startX;
149 };
150 
151 /*  return starting Y position
152  *  @return {int} starting Y position
153  */
154 DwtDragBox.prototype.getStartY =
155 function() {
156     return this._startY;
157 };