-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (153 loc) · 5.74 KB
/
index.js
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
;(function(window){
'use strict';
var TagCloud = function(tags, plugin, config){
this.config = Object.assign( {
parentNode: document.body,
width: '400px',
containerClass : null,
containerStyle: 'position: fixed; display: flex; z-Index:100; top:10%;',
itemClass: null,
itemStyle: 'text-decoration: none;\
display: inline-block;\
border-radius:3px;\
padding:2px 4px;\
position: absolute;',
plugin: plugin || TagCloud.prototype.plugins.ellpise
}, config);
var container = document.createElement('div');
if(this.config.containerClass){
container.className = this.config.containerClass;
}
if(this.config.containerStyle){
container.style.cssText = this.config.containerStyle;
}
this.getContainer = function(){
return container;
};
var running = false;
this.setRunning = function(val){
running = val;
};
this.isRunning = function(){
return running;
};
this.tagNodes = this.createTags(tags);
this.mountTo(this.config.parentNode);
};
TagCloud.prototype.plugins = {
none:{},
ellpise: {
init:function(idx, tc){
var tags = tc.tagNodes;
this.rotate = Math.PI / 2 * (1 + idx) / tags.length;
this.velocity = 0.01;
this.pos = function(t){
t = t || this.t;
var a = tc.getContainer().offsetWidth / 2;
var b = tc.getContainer().offsetHeight / 4;
var x = a * Math.cos(t);
var y = b * Math.sin(t);
var _x = x * Math.cos(-this.rotate) + y * Math.sin(-this.rotate);
var _y = y * Math.cos(-this.rotate) - x * Math.sin(-this.rotate);
this.style.left = _x + a + "px";
this.style.top = _y + a + "px";
this.style.opacity = Math.abs(Math.sin(t));
this.style.fontSize = Math.abs(Math.sin(t)) * 2 +'em';
this.t = t + this.velocity;
};
this.pos( (idx+1) / tags.length * 4 * Math.PI);
}
,
move:function(){
this.pos();
},
mouseover:function(tc){
return function(){
this.velocity = 0.001;
};
},
mouseout:function(tc){
return function(){
this.velocity = 0.01;
}
}
}
};
TagCloud.prototype.injectEvent = function(tag, config){
tag.onmouseover = (config.mouseover)(this);
tag.onmouseout = (config.mouseout)(this);
tag.init = config.init || function(){};
tag.move = config.move || function(){};
return tag;
};
TagCloud.prototype.createTags = function(tags){
var tagNodes = [];
tags.forEach(function(tag, index) {
var config = Object.assign({
mouseover:function(tc){
return function(){
tc.setRunning(false);
};
},
mouseout:function(tc){
return function(){
tc.setRunning(true);
};
},
init:function(){},
move:function(){},
className : this.config.itemClass,
href:tag.href,
style: this.config.itemStyle
}, this.config.plugin);
var tn = this.createTag(tag, config);
tn.style.zIndex = tags.length - 1 - index;
this.getContainer().appendChild(tn);
tagNodes.push(tn);
}, this);
return tagNodes;
};
TagCloud.prototype.colors = ["#34495d", "#ee7738", "#f59d2a", "#78bbe6"];
TagCloud.prototype.randomColor = function(){
return TagCloud.prototype.colors[parseInt(TagCloud.prototype.colors.length * Math.random())];
};
TagCloud.prototype.createTag = function(tag, config){
var el = document.createElement('a');
el.innerText = tag.name;
el.href = config.href;
el.title = tag.description || "";
if(config.className) el.className = config.className;
if(config.style) el.style.cssText = config.style;
el.style.backgroundColor = TagCloud.prototype.randomColor();
el.style.color = '#FFF';
el.target = config.target || "_blank";
return this.injectEvent(el, config);
};
/**
* Mount Tag Cloud Container to given dom node
*/
TagCloud.prototype.mountTo = function(domNode){
domNode.appendChild(this.getContainer());
this.getContainer().style.width = this.config.width;
this.getContainer().style.height = this.config.width;
this.tagNodes.forEach(function(tag, index){
tag.init(index, this);
}, this);
};
TagCloud.prototype.start = function(interval){
this.moveInterval = interval || this.moveInterval || 60;
this.setRunning(true);
var f = function(tc){
return function(){
if(tc.isRunning()){
tc.tagNodes.forEach(function(tag){
tag.move();
});
}
tc.timerId = window.setTimeout((f)(tc), tc.moveInterval);
}
};
f(this)();
};
window.TagCloud = TagCloud;
})(window);