Skip to content

Commit

Permalink
setEventListener supports adding multiple event listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-vanderheijden committed Dec 6, 2023
1 parent 88cbbf5 commit d344594
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
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

0 comments on commit d344594

Please sign in to comment.