mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
added event handling code
This commit is contained in:
@ -258,6 +258,7 @@
|
|||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="ui\dialogs\index.ts" />
|
<TypeScriptCompile Include="ui\dialogs\index.ts" />
|
||||||
<TypeScriptCompile Include="ui\dialogs\dialogs-common.ts" />
|
<TypeScriptCompile Include="ui\dialogs\dialogs-common.ts" />
|
||||||
|
<TypeScriptCompile Include="ui\core\event-manager.ts" />
|
||||||
<Content Include="_references.ts" />
|
<Content Include="_references.ts" />
|
||||||
<Content Include="image-source\Readme.md" />
|
<Content Include="image-source\Readme.md" />
|
||||||
<Content Include="http\Readme.md" />
|
<Content Include="http\Readme.md" />
|
||||||
|
99
ui/core/event-manager.ts
Normal file
99
ui/core/event-manager.ts
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
This is currently for reference only. Use Observable instead.
|
||||||
|
Unlike Observable, EventManager provides additional user data (called context)
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
```
|
||||||
|
var eventManagerModule = require("ui/core/event-manager");
|
||||||
|
|
||||||
|
var eventManager = new eventManagerModule.EventManager();
|
||||||
|
eventManager.on("click", function(eventData, context) {
|
||||||
|
console.log("clicked with data: " + eventData + ", context: " + context);
|
||||||
|
}, "context");
|
||||||
|
|
||||||
|
eventManager.emit("click");
|
||||||
|
eventManager.emit("click", "click1");
|
||||||
|
|
||||||
|
var f = function(eventData, context) {
|
||||||
|
console.log("tested with data: " + eventData + ", context: " + context);
|
||||||
|
};
|
||||||
|
|
||||||
|
eventManager.on("test1, click", f);
|
||||||
|
|
||||||
|
eventManager.emit("click", "should be doubled");
|
||||||
|
eventManager.emit("test1");
|
||||||
|
|
||||||
|
eventManager.off("click", f);
|
||||||
|
eventManager.emit("click", "click3");
|
||||||
|
|
||||||
|
eventManager.off("test1");
|
||||||
|
eventManager.emit("test1", "test3");
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
export interface EventHandler {
|
||||||
|
(eventData?: any, context?: any): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Event {
|
||||||
|
func: EventHandler;
|
||||||
|
context: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class EventManager {
|
||||||
|
|
||||||
|
private events: any = {};
|
||||||
|
|
||||||
|
public on(events: string, func: EventHandler, context?: any) {
|
||||||
|
var eventNames: Array<string> = events.split(",");
|
||||||
|
var that = this;
|
||||||
|
eventNames.forEach(function (event: string) {
|
||||||
|
var eventTrimmed = event.trim();
|
||||||
|
var newEvent: Event = { func: func, context: context };
|
||||||
|
var ev: Array<Event> = that.events[eventTrimmed];
|
||||||
|
if (!ev) {
|
||||||
|
ev = [newEvent];
|
||||||
|
that.events[eventTrimmed] = ev;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ev.push(newEvent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public off(events: string, func?: EventHandler) {
|
||||||
|
var eventNames:Array<string> = events.split(",");
|
||||||
|
var that = this;
|
||||||
|
eventNames.forEach(function (event: string) {
|
||||||
|
var eventTrimmed = event.trim();
|
||||||
|
if (!func) {
|
||||||
|
that.events[eventTrimmed] = undefined;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var ev: Array<Event> = that.events[eventTrimmed];
|
||||||
|
if (ev) {
|
||||||
|
ev.forEach(function (e:Event, idx:number) {
|
||||||
|
if (e.func == func) {
|
||||||
|
ev.splice(idx, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public emit(events: string, eventData?: any) {
|
||||||
|
var eventNames:Array<string> = events.split(",");
|
||||||
|
var that = this;
|
||||||
|
eventNames.forEach(function (event: string) {
|
||||||
|
var ev: Array<Event> = that.events[event.trim()];
|
||||||
|
if (ev) {
|
||||||
|
ev.forEach(function (e: Event) {
|
||||||
|
e.func(eventData, e.context);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,12 @@
|
|||||||
Changed
|
Changed
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ChangeData {
|
export interface EventData {
|
||||||
eventName: string;
|
eventName: string;
|
||||||
sender: Observable;
|
sender: Observable;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ChangeData extends EventData {
|
||||||
phase?: ChangePhase;
|
phase?: ChangePhase;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,29 +22,46 @@ export class Observable {
|
|||||||
public static propertyChangeEvent = "propertyChange";
|
public static propertyChangeEvent = "propertyChange";
|
||||||
private _observers = {};
|
private _observers = {};
|
||||||
|
|
||||||
|
public on: (eventNames: string, callback: (data: EventData) => void) => void;
|
||||||
|
public off: (eventNames: string, callback?: any) => void;
|
||||||
|
public addListener: (eventNames: string, callback: (data: EventData) => void) => void;
|
||||||
|
public removeListener: (eventNames: string, callback?: any) => void;
|
||||||
|
|
||||||
// true to track the Changing phase, false otherwise
|
// true to track the Changing phase, false otherwise
|
||||||
private _trackChanging = false;
|
private _trackChanging = false;
|
||||||
|
|
||||||
constructor(body?: any) {
|
constructor(body?: any) {
|
||||||
// TODO: Not implemented
|
this.on = this.addListener = this.addObserver;
|
||||||
|
this.off = this.removeListener = this.removeObserver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addObserver(eventName: string, callback: (data: ChangeData) => void) {
|
public addObserver(eventNames: string, callback: (data: EventData) => void) {
|
||||||
this.verifyCallback(callback);
|
Observable.verifyCallback(callback);
|
||||||
var list = this.getEventList(eventName, true);
|
var events: Array<string> = eventNames.split(",");
|
||||||
list.push(callback);
|
var that = this;
|
||||||
|
events.forEach(function (event: string) {
|
||||||
|
var list = that.getEventList(event.trim(), true);
|
||||||
|
list.push(callback);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeObserver(eventName: string, callback: any) {
|
public removeObserver(eventNames: string, callback?: any) {
|
||||||
var list = this.getEventList(eventName, false);
|
var events: Array<string> = eventNames.split(",");
|
||||||
if (!list) {
|
var that = this;
|
||||||
return;
|
events.forEach(function (event: string) {
|
||||||
}
|
if (callback) {
|
||||||
|
var list = that.getEventList(event.trim(), false);
|
||||||
var index = list.indexOf(callback);
|
if (list) {
|
||||||
if (index >= 0) {
|
var index = list.indexOf(callback);
|
||||||
list.splice(index, 1);
|
if (index >= 0) {
|
||||||
}
|
list.splice(index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
that._observers[event.trim()] = undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public setProperty(name: string, value: any) {
|
public setProperty(name: string, value: any) {
|
||||||
@ -77,7 +97,7 @@ export class Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The method will return true if the change is accepted, false otherwise
|
// The method will return true if the change is accepted, false otherwise
|
||||||
public notify(data: ChangeData) {
|
public notify(data: EventData) {
|
||||||
var observers = this.getEventList(data.eventName);
|
var observers = this.getEventList(data.eventName);
|
||||||
if (!observers) {
|
if (!observers) {
|
||||||
return;
|
return;
|
||||||
@ -105,23 +125,32 @@ export class Observable {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private getEventList(eventName: string, createIfNeeded?: boolean): Array<(data: ChangeData) => void> {
|
private getEventList(eventName: string, createIfNeeded?: boolean): Array<(data: EventData) => void> {
|
||||||
if (!eventName) {
|
if (!eventName) {
|
||||||
throw new TypeError("EventName must be valid string.");
|
throw new TypeError("EventName must be valid string.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var list = <Array<(data: ChangeData) => void>>this._observers[eventName];
|
var list = <Array<(data: EventData) => void>>this._observers[eventName];
|
||||||
if (!list && createIfNeeded) {
|
if (!list && createIfNeeded) {
|
||||||
list = new Array<(data: ChangeData) => void>();
|
list = [];
|
||||||
this._observers[eventName] = list;
|
this._observers[eventName] = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private verifyCallback(callback: any) {
|
private static verifyCallback(callback: any) {
|
||||||
if (!callback || typeof callback !== "function") {
|
if (!callback || typeof callback !== "function") {
|
||||||
throw new TypeError("Callback must be a valid function.");
|
throw new TypeError("Callback must be a valid function.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public emit(eventNames: string) {
|
||||||
|
var events: Array<string> = eventNames.split(",");
|
||||||
|
var that = this;
|
||||||
|
events.forEach(function (event: string) {
|
||||||
|
that.notify({ eventName: event.trim(), sender: this });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user