-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve #7857 Move all calls with document and window into separate f…
…ile and add a rule to eslint
- Loading branch information
OlgaLarina
committed
Mar 7, 2024
1 parent
ddb86f5
commit 190db6d
Showing
40 changed files
with
316 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"rules": { | ||
"no-restricted-properties": [ | ||
"error", | ||
{ | ||
"object": "window", | ||
"property": "document" | ||
} | ||
], | ||
"no-restricted-globals": [ | ||
"error", | ||
{ | ||
"name": "document", | ||
"message": "Do not use document into survey-core. Use methods from DomDocumentHelper" | ||
}, | ||
{ | ||
"name": "window", | ||
"message": "Do not use window into survey-core. Use method from DomWindowHelper" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
export class DomWindowHelper { | ||
public static isAvailable(): boolean { | ||
return "undefined" !== typeof window; | ||
} | ||
public static isFileReaderAvailable(): boolean { | ||
if (!DomWindowHelper.isAvailable()) return false; | ||
return !!(<any>window)["FileReader"]; | ||
} | ||
public static getLocation(): Location { | ||
if (!DomWindowHelper.isAvailable()) return; | ||
return window.location; | ||
} | ||
public static getVisualViewport(): VisualViewport | null { | ||
if(!DomWindowHelper.isAvailable()) return null; | ||
return window.visualViewport; | ||
} | ||
public static getInnerWidth(): number { | ||
if(!DomWindowHelper.isAvailable()) return; | ||
return window.innerWidth; | ||
} | ||
public static getInnerHeight(): number { | ||
if(!DomWindowHelper.isAvailable()) return null; | ||
return window.innerHeight; | ||
} | ||
public static getWindow(): Window { | ||
if(!DomWindowHelper.isAvailable()) return; | ||
return window; | ||
} | ||
public static hasOwn(propertyName: string): boolean { | ||
if(!DomWindowHelper.isAvailable()) return; | ||
return propertyName in window; | ||
} | ||
public static getSelection(): Selection | null { | ||
if (DomWindowHelper.isAvailable() && window.getSelection) { | ||
return window.getSelection(); | ||
} | ||
} | ||
public static requestAnimationFrame(callback: FrameRequestCallback): number { | ||
if (DomWindowHelper.isAvailable()) { | ||
return window.requestAnimationFrame(callback); | ||
} | ||
} | ||
public static addEventListener(type: string, listener: (e?: any) => void): void { | ||
if(!DomWindowHelper.isAvailable()) return; | ||
document.addEventListener(type, listener); | ||
} | ||
public static removeEventListener(type: string, listener: (e?: any) => void): void { | ||
if(!DomWindowHelper.isAvailable()) return; | ||
document.removeEventListener(type, listener); | ||
} | ||
} | ||
|
||
export class DomDocumentHelper { | ||
public static isAvailable(): boolean { | ||
return "undefined" !== typeof document; | ||
} | ||
public static getBody(): HTMLElement { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
return document.body; | ||
} | ||
public static getDocumentElement(): HTMLElement { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
return document.documentElement; | ||
} | ||
public static getDocument(): Document { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
return document; | ||
} | ||
public static getCookie(): string { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
return document.cookie; | ||
} | ||
public static setCookie(newCookie: string): void { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
document.cookie = newCookie; | ||
} | ||
public static activeElementBlur(): Document { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
|
||
const activeElement = document.activeElement; | ||
if(!!activeElement && !!(<any>activeElement).blur) { | ||
(<any>activeElement).blur(); | ||
} | ||
} | ||
public static createElement(tagName: string): HTMLElement { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
return document.createElement(tagName); | ||
} | ||
public static getComputedStyle(elt: Element): CSSStyleDeclaration { | ||
if(!DomDocumentHelper.isAvailable()) return new CSSStyleDeclaration(); | ||
return document.defaultView.getComputedStyle(elt); | ||
} | ||
public static addEventListener(type: string, listener: (e?: any) => void): void { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
document.addEventListener(type, listener); | ||
} | ||
public static removeEventListener(type: string, listener: (e?: any) => void): void { | ||
if(!DomDocumentHelper.isAvailable()) return; | ||
document.removeEventListener(type, listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.