(function($) {
$.fn.collect = function (options) {
var $els = $([]);
var collect = false;
var sum = 0;
var settings = {
addItem : null,
removeItem : null
}
$.extend(settings, options)
return this.each(function () {
$(this).addClass('collectable')
.bind('mousedown', function (e){
var $el = $(this);
if (!e.ctrlKey) {
clearItems()
}
if (e.shiftKey || e.ctrlKey) {
collect = true;
if ($el.hasClass('isCollected')) {
removeItem($el);
}
else {
addItem($el);
}
}
return false;
})
.bind('mouseup', function (e) {
collect = false;
return false;
})
.bind('mouseover', function (e){
if (collect && e.shiftKey && $(this).hasClass('collectable')) {
$el = $(this);
if ($el.hasClass('isCollected')) {
removeItem($el);
}
else {
addItem($el);
}
//$(document.body).append('adding.. ');
}
/* else {
$(document.body).append('hello');
}*/
return false;
});
})
function addItem($el) {
//$(document.body).append('addItem<br />');
$el.addClass('isCollected');
$els = $els.add($el);
if (settings.addItem) {
settings.addItem.call($el.get(0), $els);
}
}
function removeItem($el) {
//$(document.body).append('removeItem<br />');
$el.removeClass('isCollected');
$els = $els.not($el);
if (settings.removeItem) {
settings.removeItem.call($el.get(0), $els);
}
}
function clearItems() {
//$(document.body).append('clearItems<br />');
$els.removeClass('isCollected');
$els = $([]);
if (settings.clearItems) {
settings.clearItems();
}
}
}
})(jQuery);
(function($) {
$.fn.collectAndSum = function (options) {
var settings = {
offsetTop : -3,
offsetLeft : -3
}
$.extend(settings, options);
var $sum = $('<div />')
.appendTo(document.body)
.addClass('collectableTip')
.toggle()
.click(function () {
$(this).fadeOut('fast');
});
function addRemove($set, $this) {
var el = $this.get(0);
if ($set.length == 0) {
$sum.fadeOut('fast');
}
else {
var offset = $this.offset();
var sum = 0;
var numCount = 0;
var min = null;
var max = null;
$set.each(function () {
var val = parseFloat(this.innerHTML);
if (!isNaN(val)) {
sum += parseFloat(this.innerHTML);
numCount ++;
if (min == null) {
min = val;
}
else if (val < min) {
min = val;
}
if (max == null) {
max = val;
}
else if (val > max) {
max = val;
}
}
})
var html = 'sum=' + formatFloat(sum) + '<br />';
html += 'avg=' + formatFloat(sum/numCount) + '<br />';
html += 'min=' + formatFloat(min) + '<br />';
html += 'max=' + formatFloat(max) + '<br />';
html += 'count=' + formatInt(numCount) + '<br />';
$sum.html(html).stop(null,true).animate({
left : settings.offsetLeft + offset.left + $this.outerWidth(),
top : settings.offsetTop + offset.top + $this.outerHeight()
})
if ($sum.css('display') == 'none') {
$sum.fadeIn('slow');
}
}
}
return $(this).collect({
addItem : function ($set) {
//this is the item being added
addRemove($set, $(this));
},
removeItem : function ($set) {
//this is the item being removed
addRemove($set,$($set.get($set.length -1)));
},
clearItems : function () {
$sum.html('').fadeOut('fast');
}
});
}
})(jQuery);