2010-04-22 17:19:42 -07:00
|
|
|
// Title: groups.js (revision-a)
|
2010-03-17 01:07:00 -07:00
|
|
|
(function(){
|
|
|
|
|
|
|
|
var numCmp = function(a,b){ return a-b; }
|
|
|
|
|
|
|
|
function min(list){ return list.slice().sort(numCmp)[0]; }
|
|
|
|
function max(list){ return list.slice().sort(numCmp).reverse()[0]; }
|
|
|
|
|
2010-03-18 21:49:24 -07:00
|
|
|
function isEventOverElement(event, el){
|
|
|
|
var hit = {nodeName: null};
|
|
|
|
var isOver = false;
|
|
|
|
|
|
|
|
var hiddenEls = [];
|
|
|
|
while(hit.nodeName != "BODY" && hit.nodeName != "HTML"){
|
|
|
|
hit = document.elementFromPoint(event.clientX, event.clientY);
|
|
|
|
if( hit == el ){
|
|
|
|
isOver = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$(hit).hide();
|
|
|
|
hiddenEls.push(hit);
|
|
|
|
}
|
|
|
|
|
|
|
|
var hidden;
|
|
|
|
[$(hidden).show() for([,hidden] in Iterator(hiddenEls))];
|
|
|
|
return isOver;
|
|
|
|
}
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ##########
|
2010-04-22 17:19:42 -07:00
|
|
|
// Class: Group
|
|
|
|
// A single group in the tab candy window. Descended from <Item>.
|
|
|
|
// Note that it implements the <Subscribable> interface.
|
|
|
|
window.Group = function(listOfEls, options) {
|
|
|
|
if(typeof(options) == 'undefined')
|
|
|
|
options = {};
|
|
|
|
|
|
|
|
this._children = []; // an array of Items
|
|
|
|
this.defaultSize = new Point(TabItems.tabWidth * 1.5, TabItems.tabHeight * 1.5);
|
|
|
|
this.locked = options.locked || false;
|
|
|
|
this.isAGroup = true;
|
2010-04-26 13:37:39 -07:00
|
|
|
this.id = options.id || Groups.getNextID();
|
2010-04-26 16:48:46 -07:00
|
|
|
this.userSize = options.userSize || null;
|
2010-04-27 16:11:49 -07:00
|
|
|
this._isStacked = false;
|
|
|
|
this._stackAngles = [0];
|
2010-04-28 17:18:02 -07:00
|
|
|
this.expanded = null;
|
2010-04-22 17:19:42 -07:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2010-04-23 17:11:06 -07:00
|
|
|
var rectToBe;
|
|
|
|
if(options.bounds)
|
|
|
|
rectToBe = new Rect(options.bounds);
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(!rectToBe) {
|
|
|
|
var boundingBox = this._getBoundingBox(listOfEls);
|
|
|
|
var padding = 30;
|
|
|
|
rectToBe = new Rect(
|
|
|
|
boundingBox.left-padding,
|
|
|
|
boundingBox.top-padding,
|
|
|
|
boundingBox.width+padding*2,
|
|
|
|
boundingBox.height+padding*2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var $container = options.container;
|
|
|
|
if(!$container) {
|
|
|
|
$container = $('<div class="group" />')
|
|
|
|
.css({position: 'absolute'})
|
|
|
|
.css(rectToBe);
|
|
|
|
}
|
2010-04-14 17:12:06 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
$container
|
|
|
|
.css({zIndex: -100})
|
|
|
|
.appendTo("body")
|
|
|
|
.dequeue();
|
|
|
|
|
2010-05-03 17:11:44 -07:00
|
|
|
// ___ New Tab Button
|
2010-05-04 14:55:17 -07:00
|
|
|
this.$ntb = $("<div class='newTabButton'/>").appendTo($container);
|
2010-05-04 13:52:25 -07:00
|
|
|
this.$ntb.click(function(){
|
2010-05-04 14:55:17 -07:00
|
|
|
self.newTab();
|
2010-05-04 13:52:25 -07:00
|
|
|
});
|
2010-05-03 15:24:22 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ___ Resizer
|
|
|
|
this.$resizer = $("<div class='resizer'/>")
|
|
|
|
.css({
|
|
|
|
position: "absolute",
|
|
|
|
width: 16, height: 16,
|
|
|
|
bottom: 0, right: 0,
|
|
|
|
})
|
|
|
|
.appendTo($container)
|
|
|
|
.hide();
|
|
|
|
|
|
|
|
// ___ Titlebar
|
|
|
|
var html = "<div class='titlebar'><input class='name' value='"
|
|
|
|
+ (options.title || "")
|
|
|
|
+ "'/><div class='close'></div></div>";
|
|
|
|
|
|
|
|
this.$titlebar = $(html)
|
|
|
|
.appendTo($container);
|
|
|
|
|
|
|
|
this.$titlebar.css({
|
|
|
|
position: "absolute",
|
|
|
|
});
|
|
|
|
|
|
|
|
var $close = $('.close', this.$titlebar).click(function() {
|
2010-04-26 16:48:46 -07:00
|
|
|
self.closeAll();
|
2010-04-22 17:19:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// ___ Title
|
|
|
|
var titleUnfocus = function() {
|
|
|
|
if(!self.getTitle()) {
|
|
|
|
self.$title
|
|
|
|
.addClass("defaultName")
|
|
|
|
.val(self.defaultName);
|
|
|
|
} else {
|
|
|
|
self.$title.css({"background":"none"})
|
2010-04-28 20:12:34 -07:00
|
|
|
.animate({"paddingLeft":1}, 340, "tabcandyBounce");
|
2010-04-22 09:38:37 -07:00
|
|
|
}
|
2010-04-22 17:19:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
var handleKeyPress = function(e){
|
|
|
|
if( e.which == 13 ) { // return
|
|
|
|
self.$title.blur()
|
|
|
|
.addClass("transparentBorder")
|
|
|
|
.one("mouseout", function(){
|
|
|
|
self.$title.removeClass("transparentBorder");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$title = $('.name', this.$titlebar)
|
|
|
|
.css({backgroundRepeat: 'no-repeat'})
|
|
|
|
.blur(titleUnfocus)
|
|
|
|
.focus(function() {
|
|
|
|
if(self.locked) {
|
|
|
|
self.$title.blur();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.$title.select();
|
|
|
|
if(!self.getTitle()) {
|
|
|
|
self.$title
|
|
|
|
.removeClass("defaultName")
|
|
|
|
.val('');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.keydown(handleKeyPress);
|
|
|
|
|
|
|
|
titleUnfocus();
|
|
|
|
|
|
|
|
if(this.locked)
|
|
|
|
this.$title.addClass('name-locked');
|
|
|
|
|
|
|
|
// ___ Content
|
|
|
|
this.$content = $('<div class="group-content"/>')
|
|
|
|
.css({
|
|
|
|
left: 0,
|
|
|
|
top: this.$titlebar.height(),
|
|
|
|
position: 'absolute'
|
|
|
|
})
|
|
|
|
.appendTo($container);
|
|
|
|
|
|
|
|
// ___ locking
|
|
|
|
if(this.locked) {
|
|
|
|
$container.css({cursor: 'default'});
|
2010-04-26 16:48:46 -07:00
|
|
|
/* $close.hide(); */
|
2010-04-22 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ___ Superclass initialization
|
|
|
|
this._init($container.get(0));
|
|
|
|
|
|
|
|
if(this.$debug)
|
|
|
|
this.$debug.css({zIndex: -1000});
|
|
|
|
|
|
|
|
// ___ Children
|
|
|
|
$.each(listOfEls, function(index, el) {
|
|
|
|
self.add(el, null, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
// ___ Finish Up
|
|
|
|
this._addHandlers($container);
|
2010-04-30 17:24:03 -07:00
|
|
|
|
|
|
|
if(!this.locked)
|
|
|
|
this.setResizable(true);
|
2010-04-22 17:19:42 -07:00
|
|
|
|
|
|
|
Groups.register(this);
|
|
|
|
|
|
|
|
this.setBounds(rectToBe);
|
|
|
|
|
|
|
|
// ___ Push other objects away
|
|
|
|
if(!options.dontPush)
|
|
|
|
this.pushAway();
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
window.Group.prototype = $.extend(new Item(), new Subscribable(), {
|
|
|
|
// ----------
|
|
|
|
defaultName: "name this group...",
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
getStorageData: function() {
|
|
|
|
var data = {
|
2010-04-28 17:18:02 -07:00
|
|
|
bounds: this.getBounds(),
|
2010-04-26 16:48:46 -07:00
|
|
|
userSize: this.userSize,
|
2010-04-22 17:19:42 -07:00
|
|
|
locked: this.locked,
|
2010-04-26 13:37:39 -07:00
|
|
|
title: this.getTitle(),
|
|
|
|
id: this.id
|
2010-04-22 17:19:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
2010-04-14 17:12:06 -07:00
|
|
|
},
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
getTitle: function() {
|
|
|
|
var value = (this.$title ? this.$title.val() : '');
|
|
|
|
return (value == this.defaultName ? '' : value);
|
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-22 17:19:42 -07:00
|
|
|
setValue: function(value) {
|
|
|
|
this.$title.val(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
_getBoundingBox: function(els) {
|
2010-03-17 01:07:00 -07:00
|
|
|
var el;
|
|
|
|
var boundingBox = {
|
|
|
|
top: min( [$(el).position().top for([,el] in Iterator(els))] ),
|
|
|
|
left: min( [$(el).position().left for([,el] in Iterator(els))] ),
|
|
|
|
bottom: max( [$(el).position().top for([,el] in Iterator(els))] ) + $(els[0]).height(),
|
|
|
|
right: max( [$(el).position().left for([,el] in Iterator(els))] ) + $(els[0]).width(),
|
|
|
|
};
|
|
|
|
boundingBox.height = boundingBox.bottom - boundingBox.top;
|
|
|
|
boundingBox.width = boundingBox.right - boundingBox.left;
|
|
|
|
return boundingBox;
|
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-22 17:19:42 -07:00
|
|
|
getContentBounds: function() {
|
|
|
|
var box = this.getBounds();
|
|
|
|
var titleHeight = this.$titlebar.height();
|
|
|
|
box.top += titleHeight;
|
|
|
|
box.height -= titleHeight;
|
2010-05-03 17:11:44 -07:00
|
|
|
box.inset(6, 6);
|
|
|
|
box.height -= 15; // For new tab button
|
2010-04-22 17:19:42 -07:00
|
|
|
return box;
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-22 17:19:42 -07:00
|
|
|
reloadBounds: function() {
|
|
|
|
var bb = Utils.getBounds(this.container);
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(!this.bounds)
|
|
|
|
this.bounds = new Rect(0, 0, 0, 0);
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
this.setBounds(bb, true);
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-22 17:19:42 -07:00
|
|
|
setBounds: function(rect, immediately) {
|
|
|
|
var titleHeight = this.$titlebar.height();
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ___ Determine what has changed
|
|
|
|
var css = {};
|
|
|
|
var titlebarCSS = {};
|
|
|
|
var contentCSS = {};
|
2010-04-01 17:20:59 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(rect.left != this.bounds.left)
|
|
|
|
css.left = rect.left;
|
|
|
|
|
|
|
|
if(rect.top != this.bounds.top)
|
|
|
|
css.top = rect.top;
|
|
|
|
|
|
|
|
if(rect.width != this.bounds.width) {
|
|
|
|
css.width = rect.width;
|
|
|
|
titlebarCSS.width = rect.width;
|
|
|
|
contentCSS.width = rect.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rect.height != this.bounds.height) {
|
|
|
|
css.height = rect.height;
|
|
|
|
contentCSS.height = rect.height - titleHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($.isEmptyObject(css))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var offset = new Point(rect.left - this.bounds.left, rect.top - this.bounds.top);
|
|
|
|
this.bounds = new Rect(rect);
|
|
|
|
|
|
|
|
// ___ Deal with children
|
|
|
|
if(this._children.length) {
|
|
|
|
if(css.width || css.height) {
|
|
|
|
this.arrange({animate: !immediately}); //(immediately ? 'sometimes' : true)});
|
|
|
|
} else if(css.left || css.top) {
|
|
|
|
$.each(this._children, function(index, child) {
|
|
|
|
var box = child.getBounds();
|
|
|
|
child.setPosition(box.left + offset.x, box.top + offset.y, immediately);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ___ Update our representation
|
|
|
|
if(immediately) {
|
|
|
|
$(this.container).css(css);
|
|
|
|
this.$titlebar.css(titlebarCSS);
|
|
|
|
this.$content.css(contentCSS);
|
|
|
|
} else {
|
|
|
|
TabMirror.pausePainting();
|
2010-04-28 20:12:34 -07:00
|
|
|
$(this.container).animate(css, {
|
|
|
|
complete: function() {TabMirror.resumePainting();},
|
|
|
|
easing: "tabcandyBounce"
|
|
|
|
}).dequeue();
|
2010-04-22 17:19:42 -07:00
|
|
|
|
|
|
|
this.$titlebar.animate(titlebarCSS).dequeue();
|
|
|
|
this.$content.animate(contentCSS).dequeue();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateDebugBounds();
|
2010-04-01 17:20:59 -07:00
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
setZ: function(value) {
|
|
|
|
$(this.container).css({zIndex: value});
|
|
|
|
|
|
|
|
if(this.$debug)
|
|
|
|
this.$debug.css({zIndex: value + 1});
|
|
|
|
|
2010-04-30 17:24:03 -07:00
|
|
|
var count = this._children.length;
|
|
|
|
if(count) {
|
|
|
|
var zIndex = value + count + 1;
|
|
|
|
$.each(this._children, function(index, child) {
|
|
|
|
child.setZ(zIndex);
|
|
|
|
zIndex--;
|
|
|
|
});
|
|
|
|
}
|
2010-04-02 17:33:06 -07:00
|
|
|
},
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-26 16:48:46 -07:00
|
|
|
// ----------
|
2010-04-22 17:19:42 -07:00
|
|
|
close: function() {
|
2010-04-26 16:48:46 -07:00
|
|
|
this.removeAll();
|
|
|
|
this._sendOnClose();
|
|
|
|
Groups.unregister(this);
|
|
|
|
$(this.container).fadeOut(function() {
|
|
|
|
$(this).remove();
|
2010-03-29 16:08:48 -07:00
|
|
|
});
|
|
|
|
},
|
2010-04-26 16:48:46 -07:00
|
|
|
|
|
|
|
// ----------
|
|
|
|
closeAll: function() {
|
|
|
|
if(this._children.length) {
|
|
|
|
var toClose = $.merge([], this._children);
|
|
|
|
$.each(toClose, function(index, child) {
|
|
|
|
child.close();
|
|
|
|
});
|
2010-04-30 17:24:03 -07:00
|
|
|
} else if(!this.locked)
|
2010-04-26 16:48:46 -07:00
|
|
|
this.close();
|
|
|
|
},
|
2010-04-26 13:37:39 -07:00
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-26 13:37:39 -07:00
|
|
|
// Function: add
|
|
|
|
// Adds an item to the group.
|
|
|
|
// Parameters:
|
|
|
|
//
|
|
|
|
// a - The item to add. Can be an <Item>, a DOM element or a jQuery object.
|
|
|
|
// The latter two must refer to the container of an <Item>.
|
|
|
|
// dropPos - An object with left and top properties referring to the location dropped at. Optional.
|
|
|
|
// options - An object with optional settings for this call. Currently the only one is dontArrange.
|
|
|
|
add: function(a, dropPos, options) {
|
|
|
|
var item;
|
|
|
|
var $el;
|
|
|
|
if(a.isAnItem) {
|
|
|
|
item = a;
|
|
|
|
$el = $(a.container);
|
|
|
|
} else {
|
|
|
|
$el = $(a);
|
|
|
|
item = Items.item($el);
|
|
|
|
}
|
2010-03-17 01:07:00 -07:00
|
|
|
|
2010-05-03 15:32:11 -07:00
|
|
|
Utils.assert('shouldn\'t already be in a group', !item.parent || item.parent == this);
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(!dropPos)
|
|
|
|
dropPos = {top:window.innerWidth, left:window.innerHeight};
|
|
|
|
|
|
|
|
if(typeof(options) == 'undefined')
|
|
|
|
options = {};
|
|
|
|
|
2010-03-24 14:38:23 -07:00
|
|
|
var self = this;
|
2010-03-24 16:27:56 -07:00
|
|
|
|
2010-05-04 16:44:03 -07:00
|
|
|
var wasAlreadyInThisGroup = false;
|
|
|
|
var oldIndex = $.inArray(item, this._children);
|
|
|
|
if(oldIndex != -1) {
|
|
|
|
this._children.splice(oldIndex, 1);
|
|
|
|
wasAlreadyInThisGroup = true;
|
|
|
|
}
|
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// TODO: You should be allowed to drop in the white space at the bottom and have it go to the end
|
|
|
|
// (right now it can match the thumbnail above it and go there)
|
2010-03-24 16:27:56 -07:00
|
|
|
function findInsertionPoint(dropPos){
|
2010-05-04 16:44:03 -07:00
|
|
|
if(self.shouldStack(self._children.length + 1))
|
2010-05-03 15:32:11 -07:00
|
|
|
return 0;
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
var best = {dist: Infinity, item: null};
|
2010-03-24 16:27:56 -07:00
|
|
|
var index = 0;
|
2010-04-22 17:19:42 -07:00
|
|
|
var box;
|
2010-04-27 16:11:49 -07:00
|
|
|
$.each(self._children, function(index, child) {
|
2010-04-22 17:19:42 -07:00
|
|
|
box = child.getBounds();
|
2010-04-27 16:11:49 -07:00
|
|
|
if(box.bottom < dropPos.top || box.top > dropPos.top)
|
|
|
|
return;
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
var dist = Math.sqrt( Math.pow((box.top+box.height/2)-dropPos.top,2) + Math.pow((box.left+box.width/2)-dropPos.left,2) );
|
2010-03-24 16:27:56 -07:00
|
|
|
if( dist <= best.dist ){
|
2010-04-22 17:19:42 -07:00
|
|
|
best.item = child;
|
2010-03-24 16:27:56 -07:00
|
|
|
best.dist = dist;
|
|
|
|
best.index = index;
|
|
|
|
}
|
2010-04-27 16:11:49 -07:00
|
|
|
});
|
2010-03-24 16:27:56 -07:00
|
|
|
|
|
|
|
if( self._children.length > 0 ){
|
2010-04-27 16:11:49 -07:00
|
|
|
if(best.item) {
|
|
|
|
box = best.item.getBounds();
|
|
|
|
var insertLeft = dropPos.left <= box.left + box.width/2;
|
|
|
|
if( !insertLeft )
|
|
|
|
return best.index+1;
|
|
|
|
else
|
|
|
|
return best.index;
|
|
|
|
} else
|
|
|
|
return self._children.length;
|
2010-03-24 16:27:56 -07:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
return 0;
|
2010-03-24 16:27:56 -07:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// Insert the tab into the right position.
|
2010-03-24 16:27:56 -07:00
|
|
|
var index = findInsertionPoint(dropPos);
|
2010-04-22 17:19:42 -07:00
|
|
|
this._children.splice( index, 0, item );
|
2010-03-24 16:27:56 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
item.setZ(this.getZ() + 1);
|
2010-04-26 13:37:39 -07:00
|
|
|
$el.addClass("tabInGroup");
|
2010-03-29 16:08:48 -07:00
|
|
|
|
2010-05-03 15:32:11 -07:00
|
|
|
if(!wasAlreadyInThisGroup) {
|
|
|
|
$el.droppable("disable");
|
|
|
|
item.groupData = {};
|
|
|
|
|
|
|
|
item.addOnClose(this, function() {
|
|
|
|
self.remove($el);
|
|
|
|
});
|
|
|
|
|
|
|
|
item.parent = this;
|
|
|
|
|
|
|
|
if(typeof(item.setResizable) == 'function')
|
|
|
|
item.setResizable(false);
|
|
|
|
}
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(!options.dontArrange)
|
2010-05-04 15:57:19 -07:00
|
|
|
this.arrange();
|
2010-05-04 13:52:25 -07:00
|
|
|
|
|
|
|
if( this._nextNewTabCallback ){
|
|
|
|
this._nextNewTabCallback.apply(this, [item])
|
|
|
|
this._nextNewTabCallback = null;
|
|
|
|
}
|
|
|
|
|
2010-04-22 09:38:37 -07:00
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-26 13:37:39 -07:00
|
|
|
// Function: remove
|
|
|
|
// Removes an item from the group.
|
|
|
|
// Parameters:
|
|
|
|
//
|
|
|
|
// a - The item to remove. Can be an <Item>, a DOM element or a jQuery object.
|
|
|
|
// The latter two must refer to the container of an <Item>.
|
|
|
|
// options - An object with optional settings for this call. Currently the only one is dontArrange.
|
2010-04-22 17:19:42 -07:00
|
|
|
remove: function(a, options) {
|
|
|
|
var $el;
|
|
|
|
var item;
|
|
|
|
|
|
|
|
if(a.isAnItem) {
|
|
|
|
item = a;
|
|
|
|
$el = $(item.container);
|
|
|
|
} else {
|
|
|
|
$el = $(a);
|
|
|
|
item = Items.item($el);
|
|
|
|
}
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(typeof(options) == 'undefined')
|
|
|
|
options = {};
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
var index = $.inArray(item, this._children);
|
|
|
|
if(index != -1)
|
|
|
|
this._children.splice(index, 1);
|
2010-04-26 13:37:39 -07:00
|
|
|
|
|
|
|
item.parent = null;
|
|
|
|
$el.removeClass("tabInGroup");
|
2010-04-27 16:11:49 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
item.setSize(item.defaultSize.x, item.defaultSize.y);
|
2010-04-27 16:11:49 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
$el.droppable("enable");
|
|
|
|
item.removeOnClose(this);
|
|
|
|
|
|
|
|
if(typeof(item.setResizable) == 'function')
|
|
|
|
item.setResizable(true);
|
|
|
|
|
2010-04-26 16:48:46 -07:00
|
|
|
if(this._children.length == 0 && !this.locked && !this.getTitle()){
|
|
|
|
this.close();
|
2010-04-22 17:19:42 -07:00
|
|
|
} else if(!options.dontArrange) {
|
|
|
|
this.arrange();
|
2010-04-22 09:38:37 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
removeAll: function() {
|
2010-04-22 09:38:37 -07:00
|
|
|
var self = this;
|
2010-04-22 17:19:42 -07:00
|
|
|
var toRemove = $.merge([], this._children);
|
|
|
|
$.each(toRemove, function(index, child) {
|
|
|
|
self.remove(child, {dontArrange: true});
|
2010-04-22 09:38:37 -07:00
|
|
|
});
|
2010-04-22 12:51:48 -07:00
|
|
|
},
|
|
|
|
|
2010-05-04 16:44:03 -07:00
|
|
|
// ----------
|
|
|
|
shouldStack: function(count) {
|
|
|
|
var bb = this.getContentBounds();
|
|
|
|
var result = !(count == 1 || (bb.width * bb.height) / count > 7000);
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
arrange: function(options) {
|
2010-04-30 17:24:03 -07:00
|
|
|
if(this.expanded)
|
|
|
|
return;
|
|
|
|
|
2010-04-27 16:11:49 -07:00
|
|
|
var count = this._children.length;
|
|
|
|
if(!count)
|
|
|
|
return;
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
var bb = this.getContentBounds();
|
2010-05-04 16:44:03 -07:00
|
|
|
if(!this.shouldStack(count)) {
|
2010-04-29 00:42:37 -07:00
|
|
|
this._children.forEach(function(child){
|
|
|
|
child.removeClass("stacked")
|
|
|
|
});
|
|
|
|
|
2010-04-27 16:11:49 -07:00
|
|
|
Items.arrange(this._children, bb, options);
|
|
|
|
this._isStacked = false;
|
|
|
|
} else
|
|
|
|
this._stackArrange(bb, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
_stackArrange: function(bb, options) {
|
|
|
|
var animate;
|
|
|
|
if(!options || typeof(options.animate) == 'undefined')
|
|
|
|
animate = true;
|
|
|
|
else
|
|
|
|
animate = options.animate;
|
|
|
|
|
|
|
|
if(typeof(options) == 'undefined')
|
|
|
|
options = {};
|
|
|
|
|
|
|
|
var count = this._children.length;
|
|
|
|
if(!count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var zIndex = this.getZ() + count + 1;
|
|
|
|
|
|
|
|
var scale = 0.8;
|
|
|
|
var w;
|
|
|
|
var h;
|
|
|
|
var itemAspect = TabItems.tabHeight / TabItems.tabWidth;
|
|
|
|
var bbAspect = bb.height / bb.width;
|
|
|
|
if(bbAspect > itemAspect) { // Tall, thin group
|
|
|
|
w = bb.width * scale;
|
|
|
|
h = w * itemAspect;
|
|
|
|
} else { // Short, wide group
|
|
|
|
h = bb.height * scale;
|
|
|
|
w = h * (1 / itemAspect);
|
|
|
|
}
|
|
|
|
|
|
|
|
var x = (bb.width - w) / 2;
|
|
|
|
var y = Math.min(x, (bb.height - h) / 2);
|
|
|
|
var box = new Rect(bb.left + x, bb.top + y, w, h);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
$.each(this._children, function(index, child) {
|
2010-04-28 17:18:02 -07:00
|
|
|
if(!child.locked) {
|
|
|
|
child.setZ(zIndex);
|
|
|
|
zIndex--;
|
|
|
|
|
|
|
|
child.setBounds(box, !animate);
|
|
|
|
child.setRotation(self._randRotate(35, index));
|
2010-04-29 00:42:37 -07:00
|
|
|
child.addClass("stacked");
|
2010-04-28 17:18:02 -07:00
|
|
|
}
|
2010-04-27 16:11:49 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
self._isStacked = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
_randRotate: function(spread, index){
|
|
|
|
if( index >= this._stackAngles.length ){
|
|
|
|
var randAngle = parseInt( ((Math.random()+.6)/1.3)*spread-(spread/2) );
|
|
|
|
this._stackAngles.push(randAngle);
|
|
|
|
return randAngle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._stackAngles[index];
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
childHit: function(child) {
|
2010-04-28 17:18:02 -07:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// ___ normal click
|
|
|
|
if(!this._isStacked || this.expanded) {
|
|
|
|
setTimeout(function() {
|
|
|
|
self.collapse();
|
|
|
|
}, 200);
|
|
|
|
|
2010-04-27 16:11:49 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-28 17:18:02 -07:00
|
|
|
// ___ we're stacked, so expand
|
2010-05-01 00:21:15 -07:00
|
|
|
Groups.setActiveGroup(self);
|
2010-04-28 17:18:02 -07:00
|
|
|
var startBounds = child.getBounds();
|
|
|
|
var $tray = $("<div />").css({
|
|
|
|
top: startBounds.top,
|
|
|
|
left: startBounds.left,
|
|
|
|
width: startBounds.width,
|
|
|
|
height: startBounds.height,
|
|
|
|
position: "absolute",
|
|
|
|
zIndex: 99998
|
|
|
|
}).appendTo("body");
|
2010-04-27 16:11:49 -07:00
|
|
|
|
2010-04-29 00:42:37 -07:00
|
|
|
var w = 180;
|
2010-04-28 17:18:02 -07:00
|
|
|
var h = w * (TabItems.tabHeight / TabItems.tabWidth) * 1.1;
|
|
|
|
var padding = 20;
|
|
|
|
var col = Math.ceil(Math.sqrt(this._children.length));
|
|
|
|
var row = Math.ceil(this._children.length/col);
|
|
|
|
|
|
|
|
var overlayWidth = Math.min(window.innerWidth - (padding * 2), w*col + padding*(col+1));
|
|
|
|
var overlayHeight = Math.min(window.innerHeight - (padding * 2), h*row + padding*(row+1));
|
2010-04-27 16:11:49 -07:00
|
|
|
|
2010-04-28 17:18:02 -07:00
|
|
|
var pos = {left: startBounds.left, top: startBounds.top};
|
|
|
|
pos.left -= overlayWidth/3;
|
|
|
|
pos.top -= overlayHeight/3;
|
|
|
|
|
|
|
|
if( pos.top < 0 ) pos.top = 20;
|
|
|
|
if( pos.left < 0 ) pos.left = 20;
|
|
|
|
if( pos.top+overlayHeight > window.innerHeight ) pos.top = window.innerHeight-overlayHeight-20;
|
|
|
|
if( pos.left+overlayWidth > window.innerWidth ) pos.left = window.innerWidth-overlayWidth-20;
|
|
|
|
|
|
|
|
$tray.animate({
|
|
|
|
width: overlayWidth,
|
|
|
|
height: overlayHeight,
|
|
|
|
top: pos.top,
|
|
|
|
left: pos.left
|
2010-04-29 00:42:37 -07:00
|
|
|
}, 350, "tabcandyBounce").addClass("overlay");
|
|
|
|
|
|
|
|
this._children.forEach(function(child){
|
|
|
|
child.addClass("stack-trayed");
|
|
|
|
});
|
2010-04-28 17:18:02 -07:00
|
|
|
|
|
|
|
var box = new Rect(pos.left, pos.top, overlayWidth, overlayHeight);
|
|
|
|
box.inset(8, 8);
|
|
|
|
Items.arrange(this._children, box, {padding: 8, z: 99999});
|
|
|
|
|
|
|
|
var $shield = $('<div />')
|
|
|
|
.css({
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
position: 'absolute',
|
|
|
|
zIndex: 99997
|
|
|
|
})
|
|
|
|
.appendTo('body')
|
2010-04-27 16:11:49 -07:00
|
|
|
.mouseover(function() {
|
2010-04-28 17:18:02 -07:00
|
|
|
self.collapse();
|
2010-04-27 16:11:49 -07:00
|
|
|
})
|
2010-04-28 17:18:02 -07:00
|
|
|
.click(function() { // just in case
|
|
|
|
self.collapse();
|
2010-04-27 16:11:49 -07:00
|
|
|
});
|
2010-04-28 17:18:02 -07:00
|
|
|
|
|
|
|
this.expanded = {
|
|
|
|
$tray: $tray,
|
|
|
|
$shield: $shield
|
|
|
|
};
|
|
|
|
|
2010-04-27 16:11:49 -07:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
collapse: function() {
|
2010-04-28 17:18:02 -07:00
|
|
|
if(this.expanded) {
|
2010-04-30 17:24:03 -07:00
|
|
|
var z = this.getZ();
|
|
|
|
var box = this.getBounds();
|
|
|
|
this.expanded.$tray
|
|
|
|
.css({
|
|
|
|
zIndex: z + 1
|
|
|
|
})
|
|
|
|
.animate({
|
|
|
|
width: box.width,
|
|
|
|
height: box.height,
|
|
|
|
top: box.top,
|
|
|
|
left: box.left,
|
|
|
|
opacity: 0
|
|
|
|
}, 350, "tabcandyBounce", function() {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
|
2010-04-28 17:18:02 -07:00
|
|
|
this.expanded.$shield.remove();
|
|
|
|
this.expanded = null;
|
2010-04-29 00:42:37 -07:00
|
|
|
|
|
|
|
this._children.forEach(function(child){
|
|
|
|
child.removeClass("stack-trayed");
|
|
|
|
});
|
|
|
|
|
2010-04-30 17:24:03 -07:00
|
|
|
this.arrange({z: z + 2});
|
2010-04-27 16:11:49 -07:00
|
|
|
}
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-04-27 16:11:49 -07:00
|
|
|
_addHandlers: function(container) {
|
2010-03-17 01:07:00 -07:00
|
|
|
var self = this;
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(!this.locked) {
|
|
|
|
$(container).draggable({
|
|
|
|
scroll: false,
|
2010-04-28 17:18:02 -07:00
|
|
|
cancel: '.close, .name',
|
2010-04-30 17:24:03 -07:00
|
|
|
start: function(e, ui){
|
|
|
|
drag.info = new DragInfo(this, e);
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
drag: function(e, ui){
|
|
|
|
drag.info.drag(e, ui);
|
|
|
|
},
|
|
|
|
stop: function() {
|
|
|
|
drag.info.stop();
|
|
|
|
drag.info = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2010-03-17 01:07:00 -07:00
|
|
|
|
|
|
|
$(container).droppable({
|
2010-04-27 16:11:49 -07:00
|
|
|
tolerance: "intersect",
|
2010-03-17 01:07:00 -07:00
|
|
|
over: function(){
|
2010-04-22 17:19:42 -07:00
|
|
|
drag.info.$el.addClass("willGroup");
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
out: function(){
|
2010-04-26 16:48:46 -07:00
|
|
|
var group = drag.info.item.parent;
|
2010-04-27 16:11:49 -07:00
|
|
|
if(group) {
|
2010-04-26 13:37:39 -07:00
|
|
|
group.remove(drag.info.$el);
|
2010-04-27 16:11:49 -07:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
drag.info.$el.removeClass("willGroup");
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
2010-03-24 16:27:56 -07:00
|
|
|
drop: function(event){
|
2010-04-22 17:19:42 -07:00
|
|
|
drag.info.$el.removeClass("willGroup");
|
|
|
|
self.add( drag.info.$el, {left:event.pageX, top:event.pageY} );
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
2010-04-22 17:19:42 -07:00
|
|
|
accept: ".tab", //".tab, .group",
|
2010-03-17 01:07:00 -07:00
|
|
|
});
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
setResizable: function(value){
|
|
|
|
var self = this;
|
2010-04-02 17:33:06 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
if(value) {
|
|
|
|
this.$resizer.fadeIn();
|
|
|
|
$(this.container).resizable({
|
|
|
|
handles: "se",
|
|
|
|
aspectRatio: false,
|
2010-05-04 16:44:03 -07:00
|
|
|
minWidth: 90,
|
2010-04-22 17:19:42 -07:00
|
|
|
minHeight: 90,
|
|
|
|
resize: function(){
|
|
|
|
self.reloadBounds();
|
|
|
|
},
|
|
|
|
stop: function(){
|
|
|
|
self.reloadBounds();
|
2010-04-26 16:48:46 -07:00
|
|
|
self.setUserSize();
|
2010-04-22 17:19:42 -07:00
|
|
|
self.pushAway();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.$resizer.fadeOut();
|
|
|
|
$(this.container).resizable('disable');
|
2010-04-02 17:33:06 -07:00
|
|
|
}
|
2010-05-03 18:05:43 -07:00
|
|
|
},
|
|
|
|
|
2010-05-04 14:55:17 -07:00
|
|
|
// ----------
|
|
|
|
newTab: function() {
|
|
|
|
Groups.setActiveGroup(this);
|
|
|
|
var newTab = Tabs.open("about:blank");
|
2010-05-04 15:57:19 -07:00
|
|
|
UI.navBar.hide();
|
2010-05-04 14:55:17 -07:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var doNextTab = function(tab){
|
|
|
|
var group = Groups.getActiveGroup();
|
|
|
|
|
|
|
|
$(tab.container).css({opacity: 0});
|
|
|
|
anim = $("<div class='newTabAnimatee'/>").css({
|
2010-05-04 15:57:19 -07:00
|
|
|
top: tab.bounds.top+5,
|
|
|
|
left: tab.bounds.left+5,
|
|
|
|
width: tab.bounds.width-10,
|
|
|
|
height: tab.bounds.height-10,
|
|
|
|
zIndex: 999,
|
|
|
|
opacity: 0
|
|
|
|
})
|
2010-05-04 14:55:17 -07:00
|
|
|
.appendTo("body")
|
|
|
|
.animate({
|
2010-05-04 15:57:19 -07:00
|
|
|
opacity: 1.0
|
|
|
|
},500)
|
2010-05-04 14:55:17 -07:00
|
|
|
.animate({
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight
|
2010-05-04 15:57:19 -07:00
|
|
|
}, 270, function(){
|
2010-05-04 14:55:17 -07:00
|
|
|
$(tab.container).css({opacity: 1});
|
|
|
|
newTab.focus();
|
|
|
|
UI.tabBar.show(false);
|
2010-05-04 15:57:19 -07:00
|
|
|
UI.navBar.show();
|
2010-05-04 14:55:17 -07:00
|
|
|
UI.navBar.urlBar.focus();
|
|
|
|
anim.remove();
|
|
|
|
// We need a timeout here so that there is a chance for the
|
|
|
|
// new tab to get made! Otherwise it won't appear in the list
|
|
|
|
// of the group's tab.
|
|
|
|
// TODO: This is probably a terrible hack that sets up a race
|
|
|
|
// condition. We need a better solution.
|
|
|
|
setTimeout(function(){
|
|
|
|
UI.tabBar.showOnlyTheseTabs(Groups.getActiveGroup()._children);
|
|
|
|
}, 400);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Because this happens as a callback, there is
|
|
|
|
// sometimes a long delay before the animation occurs.
|
|
|
|
// We need to fix this--immediate response to a users
|
|
|
|
// actions is necessary for a good user experience.
|
|
|
|
|
2010-05-04 15:57:19 -07:00
|
|
|
self.onNextNewTab(doNextTab);
|
2010-05-04 14:55:17 -07:00
|
|
|
},
|
|
|
|
|
2010-05-04 13:52:25 -07:00
|
|
|
// ----------
|
|
|
|
// Function: reorderBasedOnTabOrder
|
|
|
|
// Reorderes the tabs in a group based on the arrangment of the tabs
|
|
|
|
// shown in the tab bar. It doesn't it by sorting the children
|
|
|
|
// of the group by the positions of their respective tabs in the
|
|
|
|
// tab bar.
|
|
|
|
reorderBasedOnTabOrder: function(){
|
2010-05-03 18:05:43 -07:00
|
|
|
var groupTabs = [];
|
|
|
|
for( var i=0; i<UI.tabBar.el.children.length; i++ ){
|
|
|
|
var tab = UI.tabBar.el.children[i];
|
|
|
|
if( tab.collapsed == false )
|
|
|
|
groupTabs.push(tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._children.sort(function(a,b){
|
|
|
|
return groupTabs.indexOf(a.tab.raw) - groupTabs.indexOf(b.tab.raw)
|
|
|
|
})
|
2010-05-04 13:52:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: getChild
|
|
|
|
// Returns the nth child tab or null if index is out of range.
|
|
|
|
//
|
|
|
|
// Parameters
|
|
|
|
// index - the index of the child tab to return, use negative
|
|
|
|
// numbers to index from the end (-1 is the last child)
|
|
|
|
getChild: function(index){
|
|
|
|
if( index < 0 ) index = this._children.length+index;
|
|
|
|
if( index >= this._children.length || index < 0 ) return null;
|
|
|
|
return this._children[index];
|
|
|
|
},
|
|
|
|
|
|
|
|
// ---------
|
|
|
|
// Function: onNextNewTab
|
|
|
|
// Sets up a one-time handler that gets called the next time a
|
|
|
|
// tab is added to the group.
|
|
|
|
//
|
|
|
|
// Parameters
|
|
|
|
// callback - the one-time callback that is fired when the next
|
|
|
|
// time a tab is added to a group; it gets passed the
|
|
|
|
// new tab
|
|
|
|
onNextNewTab: function(callback){
|
|
|
|
this._nextNewTabCallback = callback;
|
2010-04-22 17:19:42 -07:00
|
|
|
}
|
|
|
|
});
|
2010-03-17 01:07:00 -07:00
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ##########
|
2010-04-30 17:24:03 -07:00
|
|
|
var DragInfo = function(element, event) {
|
2010-04-22 17:19:42 -07:00
|
|
|
this.el = element;
|
|
|
|
this.$el = $(this.el);
|
|
|
|
this.item = Items.item(this.el);
|
2010-04-26 13:37:39 -07:00
|
|
|
this.parent = this.item.parent;
|
2010-04-30 17:24:03 -07:00
|
|
|
this.startPosition = new Point(event.clientX, event.clientY);
|
|
|
|
this.startTime = Utils.getMilliseconds();
|
2010-04-22 17:19:42 -07:00
|
|
|
|
|
|
|
this.$el.data('isDragging', true);
|
2010-04-27 16:11:49 -07:00
|
|
|
this.item.setZ(99999);
|
2010-04-22 17:19:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
DragInfo.prototype = {
|
|
|
|
// ----------
|
2010-04-30 17:24:03 -07:00
|
|
|
drag: function(event, ui) {
|
2010-04-22 17:19:42 -07:00
|
|
|
if(this.item.isAGroup) {
|
|
|
|
var bb = this.item.getBounds();
|
|
|
|
bb.left = ui.position.left;
|
|
|
|
bb.top = ui.position.top;
|
|
|
|
this.item.setBounds(bb, true);
|
|
|
|
} else
|
|
|
|
this.item.reloadBounds();
|
2010-04-28 17:18:02 -07:00
|
|
|
|
|
|
|
if(this.parent && this.parent.expanded) {
|
2010-04-30 17:24:03 -07:00
|
|
|
var now = Utils.getMilliseconds();
|
|
|
|
var distance = this.startPosition.distance(new Point(event.clientX, event.clientY));
|
|
|
|
if(now - this.startTime > 500 && distance > 100) {
|
|
|
|
this.item.locked = true;
|
|
|
|
this.parent.collapse();
|
|
|
|
this.item.locked = false;
|
|
|
|
}
|
2010-04-28 17:18:02 -07:00
|
|
|
}
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
stop: function() {
|
|
|
|
this.$el.data('isDragging', false);
|
2010-04-26 16:48:46 -07:00
|
|
|
|
2010-04-26 13:37:39 -07:00
|
|
|
if(this.parent && !this.parent.locked && this.parent != this.item.parent
|
2010-04-26 16:48:46 -07:00
|
|
|
&& this.parent._children.length == 1 && !this.parent.getTitle()) {
|
2010-04-22 17:19:42 -07:00
|
|
|
this.parent.remove(this.parent._children[0]);
|
2010-04-26 16:48:46 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 13:37:39 -07:00
|
|
|
if(this.item && !this.$el.hasClass('willGroup') && !this.item.parent) {
|
2010-04-22 17:19:42 -07:00
|
|
|
this.item.setZ(drag.zIndex);
|
|
|
|
drag.zIndex++;
|
|
|
|
|
|
|
|
this.item.reloadBounds();
|
|
|
|
this.item.pushAway();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var drag = {
|
|
|
|
info: null,
|
|
|
|
zIndex: 100
|
|
|
|
};
|
2010-04-01 17:20:59 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ##########
|
2010-04-26 13:37:39 -07:00
|
|
|
// Class: Groups
|
|
|
|
// Singelton for managing all <Group>s.
|
2010-03-17 01:07:00 -07:00
|
|
|
window.Groups = {
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-03-17 01:07:00 -07:00
|
|
|
dragOptions: {
|
2010-04-22 17:19:42 -07:00
|
|
|
scroll: false,
|
2010-04-27 16:11:49 -07:00
|
|
|
cancel: '.close',
|
2010-04-22 17:19:42 -07:00
|
|
|
start: function(e, ui) {
|
2010-04-30 17:24:03 -07:00
|
|
|
drag.info = new DragInfo(this, e);
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
2010-04-22 17:19:42 -07:00
|
|
|
drag: function(e, ui) {
|
|
|
|
drag.info.drag(e, ui);
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
2010-04-22 17:19:42 -07:00
|
|
|
stop: function() {
|
|
|
|
drag.info.stop();
|
|
|
|
drag.info = null;
|
|
|
|
}
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-03-17 01:07:00 -07:00
|
|
|
dropOptions: {
|
|
|
|
accept: ".tab",
|
2010-04-27 16:11:49 -07:00
|
|
|
tolerance: "intersect",
|
2010-03-17 01:07:00 -07:00
|
|
|
greedy: true,
|
|
|
|
drop: function(e){
|
2010-04-22 17:19:42 -07:00
|
|
|
$target = $(e.target);
|
|
|
|
drag.info.$el.removeClass("willGroup")
|
|
|
|
var phantom = $target.data("phantomGroup")
|
2010-04-01 17:19:21 -07:00
|
|
|
|
2010-04-26 13:37:39 -07:00
|
|
|
var group = drag.info.item.parent;
|
2010-04-22 17:19:42 -07:00
|
|
|
if( group == null ){
|
|
|
|
phantom.removeClass("phantom");
|
|
|
|
phantom.removeClass("group-content");
|
|
|
|
var group = new Group([$target, drag.info.$el], {container:phantom});
|
|
|
|
} else
|
|
|
|
group.add( drag.info.$el );
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
|
|
|
over: function(e){
|
2010-04-22 17:19:42 -07:00
|
|
|
var $target = $(e.target);
|
|
|
|
|
|
|
|
function elToRect($el){
|
|
|
|
return new Rect( $el.position().left, $el.position().top, $el.width(), $el.height() );
|
|
|
|
}
|
|
|
|
|
|
|
|
var height = elToRect($target).height * 1.5 + 20;
|
|
|
|
var width = elToRect($target).width * 1.5 + 20;
|
|
|
|
var unionRect = elToRect($target).union( elToRect(drag.info.$el) );
|
|
|
|
|
|
|
|
var newLeft = unionRect.left + unionRect.width/2 - width/2;
|
|
|
|
var newTop = unionRect.top + unionRect.height/2 - height/2;
|
|
|
|
|
|
|
|
$(".phantom").remove();
|
|
|
|
var phantom = $("<div class='group phantom group-content'/>").css({
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
position:"absolute",
|
|
|
|
top: newTop,
|
|
|
|
left: newLeft,
|
|
|
|
zIndex: -99
|
|
|
|
}).appendTo("body").hide().fadeIn();
|
|
|
|
$target.data("phantomGroup", phantom);
|
2010-03-17 01:07:00 -07:00
|
|
|
},
|
2010-04-22 17:19:42 -07:00
|
|
|
out: function(e){
|
|
|
|
$(e.target).data("phantomGroup").fadeOut(function(){
|
|
|
|
$(this).remove();
|
|
|
|
});
|
2010-03-17 01:07:00 -07:00
|
|
|
}
|
2010-03-17 17:32:49 -07:00
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
init: function() {
|
2010-04-26 13:37:39 -07:00
|
|
|
this.groups = [];
|
|
|
|
this.nextID = 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
getNextID: function() {
|
|
|
|
var result = this.nextID;
|
|
|
|
this.nextID++;
|
|
|
|
return result;
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
getStorageData: function() {
|
2010-04-26 13:37:39 -07:00
|
|
|
var data = {nextID: this.nextID, groups: []};
|
2010-04-22 17:19:42 -07:00
|
|
|
$.each(this.groups, function(index, group) {
|
|
|
|
data.groups.push(group.getStorageData());
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
2010-04-23 17:11:06 -07:00
|
|
|
// ----------
|
|
|
|
reconstitute: function(data) {
|
2010-04-26 13:37:39 -07:00
|
|
|
if(data && data.nextID)
|
|
|
|
this.nextID = data.nextID;
|
|
|
|
|
2010-04-23 17:11:06 -07:00
|
|
|
if(data && data.groups) {
|
|
|
|
$.each(data.groups, function(index, group) {
|
2010-04-30 17:24:03 -07:00
|
|
|
new Group([], $.extend({}, group, {locked: false, dontPush: true}));
|
2010-04-23 17:11:06 -07:00
|
|
|
});
|
|
|
|
}
|
2010-04-30 17:24:03 -07:00
|
|
|
|
|
|
|
var group = this.getNewTabGroup();
|
|
|
|
if(!group) {
|
|
|
|
var box = this.getBoundsForNewTabGroup();
|
|
|
|
new Group([], {bounds: box, title: 'New Tabs', dontPush: true});
|
|
|
|
}
|
2010-04-23 17:11:06 -07:00
|
|
|
},
|
|
|
|
|
2010-04-26 16:48:46 -07:00
|
|
|
// ----------
|
|
|
|
getNewTabGroup: function() {
|
|
|
|
var groupTitle = 'New Tabs';
|
|
|
|
var array = jQuery.grep(this.groups, function(group) {
|
|
|
|
return group.getTitle() == groupTitle;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(array.length)
|
|
|
|
return array[0];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
getBoundsForNewTabGroup: function() {
|
2010-04-28 20:12:34 -07:00
|
|
|
var pad = 20;
|
2010-04-26 16:48:46 -07:00
|
|
|
var sw = window.innerWidth;
|
|
|
|
var sh = window.innerHeight;
|
2010-04-28 16:39:15 -07:00
|
|
|
//var w = sw - (pad * 2);
|
2010-04-28 20:12:34 -07:00
|
|
|
var w = TabItems.tabWidth*2 + pad*2;
|
2010-04-28 16:39:15 -07:00
|
|
|
var h = TabItems.tabHeight*1.2 + pad*2;
|
2010-04-26 16:48:46 -07:00
|
|
|
return new Rect(pad, sh - (h + pad), w, h);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
repositionNewTabGroup: function() {
|
|
|
|
var box = this.getBoundsForNewTabGroup();
|
|
|
|
var group = this.getNewTabGroup();
|
|
|
|
group.setBounds(box, true);
|
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
register: function(group) {
|
|
|
|
Utils.assert('only register once per group', $.inArray(group, this.groups) == -1);
|
|
|
|
this.groups.push(group);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
unregister: function(group) {
|
|
|
|
var index = $.inArray(group, this.groups);
|
|
|
|
if(index != -1)
|
2010-05-04 16:44:03 -07:00
|
|
|
this.groups.splice(index, 1);
|
|
|
|
|
|
|
|
if(group == this._activeGroup)
|
|
|
|
this._activeGroup = null;
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
|
2010-04-26 13:37:39 -07:00
|
|
|
// ----------
|
|
|
|
// Function: group
|
|
|
|
// Given some sort of identifier, returns the appropriate group.
|
|
|
|
// Currently only supports group ids.
|
|
|
|
group: function(a) {
|
|
|
|
var result = null;
|
|
|
|
$.each(this.groups, function(index, candidate) {
|
|
|
|
if(candidate.id == a) {
|
|
|
|
result = candidate;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2010-03-25 17:22:45 -07:00
|
|
|
// ----------
|
2010-03-17 17:32:49 -07:00
|
|
|
arrange: function() {
|
2010-04-22 17:19:42 -07:00
|
|
|
var count = this.groups.length;
|
2010-03-17 17:32:49 -07:00
|
|
|
var columns = Math.ceil(Math.sqrt(count));
|
|
|
|
var rows = ((columns * columns) - count >= columns ? columns - 1 : columns);
|
|
|
|
var padding = 12;
|
|
|
|
var startX = padding;
|
2010-04-22 17:19:42 -07:00
|
|
|
var startY = Page.startY;
|
2010-03-17 17:32:49 -07:00
|
|
|
var totalWidth = window.innerWidth - startX;
|
|
|
|
var totalHeight = window.innerHeight - startY;
|
2010-04-22 17:19:42 -07:00
|
|
|
var box = new Rect(startX, startY,
|
|
|
|
(totalWidth / columns) - padding,
|
|
|
|
(totalHeight / rows) - padding);
|
2010-03-17 17:32:49 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
$.each(this.groups, function(index, group) {
|
|
|
|
group.setBounds(box, true);
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
box.left += box.width + padding;
|
|
|
|
if(index % columns == columns - 1) {
|
|
|
|
box.left = startX;
|
|
|
|
box.top += box.height + padding;
|
2010-03-17 17:32:49 -07:00
|
|
|
}
|
|
|
|
});
|
2010-03-26 11:34:09 -07:00
|
|
|
},
|
2010-03-25 17:22:45 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
|
|
|
removeAll: function() {
|
2010-04-22 17:19:42 -07:00
|
|
|
var toRemove = $.merge([], this.groups);
|
|
|
|
$.each(toRemove, function(index, group) {
|
2010-03-30 11:05:53 -07:00
|
|
|
group.removeAll();
|
|
|
|
});
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
newTab: function(tabItem) {
|
2010-04-28 16:39:15 -07:00
|
|
|
var group = this.getActiveGroup();
|
|
|
|
if( group == null )
|
|
|
|
group = this.getNewTabGroup();
|
2010-05-04 13:52:25 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
var $el = $(tabItem.container);
|
2010-05-04 15:57:19 -07:00
|
|
|
if(group) group.add($el);
|
2010-04-28 16:39:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
getActiveGroup: function() {
|
|
|
|
return this._activeGroup;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
setActiveGroup: function(group) {
|
2010-05-03 15:24:22 -07:00
|
|
|
this._activeGroup = group;
|
|
|
|
if(group)
|
|
|
|
UI.tabBar.showOnlyTheseTabs( group._children );
|
2010-03-26 11:34:09 -07:00
|
|
|
}
|
2010-04-28 16:39:15 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
};
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
|
|
|
Groups.init();
|
2010-04-22 09:38:37 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ##########
|
2010-03-17 01:07:00 -07:00
|
|
|
$(".tab").data('isDragging', false)
|
|
|
|
.draggable(window.Groups.dragOptions)
|
|
|
|
.droppable(window.Groups.dropOptions);
|
|
|
|
|
|
|
|
})();
|