7604 |
25 Feb 19 |
nicklas |
/* $Id$ |
7604 |
25 Feb 19 |
nicklas |
2 |
------------------------------------------------------------------ |
7604 |
25 Feb 19 |
nicklas |
Copyright (C) 2011 Nicklas Nordborg |
7604 |
25 Feb 19 |
nicklas |
4 |
|
7604 |
25 Feb 19 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
7604 |
25 Feb 19 |
nicklas |
Available at http://base.thep.lu.se/ |
7604 |
25 Feb 19 |
nicklas |
7 |
|
7604 |
25 Feb 19 |
nicklas |
BASE is free software; you can redistribute it and/or |
7604 |
25 Feb 19 |
nicklas |
modify it under the terms of the GNU General Public License |
7604 |
25 Feb 19 |
nicklas |
as published by the Free Software Foundation; either version 3 |
7604 |
25 Feb 19 |
nicklas |
of the License, or (at your option) any later version. |
7604 |
25 Feb 19 |
nicklas |
12 |
|
7604 |
25 Feb 19 |
nicklas |
BASE is distributed in the hope that it will be useful, |
7604 |
25 Feb 19 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
7604 |
25 Feb 19 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7604 |
25 Feb 19 |
nicklas |
GNU General Public License for more details. |
7604 |
25 Feb 19 |
nicklas |
17 |
|
7604 |
25 Feb 19 |
nicklas |
You should have received a copy of the GNU General Public License |
7604 |
25 Feb 19 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
7604 |
25 Feb 19 |
nicklas |
20 |
------------------------------------------------------------------ |
7604 |
25 Feb 19 |
nicklas |
21 |
|
7604 |
25 Feb 19 |
nicklas |
@author Nicklas |
7604 |
25 Feb 19 |
nicklas |
@version 2.0 |
7604 |
25 Feb 19 |
nicklas |
24 |
*/ |
7604 |
25 Feb 19 |
nicklas |
'use strict'; |
7604 |
25 Feb 19 |
nicklas |
26 |
|
7604 |
25 Feb 19 |
nicklas |
var DragDrop = function() |
7604 |
25 Feb 19 |
nicklas |
28 |
{ |
7604 |
25 Feb 19 |
nicklas |
var dragdrop = {}; |
7604 |
25 Feb 19 |
nicklas |
var internal = {}; |
7604 |
25 Feb 19 |
nicklas |
31 |
|
7604 |
25 Feb 19 |
nicklas |
var dragElement = null; |
7604 |
25 Feb 19 |
nicklas |
var startX = 0; |
7604 |
25 Feb 19 |
nicklas |
var startY = 0; |
7604 |
25 Feb 19 |
nicklas |
35 |
|
7604 |
25 Feb 19 |
nicklas |
36 |
/** |
7604 |
25 Feb 19 |
nicklas |
Enable dragging on the given element. Dragging can be |
7604 |
25 Feb 19 |
nicklas |
enabled independently in the horizontal or vertical direction. |
7604 |
25 Feb 19 |
nicklas |
The callback function should be a function that takes two parameters; |
7604 |
25 Feb 19 |
nicklas |
a delta-X and delta-Y which is the number of pixels the mouse has |
7604 |
25 Feb 19 |
nicklas |
moved since the last call to the callback or since the drag started. |
7604 |
25 Feb 19 |
nicklas |
42 |
*/ |
7604 |
25 Feb 19 |
nicklas |
dragdrop.enableDrag = function(element, horizontal, vertical, callback) |
7604 |
25 Feb 19 |
nicklas |
44 |
{ |
7604 |
25 Feb 19 |
nicklas |
element = Doc.element(element); |
7604 |
25 Feb 19 |
nicklas |
element.dragHorizontal = horizontal; |
7604 |
25 Feb 19 |
nicklas |
element.dragVertical = vertical; |
7604 |
25 Feb 19 |
nicklas |
element.dragCallback = callback; |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler(element, 'mousedown', dragdrop.beginDrag); |
7604 |
25 Feb 19 |
nicklas |
50 |
} |
7604 |
25 Feb 19 |
nicklas |
51 |
|
7604 |
25 Feb 19 |
nicklas |
52 |
/** |
7604 |
25 Feb 19 |
nicklas |
Start dragging. The 'event' parameter is used to get |
7604 |
25 Feb 19 |
nicklas |
the mouse position. The callback function is called |
7604 |
25 Feb 19 |
nicklas |
when the mouse is moved to handle the drag visually. |
7604 |
25 Feb 19 |
nicklas |
56 |
*/ |
7604 |
25 Feb 19 |
nicklas |
dragdrop.beginDrag = function(event) |
7604 |
25 Feb 19 |
nicklas |
58 |
{ |
7604 |
25 Feb 19 |
nicklas |
startX = event.screenX; |
7604 |
25 Feb 19 |
nicklas |
startY = event.screenY; |
7604 |
25 Feb 19 |
nicklas |
dragElement = event.currentTarget; |
7604 |
25 Feb 19 |
nicklas |
62 |
} |
7604 |
25 Feb 19 |
nicklas |
63 |
|
7604 |
25 Feb 19 |
nicklas |
64 |
/** |
7604 |
25 Feb 19 |
nicklas |
Recieve mouse move events. Calculate the delta X and Y since the |
7604 |
25 Feb 19 |
nicklas |
last position and call the callback function. |
7604 |
25 Feb 19 |
nicklas |
67 |
*/ |
7604 |
25 Feb 19 |
nicklas |
dragdrop.drag = function(event) |
7604 |
25 Feb 19 |
nicklas |
69 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (!dragElement) return; |
7604 |
25 Feb 19 |
nicklas |
var deltaX = event.screenX - startX; |
7604 |
25 Feb 19 |
nicklas |
var deltaY = -(event.screenY - startY); |
7604 |
25 Feb 19 |
nicklas |
73 |
|
7604 |
25 Feb 19 |
nicklas |
if ((dragElement.dragHorizontal && Math.abs(deltaX) > 0) || (dragElement.dragVertical && Math.abs(deltaY) > 0)) |
7604 |
25 Feb 19 |
nicklas |
75 |
{ |
7604 |
25 Feb 19 |
nicklas |
dragElement.dragCallback.call(dragElement, deltaX, deltaY); |
7604 |
25 Feb 19 |
nicklas |
startX = event.screenX; |
7604 |
25 Feb 19 |
nicklas |
startY = event.screenY; |
7604 |
25 Feb 19 |
nicklas |
79 |
} |
7604 |
25 Feb 19 |
nicklas |
80 |
} |
7604 |
25 Feb 19 |
nicklas |
81 |
|
7604 |
25 Feb 19 |
nicklas |
82 |
/** |
7604 |
25 Feb 19 |
nicklas |
Stop dragging. |
7604 |
25 Feb 19 |
nicklas |
84 |
*/ |
7604 |
25 Feb 19 |
nicklas |
dragdrop.endDrag = function(event) |
7604 |
25 Feb 19 |
nicklas |
86 |
{ |
7604 |
25 Feb 19 |
nicklas |
dragElement = null; |
7604 |
25 Feb 19 |
nicklas |
88 |
} |
7604 |
25 Feb 19 |
nicklas |
89 |
|
7604 |
25 Feb 19 |
nicklas |
internal.initDragSupport = function(element, autoInit) |
7604 |
25 Feb 19 |
nicklas |
91 |
{ |
7604 |
25 Feb 19 |
nicklas |
if (autoInit != 'drag-support') return; |
7604 |
25 Feb 19 |
nicklas |
var dd = parent.DragDrop ? parent.DragDrop : dragdrop; |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler(element, 'mousemove', dd.drag); |
7604 |
25 Feb 19 |
nicklas |
Events.addEventHandler(element, 'mouseup', dd.endDrag); |
7604 |
25 Feb 19 |
nicklas |
96 |
} |
7604 |
25 Feb 19 |
nicklas |
Doc.addElementInitializer(internal.initDragSupport); |
7604 |
25 Feb 19 |
nicklas |
98 |
|
7604 |
25 Feb 19 |
nicklas |
return dragdrop; |
7604 |
25 Feb 19 |
nicklas |
100 |
}(); |
7604 |
25 Feb 19 |
nicklas |
101 |
|