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 trench.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-17 14:52:25 -07:00
|
|
|
* Ian Gilman <ian@iangilman.com>
|
2010-06-29 16:39:48 -07:00
|
|
|
* Aza Raskin <aza@mozilla.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: trench.js
|
2010-06-16 17:19:11 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
// ##########
|
2010-06-15 12:26:35 -07:00
|
|
|
// Class: Trench
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-15 12:26:35 -07:00
|
|
|
// Class for drag-snapping regions; called "trenches" as they are long and narrow.
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// Constructor: Trench
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 14:40:14 -07:00
|
|
|
// Parameters:
|
2010-08-06 15:46:55 -07:00
|
|
|
// element - the DOM element for Item (GroupItem or TabItem) from which the trench is projected
|
2010-06-19 14:40:14 -07:00
|
|
|
// xory - either "x" or "y": whether the trench's <position> is along the x- or y-axis.
|
|
|
|
// In other words, if "x", the trench is vertical; if "y", the trench is horizontal.
|
|
|
|
// type - either "border" or "guide". Border trenches mark the border of an Item.
|
|
|
|
// Guide trenches extend out (unless they are intercepted) and act as "guides".
|
2010-07-18 08:58:10 -07:00
|
|
|
// edge - which edge of the Item that this trench corresponds to.
|
2010-06-19 14:40:14 -07:00
|
|
|
// Either "top", "left", "bottom", or "right".
|
2010-09-08 10:02:08 -07:00
|
|
|
function Trench(element, xory, type, edge) {
|
2010-06-19 14:40:14 -07:00
|
|
|
//----------
|
|
|
|
// Variable: id
|
2010-06-19 15:39:55 -07:00
|
|
|
// (integer) The id for the Trench. Set sequentially via <Trenches.nextId>
|
2010-06-19 11:56:07 -07:00
|
|
|
this.id = Trenches.nextId++;
|
2010-06-19 14:40:14 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
// ---------
|
|
|
|
// Variables: Initial parameters
|
|
|
|
// element - (DOMElement)
|
2010-07-17 20:18:43 -07:00
|
|
|
// parentItem - <Item> which projects this trench; to be set with setParentItem
|
2010-06-19 15:39:55 -07:00
|
|
|
// xory - (string) "x" or "y"
|
|
|
|
// type - (string) "border" or "guide"
|
|
|
|
// edge - (string) "top", "left", "bottom", or "right"
|
2010-06-19 11:56:07 -07:00
|
|
|
this.el = element;
|
2010-07-17 20:18:43 -07:00
|
|
|
this.parentItem = null;
|
2010-06-19 11:56:07 -07:00
|
|
|
this.xory = xory; // either "x" or "y"
|
2010-06-19 14:40:14 -07:00
|
|
|
this.type = type; // "border" or "guide"
|
2010-06-19 11:56:07 -07:00
|
|
|
this.edge = edge; // "top", "left", "bottom", or "right"
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-19 14:40:14 -07:00
|
|
|
this.$el = iQ(this.el);
|
|
|
|
|
2010-06-22 20:18:40 -07:00
|
|
|
//----------
|
|
|
|
// Variable: dom
|
|
|
|
// (array) DOM elements for visible reflexes of the Trench
|
|
|
|
this.dom = [];
|
|
|
|
|
|
|
|
//----------
|
|
|
|
// Variable: showGuide
|
|
|
|
// (boolean) Whether this trench will project a visible guide (dotted line) or not.
|
|
|
|
this.showGuide = false;
|
|
|
|
|
2010-06-19 14:40:14 -07:00
|
|
|
//----------
|
2010-06-19 15:39:55 -07:00
|
|
|
// Variable: active
|
|
|
|
// (boolean) Whether this trench is currently active or not.
|
2010-06-19 14:40:14 -07:00
|
|
|
// Basically every trench aside for those projected by the Item currently being dragged
|
|
|
|
// all become active.
|
2010-06-19 11:56:07 -07:00
|
|
|
this.active = false;
|
2010-06-19 13:08:14 -07:00
|
|
|
this.gutter = Items.defaultGutter;
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-19 14:40:14 -07:00
|
|
|
//----------
|
|
|
|
// Variable: position
|
2010-06-19 15:39:55 -07:00
|
|
|
// (integer) position is the position that we should snap to.
|
2010-06-19 11:56:07 -07:00
|
|
|
this.position = 0;
|
2010-06-19 14:40:14 -07:00
|
|
|
|
|
|
|
//----------
|
2010-06-19 15:39:55 -07:00
|
|
|
// Variables: some Ranges
|
|
|
|
// range - (<Range>) explicit range; this is along the transverse axis
|
|
|
|
// minRange - (<Range>) the minimum active range
|
|
|
|
// activeRange - (<Range>) the currently active range
|
2010-06-19 14:40:14 -07:00
|
|
|
this.range = new Range(0,10000);
|
|
|
|
this.minRange = new Range(0,0);
|
|
|
|
this.activeRange = new Range(0,10000);
|
2010-06-15 12:26:35 -07:00
|
|
|
};
|
2010-07-03 18:13:31 -07:00
|
|
|
|
2010-07-26 17:15:19 -07:00
|
|
|
Trench.prototype = {
|
2010-07-03 18:13:31 -07:00
|
|
|
//----------
|
2010-07-06 16:22:57 -07:00
|
|
|
// Variable: radius
|
2010-07-03 18:13:31 -07:00
|
|
|
// (integer) radius is how far away we should snap from
|
2010-07-17 20:18:43 -07:00
|
|
|
get radius() this.customRadius || Trenches.defaultRadius,
|
|
|
|
|
|
|
|
setParentItem: function Trench_setParentItem(item) {
|
|
|
|
if (!item.isAnItem) {
|
2010-08-10 11:13:10 -07:00
|
|
|
Utils.assert(false, "parentItem must be an Item");
|
2010-07-17 20:18:43 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.parentItem = item;
|
|
|
|
return true;
|
|
|
|
},
|
2010-07-03 18:13:31 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: setPosition
|
|
|
|
// set the trench's position.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// position - (integer) px center position of the trench
|
|
|
|
// range - (<Range>) the explicit active range of the trench
|
|
|
|
// minRange - (<Range>) the minimum range of the trench
|
2010-06-19 11:56:07 -07:00
|
|
|
setPosition: function Trench_setPos(position, range, minRange) {
|
|
|
|
this.position = position;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-23 00:03:40 -07:00
|
|
|
var page = Items.getPageBounds(true);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// optionally, set the range.
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isRange(range)) {
|
2010-06-19 14:40:14 -07:00
|
|
|
this.range = range;
|
2010-06-29 22:06:36 -07:00
|
|
|
} else {
|
2010-07-23 00:03:40 -07:00
|
|
|
this.range = new Range(0, (this.xory == 'x' ? page.height : page.width));
|
2010-06-19 11:56:07 -07:00
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// if there's a minRange, set that too.
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isRange(minRange))
|
2010-06-19 11:56:07 -07:00
|
|
|
this.minRange = minRange;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// set the appropriate bounds as a rect.
|
2010-07-30 02:54:30 -07:00
|
|
|
if (this.xory == "x") // vertical
|
|
|
|
this.rect = new Rect(this.position - this.radius, this.range.min, 2 * this.radius, this.range.extent);
|
2010-06-29 22:06:36 -07:00
|
|
|
else // horizontal
|
2010-07-30 02:54:30 -07:00
|
|
|
this.rect = new Rect(this.range.min, this.position - this.radius, this.range.extent, 2 * this.radius);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
this.show(); // DEBUG
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: setActiveRange
|
|
|
|
// set the trench's currently active range.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// activeRange - (<Range>)
|
2010-06-19 11:56:07 -07:00
|
|
|
setActiveRange: function Trench_setActiveRect(activeRange) {
|
2010-07-22 12:46:51 -07:00
|
|
|
if (!Utils.isRange(activeRange))
|
2010-06-19 14:40:14 -07:00
|
|
|
return false;
|
2010-06-19 11:56:07 -07:00
|
|
|
this.activeRange = activeRange;
|
2010-07-30 02:54:30 -07:00
|
|
|
if (this.xory == "x") { // horizontal
|
|
|
|
this.activeRect = new Rect(this.position - this.radius, this.activeRange.min, 2 * this.radius, this.activeRange.extent);
|
|
|
|
this.guideRect = new Rect(this.position, this.activeRange.min, 0, this.activeRange.extent);
|
2010-06-22 20:18:40 -07:00
|
|
|
} else { // vertical
|
2010-07-30 02:54:30 -07:00
|
|
|
this.activeRect = new Rect(this.activeRange.min, this.position - this.radius, this.activeRange.extent, 2 * this.radius);
|
|
|
|
this.guideRect = new Rect(this.activeRange.min, this.position, this.activeRange.extent, 0);
|
2010-06-22 20:18:40 -07:00
|
|
|
}
|
2010-08-11 19:01:29 -07:00
|
|
|
return true;
|
2010-06-19 11:56:07 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: setWithRect
|
|
|
|
// Set the trench's position using the given rect. We know which side of the rect we should match
|
|
|
|
// because we've already recorded this information in <edge>.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// rect - (<Rect>)
|
2010-06-19 11:56:07 -07:00
|
|
|
setWithRect: function Trench_setWithRect(rect) {
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-22 12:46:51 -07:00
|
|
|
if (!Utils.isRect(rect))
|
2010-06-19 15:39:55 -07:00
|
|
|
Utils.error('argument must be Rect');
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// First, calculate the range for this trench.
|
|
|
|
// Border trenches are always only active for the length of this range.
|
|
|
|
// Guide trenches, however, still use this value as its minRange.
|
|
|
|
if (this.xory == "x")
|
2010-06-19 14:44:19 -07:00
|
|
|
var range = new Range(rect.top - this.gutter, rect.bottom + this.gutter);
|
2010-06-19 11:56:07 -07:00
|
|
|
else
|
2010-06-19 14:44:19 -07:00
|
|
|
var range = new Range(rect.left - this.gutter, rect.right + this.gutter);
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
if (this.type == "border") {
|
|
|
|
// border trenches have a range, so set that too.
|
|
|
|
if (this.edge == "left")
|
|
|
|
this.setPosition(rect.left - this.gutter, range);
|
|
|
|
else if (this.edge == "right")
|
2010-06-19 14:44:19 -07:00
|
|
|
this.setPosition(rect.right + this.gutter, range);
|
2010-06-19 11:56:07 -07:00
|
|
|
else if (this.edge == "top")
|
|
|
|
this.setPosition(rect.top - this.gutter, range);
|
|
|
|
else if (this.edge == "bottom")
|
2010-06-19 14:44:19 -07:00
|
|
|
this.setPosition(rect.bottom + this.gutter, range);
|
2010-06-19 11:56:07 -07:00
|
|
|
} else if (this.type == "guide") {
|
|
|
|
// guide trenches have no range, but do have a minRange.
|
2010-07-18 08:58:10 -07:00
|
|
|
if (this.edge == "left")
|
2010-06-19 11:56:07 -07:00
|
|
|
this.setPosition(rect.left, false, range);
|
|
|
|
else if (this.edge == "right")
|
2010-06-19 14:44:19 -07:00
|
|
|
this.setPosition(rect.right, false, range);
|
2010-06-19 11:56:07 -07:00
|
|
|
else if (this.edge == "top")
|
|
|
|
this.setPosition(rect.top, false, range);
|
|
|
|
else if (this.edge == "bottom")
|
2010-06-19 14:44:19 -07:00
|
|
|
this.setPosition(rect.bottom, false, range);
|
2010-06-19 11:56:07 -07:00
|
|
|
}
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: show
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-22 20:18:40 -07:00
|
|
|
// Show guide (dotted line), if <showGuide> is true.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-22 20:18:40 -07:00
|
|
|
// If <Trenches.showDebug> is true, we will draw the trench. Active portions are drawn with 0.5
|
|
|
|
// opacity. If <active> is false, the entire trench will be
|
2010-06-19 15:39:55 -07:00
|
|
|
// very translucent.
|
2010-06-30 08:32:17 -07:00
|
|
|
show: function Trench_show() { // DEBUG
|
|
|
|
if (this.active && this.showGuide) {
|
|
|
|
if (!this.dom.guideTrench)
|
|
|
|
this.dom.guideTrench = iQ("<div/>").addClass('guideTrench').css({id: 'guideTrench'+this.id});
|
|
|
|
var guideTrench = this.dom.guideTrench;
|
2010-07-17 14:10:29 -07:00
|
|
|
guideTrench.css(this.guideRect.css());
|
2010-06-30 08:32:17 -07:00
|
|
|
iQ("body").append(guideTrench);
|
2010-06-22 20:18:40 -07:00
|
|
|
} else {
|
2010-06-29 21:46:15 -07:00
|
|
|
if (this.dom.guideTrench) {
|
2010-06-22 20:18:40 -07:00
|
|
|
this.dom.guideTrench.remove();
|
2010-06-29 21:46:15 -07:00
|
|
|
delete this.dom.guideTrench;
|
|
|
|
}
|
2010-06-22 20:18:40 -07:00
|
|
|
}
|
2010-06-30 08:32:17 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
if (!Trenches.showDebug) {
|
2010-07-30 02:54:30 -07:00
|
|
|
this.hide(true); // true for dontHideGuides
|
2010-06-19 11:56:07 -07:00
|
|
|
return;
|
|
|
|
}
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-22 20:18:40 -07:00
|
|
|
if (!this.dom.visibleTrench)
|
2010-06-27 23:55:26 -07:00
|
|
|
this.dom.visibleTrench = iQ("<div/>")
|
|
|
|
.addClass('visibleTrench')
|
|
|
|
.addClass(this.type) // border or guide
|
|
|
|
.css({id: 'visibleTrench'+this.id});
|
2010-06-22 20:18:40 -07:00
|
|
|
var visibleTrench = this.dom.visibleTrench;
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-27 23:55:26 -07:00
|
|
|
if (!this.dom.activeVisibleTrench)
|
|
|
|
this.dom.activeVisibleTrench = iQ("<div/>")
|
|
|
|
.addClass('activeVisibleTrench')
|
|
|
|
.addClass(this.type) // border or guide
|
|
|
|
.css({id: 'activeVisibleTrench'+this.id});
|
2010-06-22 20:18:40 -07:00
|
|
|
var activeVisibleTrench = this.dom.activeVisibleTrench;
|
2010-06-15 12:26:35 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
if (this.active)
|
2010-06-27 23:55:26 -07:00
|
|
|
activeVisibleTrench.addClass('activeTrench');
|
2010-06-19 11:56:07 -07:00
|
|
|
else
|
2010-06-27 23:55:26 -07:00
|
|
|
activeVisibleTrench.removeClass('activeTrench');
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-17 14:10:29 -07:00
|
|
|
visibleTrench.css(this.rect.css());
|
|
|
|
activeVisibleTrench.css((this.activeRect || this.rect).css());
|
2010-06-19 11:56:07 -07:00
|
|
|
iQ("body").append(visibleTrench);
|
|
|
|
iQ("body").append(activeVisibleTrench);
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: hide
|
|
|
|
// Hide the trench.
|
2010-07-30 02:54:30 -07:00
|
|
|
hide: function Trench_hide(dontHideGuides) {
|
2010-06-22 20:18:40 -07:00
|
|
|
if (this.dom.visibleTrench)
|
|
|
|
this.dom.visibleTrench.remove();
|
|
|
|
if (this.dom.activeVisibleTrench)
|
|
|
|
this.dom.activeVisibleTrench.remove();
|
|
|
|
if (!dontHideGuides && this.dom.guideTrench)
|
|
|
|
this.dom.guideTrench.remove();
|
2010-06-19 11:56:07 -07:00
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
//----------
|
|
|
|
// Function: rectOverlaps
|
|
|
|
// Given a <Rect>, compute whether it overlaps with this trench. If it does, return an
|
|
|
|
// adjusted ("snapped") <Rect>; if it does not overlap, simply return false.
|
|
|
|
//
|
|
|
|
// Note that simply overlapping is not all that is required to be affected by this function.
|
|
|
|
// Trenches can only affect certain edges of rectangles... for example, a "left"-edge guide
|
|
|
|
// trench should only affect left edges of rectangles. We don't snap right edges to left-edged
|
|
|
|
// guide trenches. For border trenches, the logic is a bit different, so left snaps to right and
|
|
|
|
// top snaps to bottom.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// rect - (<Rect>) the rectangle in question
|
2010-07-03 13:55:54 -07:00
|
|
|
// stationaryCorner - which corner is stationary? by default, the top left.
|
|
|
|
// "topleft", "bottomleft", "topright", "bottomright"
|
2010-06-19 15:39:55 -07:00
|
|
|
// assumeConstantSize - (boolean) whether the rect's dimensions are sacred or not
|
|
|
|
// keepProportional - (boolean) if we are allowed to change the rect's size, whether the
|
|
|
|
// dimensions should scaled proportionally or not.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Returns:
|
|
|
|
// false - if rect does not overlap with this trench
|
|
|
|
// newRect - (<Rect>) an adjusted version of rect, if it is affected by this trench
|
2010-07-03 13:55:54 -07:00
|
|
|
rectOverlaps: function Trench_rectOverlaps(rect,stationaryCorner,assumeConstantSize,keepProportional) {
|
2010-06-19 11:56:07 -07:00
|
|
|
var edgeToCheck;
|
|
|
|
if (this.type == "border") {
|
|
|
|
if (this.edge == "left")
|
|
|
|
edgeToCheck = "right";
|
|
|
|
else if (this.edge == "right")
|
|
|
|
edgeToCheck = "left";
|
|
|
|
else if (this.edge == "top")
|
|
|
|
edgeToCheck = "bottom";
|
|
|
|
else if (this.edge == "bottom")
|
|
|
|
edgeToCheck = "top";
|
2010-06-19 13:08:14 -07:00
|
|
|
} else { // if trench type is guide or barrier...
|
2010-06-19 11:56:07 -07:00
|
|
|
edgeToCheck = this.edge;
|
|
|
|
}
|
2010-06-17 21:50:42 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
rect.adjustedEdge = edgeToCheck;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
switch (edgeToCheck) {
|
|
|
|
case "left":
|
2010-06-19 15:47:26 -07:00
|
|
|
if (this.ruleOverlaps(rect.left, rect.yRange)) {
|
2010-07-03 13:55:54 -07:00
|
|
|
if (stationaryCorner.indexOf('right') > -1)
|
|
|
|
rect.width = rect.right - this.position;
|
2010-06-19 11:56:07 -07:00
|
|
|
rect.left = this.position;
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "right":
|
2010-06-19 15:47:26 -07:00
|
|
|
if (this.ruleOverlaps(rect.right, rect.yRange)) {
|
2010-06-19 11:56:07 -07:00
|
|
|
if (assumeConstantSize) {
|
2010-06-19 16:05:23 -07:00
|
|
|
rect.left = this.position - rect.width;
|
2010-06-19 11:56:07 -07:00
|
|
|
} else {
|
|
|
|
var newWidth = this.position - rect.left;
|
|
|
|
if (keepProportional)
|
|
|
|
rect.height = rect.height * newWidth / rect.width;
|
|
|
|
rect.width = newWidth;
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "top":
|
2010-06-19 15:47:26 -07:00
|
|
|
if (this.ruleOverlaps(rect.top, rect.xRange)) {
|
2010-07-03 13:55:54 -07:00
|
|
|
if (stationaryCorner.indexOf('bottom') > -1)
|
|
|
|
rect.height = rect.bottom - this.position;
|
2010-06-19 11:56:07 -07:00
|
|
|
rect.top = this.position;
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "bottom":
|
2010-06-19 15:47:26 -07:00
|
|
|
if (this.ruleOverlaps(rect.bottom, rect.xRange)) {
|
2010-06-19 11:56:07 -07:00
|
|
|
if (assumeConstantSize) {
|
2010-06-19 16:05:23 -07:00
|
|
|
rect.top = this.position - rect.height;
|
2010-06-19 11:56:07 -07:00
|
|
|
} else {
|
|
|
|
var newHeight = this.position - rect.top;
|
|
|
|
if (keepProportional)
|
|
|
|
rect.width = rect.width * newHeight / rect.height;
|
|
|
|
rect.height = newHeight;
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
return false;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: ruleOverlaps
|
|
|
|
// Computes whether the given "rule" (a line segment, essentially), given by the position and
|
|
|
|
// range arguments, overlaps with the current trench. Note that this function assumes that
|
|
|
|
// the rule and the trench are in the same direction: both horizontal, or both vertical.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// position - (integer) a position in px
|
|
|
|
// range - (<Range>) the rule's range
|
2010-06-19 11:56:07 -07:00
|
|
|
ruleOverlaps: function Trench_ruleOverlaps(position, range) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return (this.position - this.radius < position &&
|
|
|
|
position < this.position + this.radius &&
|
2010-09-08 18:48:51 -07:00
|
|
|
this.activeRange.overlaps(range));
|
2010-06-19 11:56:07 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: adjustRangeIfIntercept
|
|
|
|
// Computes whether the given boundary (given as a position and its active range), perpendicular
|
2010-07-18 08:58:10 -07:00
|
|
|
// to the trench, intercepts the trench or not. If it does, it returns an adjusted <Range> for
|
2010-06-19 15:39:55 -07:00
|
|
|
// the trench. If not, it returns false.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// position - (integer) the position of the boundary
|
|
|
|
// range - (<Range>) the target's range, on the trench's transverse axis
|
2010-06-19 11:56:07 -07:00
|
|
|
adjustRangeIfIntercept: function Trench_adjustRangeIfIntercept(position, range) {
|
|
|
|
if (this.position - this.radius > range.min && this.position + this.radius < range.max) {
|
2010-06-19 15:47:26 -07:00
|
|
|
var activeRange = new Range(this.activeRange);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// there are three ways this can go:
|
|
|
|
// 1. position < minRange.min
|
|
|
|
// 2. position > minRange.max
|
|
|
|
// 3. position >= minRange.min && position <= minRange.max
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
if (position < this.minRange.min) {
|
2010-07-18 08:58:10 -07:00
|
|
|
activeRange.min = Math.min(this.minRange.min,position);
|
2010-06-19 11:56:07 -07:00
|
|
|
} else if (position > this.minRange.max) {
|
|
|
|
activeRange.max = Math.max(this.minRange.max,position);
|
|
|
|
} else {
|
|
|
|
// this should be impossible because items can't overlap and we've already checked
|
|
|
|
// that the range intercepts.
|
|
|
|
}
|
|
|
|
return activeRange;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
//----------
|
|
|
|
// Function: calculateActiveRange
|
2010-08-06 15:46:55 -07:00
|
|
|
// Computes and sets the <activeRange> for the trench, based on the <GroupItems> around.
|
|
|
|
// This makes it so trenches' active ranges don't extend through other groupItems.
|
2010-06-19 11:56:07 -07:00
|
|
|
calculateActiveRange: function Trench_calculateActiveRange() {
|
2010-06-15 21:21:34 -07:00
|
|
|
|
2010-06-21 19:32:07 -07:00
|
|
|
// set it to the default: just the range itself.
|
2010-06-19 11:56:07 -07:00
|
|
|
this.setActiveRange(this.range);
|
|
|
|
|
2010-06-21 19:32:07 -07:00
|
|
|
// only guide-type trenches need to set a separate active range
|
|
|
|
if (this.type != 'guide')
|
|
|
|
return;
|
|
|
|
|
2010-08-06 15:46:55 -07:00
|
|
|
var groupItems = GroupItems.groupItems;
|
2010-06-19 11:56:07 -07:00
|
|
|
var trench = this;
|
2010-08-06 15:46:55 -07:00
|
|
|
groupItems.forEach(function(groupItem) {
|
|
|
|
if (groupItem.isDragging) // floating groupItems don't block trenches
|
2010-06-19 11:56:07 -07:00
|
|
|
return;
|
2010-08-06 15:46:55 -07:00
|
|
|
if (trench.el == groupItem.container) // groupItems don't block their own trenches
|
2010-06-19 11:56:07 -07:00
|
|
|
return;
|
2010-08-06 15:46:55 -07:00
|
|
|
var bounds = groupItem.getBounds();
|
2010-06-19 14:40:14 -07:00
|
|
|
var activeRange = new Range();
|
2010-06-19 11:56:07 -07:00
|
|
|
if (trench.xory == 'y') { // if this trench is horizontal...
|
2010-06-19 15:47:26 -07:00
|
|
|
activeRange = trench.adjustRangeIfIntercept(bounds.left, bounds.yRange);
|
2010-06-19 11:56:07 -07:00
|
|
|
if (activeRange)
|
|
|
|
trench.setActiveRange(activeRange);
|
2010-06-19 15:47:26 -07:00
|
|
|
activeRange = trench.adjustRangeIfIntercept(bounds.right, bounds.yRange);
|
2010-06-19 11:56:07 -07:00
|
|
|
if (activeRange)
|
|
|
|
trench.setActiveRange(activeRange);
|
|
|
|
} else { // if this trench is vertical...
|
2010-06-19 15:47:26 -07:00
|
|
|
activeRange = trench.adjustRangeIfIntercept(bounds.top, bounds.xRange);
|
2010-06-19 11:56:07 -07:00
|
|
|
if (activeRange)
|
|
|
|
trench.setActiveRange(activeRange);
|
2010-06-19 15:47:26 -07:00
|
|
|
activeRange = trench.adjustRangeIfIntercept(bounds.bottom, bounds.xRange);
|
2010-06-19 11:56:07 -07:00
|
|
|
if (activeRange)
|
|
|
|
trench.setActiveRange(activeRange);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2010-06-15 12:26:35 -07:00
|
|
|
};
|
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
// ##########
|
|
|
|
// Class: Trenches
|
2010-07-18 08:58:10 -07:00
|
|
|
// Singelton for managing all <Trench>es.
|
2010-06-15 12:26:35 -07:00
|
|
|
var Trenches = {
|
2010-06-19 15:39:55 -07:00
|
|
|
// ---------
|
|
|
|
// Variables:
|
|
|
|
// nextId - (integer) a counter for the next <Trench>'s <Trench.id> value.
|
|
|
|
// showDebug - (boolean) whether to draw the <Trench>es or not.
|
|
|
|
// defaultRadius - (integer) the default radius for new <Trench>es.
|
2010-09-21 10:22:07 -07:00
|
|
|
// disabled - (boolean) whether trench-snapping is disabled or not.
|
2010-06-19 11:56:07 -07:00
|
|
|
nextId: 0,
|
2010-06-19 15:39:55 -07:00
|
|
|
showDebug: false,
|
2010-06-19 13:08:14 -07:00
|
|
|
defaultRadius: 10,
|
2010-09-21 10:22:07 -07:00
|
|
|
disabled: false,
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Variables: snapping preferences; used to break ties in snapping.
|
|
|
|
// preferTop - (boolean) prefer snapping to the top to the bottom
|
|
|
|
// preferLeft - (boolean) prefer snapping to the left to the right
|
2010-06-19 11:56:07 -07:00
|
|
|
preferTop: true,
|
2010-11-07 22:38:24 -08:00
|
|
|
get preferLeft() { return !UI.rtl; },
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
trenches: [],
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: getById
|
|
|
|
// Return the specified <Trench>.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// id - (integer)
|
2010-06-19 11:56:07 -07:00
|
|
|
getById: function Trenches_getById(id) {
|
|
|
|
return this.trenches[id];
|
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: register
|
|
|
|
// Register a new <Trench> and returns the resulting <Trench> ID.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// See the constructor <Trench.Trench>'s parameters.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Returns:
|
2010-07-17 20:18:43 -07:00
|
|
|
// id - (int) the new <Trench>'s ID.
|
2010-06-19 11:56:07 -07:00
|
|
|
register: function Trenches_register(element, xory, type, edge) {
|
|
|
|
var trench = new Trench(element, xory, type, edge);
|
|
|
|
this.trenches[trench.id] = trench;
|
|
|
|
return trench.id;
|
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
2010-07-17 20:18:43 -07:00
|
|
|
// ---------
|
|
|
|
// Function: registerWithItem
|
|
|
|
// Register a whole set of <Trench>es using an <Item> and returns the resulting <Trench> IDs.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-07-17 20:18:43 -07:00
|
|
|
// Parameters:
|
|
|
|
// item - the <Item> to project trenches
|
|
|
|
// type - either "border" or "guide"
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-07-17 20:18:43 -07:00
|
|
|
// Returns:
|
|
|
|
// ids - array of the new <Trench>es' IDs.
|
|
|
|
registerWithItem: function Trenches_registerWithItem(item, type) {
|
|
|
|
var container = item.container;
|
|
|
|
var ids = {};
|
|
|
|
ids.left = Trenches.register(container,"x",type,"left");
|
|
|
|
ids.right = Trenches.register(container,"x",type,"right");
|
|
|
|
ids.top = Trenches.register(container,"y",type,"top");
|
|
|
|
ids.bottom = Trenches.register(container,"y",type,"bottom");
|
|
|
|
|
|
|
|
this.getById(ids.left).setParentItem(item);
|
|
|
|
this.getById(ids.right).setParentItem(item);
|
|
|
|
this.getById(ids.top).setParentItem(item);
|
|
|
|
this.getById(ids.bottom).setParentItem(item);
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
},
|
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
// ---------
|
|
|
|
// Function: unregister
|
|
|
|
// Unregister one or more <Trench>es.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// ids - (integer) a single <Trench> ID or (array) a list of <Trench> IDs.
|
2010-06-19 11:56:07 -07:00
|
|
|
unregister: function Trenches_unregister(ids) {
|
2010-07-26 11:02:52 -07:00
|
|
|
if (!Array.isArray(ids))
|
2010-06-19 11:56:07 -07:00
|
|
|
ids = [ids];
|
|
|
|
var self = this;
|
2010-07-30 02:54:30 -07:00
|
|
|
ids.forEach(function(id) {
|
2010-06-19 11:56:07 -07:00
|
|
|
self.trenches[id].hide();
|
|
|
|
delete self.trenches[id];
|
|
|
|
});
|
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: activateOthersTrenches
|
|
|
|
// Activate all <Trench>es other than those projected by the current element.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Parameters:
|
|
|
|
// element - (DOMElement) the DOM element of the Item being dragged or resized.
|
2010-06-19 11:56:07 -07:00
|
|
|
activateOthersTrenches: function Trenches_activateOthersTrenches(element) {
|
|
|
|
this.trenches.forEach(function(t) {
|
|
|
|
if (t.el === element)
|
|
|
|
return;
|
2010-08-10 16:20:05 -07:00
|
|
|
if (t.parentItem && (t.parentItem.isAFauxItem ||
|
|
|
|
t.parentItem.isDragging ||
|
|
|
|
t.parentItem.isDropTarget))
|
2010-07-18 08:16:32 -07:00
|
|
|
return;
|
2010-06-19 11:56:07 -07:00
|
|
|
t.active = true;
|
|
|
|
t.calculateActiveRange();
|
|
|
|
t.show(); // debug
|
|
|
|
});
|
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: disactivate
|
|
|
|
// After <activateOthersTrenches>, disactivates all the <Trench>es again.
|
2010-06-19 11:56:07 -07:00
|
|
|
disactivate: function Trenches_disactivate() {
|
|
|
|
this.trenches.forEach(function(t) {
|
|
|
|
t.active = false;
|
2010-06-22 20:18:40 -07:00
|
|
|
t.showGuide = false;
|
2010-06-27 23:55:26 -07:00
|
|
|
t.show();
|
2010-06-19 11:56:07 -07:00
|
|
|
});
|
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
2010-06-22 20:18:40 -07:00
|
|
|
// ---------
|
|
|
|
// Function: hideGuides
|
|
|
|
// Hide all guides (dotted lines) en masse.
|
2010-06-30 08:32:17 -07:00
|
|
|
hideGuides: function Trenches_hideGuides() {
|
2010-06-22 20:18:40 -07:00
|
|
|
this.trenches.forEach(function(t) {
|
|
|
|
t.showGuide = false;
|
|
|
|
t.show();
|
2010-07-18 08:58:10 -07:00
|
|
|
});
|
2010-06-22 20:18:40 -07:00
|
|
|
},
|
|
|
|
|
2010-06-19 15:39:55 -07:00
|
|
|
// ---------
|
|
|
|
// Function: snap
|
|
|
|
// Used to "snap" an object's bounds to active trenches and to the edge of the window.
|
|
|
|
// If the meta key is down (<Key.meta>), it will not snap but will still enforce the rect
|
|
|
|
// not leaving the safe bounds of the window.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2010-07-18 08:58:10 -07:00
|
|
|
// rect - (<Rect>) the object's current bounds
|
2010-07-03 13:55:54 -07:00
|
|
|
// stationaryCorner - which corner is stationary? by default, the top left.
|
|
|
|
// "topleft", "bottomleft", "topright", "bottomright"
|
2010-06-19 15:39:55 -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.
|
2010-07-18 08:58:10 -07:00
|
|
|
//
|
2010-06-19 15:39:55 -07:00
|
|
|
// Returns:
|
|
|
|
// (<Rect>) - the updated bounds, if they were updated
|
|
|
|
// false - if the bounds were not updated
|
2010-07-03 13:55:54 -07:00
|
|
|
snap: function Trenches_snap(rect,stationaryCorner,assumeConstantSize,keepProportional) {
|
|
|
|
// hide all the guide trenches, because the correct ones will be turned on later.
|
|
|
|
Trenches.hideGuides();
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
var updated = false;
|
|
|
|
var updatedX = false;
|
|
|
|
var updatedY = false;
|
2010-06-22 21:19:10 -07:00
|
|
|
|
|
|
|
var snappedTrenches = {};
|
|
|
|
|
2010-07-15 01:41:08 -07:00
|
|
|
for (var i in this.trenches) {
|
2010-06-19 11:56:07 -07:00
|
|
|
var t = this.trenches[i];
|
2010-07-22 20:40:25 -07:00
|
|
|
if (!t.active || t.parentItem.isDropTarget)
|
2010-06-19 11:56:07 -07:00
|
|
|
continue;
|
|
|
|
// newRect will be a new rect, or false
|
2010-07-03 13:55:54 -07:00
|
|
|
var newRect = t.rectOverlaps(rect,stationaryCorner,assumeConstantSize,keepProportional);
|
2010-06-15 21:21:34 -07:00
|
|
|
|
2010-06-22 20:18:40 -07:00
|
|
|
if (newRect) { // if rectOverlaps returned an updated rect...
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
if (assumeConstantSize && updatedX && updatedY)
|
|
|
|
break;
|
|
|
|
if (assumeConstantSize && updatedX && (newRect.adjustedEdge == "left"||newRect.adjustedEdge == "right"))
|
|
|
|
continue;
|
|
|
|
if (assumeConstantSize && updatedY && (newRect.adjustedEdge == "top"||newRect.adjustedEdge == "bottom"))
|
|
|
|
continue;
|
2010-06-22 20:18:40 -07:00
|
|
|
|
|
|
|
rect = newRect;
|
|
|
|
updated = true;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-22 21:19:10 -07:00
|
|
|
// register this trench as the "snapped trench" for the appropriate edge.
|
|
|
|
snappedTrenches[newRect.adjustedEdge] = t;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// if updatedX, we don't need to update x any more.
|
|
|
|
if (newRect.adjustedEdge == "left" && this.preferLeft)
|
|
|
|
updatedX = true;
|
|
|
|
if (newRect.adjustedEdge == "right" && !this.preferLeft)
|
|
|
|
updatedX = true;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-19 11:56:07 -07:00
|
|
|
// if updatedY, we don't need to update x any more.
|
|
|
|
if (newRect.adjustedEdge == "top" && this.preferTop)
|
|
|
|
updatedY = true;
|
|
|
|
if (newRect.adjustedEdge == "bottom" && !this.preferTop)
|
|
|
|
updatedY = true;
|
2010-06-15 21:21:34 -07:00
|
|
|
|
2010-06-29 21:46:15 -07:00
|
|
|
}
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-22 20:18:40 -07:00
|
|
|
if (updated) {
|
2010-06-22 21:19:10 -07:00
|
|
|
rect.snappedTrenches = snappedTrenches;
|
2010-06-19 11:56:07 -07:00
|
|
|
return rect;
|
2010-06-22 20:18:40 -07:00
|
|
|
}
|
2010-07-11 16:48:05 -07:00
|
|
|
return false;
|
2010-06-19 11:56:07 -07:00
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: show
|
|
|
|
// <Trench.show> all <Trench>es.
|
2010-06-19 11:56:07 -07:00
|
|
|
show: function Trenches_show() {
|
2010-07-30 02:54:30 -07:00
|
|
|
this.trenches.forEach(function(t) {
|
2010-06-19 11:56:07 -07:00
|
|
|
t.show();
|
|
|
|
});
|
2010-07-18 08:58:10 -07:00
|
|
|
},
|
2010-06-19 15:39:55 -07:00
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: toggleShown
|
|
|
|
// Toggle <Trenches.showDebug> and trigger <Trenches.show>
|
2010-06-19 11:56:07 -07:00
|
|
|
toggleShown: function Trenches_toggleShown() {
|
2010-06-17 14:52:25 -07:00
|
|
|
this.showDebug = !this.showDebug;
|
|
|
|
this.show();
|
|
|
|
}
|
2010-06-15 12:26:35 -07:00
|
|
|
};
|