Skip to content

Commit ef6a03c

Browse files
committed
Update tests & CCL Manager
Prepare for the move to typescript when compiling the main library (the comment manager)
1 parent 9bd7d64 commit ef6a03c

6 files changed

+228
-7
lines changed

spec/CommentCoreLibrary_spec.coffee

+17-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ describe 'CommentManager', ->
6666

6767
describe '.start', ->
6868
it 'starts the timer', ->
69-
manager.start()
7069
manager.start()
7170
# TODO: figure out how to test the timer
7271
# maybe just add spy on window.setInterval
@@ -90,3 +89,20 @@ describe 'CommentManager', ->
9089
expect(manager.timeline).toEqual [c3, c5]
9190
manager.insert c4
9291
expect(manager.timeline).toEqual [c3, c4 , c5]
92+
93+
describe '.addEventListener .dispatchEvent', ->
94+
it 'add one event listener', ->
95+
hasDispatchedEvent = false
96+
manager.addEventListener 'myCustomEvent', ->
97+
hasDispatchedEvent = true
98+
manager.dispatchEvent 'myCustomEvent'
99+
expect(hasDispatchedEvent).toBe true
100+
101+
it 'add multiple event listeners', ->
102+
dispatchedEventId = 0
103+
manager.addEventListener 'myCustomEvent', ->
104+
dispatchedEventId = 1
105+
manager.addEventListener 'myCustomEvent', ->
106+
dispatchedEventId = 2
107+
manager.dispatchEvent 'myCustomEvent'
108+
expect(dispatchedEventId).toBe 2

spec/core/CommentSpaceAllocator_spec.coffee

+23
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,48 @@ describe 'CommentSpaceAllocators', ->
3434
describe 'AnchorCommentSpaceAllocator', ->
3535
it 'comments always collide', ->
3636
expect(anchorCSA.willCollide(c1, c2)).toBe true
37+
3738
it 'same comment must collide with self', ->
3839
expect(anchorCSA.willCollide(c1, c1)).toBe true
3940
expect(anchorCSA.willCollide(c2, c2)).toBe true
41+
4042
it 'path check passes for y = 0 in empty pool', ->
4143
expect(anchorCSA.pathCheck(0, c1, [])).toBe true
44+
4245
it 'path check fails for y = 0 in self pool', ->
4346
expect(anchorCSA.pathCheck(0, c1, [c1])).toBe false
47+
4448
it 'path check fails for y = height + 1 in self pool', ->
4549
expect(anchorCSA.pathCheck(c1.height + 1, c1, [c1])).toBe true
50+
51+
it 'removal for cindex < 0', ->
52+
c1.cindex = -1
53+
expect(typeof anchorCSA.remove(c1)).toBe 'undefined'
54+
55+
it 'removal for cindex < 0', ->
56+
c1.cindex = -1
57+
expect(typeof anchorCSA.remove(c1)).toBe 'undefined'
4658
# TODO: We need more extensive test cases
4759

4860
describe 'CommentSpaceAllocator', ->
4961
it 'same comment must collide with self', ->
5062
expect(scrollCSA.willCollide(s1, s1)).toBe true
5163
expect(scrollCSA.willCollide(s2, s2)).toBe true
64+
5265
it 'path check passes for y = 0 in empty pool', ->
5366
expect(scrollCSA.pathCheck(0, s1, [])).toBe true
67+
5468
it 'path check passes for y = 0 in self pool', ->
5569
expect(scrollCSA.pathCheck(0, s1, [s1])).toBe false
70+
5671
it 'path check passes for y = height + 1 in self pool', ->
5772
expect(scrollCSA.pathCheck(s1.height + 1, s1, [s1])).toBe true
73+
74+
it 'removal for cindex < 0', ->
75+
s1.cindex = -1
76+
expect(typeof scrollCSA.remove(s1)).toBe 'undefined'
77+
78+
it 'removal for cindex < 0', ->
79+
s1.cindex = -1
80+
expect(typeof scrollCSA.remove(s1)).toBe 'undefined'
5881
# TODO: We need more extensive test cases

src/core/Comment.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
* Basic Comment Abstraction
3-
* Built on DOM actions
3+
*
4+
* @author Jim Chen
5+
* @license MIT License
6+
* @description Comment abstraction based on DOM implementation
47
*/
58
/// <reference path="Core.d.ts" />
69
class CoreComment implements IComment {
@@ -42,10 +45,10 @@ class CoreComment implements IComment {
4245
private _shadow:boolean = true;
4346
private _font:string = "";
4447

45-
public parent:CommentManager;
48+
public parent:ICommentManager;
4649
public dom:HTMLDivElement;
4750

48-
constructor(parent:CommentManager, init:Object = {}) {
51+
constructor(parent:ICommentManager, init:Object = {}) {
4952
if (!parent) {
5053
throw new Error("Comment not bound to comment manager.");
5154
} else {
@@ -409,7 +412,7 @@ class CoreComment implements IComment {
409412
}
410413

411414
class ScrollComment extends CoreComment {
412-
constructor(parent:CommentManager, data:Object) {
415+
constructor(parent:ICommentManager, data:Object) {
413416
super(parent, data);
414417
this.dur *= this.parent.options.scroll.scale;
415418
this.ttl *= this.parent.options.scroll.scale;

src/core/CommentCoreLibrary.ts

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* Comment Core Library
3+
*
4+
* @author Jim Chen
5+
* @license MIT License
6+
* @description Comment management unit for CCL
7+
*/
8+
/// <reference path="Core.d.ts" />
9+
class CommentManager implements ICommentManager {
10+
private _width:number = 0;
11+
private _height:number = 0;
12+
private _status:string = "stopped";
13+
private _stage:HTMLDivElement;
14+
private _listeners:Object = {};
15+
private _csa:Object = {};
16+
17+
public options:CCLOptions = {
18+
"global": {
19+
"scale": 1,
20+
"opacity": 1,
21+
"className": "cmt"
22+
},
23+
"scroll": {
24+
"scale": 1,
25+
"opacity": 1
26+
}
27+
};
28+
public timeline:Array<Object> = [];
29+
public runline:Array<IComment> = [];
30+
public position:number = 0;
31+
32+
get width():number {
33+
return this._width;
34+
}
35+
36+
get height():number {
37+
return this._height;
38+
}
39+
40+
get stage():HTMLDivElement {
41+
return this._stage;
42+
}
43+
44+
get status():string {
45+
return this._status;
46+
}
47+
48+
constructor(stage:HTMLDivElement) {
49+
this._stage = stage;
50+
}
51+
52+
public static getCommentType(data:Object):string{
53+
return "";
54+
}
55+
56+
/**
57+
* Start the comment manager
58+
*/
59+
public start():void {
60+
this._status = "running";
61+
}
62+
63+
/**
64+
* Stop the comment manager
65+
*/
66+
public stop():void {
67+
this._status = "stopped";
68+
}
69+
70+
/**
71+
* Load a list of comments into the time line
72+
* @param data - list of abstract comment data
73+
*/
74+
public load(data:Array<Object>):void {
75+
this.timeline = data;
76+
77+
}
78+
79+
/**
80+
* Inserts an abstract comment data into the time line
81+
* @param data - abstract comment data
82+
*/
83+
public insert(data:Object):void {
84+
85+
}
86+
87+
/**
88+
* Clears all comments managed from the stage
89+
*/
90+
public clear():void {
91+
92+
}
93+
94+
/**
95+
* Sends a comment onto the stage
96+
* @param data - abstract comment data
97+
*/
98+
public send(data:Object):void {
99+
100+
}
101+
102+
/**
103+
* Set the bounds for the comment manager
104+
* @param width - width value px
105+
* @param height - height value px
106+
*/
107+
public setBounds(width:number = this.stage.offsetWidth, height:number = this.stage.offsetHeight):void {
108+
this._width = width;
109+
this._height = height;
110+
this.dispatchEvent("resize");
111+
for(var allocator in this._csa){
112+
if(this._csa.hasOwnProperty(allocator)){
113+
var csa:CommentSpaceAllocator = this._csa[allocator];
114+
csa.setBounds(this._width, this._height);
115+
}
116+
}
117+
this.stage.style.perspective = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
118+
// TODO: Remove webkit prefix
119+
this.stage.style["webkitPerspective"] = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
120+
}
121+
122+
/**
123+
* Dispatches an event
124+
* @param name - event name
125+
* @param data - corresponding data
126+
*/
127+
public dispatchEvent(name:String, data:Object = null):void {
128+
if (this._listeners.hasOwnProperty(name)) {
129+
var listenerList:Array<Function> = this._listeners[name];
130+
for (var i = 0; i < listenerList.length; i++) {
131+
try {
132+
listenerList[i](data);
133+
} catch (e) {
134+
console.warn(e);
135+
}
136+
}
137+
}
138+
}
139+
140+
/**
141+
* Add an event listener
142+
* @param name - event name
143+
* @param listener - listener function
144+
*/
145+
public addEventListener(name:String, listener:Function):void {
146+
if (this._listeners.hasOwnProperty(name)) {
147+
(Array<Function>)(this._listeners[name]).push(listener);
148+
} else {
149+
this._listeners[name] = [listener];
150+
}
151+
}
152+
153+
public finish(cmt:IComment):void {
154+
155+
}
156+
157+
}

src/core/CommentSpaceAllocator.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/**
22
* Basic Space Allocation Unit
3-
* For Static and Movable comments
3+
*
4+
* @author Jim Chen (jabbany)
5+
* @license MIT License
6+
* @description Comment space allocation units for static and movable comments
47
*/
58
/// <reference path="Core.d.ts" />
69
interface ISpaceAllocator {

src/core/Core.d.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
/**
22
* Core Definitions file
3+
*
4+
* @author Jim Chen
5+
* @description Definitions file for interfaces used in CCL
36
*/
47

58
interface IBinArray {
9+
/**
10+
* Binary insert into array
11+
* @param arr - target array
12+
* @param inserted - element to be inserted
13+
* @param how - comparison function
14+
*/
615
binsert(arr:Array<any>, inserted:any, how:Function):number;
16+
/**
17+
* Binary loose search
18+
* @param arr - array to look in
19+
* @param what - object to try to find
20+
* @param how - comparison function
21+
*/
722
bsearch(arr:Array<any>, what:any, how:Function):number;
823
}
924
declare var BinArray:IBinArray;
@@ -20,10 +35,14 @@ interface CCLOptions {
2035
}
2136
}
2237

23-
interface CommentManager {
38+
interface ICommentManager {
2439
width:number;
2540
height:number;
2641
options:CCLOptions;
42+
/**
43+
* Cleanup the given comment since it has finished
44+
* @param c - IComment
45+
*/
2746
finish(c:IComment):void;
2847
}
2948

0 commit comments

Comments
 (0)