From b0f7469b6215023a3b347f4b207e2b3a81cc3833 Mon Sep 17 00:00:00 2001 From: Kenneth Rohde Christiansen Date: Fri, 15 Sep 2017 11:59:31 +0200 Subject: [PATCH] Add definitions for W3C Screen Orientation API --- types/w3c-screen-orientation/index.d.ts | 19 ++++++++++ types/w3c-screen-orientation/tsconfig.json | 23 ++++++++++++ types/w3c-screen-orientation/tslint.json | 1 + .../w3c-screen-orientation-tests.ts | 36 +++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 types/w3c-screen-orientation/index.d.ts create mode 100644 types/w3c-screen-orientation/tsconfig.json create mode 100644 types/w3c-screen-orientation/tslint.json create mode 100644 types/w3c-screen-orientation/w3c-screen-orientation-tests.ts diff --git a/types/w3c-screen-orientation/index.d.ts b/types/w3c-screen-orientation/index.d.ts new file mode 100644 index 0000000000..12954e6f59 --- /dev/null +++ b/types/w3c-screen-orientation/index.d.ts @@ -0,0 +1,19 @@ +// Type definitions for W3C Screen Orientation API 1.0 +// Project: https://www.w3.org/TR/screen-orientation/ +// Definitions by: Kenneth Rohde Christiansen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +type OrientationType = "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary"; +type OrientationLockType = "any" | "natural" | "landscape" | "portrait" | "portrait-primary" | "portrait-secondary" | "landscape-primary" | "landscape-secondary"; + +interface ScreenOrientation extends EventTarget { + lock(orientation: OrientationLockType): Promise; + unlock(): void; + readonly type: OrientationType; + readonly angle: number; + addEventListener(type: "change", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; +} + +interface Screen { + readonly orientation: ScreenOrientation; +} diff --git a/types/w3c-screen-orientation/tsconfig.json b/types/w3c-screen-orientation/tsconfig.json new file mode 100644 index 0000000000..664471c21d --- /dev/null +++ b/types/w3c-screen-orientation/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "w3c-screen-orientation-tests.ts" + ] +} diff --git a/types/w3c-screen-orientation/tslint.json b/types/w3c-screen-orientation/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/w3c-screen-orientation/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/w3c-screen-orientation/w3c-screen-orientation-tests.ts b/types/w3c-screen-orientation/w3c-screen-orientation-tests.ts new file mode 100644 index 0000000000..63963da5ac --- /dev/null +++ b/types/w3c-screen-orientation/w3c-screen-orientation-tests.ts @@ -0,0 +1,36 @@ +// Example 1 (from the spec): +const show = () => { + console.log("Orientation type is " + screen.orientation.type); + console.log("Orientation angle is " + screen.orientation.angle); +}; + +screen.orientation.addEventListener("change", show); +window.onload = show; + +// Example 2 (from the spec): +const start = () => { + document.onfullscreenchange = () => { + screen.orientation.lock('natural').then(startInternal); + }; + document.documentElement.requestFullscreen(); +}; + +// Example 3 (from the spec): +const startInternal = () => {}; + +const start2 = () => { + screen.orientation.lock('landscape-primary').then(startInternal, () => { + alert('To start, rotate your screen to landscape.'); + + const orientationChangeHandler = () => { + if (!screen.orientation.type.startsWith('landscape')) { + return; + } + screen.orientation.removeEventListener('change', orientationChangeHandler); + startInternal(); + }; + + screen.orientation.addEventListener('change', orientationChangeHandler); + }); +}; +window.onload = start2;