forked from jabbany/CommentCoreLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentCoreLibrary.js
More file actions
295 lines (282 loc) · 7.95 KB
/
CommentCoreLibrary.js
File metadata and controls
295 lines (282 loc) · 7.95 KB
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
/*!
* Comment Core Library CommentManager
* @license MIT
* @author Jim Chen
*
* Copyright (c) 2014 Jim Chen
*/
var CommentManager = (function() {
var getRotMatrix = function(yrot, zrot) {
// Courtesy of @StarBrilliant, re-adapted to look better
var DEG2RAD = Math.PI/180;
var yr = yrot * DEG2RAD;
var zr = zrot * DEG2RAD;
var COS = Math.cos;
var SIN = Math.sin;
var matrix = [
COS(yr) * COS(zr) , COS(yr) * SIN(zr) , SIN(yr) , 0,
(-SIN(zr)) , COS(zr) , 0 , 0,
(-SIN(yr) * COS(zr)) , (-SIN(yr) * SIN(zr)) , COS(yr) , 0,
0 , 0 , 0 , 1
];
// CSS does not recognize scientific notation (e.g. 1e-6), truncating it.
for(var i = 0; i < matrix.length;i++){
if(Math.abs(matrix[i]) < 0.000001){
matrix[i] = 0;
}
}
return "matrix3d(" + matrix.join(",") + ")";
};
function CommentManager(stageObject){
var __timer = 0;
this._listeners = {};
this._lastPosition = 0;
this.stage = stageObject;
this.options = {
global:{
opacity:1,
scale:1,
className:"cmt"
},
scroll:{
opacity:1,
scale:1
},
limit: 0
};
this.timeline = [];
this.runline = [];
this.position = 0;
this.limiter = 0;
this.filter = null;
this.csa = {
scroll: new CommentSpaceAllocator(0,0),
top:new AnchorCommentSpaceAllocator(0,0),
bottom:new AnchorCommentSpaceAllocator(0,0),
reverse:new CommentSpaceAllocator(0,0),
scrollbtm:new CommentSpaceAllocator(0,0)
};
/** Precompute the offset width **/
this.width = this.stage.offsetWidth;
this.height = this.stage.offsetHeight;
this.startTimer = function(){
if(__timer > 0)
return;
var lastTPos = new Date().getTime();
var cmMgr = this;
__timer = window.setInterval(function(){
var elapsed = new Date().getTime() - lastTPos;
lastTPos = new Date().getTime();
cmMgr.onTimerEvent(elapsed,cmMgr);
},10);
};
this.stopTimer = function(){
window.clearInterval(__timer);
__timer = 0;
};
}
/** Public **/
CommentManager.prototype.stop = function(){
this.stopTimer();
};
CommentManager.prototype.start = function(){
this.startTimer();
};
CommentManager.prototype.seek = function(time){
this.position = BinArray.bsearch(this.timeline, time, function(a,b){
if(a < b.stime) return -1
else if(a > b.stime) return 1;
else return 0;
});
};
CommentManager.prototype.validate = function(cmt){
if(cmt == null)
return false;
return this.filter.doValidate(cmt);
};
CommentManager.prototype.load = function(a){
this.timeline = a;
this.timeline.sort(function(a,b){
if(a.stime > b.stime) return 2;
else if(a.stime < b.stime) return -2;
else{
if(a.date > b.date) return 1;
else if(a.date < b.date) return -1;
else if(a.dbid != null && b.dbid != null){
if(a.dbid > b.dbid) return 1;
else if(a.dbid < b.dbid) return -1;
return 0;
}else
return 0;
}
});
this.dispatchEvent("load");
};
CommentManager.prototype.insert = function(c){
var index = BinArray.binsert(this.timeline, c, function(a,b){
if(a.stime > b.stime) return 2;
else if(a.stime < b.stime) return -2;
else{
if(a.date > b.date) return 1;
else if(a.date < b.date) return -1;
else if(a.dbid != null && b.dbid != null){
if(a.dbid > b.dbid) return 1;
else if(a.dbid < b.dbid) return -1;
return 0;
}else
return 0;
}
});
if(index <= this.position){
this.position++;
}
this.dispatchEvent("insert");
};
CommentManager.prototype.clear = function(){
while(this.runline.length > 0){
this.runline[0].finish();
}
this.dispatchEvent("clear");
};
CommentManager.prototype.setBounds = function(){
this.width = this.stage.offsetWidth;
this.height= this.stage.offsetHeight;
this.dispatchEvent("resize");
for(var comAlloc in this.csa){
this.csa[comAlloc].setBounds(this.width,this.height);
}
// Update 3d perspective
this.stage.style.perspective = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
this.stage.style.webkitPerspective = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
};
CommentManager.prototype.init = function(){
this.setBounds();
if(this.filter == null) {
this.filter = new CommentFilter(); //Only create a filter if none exist
}
};
CommentManager.prototype.time = function(time){
time = time - 1;
if(this.position >= this.timeline.length || Math.abs(this._lastPosition - time) >= 2000){
this.seek(time);
this._lastPosition = time;
if(this.timeline.length <= this.position) {
return;
}
}else{
this._lastPosition = time;
}
for(;this.position < this.timeline.length;this.position++){
if(this.timeline[this.position]['stime']<=time){
if(this.options.limit > 0 && this.runline.length > this.limiter) {
continue; // Skip comments but still move the position pointer
} else if(this.validate(this.timeline[this.position])){
this.send(this.timeline[this.position]);
}
}else{
break;
}
}
};
CommentManager.prototype.rescale = function(){
};
CommentManager.prototype.send = function(data){
if(data.mode === 8){
console.log(data);
if(this.scripting){
console.log(this.scripting.eval(data.code));
}
return;
}
if(this.filter != null){
data = this.filter.doModify(data);
if(data == null) return;
}
if(data.mode === 1 || data.mode === 2 || data.mode === 6){
var cmt = new ScrollComment(this, data);
}else{
var cmt = new CoreComment(this, data);
}
switch(cmt.mode){
case 1:cmt.align = 0;break;
case 2:cmt.align = 2;break;
case 4:cmt.align = 2;break;
case 5:cmt.align = 0;break;
case 6:cmt.align = 1;break;
}
cmt.init();
this.stage.appendChild(cmt.dom);
switch(cmt.mode){
default:
case 1:{this.csa.scroll.add(cmt);}break;
case 2:{this.csa.scrollbtm.add(cmt);}break;
case 4:{this.csa.bottom.add(cmt);}break;
case 5:{this.csa.top.add(cmt);}break;
case 6:{this.csa.reverse.add(cmt);}break;
case 17:
case 7:{
if(data.rY !== 0 || data.rZ !== 0){
/** TODO: revise when browser manufacturers make up their mind on Transform APIs **/
cmt.dom.style.transform = getRotMatrix(data.rY, data.rZ);
cmt.dom.style.webkitTransform = getRotMatrix(data.rY, data.rZ);
cmt.dom.style.OTransform = getRotMatrix(data.rY, data.rZ);
cmt.dom.style.MozTransform = getRotMatrix(data.rY, data.rZ);
cmt.dom.style.MSTransform = getRotMatrix(data.rY, data.rZ);
}
}break;
}
cmt.y = cmt.y;
this.dispatchEvent("enterComment", cmt);
this.runline.push(cmt);
};
CommentManager.prototype.sendComment = function(data){
console.log("CommentManager.sendComment is deprecated. Please use send instead");
this.send(data); // Wrapper for Backwards Compatible APIs
};
CommentManager.prototype.finish = function(cmt){
this.dispatchEvent("exitComment", cmt);
this.stage.removeChild(cmt.dom);
var index = this.runline.indexOf(cmt);
if(index >= 0){
this.runline.splice(index, 1);
}
switch(cmt.mode){
default:
case 1:{this.csa.scroll.remove(cmt);}break;
case 2:{this.csa.scrollbtm.remove(cmt);}break;
case 4:{this.csa.bottom.remove(cmt);}break;
case 5:{this.csa.top.remove(cmt);}break;
case 6:{this.csa.reverse.remove(cmt);}break;
case 7:break;
}
};
CommentManager.prototype.addEventListener = function(event, listener){
if(typeof this._listeners[event] !== "undefined"){
this._listeners[event].push(listener);
}else{
this._listeners[event] = [listener];
}
};
CommentManager.prototype.dispatchEvent = function(event, data){
if(typeof this._listeners[event] !== "undefined"){
for(var i = 0; i < this._listeners[event].length; i++){
try{
this._listeners[event][i](data);
}catch(e){
console.err(e.stack);
}
}
}
};
/** Static Functions **/
CommentManager.prototype.onTimerEvent = function(timePassed,cmObj){
for(var i= 0;i < cmObj.runline.length; i++){
var cmt = cmObj.runline[i];
if(cmt.hold){
continue;
}
cmt.time(timePassed);
}
};
return CommentManager;
})();