Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change setEventListener to support adding multiple event listeners. #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/model/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class Node {
/** @internal */
protected _visible: boolean;
/** @internal */
protected _listeners: Record<string, (params: any) => void>;
protected _listeners: Record<string, ((params: any) => void)[]>;
/** @internal */
protected _dirty: boolean = false;
/** @internal */
Expand Down Expand Up @@ -83,14 +83,38 @@ export abstract class Node {
return Orientation.flip(this._parent.getOrientation());
}
}

// event can be: resize, visibility, maximize (on tabset), close
setEventListener(event: string, callback: (params: any) => void) {
this._listeners[event] = callback;

/**
* Adds the given event listener. Can optionally replace existing listeners.
* @param event valid events: resize, visibility, save, close
* @param callback
* @param replace if set to true, existing listeners will be cleared
*/
setEventListener(event: string, callback: (params: any) => void, replace:boolean=true) {
if (replace || this._listeners[event] === undefined) {
this._listeners[event] = [callback];
} else {
this._listeners[event].push(callback);
}
}

removeEventListener(event: string) {
delete this._listeners[event];
/**
* If a callback is provided, it will be removed from the event.
* If no callback is provided, all callbacks will be removed from the event.
* @param event valid events: resize, visibility, save, close
* @param callback the specific callback to remove
*/
removeEventListener(event: string, callback?: (params: any) => void) {
if (this._listeners[event] !== undefined) {
if (callback) {
const index = this._listeners[event].indexOf(callback);
if (index !== -1) {
this._listeners[event].splice(index, 1);
}
} else {
this._listeners[event] = [];
}
}
}

abstract toJson(): IJsonRowNode | IJsonBorderNode | IJsonTabSetNode | IJsonTabNode | undefined;
Expand All @@ -104,7 +128,9 @@ export abstract class Node {
_fireEvent(event: string, params: any) {
// console.log(this._type, " fireEvent " + event + " " + JSON.stringify(params));
if (this._listeners[event] !== undefined) {
this._listeners[event](params);
for(const callback of this._listeners[event]) {
callback(params);
}
}
}

Expand Down
27 changes: 21 additions & 6 deletions test/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,32 @@ describe("Tree", function () {
});

it("close tab", () => {
let closed = false;
tab("/ts0/t0").setEventListener("close", () => { closed = true; })
let closed1 = false;
let closed2 = false;
let closed3 = false;
let closed4 = false;
tab("/ts0/t0").setEventListener("close", () => { closed1 = true; }, false);
tab("/ts0/t0").setEventListener("close", () => { closed2 = true; });
tab("/ts0/t0").setEventListener("close", () => { closed3 = true; }, false);
const close4Callback = () => { closed4 = true; }
tab("/ts0/t0").setEventListener("close", close4Callback, false);
tab("/ts0/t0").removeEventListener("close", close4Callback);
doAction(Actions.deleteTab(tab("/ts0/t0").getId()));
expect(closed).equals(true);
expect(closed1).equals(false);
expect(closed2).equals(true);
expect(closed3).equals(true);
expect(closed4).equals(false);
});

it("save tab", () => {
let saved = false;
tab("/ts0/t0").setEventListener("save", () => { saved = true; })
let saved1 = false;
let saved2 = false;
tab("/ts0/t0").setEventListener("save", () => { saved1 = true; });
tab("/ts0/t0").removeEventListener("save");
tab("/ts0/t0").setEventListener("save", () => { saved2 = true; }, false);
model.toJson();
expect(saved).equals(true);
expect(saved1).equals(false);
expect(saved2).equals(true);
});

it("visibility tab", () => {
Expand Down