File: tagcloud.js

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (109 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download | duplicates (2)
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
/*
 *  $Id: tagcloud.js,v 1.3.2.3 2010/04/06 16:46:12 source Exp $
 *
 *  This file is part of the OpenLink Software Ajax Toolkit (OAT) project.
 *
 *  Copyright (C) 2005-2010 OpenLink Software
 *
 *  See LICENSE file for details.
 */
/*
	var tc = new OAT.TagCloud(elm, optObj);
	tc.clearItems();
	tc.addItem(name,link,frequency = 1);
	tc.draw();
*/

OAT.TagCloudData = {
	COLOR_SIZE:0,
	COLOR_CYCLE:1,
	COLOR_RANDOM:2
}

OAT.TagCloud = function(elm, optObj) {
	var self = this;
	this.options = {
		separator:" ",
		colors:["#f00","#0f0","#00f"],
		sizes:["80%","100%","120%"],
		colorMapping:OAT.TagCloudData.COLOR_SIZE
	}
	for (var p in optObj) { self.options[p] = optObj[p]; }
	this.elm = $(elm);

	this.items = {};
	this.min = 0;
	this.max = 0;

	this.getColor = function(item,index) {
		var count = self.options.colors.length;
		switch (self.options.colorMapping) {
			case OAT.TagCloudData.COLOR_SIZE:
				var piece = (self.max - self.min) / count;
				var f = item.freq;
				var idx = Math.floor((f-self.min) / piece);
				if (idx >= count) { idx--; }
				return self.options.colors[idx];
			break;
			case OAT.TagCloudData.COLOR_CYCLE:
				return self.options.colors[index % count];
			break;
			case OAT.TagCloudData.COLOR_RANDOM:
				var idx = Math.floor(Math.random()*count);
				return self.options.colors[idx];
			break;
		}
	}

	this.getSize = function(item,index) {
		var count = self.options.sizes.length;
		var piece = (self.max - self.min) / count;
		var f = item.freq;
		var idx = Math.floor((f-self.min) / piece);
		if (idx >= count) { idx--; }
		return self.options.sizes[idx];
	}

	this.clearItems = function() {
		this.items = {};
		this.min = 99999;
		this.max = 0
	}

	this.addItem = function(name,link,frequency) {
		var freq = frequency || 1;
		if (name in self.items) {
			var o = self.items[name];
		} else {
			var o = {
				link:link,
				freq:0
			}
			self.items[name] = o;
		}
		o.freq += freq;
		if (o.freq > self.max) { self.max = o.freq; }
		if (o.freq < self.min) { self.min = o.freq; }
	}

	this.draw = function() {
		OAT.Dom.clear(self.elm);
		var counter = 0;
		for (var p in self.items) {
			var item = self.items[p];
			var a = OAT.Dom.create("a");
			a.href = item.link;
			a.innerHTML = p;
			var color = self.getColor(item,counter);
			var size = self.getSize(item,counter);
			if (color) { a.style.color = color; }
			if (size) { a.style.fontSize = size; }
			self.elm.appendChild(a);
			var separator = self.elm.appendChild(OAT.Dom.text(self.options.separator));
			counter++;
		}
		OAT.Dom.unlink(separator);
	}

	this.clearItems();
}