1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 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) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 
 25 /**
 26  * Creates a helper class for mouse event capturing.
 27  * @constructor
 28  * @class
 29  *
 30  * @author Ross Dargahi
 31  *
 32  * @param {hash}		params					a hash of parameters:
 33  * @param {Element}		params.targetObj		the target element
 34  * @param {string}		params.id				the ID for this capture instance.
 35  * @param {function}	params.mouseOverHdlr	the browser event handler
 36  * @param {function}	params.mouseDownHdlr	the browser event handler
 37  * @param {function}	params.mouseMoveHdlr	the browser event handler
 38  * @param {function}	params.mouseUpHdlr		the browser event handler
 39  * @param {function}	params.mouseOutHdlr		the browser event handler
 40  * @param {function}	params.mouseWheelHdlr	the browser event handler
 41  * @param {boolean}		params.hardCapture		if <code>true</code>, event propagation is halted at this element (IE only)
 42  *
 43  * @private
 44  */
 45 DwtMouseEventCapture = function(params) {
 46 
 47 	params = Dwt.getParams(arguments, DwtMouseEventCapture.PARAMS);
 48 
 49 	this.targetObj = params.targetObj;
 50 	this._id = params.id;
 51 	this._mouseOverHdlr = params.mouseOverHdlr || DwtMouseEventCapture.emptyHdlr;
 52 	this._mouseDownHdlr = params.mouseDownHdlr || DwtMouseEventCapture.emptyHdlr;
 53 	this._mouseMoveHdlr = params.mouseMoveHdlr || DwtMouseEventCapture.emptyHdlr;
 54 	this._mouseUpHdlr = params.mouseUpHdlr || DwtMouseEventCapture.emptyHdlr;
 55 	this._mouseOutHdlr = params.mouseOutHdlr || DwtMouseEventCapture.emptyHdlr;
 56 	this._mouseWheelHdlr = params.mouseWheelHdlr || DwtMouseEventCapture.emptyHdlr;
 57 	this._hardCapture = (params.hardCapture !== false)
 58 
 59 	this._supportsCapture = (document.body && document.body.setCapture &&
 60 	                         AjxEnv.isIE && !AjxEnv.isIE9up);
 61 }
 62 
 63 DwtMouseEventCapture.PARAMS = ["targetObj", "id", "mouseOverHdlr", "mouseDownHdlr", "mouseMoveHdlr",
 64 							   "mouseUpHdlr", "mouseOutHdlr", "mouseWheelHdlr", "hardCapture"];
 65 
 66 DwtMouseEventCapture._capturing = false;
 67 
 68 DwtMouseEventCapture.getCaptureObj =
 69 function() {
 70 	return window._mouseEventCaptureObj;
 71 }
 72 
 73 DwtMouseEventCapture.getTargetObj =
 74 function() {
 75 	return window._mouseEventCaptureObj ? window._mouseEventCaptureObj.targetObj : null;
 76 }
 77 
 78 DwtMouseEventCapture.getId =
 79 function() {
 80 	return window._mouseEventCaptureObj ? window._mouseEventCaptureObj._id : null;
 81 }
 82 
 83 DwtMouseEventCapture.prototype.toString = 
 84 function() {
 85 	return "DwtMouseEventCapture";
 86 }
 87 
 88 DwtMouseEventCapture.prototype.capturing =
 89 function() {
 90 	return DwtMouseEventCapture._capturing;
 91 }
 92 
 93 DwtMouseEventCapture.prototype.capture =
 94 function() {
 95 
 96 	if (window._mouseEventCaptureObj) {
 97 		window._mouseEventCaptureObj.release();
 98 	}
 99 
100 	if (document.body != null && document.body.addEventListener != null) {
101 		document.body.addEventListener("mouseover", this._mouseOverHdlr, true);
102 		document.body.addEventListener("mousedown", this._mouseDownHdlr, true);
103 		document.body.addEventListener("mousemove", this._mouseMoveHdlr, true);
104 		document.body.addEventListener("mouseup", this._mouseUpHdlr, true);
105 		document.body.addEventListener("mouseout", this._mouseOutHdlr, true);
106 		document.body.addEventListener("DOMMouseScroll", this._mouseWheelHdlr, true);
107 	} else {
108 		this._savedMouseOverHdlr = document.onmouseover;
109 		this._savedMouseDownHdlr = document.onmousedown;
110 		this._savedMouseMoveHdlr = document.onmousemove;
111 		this._savedMouseUpHdlr = document.onmouseup;
112 		this._savedMouseOutHdlr = document.onmouseout;
113 		this._savedMouseWheelHdlr = document.onmousewheel;
114 		document.onmouseover = this._mouseOverHdlr;
115 		document.onmousedown = this._mouseDownHdlr;
116 		document.onmousemove = this._mouseMoveHdlr;
117 		document.onmouseup = this._mouseUpHdlr;
118 		document.onmouseout = this._mouseOutHdlr;
119 		document.onmousewheel = this._mouseWheelHdlr;
120 	}
121 	if (this._hardCapture && this._supportsCapture) {
122 		document.body.setCapture(true);
123 	}
124 	window._mouseEventCaptureObj = this;
125 	DwtMouseEventCapture._capturing = true;
126 }
127 
128 
129 DwtMouseEventCapture.prototype.release = 
130 function() {
131 
132 	if (window._mouseEventCaptureObj == null) { return; }
133 
134 	var obj = window._shellCaptureObj;
135 	if (document.body && document.body.addEventListener) {
136 		document.body.removeEventListener("mouseover", this._mouseOverHdlr, true);
137 		document.body.removeEventListener("mousedown", this._mouseDownHdlr, true);
138 		document.body.removeEventListener("mousemove", this._mouseMoveHdlr, true);
139 		document.body.removeEventListener("mouseup", this._mouseUpHdlr, true);
140 		document.body.removeEventListener("mouseout", this._mouseOutHdlr, true);
141 		document.body.removeEventListener("DOMMouseScroll", this._mouseWheelHdlr, true);
142 	} else {
143 		document.onmouseover = this._savedMouseOverHdlr
144 		document.onmousedown = this._savedMouseDownHdlr;
145 		document.onmousemove = this._savedMouseMoveHdlr;
146 		document.onmouseup = this._savedMouseUpHdlr;
147 		document.onmouseout = this._savedMouseOutHdlr;
148 		document.onmousewheel = this._savedMouseWheelHdlr;
149 	}
150 	if (this._hardCapture && this._supportsCapture) {
151 		document.body.releaseCapture();
152 	}
153 	window._mouseEventCaptureObj = null;
154 	DwtMouseEventCapture._capturing = false;
155 }
156 
157 DwtMouseEventCapture.emptyHdlr =
158 function(ev) {
159 	var capObj = DwtMouseEventCapture.getCaptureObj();
160 	var mouseEv = DwtShell.mouseEvent;
161 	mouseEv.setFromDhtmlEvent(ev);	
162 	if (capObj._hardCapture) {
163 		mouseEv._stopPropagation = true;
164 		mouseEv._returnValue = false;
165 		mouseEv.setToDhtmlEvent(ev);
166 		return false;	
167 	} else {
168 		mouseEv._stopPropagation = false;
169 		mouseEv._returnValue = true;
170 		mouseEv.setToDhtmlEvent(ev);
171 		return true;
172 	}	
173 }
174