REQUIREMENTS jQuery http jquery com and jQuery Live Plugin http plugin

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
[ REQUIREMENTS ]
jQuery (http://jquery.com/) and jQuery Live Plugin (http://plugins.jquery.com/project/livequery).
[ OPTIMIZATION ]
If this script runs slowly, you may apply the following optiomizations:
1. Selection of a note by tag is a very common operation, which is rather slow.
at the moment.
I do not know how fast jQuery functions show() and hide() are.
If they are fast, optimization of the selection mentioned above can
significantly improve performance.
We need an associative array that maps a tag to a set of notes tagged
with this tag. Of course, tags means tag names here and notes means id's of
HTML elements corresponding to notes (i.e., elements which have 'note' class).
This associative array is simple to implement in JavaScript. The sets of notes
can be simulated with another associative arrays.
We need to build the array during our initialization. When user selects or
deselects s tag, we can use the array to determine which notes to show and
which ones to hide. That will be much faster then iterating through notes
checking their tags (the way applied at the moment).
2. Calculating tag's 'weight' in the tags cloud is as important as the
operation described above.
We can use the same array. To calculate the weight of a tag we just need to
count the notes in the set the tag maps to, which are currently visible.
[ NOTES ]
1. CSS class selectors (".class") are slow if no tag is specified. Thus I use
"tag.class" if the tag is known.
2. We can use jQuery Live Plugin or wait until jQuery 1.3 release become
available in order to simplify the code a little by removing
tag_setClickHandler() from the code.
In actual, I've already tried to use both the plugin and jQuery 1.3 Beta 1,
but they did not work.
The plugin did not assign click handler as there were no plugin at all.
And jQuery 1.3 Beta is not still usable due to an
NS_ERROR_INVALID_POINTER exception. I've even tried to convert everything
from XHTML to HTML, but it did not help.
*/
/* --- Utils ---------------------------------------------------------------- */
jQuery.fn.exists = function(fn){
var ret = false;
$(this).each(function(){
this.fn = fn; // 'this' must be available in 'fn'
if (this.fn()){
ret = true;
return false; // break from 'each'
}
});
return ret;
}
jQuery.fn.existsText = function(text){
return $(this).exists(function(){
return $(this).text() == text;
});
}
jQuery.fn.select = function(fn, callback){
$(this).each(function(){
this.fn = fn; // 'this' must be available in 'fn'
if (this.fn()){
this.callback = callback;
return this.callback(); // 'this' must be available in 'callback'
}
});
}
function array_remove(array, element){
for(i in array)
if (element == array[i])
array.splice(i, 1);
}
/* --- Cloud ---------------------------------------------------------------- */
var Cloud = new function(){
this.addTag = function(tag, size){
var $allTags = $("#all_tags").append(" ");
var $a = $("<a href='' class='note_tag tag_size_" + size + "'>" + tag + "</a>").appendTo($allTags);
tag_setClickHandler($a);
if (size < 3){
var $popularTags = $("#popular_tags").append(" ");
var $b = $a.clone().appendTo($popularTags);
tag_setClickHandler($b);
}
}
this.recalculate = function(){
//console.profile();
$("#all_tags a.note_tag").remove();
$("#popular_tags a.note_tag").remove();
var tags = new Object();
var tagsArray = new Array();
$("table.note:visible a.note_tag").each(function(){
tag = $(this).text();
if (!tags[tag]){
tags[tag] = 1;
tagsArray.push(tag);
}
else
tags[tag]++;
});
tagsArray.sort(function(a, b){
var a1 = a.toLowerCase();
var b1 = b.toLowerCase();
if (a1 == b1) return 0;
if (a1 < b1) return -1;
return 1;
});
var notesCount = $("table.note:visible").length;
for (index in tagsArray) {
var tag = tagsArray[index];
if (Filter.isTagChosen(tag))
continue;
var _size = tags[tag] / notesCount;
if (_size > 0.05)
_size = 1;
else if (_size > 0.01)
_size = 2;
else
_size = 3;
this.addTag(tag, _size);
}
//console.profileEnd();
}
}();
/* --- Filter --------------------------------------------------------------- */
var Filter = new function(){
this.tags = new Array();
this.isEmpty = function(){
return this.tags.length == 0;
}
this.addTag = function(tag){
this.tags.push(tag);
$("#chosen_tags").show();
$("#chosen_tags").append("<a href='' class='chosen_tag'>" + tag + "</a> ");
tag_setClickHandler($("#chosen_tags a.chosen_tag:last"));
Notes.updateForSelectedTag(tag);
Cloud.recalculate();
}
this.removeTag = function(tag){
$("#chosen_tags a.chosen_tag").select(
function(){
return $(this).text() == tag;
},
function(){
$(this).remove();
return false; // break (select only one)
}
);
array_remove(this.tags, tag);
if (this.isEmpty())
$("#chosen_tags").hide();
Notes.updateForDeselectedTag(tag);
Cloud.recalculate();
}
this.isTagChosen = function(tag){
return this.tags.indexOf(tag) != -1;
}
}();
/* --- Notes ---------------------------------------------------------------- */
var Notes = new function(){
this.updateForSelectedTag = function(tag){
$("table.note:visible").each(function(){
if (! $(this).find("a.note_tag").existsText(tag)){
$(this).hide();
}
});
$("table.note a.note_tag").select(
function(){
return $(this).text() == tag;
},
function(){
$(this).addClass("chosen_tag");
}
);
}
this.updateForDeselectedTag = function(tag){
if (Filter.isEmpty()){
$("table.note:hidden").show();
return;
}
$("table.note:hidden").each(function(){
var $tags = $(this).find("a.note_tag");
for (var i = 0; i < Filter.tags.length; i++){
if (! $tags.existsText(Filter.tags[i])){
return;
}
}
$(this).show();
});
$("table.note a.note_tag").select(
function(){
return $(this).text() == tag;
},
function(){
$(this).removeClass("chosen_tag");
}
);
}
/*
This method is not actually used. It is just a template for
updateForSelectedTag() and updateForDeselectedTag().
*/
this.update = function(){
$("table.note").each(function(){
var $tags = $(this).find("a.note_tag");
for (var i = 0; i < Filter.tags.length; i++){
if (! $tags.existsText(Filter.tags[i])){
$(this).hide();
return;
}
}
$(this).show();
});
$("table.note a.note_tag").each(function(){
if (Filter.isTagChosen($(this).text())){
$(this).addClass("chosen_tag");
}
else{
$(this).removeClass("chosen_tag");
}
});
}
}();
/* --- Tag utils ------------------------------------------------------------ */
function tag_setClickHandler($tags){
$tags.unbind("click").click(function(){
if ($(this).hasClass("chosen_tag"))
Filter.removeTag($(this).text());
else
Filter.addTag($(this).text());
return false;
});
}
/* --- Startup -------------------------------------------------------------- */
$(document).ready(function(){
Cloud.recalculate();
tag_setClickHandler($("a.note_tag"));
$("#toggle_tags").click(function(){
if (!$(this).hasClass("active_link")){
$("#toggle_popular_tags").removeClass("active_link");
$("#toggle_all_tags").removeClass("active_link");
$(this).addClass("active_link");
$("#all_tags").hide();
$("#popular_tags").hide();
}
return false;
});
$("#toggle_popular_tags").click(function(){
if (!$(this).hasClass("active_link")){
$("#toggle_tags").removeClass("active_link");
$("#toggle_all_tags").removeClass("active_link");
$(this).addClass("active_link");
$("#all_tags").hide();
$("#popular_tags").show();
}
return false;
});
$("#toggle_all_tags").click(function(){
if (!$(this).hasClass("active_link")){
$("#toggle_tags").removeClass("active_link");
$("#toggle_popular_tags").removeClass("active_link");
$(this).addClass("active_link");
$("#popular_tags").hide();
$("#all_tags").show();
}
return false;
});
});