Skip to content

Commit

Permalink
adding enablePrintCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin Gupta committed Sep 21, 2018
1 parent 8fa6f30 commit 5aff532
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
32 changes: 22 additions & 10 deletions src/MicrosoftTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ interface Window {
onNativeMessage(evt: MessageEvent): void;
}

/**
* adding ctrl+P and cmd+P handler
*/
document.addEventListener("keydown", function(event: KeyboardEvent): void {
if ((event.ctrlKey || event.metaKey) && event.keyCode === 80) {
microsoftTeams.printHandler();
microsoftTeams.print();
event.cancelBubble = true;
event.preventDefault();
event.stopImmediatePropagation();
Expand Down Expand Up @@ -508,6 +511,7 @@ namespace microsoftTeams {

let themeChangeHandler: (theme: string) => void;
let customPrintHandler: () => void;
let printCapabilityEnabled: boolean = false;
handlers["themeChange"] = handleThemeChange;

let fullScreenChangeHandler: (isFullScreen: boolean) => void;
Expand Down Expand Up @@ -598,23 +602,31 @@ namespace microsoftTeams {
};
}

/**
* enable print capability
*/
export function enablePrintCapability(): void {
printCapabilityEnabled = true;
}

/**
* Registers a handler for print.
* default print handler
*/
export function printHandler(): void {
ensureInitialized();
if (customPrintHandler) {
customPrintHandler();
} else {
window.print();
export function print(): void {
if (printCapabilityEnabled) {
ensureInitialized();
if (customPrintHandler) {
customPrintHandler();
} else {
window.print();
}
}
}

/**
* Registers a handler for print.
* Only one handler can be registered at a time. A subsequent registration replaces an existing registration.
* @param handler The handler to invoke when the user changes their theme.
* Registers a custom handler for print.
* @param handler The handler to invoke when printHandler is called.
*/
export function registerCustomPrintHandler(handler: () => void): void {
ensureInitialized();
Expand Down
24 changes: 20 additions & 4 deletions test/MicrosoftTeams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,22 +599,36 @@ describe("MicrosoftTeams", () => {
expect(handlerCalled).toBe(true);
});

it("print handler should not called if print capability is disabled", () => {
let handlerCalled = false;
microsoftTeams.initialize();
spyOn(window, "print").and.callFake((): void => {
handlerCalled = true;
});

microsoftTeams.print();

expect(handlerCalled).toBe(false);
});

it("print handler should successfully call default print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
microsoftTeams.enablePrintCapability();
spyOn(window, "print").and.callFake((): void => {
handlerCalled = true;
});

microsoftTeams.printHandler();
microsoftTeams.print();

expect(handlerCalled).toBe(true);
});

it("Ctrl+P should successfully call print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
spyOn(microsoftTeams, "printHandler").and.callFake((): void => {
microsoftTeams.enablePrintCapability();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
Expand All @@ -630,7 +644,8 @@ describe("MicrosoftTeams", () => {
it("Cmd+P should successfully call print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
spyOn(microsoftTeams, "printHandler").and.callFake((): void => {
microsoftTeams.enablePrintCapability();
spyOn(microsoftTeams, "print").and.callFake((): void => {
handlerCalled = true;
});
let printEvent = new Event("keydown");
Expand All @@ -646,11 +661,12 @@ describe("MicrosoftTeams", () => {
it("should successfully register a print handler", () => {
let handlerCalled = false;
microsoftTeams.initialize();
microsoftTeams.enablePrintCapability();
microsoftTeams.registerCustomPrintHandler(() => {
handlerCalled = true;
});

microsoftTeams.printHandler();
microsoftTeams.print();

expect(handlerCalled).toBe(true);
});
Expand Down

0 comments on commit 5aff532

Please sign in to comment.