Skip to content

Commit 67d440f

Browse files
author
Stanimir Karoserov
committed
added event handling code
1 parent e7fd4e7 commit 67d440f

File tree

3 files changed

+150
-21
lines changed

3 files changed

+150
-21
lines changed

BCL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
</TypeScriptCompile>
259259
<TypeScriptCompile Include="ui\dialogs\index.ts" />
260260
<TypeScriptCompile Include="ui\dialogs\dialogs-common.ts" />
261+
<TypeScriptCompile Include="ui\core\event-manager.ts" />
261262
<Content Include="_references.ts" />
262263
<Content Include="image-source\Readme.md" />
263264
<Content Include="http\Readme.md" />

ui/core/event-manager.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+

2+
/**
3+
This is currently for reference only. Use Observable instead.
4+
Unlike Observable, EventManager provides additional user data (called context)
5+
6+
Usage:
7+
8+
```
9+
var eventManagerModule = require("ui/core/event-manager");
10+
11+
var eventManager = new eventManagerModule.EventManager();
12+
eventManager.on("click", function(eventData, context) {
13+
console.log("clicked with data: " + eventData + ", context: " + context);
14+
}, "context");
15+
16+
eventManager.emit("click");
17+
eventManager.emit("click", "click1");
18+
19+
var f = function(eventData, context) {
20+
console.log("tested with data: " + eventData + ", context: " + context);
21+
};
22+
23+
eventManager.on("test1, click", f);
24+
25+
eventManager.emit("click", "should be doubled");
26+
eventManager.emit("test1");
27+
28+
eventManager.off("click", f);
29+
eventManager.emit("click", "click3");
30+
31+
eventManager.off("test1");
32+
eventManager.emit("test1", "test3");
33+
```
34+
*/
35+
export interface EventHandler {
36+
(eventData?: any, context?: any): void;
37+
}
38+
39+
interface Event {
40+
func: EventHandler;
41+
context: any;
42+
}
43+
44+
export class EventManager {
45+
46+
private events: any = {};
47+
48+
public on(events: string, func: EventHandler, context?: any) {
49+
var eventNames: Array<string> = events.split(",");
50+
var that = this;
51+
eventNames.forEach(function (event: string) {
52+
var eventTrimmed = event.trim();
53+
var newEvent: Event = { func: func, context: context };
54+
var ev: Array<Event> = that.events[eventTrimmed];
55+
if (!ev) {
56+
ev = [newEvent];
57+
that.events[eventTrimmed] = ev;
58+
}
59+
else {
60+
ev.push(newEvent);
61+
}
62+
});
63+
}
64+
65+
public off(events: string, func?: EventHandler) {
66+
var eventNames:Array<string> = events.split(",");
67+
var that = this;
68+
eventNames.forEach(function (event: string) {
69+
var eventTrimmed = event.trim();
70+
if (!func) {
71+
that.events[eventTrimmed] = undefined;
72+
}
73+
else {
74+
var ev: Array<Event> = that.events[eventTrimmed];
75+
if (ev) {
76+
ev.forEach(function (e:Event, idx:number) {
77+
if (e.func == func) {
78+
ev.splice(idx, 1);
79+
return;
80+
}
81+
});
82+
}
83+
}
84+
});
85+
}
86+
87+
public emit(events: string, eventData?: any) {
88+
var eventNames:Array<string> = events.split(",");
89+
var that = this;
90+
eventNames.forEach(function (event: string) {
91+
var ev: Array<Event> = that.events[event.trim()];
92+
if (ev) {
93+
ev.forEach(function (e: Event) {
94+
e.func(eventData, e.context);
95+
});
96+
}
97+
});
98+
}
99+
}

ui/core/observable.ts

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
Changed
44
}
55

6-
export interface ChangeData {
6+
export interface EventData {
77
eventName: string;
88
sender: Observable;
9+
}
10+
11+
export interface ChangeData extends EventData {
912
phase?: ChangePhase;
1013
}
1114

@@ -19,29 +22,46 @@ export class Observable {
1922
public static propertyChangeEvent = "propertyChange";
2023
private _observers = {};
2124

25+
public on: (eventNames: string, callback: (data: EventData) => void) => void;
26+
public off: (eventNames: string, callback?: any) => void;
27+
public addListener: (eventNames: string, callback: (data: EventData) => void) => void;
28+
public removeListener: (eventNames: string, callback?: any) => void;
29+
2230
// true to track the Changing phase, false otherwise
2331
private _trackChanging = false;
2432

2533
constructor(body?: any) {
26-
// TODO: Not implemented
34+
this.on = this.addListener = this.addObserver;
35+
this.off = this.removeListener = this.removeObserver;
2736
}
2837

29-
public addObserver(eventName: string, callback: (data: ChangeData) => void) {
30-
this.verifyCallback(callback);
31-
var list = this.getEventList(eventName, true);
32-
list.push(callback);
38+
public addObserver(eventNames: string, callback: (data: EventData) => void) {
39+
Observable.verifyCallback(callback);
40+
var events: Array<string> = eventNames.split(",");
41+
var that = this;
42+
events.forEach(function (event: string) {
43+
var list = that.getEventList(event.trim(), true);
44+
list.push(callback);
45+
});
3346
}
3447

35-
public removeObserver(eventName: string, callback: any) {
36-
var list = this.getEventList(eventName, false);
37-
if (!list) {
38-
return;
39-
}
40-
41-
var index = list.indexOf(callback);
42-
if (index >= 0) {
43-
list.splice(index, 1);
44-
}
48+
public removeObserver(eventNames: string, callback?: any) {
49+
var events: Array<string> = eventNames.split(",");
50+
var that = this;
51+
events.forEach(function (event: string) {
52+
if (callback) {
53+
var list = that.getEventList(event.trim(), false);
54+
if (list) {
55+
var index = list.indexOf(callback);
56+
if (index >= 0) {
57+
list.splice(index, 1);
58+
}
59+
}
60+
}
61+
else {
62+
that._observers[event.trim()] = undefined;
63+
}
64+
});
4565
}
4666

4767
public setProperty(name: string, value: any) {
@@ -77,7 +97,7 @@ export class Observable {
7797
}
7898

7999
// The method will return true if the change is accepted, false otherwise
80-
public notify(data: ChangeData) {
100+
public notify(data: EventData) {
81101
var observers = this.getEventList(data.eventName);
82102
if (!observers) {
83103
return;
@@ -105,23 +125,32 @@ export class Observable {
105125
};
106126
}
107127

108-
private getEventList(eventName: string, createIfNeeded?: boolean): Array<(data: ChangeData) => void> {
128+
private getEventList(eventName: string, createIfNeeded?: boolean): Array<(data: EventData) => void> {
109129
if (!eventName) {
110130
throw new TypeError("EventName must be valid string.");
111131
}
112132

113-
var list = <Array<(data: ChangeData) => void>>this._observers[eventName];
133+
var list = <Array<(data: EventData) => void>>this._observers[eventName];
114134
if (!list && createIfNeeded) {
115-
list = new Array<(data: ChangeData) => void>();
135+
list = [];
116136
this._observers[eventName] = list;
117137
}
118138

119139
return list;
120140
}
121141

122-
private verifyCallback(callback: any) {
142+
private static verifyCallback(callback: any) {
123143
if (!callback || typeof callback !== "function") {
124144
throw new TypeError("Callback must be a valid function.");
125145
}
126146
}
147+
148+
public emit(eventNames: string) {
149+
var events: Array<string> = eventNames.split(",");
150+
var that = this;
151+
events.forEach(function (event: string) {
152+
that.notify({ eventName: event.trim(), sender: this });
153+
});
154+
}
155+
127156
}

0 commit comments

Comments
 (0)