1 |
2 |
- |
(function($) {
|
2 |
|
|
$.fn.collect = function (options) {
|
3 |
|
|
var $els = $([]);
|
4 |
|
|
var collect = false;
|
5 |
|
|
var sum = 0;
|
6 |
|
|
|
7 |
|
|
var settings = {
|
8 |
|
|
addItem : null,
|
9 |
|
|
removeItem : null
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
$.extend(settings, options)
|
13 |
|
|
|
14 |
|
|
return this.each(function () {
|
15 |
|
|
$(this).addClass('collectable')
|
16 |
|
|
.bind('mousedown', function (e){
|
17 |
|
|
var $el = $(this);
|
18 |
|
|
|
19 |
|
|
if (!e.ctrlKey) {
|
20 |
|
|
clearItems()
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
if (e.shiftKey || e.ctrlKey) {
|
24 |
|
|
collect = true;
|
25 |
|
|
if ($el.hasClass('isCollected')) {
|
26 |
|
|
removeItem($el);
|
27 |
|
|
}
|
28 |
|
|
else {
|
29 |
|
|
addItem($el);
|
30 |
|
|
}
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
return false;
|
34 |
|
|
})
|
35 |
|
|
.bind('mouseup', function (e) {
|
36 |
|
|
collect = false;
|
37 |
|
|
|
38 |
|
|
return false;
|
39 |
|
|
})
|
40 |
|
|
.bind('mouseover', function (e){
|
41 |
|
|
if (collect && e.shiftKey && $(this).hasClass('collectable')) {
|
42 |
|
|
$el = $(this);
|
43 |
|
|
if ($el.hasClass('isCollected')) {
|
44 |
|
|
removeItem($el);
|
45 |
|
|
}
|
46 |
|
|
else {
|
47 |
|
|
addItem($el);
|
48 |
|
|
}
|
49 |
|
|
//$(document.body).append('adding.. ');
|
50 |
|
|
|
51 |
|
|
}
|
52 |
|
|
/* else {
|
53 |
|
|
$(document.body).append('hello');
|
54 |
|
|
}*/
|
55 |
|
|
return false;
|
56 |
|
|
});
|
57 |
|
|
})
|
58 |
|
|
|
59 |
|
|
function addItem($el) {
|
60 |
|
|
//$(document.body).append('addItem<br />');
|
61 |
|
|
$el.addClass('isCollected');
|
62 |
|
|
|
63 |
|
|
$els = $els.add($el);
|
64 |
|
|
|
65 |
|
|
if (settings.addItem) {
|
66 |
|
|
settings.addItem.call($el.get(0), $els);
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
function removeItem($el) {
|
71 |
|
|
//$(document.body).append('removeItem<br />');
|
72 |
|
|
$el.removeClass('isCollected');
|
73 |
|
|
|
74 |
|
|
$els = $els.not($el);
|
75 |
|
|
|
76 |
|
|
if (settings.removeItem) {
|
77 |
|
|
settings.removeItem.call($el.get(0), $els);
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
function clearItems() {
|
82 |
|
|
//$(document.body).append('clearItems<br />');
|
83 |
|
|
$els.removeClass('isCollected');
|
84 |
|
|
$els = $([]);
|
85 |
|
|
|
86 |
|
|
if (settings.clearItems) {
|
87 |
|
|
settings.clearItems();
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
})(jQuery);
|
93 |
|
|
|
94 |
|
|
(function($) {
|
95 |
|
|
$.fn.collectAndSum = function (options) {
|
96 |
|
|
var settings = {
|
97 |
|
|
offsetTop : -3,
|
98 |
|
|
offsetLeft : -3
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
$.extend(settings, options);
|
102 |
|
|
|
103 |
|
|
var $sum = $('<div />')
|
104 |
|
|
.appendTo(document.body)
|
105 |
|
|
.addClass('collectableTip')
|
106 |
|
|
.toggle()
|
107 |
|
|
.click(function () {
|
108 |
|
|
$(this).fadeOut('fast');
|
109 |
|
|
});
|
110 |
|
|
|
111 |
|
|
function addRemove($set, $this) {
|
112 |
|
|
var el = $this.get(0);
|
113 |
|
|
|
114 |
|
|
if ($set.length == 0) {
|
115 |
|
|
$sum.fadeOut('fast');
|
116 |
|
|
}
|
117 |
|
|
else {
|
118 |
|
|
|
119 |
|
|
var offset = $this.offset();
|
120 |
|
|
|
121 |
|
|
var sum = 0;
|
122 |
|
|
var numCount = 0;
|
123 |
|
|
var min = null;
|
124 |
|
|
var max = null;
|
125 |
|
|
|
126 |
|
|
$set.each(function () {
|
127 |
|
|
var val = parseFloat(this.innerHTML);
|
128 |
|
|
if (!isNaN(val)) {
|
129 |
|
|
sum += parseFloat(this.innerHTML);
|
130 |
|
|
numCount ++;
|
131 |
|
|
if (min == null) {
|
132 |
|
|
min = val;
|
133 |
|
|
}
|
134 |
|
|
else if (val < min) {
|
135 |
|
|
min = val;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
if (max == null) {
|
139 |
|
|
max = val;
|
140 |
|
|
}
|
141 |
|
|
else if (val > max) {
|
142 |
|
|
max = val;
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
})
|
146 |
|
|
|
147 |
|
|
var html = 'sum=' + formatFloat(sum) + '<br />';
|
148 |
|
|
html += 'avg=' + formatFloat(sum/numCount) + '<br />';
|
149 |
|
|
html += 'min=' + formatFloat(min) + '<br />';
|
150 |
|
|
html += 'max=' + formatFloat(max) + '<br />';
|
151 |
|
|
html += 'count=' + formatInt(numCount) + '<br />';
|
152 |
|
|
|
153 |
|
|
$sum.html(html).stop(null,true).animate({
|
154 |
|
|
left : settings.offsetLeft + offset.left + $this.outerWidth(),
|
155 |
|
|
top : settings.offsetTop + offset.top + $this.outerHeight()
|
156 |
|
|
})
|
157 |
|
|
|
158 |
|
|
if ($sum.css('display') == 'none') {
|
159 |
|
|
$sum.fadeIn('slow');
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
return $(this).collect({
|
165 |
|
|
addItem : function ($set) {
|
166 |
|
|
//this is the item being added
|
167 |
|
|
addRemove($set, $(this));
|
168 |
|
|
},
|
169 |
|
|
removeItem : function ($set) {
|
170 |
|
|
//this is the item being removed
|
171 |
|
|
addRemove($set,$($set.get($set.length -1)));
|
172 |
|
|
},
|
173 |
|
|
clearItems : function () {
|
174 |
|
|
$sum.html('').fadeOut('fast');
|
175 |
|
|
}
|
176 |
|
|
});
|
177 |
|
|
}
|
178 |
|
|
})(jQuery);
|