2010-04-16 15:09:23 -07:00
|
|
|
// Title: items.js (revision-a)
|
2010-04-30 17:24:03 -07:00
|
|
|
|
2010-03-29 16:08:48 -07:00
|
|
|
// ##########
|
2010-04-30 17:24:03 -07:00
|
|
|
// Class: Item
|
2010-05-13 17:24:37 -07:00
|
|
|
// Superclass for all visible objects (<TabItem>s and <Group>s).
|
|
|
|
//
|
|
|
|
// If you subclass, in addition to the things Item provides, you need to also provide these methods:
|
|
|
|
// reloadBounds - function()
|
|
|
|
// setBounds - function(rect, immediately)
|
|
|
|
// setZ - function(value)
|
|
|
|
// close - function()
|
|
|
|
// addOnClose - function(referenceObject, callback)
|
|
|
|
// removeOnClose - function(referenceObject)
|
2010-05-24 14:49:03 -07:00
|
|
|
// save - function()
|
2010-05-13 17:24:37 -07:00
|
|
|
//
|
2010-04-30 17:24:03 -07:00
|
|
|
// ... and this property:
|
2010-05-13 17:24:37 -07:00
|
|
|
// defaultSize - a Point
|
|
|
|
// locked - an object (see below)
|
|
|
|
//
|
2010-04-30 17:24:03 -07:00
|
|
|
// Make sure to call _init() from your subclass's constructor.
|
2010-03-29 16:08:48 -07:00
|
|
|
window.Item = function() {
|
2010-04-08 16:36:11 -07:00
|
|
|
// Variable: isAnItem
|
|
|
|
// Always true for Items
|
2010-03-29 16:08:48 -07:00
|
|
|
this.isAnItem = true;
|
2010-04-08 16:36:11 -07:00
|
|
|
|
|
|
|
// Variable: bounds
|
|
|
|
// The position and size of this Item, represented as a <Rect>.
|
2010-04-01 17:20:59 -07:00
|
|
|
this.bounds = null;
|
2010-04-08 16:36:11 -07:00
|
|
|
|
|
|
|
// Variable: debug
|
|
|
|
// When set to true, displays a rectangle on the screen that corresponds with bounds.
|
|
|
|
// May be used for additional debugging features in the future.
|
2010-04-01 17:20:59 -07:00
|
|
|
this.debug = false;
|
2010-04-08 16:36:11 -07:00
|
|
|
|
|
|
|
// Variable: $debug
|
2010-05-27 17:25:14 -07:00
|
|
|
// If <debug> is true, this will be the iQ object for the visible rectangle.
|
2010-04-01 17:20:59 -07:00
|
|
|
this.$debug = null;
|
2010-04-08 16:36:11 -07:00
|
|
|
|
|
|
|
// Variable: container
|
|
|
|
// The outermost DOM element that describes this item on screen.
|
2010-04-01 17:20:59 -07:00
|
|
|
this.container = null;
|
2010-04-16 17:21:03 -07:00
|
|
|
|
|
|
|
// Variable: locked
|
|
|
|
// Affects whether an item can be pushed, closed, renamed, etc
|
2010-05-13 17:24:37 -07:00
|
|
|
//
|
2010-05-07 15:49:54 -07:00
|
|
|
// The object may have properties to specify what can't be changed:
|
2010-05-13 17:24:37 -07:00
|
|
|
// .bounds - true if it can't be pushed, dragged, resized, etc
|
|
|
|
// .close - true if it can't be closed
|
|
|
|
// .title - true if it can't be renamed
|
2010-05-11 12:03:31 -07:00
|
|
|
this.locked = null;
|
2010-04-26 13:37:39 -07:00
|
|
|
|
|
|
|
// Variable: parent
|
|
|
|
// The group that this item is a child of
|
|
|
|
this.parent = null;
|
2010-04-26 16:48:46 -07:00
|
|
|
|
|
|
|
// Variable: userSize
|
|
|
|
// A <Point> that describes the last size specifically chosen by the user.
|
|
|
|
// Used by unsquish.
|
|
|
|
this.userSize = null;
|
2010-06-15 14:38:55 -07:00
|
|
|
|
2010-06-16 16:30:48 -07:00
|
|
|
// Variable: dragOptions
|
|
|
|
// Used to pass into iQ.fn.draggable
|
|
|
|
this.dragOptions = null;
|
2010-06-15 14:38:55 -07:00
|
|
|
|
2010-06-16 16:30:48 -07:00
|
|
|
// Variable: dropOptions
|
|
|
|
// Used to pass into iQ.fn.droppable
|
|
|
|
this.dropOptions = null;
|
2010-03-29 16:08:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
window.Item.prototype = {
|
2010-04-01 17:20:59 -07:00
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: _init
|
|
|
|
// Initializes the object. To be called from the subclass's intialization function.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// container - the outermost DOM element that describes this item onscreen.
|
2010-04-01 17:20:59 -07:00
|
|
|
_init: function(container) {
|
2010-04-30 17:24:03 -07:00
|
|
|
Utils.assert('container must be a DOM element', Utils.isDOMElement(container));
|
|
|
|
Utils.assert('Subclass must provide reloadBounds', typeof(this.reloadBounds) == 'function');
|
|
|
|
Utils.assert('Subclass must provide setBounds', typeof(this.setBounds) == 'function');
|
|
|
|
Utils.assert('Subclass must provide setZ', typeof(this.setZ) == 'function');
|
|
|
|
Utils.assert('Subclass must provide close', typeof(this.close) == 'function');
|
|
|
|
Utils.assert('Subclass must provide addOnClose', typeof(this.addOnClose) == 'function');
|
|
|
|
Utils.assert('Subclass must provide removeOnClose', typeof(this.removeOnClose) == 'function');
|
2010-05-24 14:49:03 -07:00
|
|
|
Utils.assert('Subclass must provide save', typeof(this.save) == 'function');
|
2010-05-11 12:03:31 -07:00
|
|
|
Utils.assert('Subclass must provide defaultSize', isPoint(this.defaultSize));
|
|
|
|
Utils.assert('Subclass must provide locked', this.locked);
|
2010-04-30 17:24:03 -07:00
|
|
|
|
2010-04-01 17:20:59 -07:00
|
|
|
this.container = container;
|
|
|
|
|
|
|
|
if(this.debug) {
|
2010-05-27 17:25:14 -07:00
|
|
|
this.$debug = iQ('<div>')
|
2010-04-01 17:20:59 -07:00
|
|
|
.css({
|
|
|
|
border: '2px solid green',
|
|
|
|
zIndex: -10,
|
|
|
|
position: 'absolute'
|
|
|
|
})
|
2010-05-27 17:25:14 -07:00
|
|
|
.appendTo('body');
|
2010-04-01 17:20:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.reloadBounds();
|
2010-04-30 17:24:03 -07:00
|
|
|
Utils.assert('reloadBounds must set up this.bounds', this.bounds);
|
|
|
|
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ(this.container).data('item', this);
|
2010-06-16 16:30:48 -07:00
|
|
|
|
|
|
|
// ___ drag
|
|
|
|
this.dragOptions = {
|
|
|
|
cancelClass: 'close',
|
|
|
|
start: function(e, ui) {
|
|
|
|
drag.info = new Drag(this, e);
|
|
|
|
},
|
|
|
|
drag: function(e, ui) {
|
|
|
|
drag.info.drag(e, ui);
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
drag.info.stop();
|
|
|
|
drag.info = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ___ drop
|
|
|
|
this.dropOptions = {
|
|
|
|
over: function(){},
|
|
|
|
out: function(){
|
|
|
|
var group = drag.info.item.parent;
|
|
|
|
if(group) {
|
|
|
|
group.remove(drag.info.$el, {dontClose: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
iQ(this).removeClass("acceptsDrop");
|
|
|
|
},
|
|
|
|
drop: function(event){
|
|
|
|
iQ(this).removeClass("acceptsDrop");
|
|
|
|
},
|
|
|
|
// Function: dropAcceptFunction
|
|
|
|
// Given a DOM element, returns true if it should accept tabs being dropped on it.
|
|
|
|
// Private to this file.
|
|
|
|
accept: function dropAcceptFunction(el) {
|
|
|
|
var $el = iQ(el);
|
|
|
|
if($el.hasClass('tab')) {
|
|
|
|
var item = Items.item($el);
|
|
|
|
if(item && (!item.parent || !item.parent.expanded)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2010-04-01 17:20:59 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: getBounds
|
|
|
|
// Returns a copy of the Item's bounds as a <Rect>.
|
2010-04-01 17:20:59 -07:00
|
|
|
getBounds: function() {
|
2010-05-11 12:03:31 -07:00
|
|
|
Utils.assert('this.bounds', isRect(this.bounds));
|
2010-04-01 17:20:59 -07:00
|
|
|
return new Rect(this.bounds);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: setPosition
|
|
|
|
// Moves the Item to the specified location.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// left - the new left coordinate relative to the window
|
|
|
|
// top - the new top coordinate relative to the window
|
|
|
|
// immediately - if false or omitted, animates to the new position;
|
|
|
|
// otherwise goes there immediately
|
2010-04-01 17:20:59 -07:00
|
|
|
setPosition: function(left, top, immediately) {
|
2010-05-11 12:03:31 -07:00
|
|
|
Utils.assert('this.bounds', isRect(this.bounds));
|
2010-04-01 17:20:59 -07:00
|
|
|
this.setBounds(new Rect(left, top, this.bounds.width, this.bounds.height), immediately);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: setSize
|
|
|
|
// Resizes the Item to the specified size.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// width - the new width in pixels
|
|
|
|
// height - the new height in pixels
|
|
|
|
// immediately - if false or omitted, animates to the new size;
|
|
|
|
// otherwise resizes immediately
|
2010-04-01 17:20:59 -07:00
|
|
|
setSize: function(width, height, immediately) {
|
2010-05-11 12:03:31 -07:00
|
|
|
Utils.assert('this.bounds', isRect(this.bounds));
|
2010-04-01 17:20:59 -07:00
|
|
|
this.setBounds(new Rect(this.bounds.left, this.bounds.top, width, height), immediately);
|
|
|
|
},
|
|
|
|
|
2010-04-26 16:48:46 -07:00
|
|
|
// ----------
|
|
|
|
// Function: setUserSize
|
|
|
|
// Remembers the current size as one the user has chosen.
|
|
|
|
setUserSize: function() {
|
2010-05-11 12:03:31 -07:00
|
|
|
Utils.assert('this.bounds', isRect(this.bounds));
|
2010-04-26 16:48:46 -07:00
|
|
|
this.userSize = new Point(this.bounds.width, this.bounds.height);
|
2010-05-24 14:49:03 -07:00
|
|
|
this.save();
|
2010-04-26 16:48:46 -07:00
|
|
|
},
|
|
|
|
|
2010-04-02 17:33:06 -07:00
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: getZ
|
|
|
|
// Returns the zIndex of the Item.
|
2010-04-02 17:33:06 -07:00
|
|
|
getZ: function() {
|
2010-05-27 17:25:14 -07:00
|
|
|
return parseInt(iQ(this.container).css('zIndex'));
|
2010-04-02 17:33:06 -07:00
|
|
|
},
|
2010-04-27 16:11:49 -07:00
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: setRotation
|
|
|
|
// Rotates the object to the given number of degrees.
|
|
|
|
setRotation: function(degrees) {
|
|
|
|
var value = "rotate(%deg)".replace(/%/, degrees);
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ(this.container).css({"-moz-transform": value});
|
2010-04-27 16:11:49 -07:00
|
|
|
},
|
2010-04-02 17:33:06 -07:00
|
|
|
|
2010-05-24 14:49:03 -07:00
|
|
|
// ----------
|
|
|
|
// Function: setParent
|
|
|
|
//
|
|
|
|
setParent: function(parent) {
|
|
|
|
this.parent = parent;
|
2010-06-15 21:08:39 -07:00
|
|
|
this.removeTrenches();
|
2010-05-24 14:49:03 -07:00
|
|
|
this.save();
|
|
|
|
},
|
|
|
|
|
2010-03-29 16:08:48 -07:00
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: pushAway
|
|
|
|
// Pushes all other items away so none overlap this Item.
|
2010-03-29 16:08:48 -07:00
|
|
|
pushAway: function() {
|
2010-05-03 15:24:22 -07:00
|
|
|
var buffer = 2;
|
2010-03-29 16:08:48 -07:00
|
|
|
|
|
|
|
var items = Items.getTopLevelItems();
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-03-29 16:08:48 -07:00
|
|
|
var data = {};
|
|
|
|
data.bounds = item.getBounds();
|
|
|
|
data.startBounds = new Rect(data.bounds);
|
|
|
|
data.generation = Infinity;
|
|
|
|
item.pushAwayData = data;
|
|
|
|
});
|
|
|
|
|
|
|
|
var itemsToPush = [this];
|
|
|
|
this.pushAwayData.generation = 0;
|
|
|
|
|
|
|
|
var pushOne = function(baseItem) {
|
|
|
|
var baseData = baseItem.pushAwayData;
|
|
|
|
var bb = new Rect(baseData.bounds);
|
|
|
|
bb.inset(-buffer, -buffer);
|
|
|
|
var bbc = bb.center();
|
|
|
|
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-05-07 15:49:54 -07:00
|
|
|
if(item == baseItem || item.locked.bounds)
|
2010-03-29 16:08:48 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var data = item.pushAwayData;
|
|
|
|
if(data.generation <= baseData.generation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bounds = data.bounds;
|
|
|
|
var box = new Rect(bounds);
|
|
|
|
box.inset(-buffer, -buffer);
|
|
|
|
if(box.intersects(bb)) {
|
|
|
|
var offset = new Point();
|
|
|
|
var center = box.center();
|
|
|
|
if(Math.abs(center.x - bbc.x) < Math.abs(center.y - bbc.y)) {
|
2010-04-16 17:21:03 -07:00
|
|
|
/* offset.x = Math.floor((Math.random() * 10) - 5); */
|
2010-03-29 16:08:48 -07:00
|
|
|
if(center.y > bbc.y)
|
|
|
|
offset.y = bb.bottom - box.top;
|
|
|
|
else
|
|
|
|
offset.y = bb.top - box.bottom;
|
|
|
|
} else {
|
2010-04-16 17:21:03 -07:00
|
|
|
/* offset.y = Math.floor((Math.random() * 10) - 5); */
|
2010-03-29 16:08:48 -07:00
|
|
|
if(center.x > bbc.x)
|
|
|
|
offset.x = bb.right - box.left;
|
|
|
|
else
|
|
|
|
offset.x = bb.left - box.right;
|
|
|
|
}
|
|
|
|
|
|
|
|
bounds.offset(offset);
|
|
|
|
data.generation = baseData.generation + 1;
|
2010-04-16 17:21:03 -07:00
|
|
|
data.pusher = baseItem;
|
2010-03-29 16:08:48 -07:00
|
|
|
itemsToPush.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
while(itemsToPush.length)
|
|
|
|
pushOne(itemsToPush.shift());
|
|
|
|
|
2010-04-16 17:21:03 -07:00
|
|
|
// ___ Squish!
|
2010-04-20 14:45:29 -07:00
|
|
|
var pageBounds = Items.getPageBounds();
|
2010-04-20 15:42:06 -07:00
|
|
|
if(Items.squishMode == 'squish') {
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-04-16 17:21:03 -07:00
|
|
|
var data = item.pushAwayData;
|
2010-05-07 15:49:54 -07:00
|
|
|
if(data.generation == 0 || item.locked.bounds)
|
2010-04-16 17:21:03 -07:00
|
|
|
return;
|
|
|
|
|
2010-04-20 11:47:54 -07:00
|
|
|
function apply(item, posStep, posStep2, sizeStep) {
|
2010-04-16 17:21:03 -07:00
|
|
|
var data = item.pushAwayData;
|
|
|
|
if(data.generation == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bounds = data.bounds;
|
|
|
|
bounds.width -= sizeStep.x;
|
|
|
|
bounds.height -= sizeStep.y;
|
|
|
|
bounds.left += posStep.x;
|
|
|
|
bounds.top += posStep.y;
|
|
|
|
|
2010-04-26 17:23:15 -07:00
|
|
|
if(!item.isAGroup) {
|
|
|
|
if(sizeStep.y > sizeStep.x) {
|
|
|
|
var newWidth = bounds.height * (TabItems.tabWidth / TabItems.tabHeight);
|
|
|
|
bounds.left += (bounds.width - newWidth) / 2;
|
|
|
|
bounds.width = newWidth;
|
|
|
|
} else {
|
|
|
|
var newHeight = bounds.width * (TabItems.tabHeight / TabItems.tabWidth);
|
|
|
|
bounds.top += (bounds.height - newHeight) / 2;
|
|
|
|
bounds.height = newHeight;
|
|
|
|
}
|
2010-04-16 17:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var pusher = data.pusher;
|
|
|
|
if(pusher)
|
|
|
|
apply(pusher, posStep.plus(posStep2), posStep2, sizeStep);
|
|
|
|
}
|
|
|
|
|
|
|
|
var bounds = data.bounds;
|
|
|
|
var posStep = new Point();
|
|
|
|
var posStep2 = new Point();
|
|
|
|
var sizeStep = new Point();
|
2010-04-20 11:47:54 -07:00
|
|
|
|
|
|
|
if(bounds.left < pageBounds.left) {
|
|
|
|
posStep.x = pageBounds.left - bounds.left;
|
|
|
|
sizeStep.x = posStep.x / data.generation;
|
|
|
|
posStep2.x = -sizeStep.x;
|
|
|
|
} else if(bounds.right > pageBounds.right) {
|
|
|
|
posStep.x = pageBounds.right - bounds.right;
|
|
|
|
sizeStep.x = -posStep.x / data.generation;
|
|
|
|
posStep.x += sizeStep.x;
|
|
|
|
posStep2.x = sizeStep.x;
|
|
|
|
}
|
|
|
|
|
2010-04-16 17:21:03 -07:00
|
|
|
if(bounds.top < pageBounds.top) {
|
|
|
|
posStep.y = pageBounds.top - bounds.top;
|
|
|
|
sizeStep.y = posStep.y / data.generation;
|
|
|
|
posStep2.y = -sizeStep.y;
|
|
|
|
} else if(bounds.bottom > pageBounds.bottom) {
|
|
|
|
posStep.y = pageBounds.bottom - bounds.bottom;
|
|
|
|
sizeStep.y = -posStep.y / data.generation;
|
|
|
|
posStep.y += sizeStep.y;
|
|
|
|
posStep2.y = sizeStep.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(posStep.x || posStep.y || sizeStep.x || sizeStep.y)
|
|
|
|
apply(item, posStep, posStep2, sizeStep);
|
|
|
|
});
|
2010-04-20 15:42:06 -07:00
|
|
|
} else if(Items.squishMode == 'all') {
|
|
|
|
var newPageBounds = null;
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-05-07 15:49:54 -07:00
|
|
|
if(item.locked.bounds)
|
2010-04-20 15:42:06 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var data = item.pushAwayData;
|
|
|
|
var bounds = data.bounds;
|
|
|
|
newPageBounds = (newPageBounds ? newPageBounds.union(bounds) : new Rect(bounds));
|
|
|
|
});
|
|
|
|
|
|
|
|
var wScale = pageBounds.width / newPageBounds.width;
|
|
|
|
var hScale = pageBounds.height / newPageBounds.height;
|
|
|
|
var scale = Math.min(hScale, wScale);
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-05-07 15:49:54 -07:00
|
|
|
if(item.locked.bounds)
|
2010-04-20 15:42:06 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
var data = item.pushAwayData;
|
|
|
|
var bounds = data.bounds;
|
|
|
|
|
|
|
|
bounds.left -= newPageBounds.left;
|
|
|
|
bounds.left *= scale;
|
|
|
|
bounds.width *= scale;
|
|
|
|
|
|
|
|
bounds.top -= newPageBounds.top;
|
|
|
|
bounds.top *= scale;
|
|
|
|
bounds.height *= scale;
|
|
|
|
});
|
2010-04-16 17:21:03 -07:00
|
|
|
}
|
|
|
|
|
2010-04-20 14:45:29 -07:00
|
|
|
// ___ Unsquish
|
2010-05-07 15:49:54 -07:00
|
|
|
var pairs = [];
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-04-20 14:45:29 -07:00
|
|
|
var data = item.pushAwayData;
|
2010-05-07 15:49:54 -07:00
|
|
|
pairs.push({
|
|
|
|
item: item,
|
|
|
|
bounds: data.bounds
|
|
|
|
});
|
2010-04-20 14:45:29 -07:00
|
|
|
});
|
2010-05-07 15:49:54 -07:00
|
|
|
|
|
|
|
Items.unsquish(pairs);
|
2010-04-20 14:45:29 -07:00
|
|
|
|
2010-04-16 17:21:03 -07:00
|
|
|
// ___ Apply changes
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-03-29 16:08:48 -07:00
|
|
|
var data = item.pushAwayData;
|
2010-04-16 17:21:03 -07:00
|
|
|
var bounds = data.bounds;
|
|
|
|
if(!bounds.equals(data.startBounds)) {
|
|
|
|
item.setBounds(bounds);
|
|
|
|
}
|
2010-03-29 16:08:48 -07:00
|
|
|
});
|
2010-04-01 17:20:59 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: _updateDebugBounds
|
|
|
|
// Called by a subclass when its bounds change, to update the debugging rectangles on screen.
|
|
|
|
// This functionality is enabled only by the debug property.
|
2010-04-01 17:20:59 -07:00
|
|
|
_updateDebugBounds: function() {
|
|
|
|
if(this.$debug) {
|
|
|
|
this.$debug.css({
|
|
|
|
left: this.bounds.left,
|
|
|
|
top: this.bounds.top,
|
|
|
|
width: this.bounds.width,
|
|
|
|
height: this.bounds.height
|
|
|
|
});
|
|
|
|
}
|
2010-06-15 16:08:21 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
setTrenches: function(rect) {
|
|
|
|
|
2010-06-15 21:08:39 -07:00
|
|
|
if (this.parent !== null)
|
|
|
|
return;
|
|
|
|
|
2010-06-15 16:08:21 -07:00
|
|
|
var container = this.container;
|
|
|
|
|
|
|
|
if (!this.borderTrenches) {
|
|
|
|
var bT = this.borderTrenches = {};
|
|
|
|
bT.left = Trenches.register(container,"x","border","left");
|
|
|
|
bT.right = Trenches.register(container,"x","border","right");
|
|
|
|
bT.top = Trenches.register(container,"y","border","top");
|
|
|
|
bT.bottom = Trenches.register(container,"y","border","bottom");
|
|
|
|
}
|
|
|
|
var bT = this.borderTrenches;
|
2010-06-15 21:08:39 -07:00
|
|
|
Trenches.getById(bT.left).setWithRect(rect);
|
|
|
|
Trenches.getById(bT.right).setWithRect(rect);
|
|
|
|
Trenches.getById(bT.top).setWithRect(rect);
|
|
|
|
Trenches.getById(bT.bottom).setWithRect(rect);
|
2010-06-15 16:08:21 -07:00
|
|
|
|
|
|
|
if (!this.guideTrenches) {
|
|
|
|
var gT = this.guideTrenches = {};
|
|
|
|
gT.left = Trenches.register(container,"x","guide","left");
|
|
|
|
gT.right = Trenches.register(container,"x","guide","right");
|
|
|
|
gT.top = Trenches.register(container,"y","guide","top");
|
|
|
|
gT.bottom = Trenches.register(container,"y","guide","bottom");
|
|
|
|
}
|
|
|
|
var gT = this.guideTrenches;
|
2010-06-15 21:08:39 -07:00
|
|
|
Trenches.getById(gT.left).setWithRect(rect);
|
|
|
|
Trenches.getById(gT.right).setWithRect(rect);
|
|
|
|
Trenches.getById(gT.top).setWithRect(rect);
|
|
|
|
Trenches.getById(gT.bottom).setWithRect(rect);
|
2010-06-15 16:08:21 -07:00
|
|
|
|
|
|
|
},
|
2010-06-15 21:08:39 -07:00
|
|
|
removeTrenches: function() {
|
|
|
|
for (let edge in this.borderTrenches) {
|
|
|
|
Trenches.unregister(this.borderTrenches[edge]); // unregister can take an array
|
|
|
|
}
|
|
|
|
this.borderTrenches = null;
|
|
|
|
for (let edge in this.guideTrenches) {
|
|
|
|
Trenches.unregister(this.guideTrenches[edge]); // unregister can take an array
|
|
|
|
}
|
|
|
|
this.guideTrenches = null;
|
|
|
|
}
|
2010-03-29 16:08:48 -07:00
|
|
|
};
|
|
|
|
|
2010-03-29 11:55:13 -07:00
|
|
|
// ##########
|
2010-04-08 16:36:11 -07:00
|
|
|
// Class: Items
|
|
|
|
// Keeps track of all Items.
|
2010-03-29 11:55:13 -07:00
|
|
|
window.Items = {
|
2010-04-20 15:42:06 -07:00
|
|
|
// ----------
|
|
|
|
// Variable: squishMode
|
|
|
|
// How to deal when things go off the edge.
|
|
|
|
// Options include: all, squish, push
|
|
|
|
squishMode: 'squish',
|
|
|
|
|
2010-04-20 14:45:29 -07:00
|
|
|
// ----------
|
|
|
|
// Function: init
|
|
|
|
// Initialize the object
|
|
|
|
init: function() {
|
|
|
|
},
|
|
|
|
|
2010-03-29 16:08:48 -07:00
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: item
|
|
|
|
// Given a DOM element representing an Item, returns the Item.
|
2010-03-29 16:08:48 -07:00
|
|
|
item: function(el) {
|
2010-05-27 17:25:14 -07:00
|
|
|
return iQ(el).data('item');
|
2010-03-29 16:08:48 -07:00
|
|
|
},
|
|
|
|
|
2010-03-29 11:55:13 -07:00
|
|
|
// ----------
|
2010-04-08 16:36:11 -07:00
|
|
|
// Function: getTopLevelItems
|
|
|
|
// Returns an array of all Items not grouped into groups.
|
2010-03-29 11:55:13 -07:00
|
|
|
getTopLevelItems: function() {
|
|
|
|
var items = [];
|
|
|
|
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ('.tab, .group').each(function() {
|
|
|
|
var $this = iQ(this);
|
2010-04-26 13:37:39 -07:00
|
|
|
var item = $this.data('item');
|
2010-05-03 15:32:11 -07:00
|
|
|
if(item && !item.parent && !$this.hasClass('phantom'))
|
2010-04-26 13:37:39 -07:00
|
|
|
items.push(item);
|
2010-03-29 11:55:13 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return items;
|
2010-04-14 17:12:06 -07:00
|
|
|
},
|
|
|
|
|
2010-04-16 17:21:03 -07:00
|
|
|
// ----------
|
|
|
|
// Function: getPageBounds
|
|
|
|
// Returns a <Rect> defining the area of the page <Item>s should stay within.
|
|
|
|
getPageBounds: function() {
|
2010-05-06 17:01:53 -07:00
|
|
|
var top = 0;
|
2010-05-24 17:14:10 -07:00
|
|
|
var bottom = TabItems.tabHeight + 10; // MAGIC NUMBER: giving room for the "new tabs" group
|
2010-04-26 16:48:46 -07:00
|
|
|
var width = Math.max(100, window.innerWidth);
|
|
|
|
var height = Math.max(100, window.innerHeight - (top + bottom));
|
|
|
|
return new Rect(0, top, width, height);
|
2010-04-16 17:21:03 -07:00
|
|
|
},
|
|
|
|
|
2010-04-14 17:12:06 -07:00
|
|
|
// ----------
|
|
|
|
// Function: arrange
|
|
|
|
// Arranges the given items in a grid within the given bounds,
|
|
|
|
// maximizing item size but maintaining standard tab aspect ratio for each
|
|
|
|
//
|
|
|
|
// Parameters:
|
2010-05-03 17:11:44 -07:00
|
|
|
// items - an array of <Item>s. Can be null if the pretend and count options are set.
|
2010-04-14 17:12:06 -07:00
|
|
|
// bounds - a <Rect> defining the space to arrange within
|
2010-05-13 17:24:37 -07:00
|
|
|
// options - an object with various properites (see below)
|
|
|
|
//
|
|
|
|
// Possible "options" properties:
|
|
|
|
// animate - whether to animate; default: true.
|
|
|
|
// z - the z index to set all the items; default: don't change z.
|
|
|
|
// pretend - whether to collect and return the rectangle rather than moving the items; default: false
|
|
|
|
// count - overrides the item count for layout purposes; default: the actual item count
|
|
|
|
// padding - pixels between each item
|
2010-05-03 17:11:44 -07:00
|
|
|
//
|
2010-05-13 17:24:37 -07:00
|
|
|
// Returns:
|
|
|
|
// the list of rectangles if the pretend option is set; otherwise null
|
2010-04-14 17:12:06 -07:00
|
|
|
arrange: function(items, bounds, options) {
|
|
|
|
var animate;
|
|
|
|
if(!options || typeof(options.animate) == 'undefined')
|
|
|
|
animate = true;
|
|
|
|
else
|
|
|
|
animate = options.animate;
|
|
|
|
|
|
|
|
if(typeof(options) == 'undefined')
|
|
|
|
options = {};
|
|
|
|
|
2010-05-03 17:11:44 -07:00
|
|
|
var rects = null;
|
|
|
|
if(options.pretend)
|
|
|
|
rects = [];
|
|
|
|
|
2010-04-14 17:12:06 -07:00
|
|
|
var tabAspect = TabItems.tabHeight / TabItems.tabWidth;
|
2010-05-03 17:11:44 -07:00
|
|
|
var count = options.count || (items ? items.length : 0);
|
2010-04-14 17:12:06 -07:00
|
|
|
if(!count)
|
2010-05-03 17:11:44 -07:00
|
|
|
return rects;
|
2010-04-14 17:12:06 -07:00
|
|
|
|
|
|
|
var columns = 1;
|
|
|
|
var padding = options.padding || 0;
|
|
|
|
var yScale = 1.1; // to allow for titles
|
|
|
|
var rows;
|
|
|
|
var tabWidth;
|
|
|
|
var tabHeight;
|
|
|
|
var totalHeight;
|
|
|
|
|
|
|
|
function figure() {
|
|
|
|
rows = Math.ceil(count / columns);
|
|
|
|
tabWidth = (bounds.width - (padding * (columns - 1))) / columns;
|
|
|
|
tabHeight = tabWidth * tabAspect;
|
|
|
|
totalHeight = (tabHeight * yScale * rows) + (padding * (rows - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
figure();
|
|
|
|
|
|
|
|
while(rows > 1 && totalHeight > bounds.height) {
|
|
|
|
columns++;
|
|
|
|
figure();
|
|
|
|
}
|
|
|
|
|
2010-06-16 16:30:48 -07:00
|
|
|
if(rows == 1) {
|
2010-06-15 13:48:37 -07:00
|
|
|
var maxWidth = Math.max(TabItems.tabWidth, bounds.width / 2);
|
|
|
|
tabWidth = Math.min(Math.min(maxWidth, bounds.width / count), bounds.height / tabAspect);
|
2010-04-14 17:12:06 -07:00
|
|
|
tabHeight = tabWidth * tabAspect;
|
|
|
|
}
|
|
|
|
|
|
|
|
var box = new Rect(bounds.left, bounds.top, tabWidth, tabHeight);
|
|
|
|
var row = 0;
|
|
|
|
var column = 0;
|
|
|
|
var immediately;
|
|
|
|
|
2010-05-03 17:11:44 -07:00
|
|
|
var a;
|
|
|
|
for(a = 0; a < count; a++) {
|
2010-04-14 17:12:06 -07:00
|
|
|
/*
|
|
|
|
if(animate == 'sometimes')
|
|
|
|
immediately = (typeof(item.groupData.row) == 'undefined' || item.groupData.row == row);
|
|
|
|
else
|
|
|
|
*/
|
|
|
|
immediately = !animate;
|
|
|
|
|
2010-05-03 17:11:44 -07:00
|
|
|
if(rects)
|
|
|
|
rects.push(new Rect(box));
|
|
|
|
else if(items && a < items.length) {
|
|
|
|
var item = items[a];
|
2010-05-07 15:49:54 -07:00
|
|
|
if(!item.locked.bounds) {
|
2010-05-03 17:11:44 -07:00
|
|
|
item.setBounds(box, immediately);
|
|
|
|
item.setRotation(0);
|
|
|
|
if(options.z)
|
|
|
|
item.setZ(options.z);
|
|
|
|
}
|
2010-04-28 17:18:02 -07:00
|
|
|
}
|
|
|
|
|
2010-04-14 17:12:06 -07:00
|
|
|
/*
|
|
|
|
item.groupData.column = column;
|
|
|
|
item.groupData.row = row;
|
|
|
|
*/
|
|
|
|
|
|
|
|
box.left += box.width + padding;
|
|
|
|
column++;
|
|
|
|
if(column == columns) {
|
|
|
|
box.left = bounds.left;
|
|
|
|
box.top += (box.height * yScale) + padding;
|
|
|
|
column = 0;
|
|
|
|
row++;
|
|
|
|
}
|
2010-05-03 17:11:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rects;
|
2010-05-07 15:49:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: unsquish
|
|
|
|
// Checks to see which items can now be unsquished.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// pairs - an array of objects, each with two properties: item and bounds. The bounds are
|
|
|
|
// modified as appropriate, but the items are not changed. If pairs is null, the
|
|
|
|
// operation is performed directly on all of the top level items.
|
|
|
|
// ignore - an <Item> to not include in calculations (because it's about to be closed, for instance)
|
|
|
|
unsquish: function(pairs, ignore) {
|
|
|
|
var pairsProvided = (pairs ? true : false);
|
|
|
|
if(!pairsProvided) {
|
|
|
|
var items = Items.getTopLevelItems();
|
|
|
|
pairs = [];
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(items, function(index, item) {
|
2010-05-07 15:49:54 -07:00
|
|
|
pairs.push({
|
|
|
|
item: item,
|
|
|
|
bounds: item.getBounds()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var pageBounds = Items.getPageBounds();
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(pairs, function(index, pair) {
|
2010-05-07 15:49:54 -07:00
|
|
|
var item = pair.item;
|
|
|
|
if(item.locked.bounds || item == ignore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bounds = pair.bounds;
|
|
|
|
var newBounds = new Rect(bounds);
|
|
|
|
|
|
|
|
var newSize;
|
2010-05-11 12:03:31 -07:00
|
|
|
if(isPoint(item.userSize))
|
2010-05-07 15:49:54 -07:00
|
|
|
newSize = new Point(item.userSize);
|
|
|
|
else
|
|
|
|
newSize = new Point(TabItems.tabWidth, TabItems.tabHeight);
|
|
|
|
|
|
|
|
if(item.isAGroup) {
|
|
|
|
newBounds.width = Math.max(newBounds.width, newSize.x);
|
|
|
|
newBounds.height = Math.max(newBounds.height, newSize.y);
|
|
|
|
} else {
|
|
|
|
if(bounds.width < newSize.x) {
|
|
|
|
newBounds.width = newSize.x;
|
|
|
|
newBounds.height = newSize.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newBounds.left -= (newBounds.width - bounds.width) / 2;
|
|
|
|
newBounds.top -= (newBounds.height - bounds.height) / 2;
|
|
|
|
|
|
|
|
var offset = new Point();
|
|
|
|
if(newBounds.left < pageBounds.left)
|
|
|
|
offset.x = pageBounds.left - newBounds.left;
|
|
|
|
else if(newBounds.right > pageBounds.right)
|
|
|
|
offset.x = pageBounds.right - newBounds.right;
|
|
|
|
|
|
|
|
if(newBounds.top < pageBounds.top)
|
|
|
|
offset.y = pageBounds.top - newBounds.top;
|
|
|
|
else if(newBounds.bottom > pageBounds.bottom)
|
|
|
|
offset.y = pageBounds.bottom - newBounds.bottom;
|
|
|
|
|
|
|
|
newBounds.offset(offset);
|
|
|
|
|
|
|
|
if(!bounds.equals(newBounds)) {
|
|
|
|
var blocked = false;
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(pairs, function(index, pair2) {
|
2010-05-07 15:49:54 -07:00
|
|
|
if(pair2 == pair || pair2.item == ignore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var bounds2 = pair2.bounds;
|
|
|
|
if(bounds2.intersects(newBounds)) {
|
|
|
|
blocked = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!blocked) {
|
|
|
|
pair.bounds.copy(newBounds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!pairsProvided) {
|
2010-05-27 17:25:14 -07:00
|
|
|
iQ.each(pairs, function(index, pair) {
|
2010-05-07 15:49:54 -07:00
|
|
|
pair.item.setBounds(pair.bounds);
|
|
|
|
});
|
|
|
|
}
|
2010-03-29 11:55:13 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-04-20 14:45:29 -07:00
|
|
|
window.Items.init();
|
|
|
|
|