2010-06-16 17:19:11 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is drag.js.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2010-08-11 15:28:45 -07:00
|
|
|
* the Mozilla Foundation.
|
2010-06-16 17:19:11 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2010-08-11 15:28:45 -07:00
|
|
|
* Michael Yoshitaka Erlewine <mitcho@mitcho.com>
|
2010-06-16 17:19:11 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
// **********
|
2010-07-18 08:58:10 -07:00
|
|
|
// Title: drag.js
|
2010-06-16 17:19:11 -07:00
|
|
|
|
2010-06-15 12:26:35 -07:00
|
|
|
// ----------
|
|
|
|
// Variable: drag
|
2010-07-18 08:58:10 -07:00
|
|
|
// The Drag that's currently in process.
|
2010-06-15 12:26:35 -07:00
|
|
|
var drag = {
|
|
|
|
info: null,
|
2010-09-09 22:29:00 -07:00
|
|
|
zIndex: 100,
|
|
|
|
lastMoveTime: 0
|
2010-06-15 12:26:35 -07:00
|
|
|
};
|
|
|
|
|
2010-09-09 22:29:00 -07:00
|
|
|
//----------
|
|
|
|
//Variable: resize
|
|
|
|
//The resize (actually a Drag) that is currently in process
|
|
|
|
var resize = {
|
|
|
|
info: null,
|
|
|
|
lastMoveTime: 0
|
|
|
|
};
|
2010-06-15 12:26:35 -07:00
|
|
|
|
|
|
|
// ##########
|
|
|
|
// Class: Drag (formerly DragInfo)
|
|
|
|
// Helper class for dragging <Item>s
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-15 12:26:35 -07:00
|
|
|
// ----------
|
|
|
|
// Constructor: Drag
|
2010-06-22 16:42:06 -07:00
|
|
|
// Called to create a Drag in response to an <Item> draggable "start" event.
|
|
|
|
// Note that it is also used partially during <Item>'s resizable method as well.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
|
|
|
// Parameters:
|
2010-06-22 16:42:06 -07:00
|
|
|
// item - The <Item> being dragged
|
|
|
|
// event - The DOM event that kicks off the drag
|
2011-07-04 15:23:35 -07:00
|
|
|
function Drag(item, event) {
|
2010-08-11 20:08:33 -07:00
|
|
|
Utils.assert(item && (item.isAnItem || item.isAFauxItem),
|
|
|
|
'must be an item, or at least a faux item');
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-08-11 20:08:33 -07:00
|
|
|
this.item = item;
|
|
|
|
this.el = item.container;
|
|
|
|
this.$el = iQ(this.el);
|
|
|
|
this.parent = this.item.parent;
|
|
|
|
this.startPosition = new Point(event.clientX, event.clientY);
|
|
|
|
this.startTime = Date.now();
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-08-11 20:08:33 -07:00
|
|
|
this.item.isDragging = true;
|
|
|
|
this.item.setZ(999999);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-08-11 20:08:33 -07:00
|
|
|
this.safeWindowBounds = Items.getSafeWindowBounds();
|
2010-06-22 20:35:38 -07:00
|
|
|
|
2010-08-11 20:08:33 -07:00
|
|
|
Trenches.activateOthersTrenches(this.el);
|
2010-06-15 12:26:35 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Drag.prototype = {
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Drag (item)] for debug use
|
|
|
|
toString: function Drag_toString() {
|
|
|
|
return "[Drag (" + this.item + ")]";
|
|
|
|
},
|
|
|
|
|
2010-07-03 13:55:54 -07:00
|
|
|
// ----------
|
2010-07-17 21:52:47 -07:00
|
|
|
// Function: snapBounds
|
|
|
|
// Adjusts the given bounds according to the currently active trenches. Used by <Drag.snap>
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
|
|
|
// Parameters:
|
2010-07-17 21:52:47 -07:00
|
|
|
// bounds - (<Rect>) bounds
|
2010-11-07 22:38:24 -08:00
|
|
|
// stationaryCorner - which corner is stationary? by default, the top left in LTR mode,
|
|
|
|
// and top right in RTL mode.
|
2010-07-03 13:55:54 -07:00
|
|
|
// "topleft", "bottomleft", "topright", "bottomright"
|
|
|
|
// assumeConstantSize - (boolean) whether the bounds' dimensions are sacred or not.
|
2010-07-18 08:58:10 -07:00
|
|
|
// keepProportional - (boolean) if assumeConstantSize is false, whether we should resize
|
2010-07-03 13:55:54 -07:00
|
|
|
// proportionally or not
|
2010-07-22 21:35:07 -07:00
|
|
|
// checkItemStatus - (boolean) make sure this is a valid item which should be snapped
|
2010-07-22 20:40:25 -07:00
|
|
|
snapBounds: function Drag_snapBounds(bounds, stationaryCorner, assumeConstantSize, keepProportional, checkItemStatus) {
|
2010-08-12 16:32:18 -07:00
|
|
|
if (!stationaryCorner)
|
2010-11-07 22:38:24 -08:00
|
|
|
stationaryCorner = UI.rtl ? 'topright' : 'topleft';
|
2010-06-19 13:08:14 -07:00
|
|
|
var update = false; // need to update
|
|
|
|
var updateX = false;
|
|
|
|
var updateY = false;
|
2010-06-19 13:18:30 -07:00
|
|
|
var newRect;
|
2010-06-22 21:19:10 -07:00
|
|
|
var snappedTrenches = {};
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// OH SNAP!
|
2010-08-11 20:08:33 -07:00
|
|
|
|
2010-09-21 10:22:07 -07:00
|
|
|
// if we aren't holding down the meta key or have trenches disabled...
|
|
|
|
if (!Keys.meta && !Trenches.disabled) {
|
2010-08-11 20:08:33 -07:00
|
|
|
// snappable = true if we aren't a tab on top of something else, and
|
|
|
|
// there's no active drop site...
|
|
|
|
let snappable = !(this.item.isATabItem &&
|
|
|
|
this.item.overlapsWithOtherItems()) &&
|
|
|
|
!iQ(".acceptsDrop").length;
|
|
|
|
if (!checkItemStatus || snappable) {
|
|
|
|
newRect = Trenches.snap(bounds, stationaryCorner, assumeConstantSize,
|
|
|
|
keepProportional);
|
|
|
|
if (newRect) { // might be false if no changes were made
|
|
|
|
update = true;
|
|
|
|
snappedTrenches = newRect.snappedTrenches || {};
|
|
|
|
bounds = newRect;
|
|
|
|
}
|
2010-06-19 17:45:23 -07:00
|
|
|
}
|
2010-06-19 13:18:30 -07:00
|
|
|
}
|
2010-06-19 13:08:14 -07:00
|
|
|
|
|
|
|
// make sure the bounds are in the window.
|
2010-08-11 20:08:33 -07:00
|
|
|
newRect = this.snapToEdge(bounds, stationaryCorner, assumeConstantSize,
|
|
|
|
keepProportional);
|
2010-06-19 17:45:23 -07:00
|
|
|
if (newRect) {
|
|
|
|
update = true;
|
|
|
|
bounds = newRect;
|
2010-08-11 20:08:33 -07:00
|
|
|
Utils.extend(snappedTrenches, newRect.snappedTrenches);
|
2010-06-22 21:19:10 -07:00
|
|
|
}
|
|
|
|
|
2010-06-30 08:32:17 -07:00
|
|
|
Trenches.hideGuides();
|
2010-07-15 01:41:08 -07:00
|
|
|
for (var edge in snappedTrenches) {
|
|
|
|
var trench = snappedTrenches[edge];
|
2010-06-22 21:19:10 -07:00
|
|
|
if (typeof trench == 'object') {
|
|
|
|
trench.showGuide = true;
|
|
|
|
trench.show();
|
|
|
|
}
|
2010-06-19 17:45:23 -07:00
|
|
|
}
|
2010-06-19 13:08:14 -07:00
|
|
|
|
2010-07-17 21:52:47 -07:00
|
|
|
return update ? bounds : false;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-17 21:52:47 -07:00
|
|
|
// ----------
|
|
|
|
// Function: snap
|
|
|
|
// Called when a drag or mousemove occurs. Set the bounds based on the mouse move first, then
|
|
|
|
// call snap and it will adjust the item's bounds if appropriate. Also triggers the display of
|
|
|
|
// trenches that it snapped to.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
|
|
|
// Parameters:
|
2010-11-07 22:38:24 -08:00
|
|
|
// stationaryCorner - which corner is stationary? by default, the top left in LTR mode,
|
|
|
|
// and top right in RTL mode.
|
2010-07-17 21:52:47 -07:00
|
|
|
// "topleft", "bottomleft", "topright", "bottomright"
|
|
|
|
// assumeConstantSize - (boolean) whether the bounds' dimensions are sacred or not.
|
2010-07-18 08:58:10 -07:00
|
|
|
// keepProportional - (boolean) if assumeConstantSize is false, whether we should resize
|
2010-07-17 21:52:47 -07:00
|
|
|
// proportionally or not
|
|
|
|
snap: function Drag_snap(stationaryCorner, assumeConstantSize, keepProportional) {
|
|
|
|
var bounds = this.item.getBounds();
|
2010-07-22 20:40:25 -07:00
|
|
|
bounds = this.snapBounds(bounds, stationaryCorner, assumeConstantSize, keepProportional, true);
|
2010-07-17 21:52:47 -07:00
|
|
|
if (bounds) {
|
2010-08-11 20:08:33 -07:00
|
|
|
this.item.setBounds(bounds, true);
|
2010-07-17 21:52:47 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-06-19 17:45:23 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 17:45:23 -07:00
|
|
|
// --------
|
|
|
|
// Function: snapToEdge
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns a version of the bounds snapped to the edge if it is close enough. If not,
|
|
|
|
// returns false. If <Keys.meta> is true, this function will simply enforce the
|
2010-06-19 17:45:23 -07:00
|
|
|
// window edges.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 17:45:23 -07:00
|
|
|
// Parameters:
|
|
|
|
// rect - (<Rect>) current bounds of the object
|
2010-11-07 22:38:24 -08:00
|
|
|
// stationaryCorner - which corner is stationary? by default, the top left in LTR mode,
|
|
|
|
// and top right in RTL mode.
|
2010-07-03 13:55:54 -07:00
|
|
|
// "topleft", "bottomleft", "topright", "bottomright"
|
2010-06-19 17:45:23 -07:00
|
|
|
// assumeConstantSize - (boolean) whether the rect's dimensions are sacred or not
|
2010-07-03 13:55:54 -07:00
|
|
|
// keepProportional - (boolean) if we are allowed to change the rect's size, whether the
|
|
|
|
// dimensions should scaled proportionally or not.
|
|
|
|
snapToEdge: function Drag_snapToEdge(rect, stationaryCorner, assumeConstantSize, keepProportional) {
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 13:08:14 -07:00
|
|
|
var swb = this.safeWindowBounds;
|
2010-06-19 17:45:23 -07:00
|
|
|
var update = false;
|
|
|
|
var updateX = false;
|
|
|
|
var updateY = false;
|
2010-06-22 21:19:10 -07:00
|
|
|
var snappedTrenches = {};
|
2010-06-19 13:08:14 -07:00
|
|
|
|
2010-07-30 02:54:30 -07:00
|
|
|
var snapRadius = (Keys.meta ? 0 : Trenches.defaultRadius);
|
2010-06-19 17:45:23 -07:00
|
|
|
if (rect.left < swb.left + snapRadius ) {
|
2010-11-11 18:11:28 -08:00
|
|
|
if (stationaryCorner.indexOf('right') > -1 && !assumeConstantSize)
|
2010-07-03 13:55:54 -07:00
|
|
|
rect.width = rect.right - swb.left;
|
2010-06-19 17:45:23 -07:00
|
|
|
rect.left = swb.left;
|
2010-06-19 13:08:14 -07:00
|
|
|
update = true;
|
|
|
|
updateX = true;
|
2010-06-22 21:19:10 -07:00
|
|
|
snappedTrenches.left = 'edge';
|
2010-06-19 13:08:14 -07:00
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 17:45:23 -07:00
|
|
|
if (rect.right > swb.right - snapRadius) {
|
|
|
|
if (updateX || !assumeConstantSize) {
|
|
|
|
var newWidth = swb.right - rect.left;
|
|
|
|
if (keepProportional)
|
|
|
|
rect.height = rect.height * newWidth / rect.width;
|
|
|
|
rect.width = newWidth;
|
|
|
|
update = true;
|
|
|
|
} else if (!updateX || !Trenches.preferLeft) {
|
|
|
|
rect.left = swb.right - rect.width;
|
|
|
|
update = true;
|
|
|
|
}
|
2010-06-22 21:19:10 -07:00
|
|
|
snappedTrenches.right = 'edge';
|
|
|
|
delete snappedTrenches.left;
|
2010-06-19 13:08:14 -07:00
|
|
|
}
|
2010-06-19 17:45:23 -07:00
|
|
|
if (rect.top < swb.top + snapRadius) {
|
2010-11-11 18:11:28 -08:00
|
|
|
if (stationaryCorner.indexOf('bottom') > -1 && !assumeConstantSize)
|
2010-07-03 13:55:54 -07:00
|
|
|
rect.height = rect.bottom - swb.top;
|
2010-06-19 17:45:23 -07:00
|
|
|
rect.top = swb.top;
|
2010-06-19 13:08:14 -07:00
|
|
|
update = true;
|
|
|
|
updateY = true;
|
2010-06-22 21:19:10 -07:00
|
|
|
snappedTrenches.top = 'edge';
|
2010-06-19 13:08:14 -07:00
|
|
|
}
|
2010-06-19 17:45:23 -07:00
|
|
|
if (rect.bottom > swb.bottom - snapRadius) {
|
|
|
|
if (updateY || !assumeConstantSize) {
|
|
|
|
var newHeight = swb.bottom - rect.top;
|
|
|
|
if (keepProportional)
|
|
|
|
rect.width = rect.width * newHeight / rect.height;
|
|
|
|
rect.height = newHeight;
|
|
|
|
update = true;
|
|
|
|
} else if (!updateY || !Trenches.preferTop) {
|
|
|
|
rect.top = swb.bottom - rect.height;
|
|
|
|
update = true;
|
|
|
|
}
|
2010-06-22 21:19:10 -07:00
|
|
|
snappedTrenches.top = 'edge';
|
|
|
|
delete snappedTrenches.bottom;
|
2010-06-19 13:08:14 -07:00
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-22 21:19:10 -07:00
|
|
|
if (update) {
|
|
|
|
rect.snappedTrenches = snappedTrenches;
|
2010-06-19 17:45:23 -07:00
|
|
|
return rect;
|
2010-07-11 16:48:05 -07:00
|
|
|
}
|
|
|
|
return false;
|
2010-06-15 12:26:35 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
|
|
|
// ----------
|
2010-06-15 12:26:35 -07:00
|
|
|
// Function: drag
|
2010-06-22 16:42:06 -07:00
|
|
|
// Called in response to an <Item> draggable "drag" event.
|
2010-09-08 10:02:08 -07:00
|
|
|
drag: function Drag_drag(event) {
|
2010-11-07 22:38:24 -08:00
|
|
|
this.snap(UI.rtl ? 'topright' : 'topleft', true);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-03 12:44:35 -07:00
|
|
|
if (this.parent && this.parent.expanded) {
|
2010-06-15 12:26:35 -07:00
|
|
|
var distance = this.startPosition.distance(new Point(event.clientX, event.clientY));
|
2010-08-11 20:08:33 -07:00
|
|
|
if (distance > 100) {
|
2010-06-15 12:26:35 -07:00
|
|
|
this.parent.remove(this.item);
|
|
|
|
this.parent.collapse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-07-18 08:58:10 -07:00
|
|
|
// ----------
|
2010-06-15 12:26:35 -07:00
|
|
|
// Function: stop
|
2010-06-22 16:42:06 -07:00
|
|
|
// Called in response to an <Item> draggable "stop" event.
|
2010-10-09 12:46:18 -07:00
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// immediately - bool for doing the pushAway immediately, without animation
|
|
|
|
stop: function Drag_stop(immediately) {
|
2010-07-03 12:44:35 -07:00
|
|
|
Trenches.hideGuides();
|
2010-06-19 11:56:07 -07:00
|
|
|
this.item.isDragging = false;
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2011-07-04 15:23:33 -07:00
|
|
|
if (this.parent && this.parent != this.item.parent)
|
|
|
|
this.parent.closeIfEmpty();
|
2011-07-03 17:11:40 -07:00
|
|
|
|
2010-07-03 12:44:35 -07:00
|
|
|
if (this.parent && this.parent.expanded)
|
2010-06-15 12:26:35 -07:00
|
|
|
this.parent.arrange();
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2011-01-12 13:48:42 -08:00
|
|
|
if (this.item.parent)
|
|
|
|
this.item.parent.arrange();
|
|
|
|
|
2011-07-04 15:23:35 -07:00
|
|
|
if (this.item.isAGroupItem) {
|
2010-06-15 12:26:35 -07:00
|
|
|
this.item.setZ(drag.zIndex);
|
|
|
|
drag.zIndex++;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-10-09 12:46:18 -07:00
|
|
|
this.item.pushAway(immediately);
|
2010-06-15 12:26:35 -07:00
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-15 12:26:35 -07:00
|
|
|
Trenches.disactivate();
|
|
|
|
}
|
|
|
|
};
|