Skip to content

Commit

Permalink
Update tests & CCL Manager
Browse files Browse the repository at this point in the history
Prepare for the move to typescript when compiling the main library (the comment manager)
  • Loading branch information
jabbany committed Nov 26, 2014
1 parent 9bd7d64 commit ef6a03c
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 7 deletions.
18 changes: 17 additions & 1 deletion spec/CommentCoreLibrary_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe 'CommentManager', ->

describe '.start', ->
it 'starts the timer', ->
manager.start()
manager.start()
# TODO: figure out how to test the timer
# maybe just add spy on window.setInterval
Expand All @@ -90,3 +89,20 @@ describe 'CommentManager', ->
expect(manager.timeline).toEqual [c3, c5]
manager.insert c4
expect(manager.timeline).toEqual [c3, c4 , c5]

describe '.addEventListener .dispatchEvent', ->
it 'add one event listener', ->
hasDispatchedEvent = false
manager.addEventListener 'myCustomEvent', ->
hasDispatchedEvent = true
manager.dispatchEvent 'myCustomEvent'
expect(hasDispatchedEvent).toBe true

it 'add multiple event listeners', ->
dispatchedEventId = 0
manager.addEventListener 'myCustomEvent', ->
dispatchedEventId = 1
manager.addEventListener 'myCustomEvent', ->
dispatchedEventId = 2
manager.dispatchEvent 'myCustomEvent'
expect(dispatchedEventId).toBe 2
23 changes: 23 additions & 0 deletions spec/core/CommentSpaceAllocator_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,48 @@ describe 'CommentSpaceAllocators', ->
describe 'AnchorCommentSpaceAllocator', ->
it 'comments always collide', ->
expect(anchorCSA.willCollide(c1, c2)).toBe true

it 'same comment must collide with self', ->
expect(anchorCSA.willCollide(c1, c1)).toBe true
expect(anchorCSA.willCollide(c2, c2)).toBe true

it 'path check passes for y = 0 in empty pool', ->
expect(anchorCSA.pathCheck(0, c1, [])).toBe true

it 'path check fails for y = 0 in self pool', ->
expect(anchorCSA.pathCheck(0, c1, [c1])).toBe false

it 'path check fails for y = height + 1 in self pool', ->
expect(anchorCSA.pathCheck(c1.height + 1, c1, [c1])).toBe true

it 'removal for cindex < 0', ->
c1.cindex = -1
expect(typeof anchorCSA.remove(c1)).toBe 'undefined'

it 'removal for cindex < 0', ->
c1.cindex = -1
expect(typeof anchorCSA.remove(c1)).toBe 'undefined'
# TODO: We need more extensive test cases

describe 'CommentSpaceAllocator', ->
it 'same comment must collide with self', ->
expect(scrollCSA.willCollide(s1, s1)).toBe true
expect(scrollCSA.willCollide(s2, s2)).toBe true

it 'path check passes for y = 0 in empty pool', ->
expect(scrollCSA.pathCheck(0, s1, [])).toBe true

it 'path check passes for y = 0 in self pool', ->
expect(scrollCSA.pathCheck(0, s1, [s1])).toBe false

it 'path check passes for y = height + 1 in self pool', ->
expect(scrollCSA.pathCheck(s1.height + 1, s1, [s1])).toBe true

it 'removal for cindex < 0', ->
s1.cindex = -1
expect(typeof scrollCSA.remove(s1)).toBe 'undefined'

it 'removal for cindex < 0', ->
s1.cindex = -1
expect(typeof scrollCSA.remove(s1)).toBe 'undefined'
# TODO: We need more extensive test cases
11 changes: 7 additions & 4 deletions src/core/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* Basic Comment Abstraction
* Built on DOM actions
*
* @author Jim Chen
* @license MIT License
* @description Comment abstraction based on DOM implementation
*/
/// <reference path="Core.d.ts" />
class CoreComment implements IComment {
Expand Down Expand Up @@ -42,10 +45,10 @@ class CoreComment implements IComment {
private _shadow:boolean = true;
private _font:string = "";

public parent:CommentManager;
public parent:ICommentManager;
public dom:HTMLDivElement;

constructor(parent:CommentManager, init:Object = {}) {
constructor(parent:ICommentManager, init:Object = {}) {
if (!parent) {
throw new Error("Comment not bound to comment manager.");
} else {
Expand Down Expand Up @@ -409,7 +412,7 @@ class CoreComment implements IComment {
}

class ScrollComment extends CoreComment {
constructor(parent:CommentManager, data:Object) {
constructor(parent:ICommentManager, data:Object) {
super(parent, data);
this.dur *= this.parent.options.scroll.scale;
this.ttl *= this.parent.options.scroll.scale;
Expand Down
157 changes: 157 additions & 0 deletions src/core/CommentCoreLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* Comment Core Library
*
* @author Jim Chen
* @license MIT License
* @description Comment management unit for CCL
*/
/// <reference path="Core.d.ts" />
class CommentManager implements ICommentManager {
private _width:number = 0;
private _height:number = 0;
private _status:string = "stopped";
private _stage:HTMLDivElement;
private _listeners:Object = {};
private _csa:Object = {};

public options:CCLOptions = {
"global": {
"scale": 1,
"opacity": 1,
"className": "cmt"
},
"scroll": {
"scale": 1,
"opacity": 1
}
};
public timeline:Array<Object> = [];
public runline:Array<IComment> = [];
public position:number = 0;

get width():number {
return this._width;
}

get height():number {
return this._height;
}

get stage():HTMLDivElement {
return this._stage;
}

get status():string {
return this._status;
}

constructor(stage:HTMLDivElement) {
this._stage = stage;
}

public static getCommentType(data:Object):string{
return "";
}

/**
* Start the comment manager
*/
public start():void {
this._status = "running";
}

/**
* Stop the comment manager
*/
public stop():void {
this._status = "stopped";
}

/**
* Load a list of comments into the time line
* @param data - list of abstract comment data
*/
public load(data:Array<Object>):void {
this.timeline = data;

}

/**
* Inserts an abstract comment data into the time line
* @param data - abstract comment data
*/
public insert(data:Object):void {

}

/**
* Clears all comments managed from the stage
*/
public clear():void {

}

/**
* Sends a comment onto the stage
* @param data - abstract comment data
*/
public send(data:Object):void {

}

/**
* Set the bounds for the comment manager
* @param width - width value px
* @param height - height value px
*/
public setBounds(width:number = this.stage.offsetWidth, height:number = this.stage.offsetHeight):void {
this._width = width;
this._height = height;
this.dispatchEvent("resize");
for(var allocator in this._csa){
if(this._csa.hasOwnProperty(allocator)){
var csa:CommentSpaceAllocator = this._csa[allocator];
csa.setBounds(this._width, this._height);
}
}
this.stage.style.perspective = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
// TODO: Remove webkit prefix
this.stage.style["webkitPerspective"] = this.width * Math.tan(40 * Math.PI/180) / 2 + "px";
}

/**
* Dispatches an event
* @param name - event name
* @param data - corresponding data
*/
public dispatchEvent(name:String, data:Object = null):void {
if (this._listeners.hasOwnProperty(name)) {
var listenerList:Array<Function> = this._listeners[name];
for (var i = 0; i < listenerList.length; i++) {
try {
listenerList[i](data);
} catch (e) {
console.warn(e);
}
}
}
}

/**
* Add an event listener
* @param name - event name
* @param listener - listener function
*/
public addEventListener(name:String, listener:Function):void {
if (this._listeners.hasOwnProperty(name)) {
(Array<Function>)(this._listeners[name]).push(listener);
} else {
this._listeners[name] = [listener];
}
}

public finish(cmt:IComment):void {

}

}
5 changes: 4 additions & 1 deletion src/core/CommentSpaceAllocator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* Basic Space Allocation Unit
* For Static and Movable comments
*
* @author Jim Chen (jabbany)
* @license MIT License
* @description Comment space allocation units for static and movable comments
*/
/// <reference path="Core.d.ts" />
interface ISpaceAllocator {
Expand Down
21 changes: 20 additions & 1 deletion src/core/Core.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/**
* Core Definitions file
*
* @author Jim Chen
* @description Definitions file for interfaces used in CCL
*/

interface IBinArray {
/**
* Binary insert into array
* @param arr - target array
* @param inserted - element to be inserted
* @param how - comparison function
*/
binsert(arr:Array<any>, inserted:any, how:Function):number;
/**
* Binary loose search
* @param arr - array to look in
* @param what - object to try to find
* @param how - comparison function
*/
bsearch(arr:Array<any>, what:any, how:Function):number;
}
declare var BinArray:IBinArray;
Expand All @@ -20,10 +35,14 @@ interface CCLOptions {
}
}

interface CommentManager {
interface ICommentManager {
width:number;
height:number;
options:CCLOptions;
/**
* Cleanup the given comment since it has finished
* @param c - IComment
*/
finish(c:IComment):void;
}

Expand Down

0 comments on commit ef6a03c

Please sign in to comment.