web-window-manager 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
File without changes
@@ -0,0 +1,29 @@
1
+ import { CacheObject, WindowManagerCacheType } from './WindowManagerCache';
2
+ declare class WindowManagerCache extends Object implements WindowManagerCacheType {
3
+ #private;
4
+ constructor(win: Window);
5
+ removeItemFromCache(item: string): void;
6
+ addItemToCache(item: string): void;
7
+ getCache(): CacheObject;
8
+ init(): this;
9
+ /**
10
+ * Takes in an array of window names and sets the key of `this.#win.name` to the new array
11
+ * @param data an array of window names
12
+ */
13
+ private hardSetCache;
14
+ /**
15
+ * sets the cache to an empty array
16
+ */
17
+ private clearCache;
18
+ /**
19
+ * Adds a CacheObject to session storage for window name persistance
20
+ * @param data the CacheObject to update the browser cache to
21
+ */
22
+ private updateStorage;
23
+ /**
24
+ * pulls the cache from session storage. If there is no cache, returns a falsy value
25
+ * @returns a CacheObject or undefined
26
+ */
27
+ private getFromStorage;
28
+ }
29
+ export default WindowManagerCache;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _WindowManagerCache_dataKey, _WindowManagerCache_win, _WindowManagerCache_cache;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const lodash_1 = require("lodash");
16
+ class WindowManagerCache extends Object {
17
+ constructor(win) {
18
+ super();
19
+ _WindowManagerCache_dataKey.set(this, void 0);
20
+ _WindowManagerCache_win.set(this, void 0);
21
+ _WindowManagerCache_cache.set(this, void 0);
22
+ __classPrivateFieldSet(this, _WindowManagerCache_dataKey, 'window_manager_cache', "f");
23
+ __classPrivateFieldSet(this, _WindowManagerCache_win, win, "f");
24
+ __classPrivateFieldSet(this, _WindowManagerCache_cache, new Proxy({ [__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name]: [] }, {
25
+ get: (target, property) => target[property],
26
+ set: (target, property, value) => {
27
+ target[property] = value;
28
+ this.updateStorage(target);
29
+ return true;
30
+ }
31
+ }), "f");
32
+ }
33
+ removeItemFromCache(item) {
34
+ const cache = this.getCache();
35
+ if ((0, lodash_1.isEmpty)(cache))
36
+ return;
37
+ this.hardSetCache(cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name].filter(name => item !== name));
38
+ }
39
+ addItemToCache(item) {
40
+ const cache = this.getCache();
41
+ const temp = cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name];
42
+ cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name] = [...temp, item];
43
+ }
44
+ getCache() {
45
+ return __classPrivateFieldGet(this, _WindowManagerCache_cache, "f");
46
+ }
47
+ init() {
48
+ const cacheData = this.getFromStorage();
49
+ cacheData
50
+ ? this.hardSetCache((0, lodash_1.get)(cacheData, __classPrivateFieldGet(this, _WindowManagerCache_win, "f").name, []))
51
+ : this.clearCache();
52
+ return this;
53
+ }
54
+ /**
55
+ * Takes in an array of window names and sets the key of `this.#win.name` to the new array
56
+ * @param data an array of window names
57
+ */
58
+ hardSetCache(data) {
59
+ __classPrivateFieldGet(this, _WindowManagerCache_cache, "f")[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name] = data;
60
+ }
61
+ /**
62
+ * sets the cache to an empty array
63
+ */
64
+ clearCache() {
65
+ this.hardSetCache([]);
66
+ }
67
+ /**
68
+ * Adds a CacheObject to session storage for window name persistance
69
+ * @param data the CacheObject to update the browser cache to
70
+ */
71
+ updateStorage(data) {
72
+ sessionStorage.setItem(__classPrivateFieldGet(this, _WindowManagerCache_dataKey, "f"), JSON.stringify(data));
73
+ }
74
+ /**
75
+ * pulls the cache from session storage. If there is no cache, returns a falsy value
76
+ * @returns a CacheObject or undefined
77
+ */
78
+ getFromStorage() {
79
+ const data = sessionStorage.getItem(__classPrivateFieldGet(this, _WindowManagerCache_dataKey, "f"));
80
+ if (!data)
81
+ return;
82
+ const cache = JSON.parse(data);
83
+ return cache;
84
+ }
85
+ }
86
+ _WindowManagerCache_dataKey = new WeakMap(), _WindowManagerCache_win = new WeakMap(), _WindowManagerCache_cache = new WeakMap();
87
+ exports.default = WindowManagerCache;
package/lib/index.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ import { WindowManagerType } from './WindowManager';
2
+ declare global {
3
+ interface Window {
4
+ /**
5
+ * A window manager that keeps track of all open child windows, opened by this instance.
6
+ * It is responsible for traversing the tree of windows and closing from furthest depth first.
7
+ *
8
+ * This class will instantiate itself and attach itself to the window object on first import
9
+ *
10
+ * Add an import to your entry file like so:
11
+ * ``` typescript
12
+ * import 'PATH_TO_WINDOW_MANAGER'
13
+ * ```
14
+ *
15
+ * Then use the managers available functions by accessing it via the window object
16
+ * ``` typescript
17
+ * window.WindowManager.CallFunction()
18
+ * ```
19
+ *
20
+ * @example ``` typescript
21
+ * // you can use this when logging out if you want to ensure all child windows are closed
22
+ * const logout = () => {
23
+ * // do some stuff
24
+ * window.WindowManager.recursivelyCloseChildren()
25
+ * }
26
+ * ```
27
+ */
28
+ WindowManager: WindowManager;
29
+ }
30
+ }
31
+ /**
32
+ * A window manager that keeps track of all open child windows, opened by this instance.
33
+ */
34
+ declare class WindowManager implements WindowManagerType {
35
+ #private;
36
+ constructor(win: Window);
37
+ openWindow: ({ link, name }: {
38
+ link: string;
39
+ name?: string | undefined;
40
+ }) => void;
41
+ popThisWindowFromParent: (childWindowName: string) => void;
42
+ recursivelyCloseChildren: (id?: number) => void;
43
+ newUnloadCallback(cb: () => void): void;
44
+ init: () => this;
45
+ /**
46
+ * Gets the length of `this.#children` and type casts it to boolean
47
+ * @returns boolean
48
+ */
49
+ private get hasChildren();
50
+ /**
51
+ * Calls `this.#win.close()` on this instance to close itself
52
+ */
53
+ private close;
54
+ /**
55
+ * Takes a window reference and adds it
56
+ */
57
+ private setChild;
58
+ /**
59
+ * Removes this window from the parent window's window manager.
60
+ * It calls each callback assigned to `this.#onUnloadCallbacks`
61
+ */
62
+ private handleUnload;
63
+ }
64
+ declare const NewWindowManagerInstance: WindowManager;
65
+ export default NewWindowManagerInstance;
package/lib/index.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _WindowManager_win, _WindowManager_newWindowDetails, _WindowManager_children, _WindowManager_windowManagerCache, _WindowManager_onUnloadCallbacks, _WindowManager_parent;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const WindowManagerCache_1 = require("./WindowManagerCache");
16
+ const lodash_1 = require("lodash");
17
+ /**
18
+ * A window manager that keeps track of all open child windows, opened by this instance.
19
+ */
20
+ class WindowManager {
21
+ constructor(win) {
22
+ _WindowManager_win.set(this, void 0);
23
+ _WindowManager_newWindowDetails.set(this, void 0);
24
+ _WindowManager_children.set(this, void 0);
25
+ _WindowManager_windowManagerCache.set(this, void 0);
26
+ _WindowManager_onUnloadCallbacks.set(this, void 0);
27
+ _WindowManager_parent.set(this, void 0);
28
+ this.openWindow = ({ link, name }) => {
29
+ var _a, _b, _c;
30
+ const childWindow = __classPrivateFieldGet(this, _WindowManager_win, "f").open(link, name || link, `
31
+ width=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").WIDTH},
32
+ height=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").HEIGHT},
33
+ left=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").LEFT},
34
+ top=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").TOP},
35
+ resizable=yes,
36
+ scrollbars=yes
37
+ `);
38
+ if (!childWindow)
39
+ return;
40
+ this.setChild(childWindow);
41
+ if (link)
42
+ return (_a = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _a === void 0 ? void 0 : _a.addItemToCache(childWindow.name);
43
+ if (((_b = childWindow.document) === null || _b === void 0 ? void 0 : _b.URL) !== 'about:blank')
44
+ return;
45
+ childWindow.close();
46
+ (_c = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _c === void 0 ? void 0 : _c.removeItemFromCache(name);
47
+ };
48
+ this.popThisWindowFromParent = (childWindowName) => {
49
+ var _a;
50
+ __classPrivateFieldSet(this, _WindowManager_children, (0, lodash_1.remove)(__classPrivateFieldGet(this, _WindowManager_children, "f"), kid => kid.name !== childWindowName), "f");
51
+ (_a = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _a === void 0 ? void 0 : _a.removeItemFromCache(childWindowName);
52
+ };
53
+ this.recursivelyCloseChildren = (id = 0) => {
54
+ if (this.hasChildren) {
55
+ const copy = [...__classPrivateFieldGet(this, _WindowManager_children, "f")];
56
+ (0, lodash_1.forEachRight)(copy, child => {
57
+ var _a;
58
+ (_a = child === null || child === void 0 ? void 0 : child.WindowManager) === null || _a === void 0 ? void 0 : _a.recursivelyCloseChildren(id + 1);
59
+ });
60
+ }
61
+ if (!id)
62
+ return;
63
+ this.close();
64
+ };
65
+ this.init = () => {
66
+ window.addEventListener('beforeunload', e => this.handleUnload());
67
+ __classPrivateFieldGet(this, _WindowManager_win, "f").WindowManager = this;
68
+ if (!__classPrivateFieldGet(this, _WindowManager_win, "f").name)
69
+ __classPrivateFieldGet(this, _WindowManager_win, "f").name = 'main';
70
+ __classPrivateFieldSet(this, _WindowManager_windowManagerCache, new WindowManagerCache_1.default(__classPrivateFieldGet(this, _WindowManager_win, "f")).init(), "f");
71
+ (0, lodash_1.forEach)(__classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f").getCache()[__classPrivateFieldGet(this, _WindowManager_win, "f").name], name => this.openWindow({ link: '', name }));
72
+ return this;
73
+ };
74
+ /**
75
+ * Calls `this.#win.close()` on this instance to close itself
76
+ */
77
+ this.close = () => {
78
+ const p = __classPrivateFieldGet(this, _WindowManager_parent, "f");
79
+ if (!!p)
80
+ p.WindowManager.popThisWindowFromParent(__classPrivateFieldGet(this, _WindowManager_win, "f").name);
81
+ __classPrivateFieldGet(this, _WindowManager_win, "f").close();
82
+ };
83
+ __classPrivateFieldSet(this, _WindowManager_win, win, "f");
84
+ __classPrivateFieldSet(this, _WindowManager_parent, win.opener, "f");
85
+ __classPrivateFieldSet(this, _WindowManager_children, [], "f");
86
+ __classPrivateFieldSet(this, _WindowManager_onUnloadCallbacks, [], "f");
87
+ __classPrivateFieldSet(this, _WindowManager_newWindowDetails, {
88
+ WIDTH: 900,
89
+ HEIGHT: 600,
90
+ LEFT: 0,
91
+ RIGHT: 0
92
+ }, "f");
93
+ }
94
+ newUnloadCallback(cb) {
95
+ __classPrivateFieldGet(this, _WindowManager_onUnloadCallbacks, "f").push(cb);
96
+ }
97
+ /**
98
+ * Gets the length of `this.#children` and type casts it to boolean
99
+ * @returns boolean
100
+ */
101
+ get hasChildren() {
102
+ return !!__classPrivateFieldGet(this, _WindowManager_children, "f").length;
103
+ }
104
+ /**
105
+ * Takes a window reference and adds it
106
+ */
107
+ setChild(child) {
108
+ __classPrivateFieldSet(this, _WindowManager_children, [...new Set([...__classPrivateFieldGet(this, _WindowManager_children, "f"), child])].filter(kid => !kid.closed), "f");
109
+ }
110
+ /**
111
+ * Removes this window from the parent window's window manager.
112
+ * It calls each callback assigned to `this.#onUnloadCallbacks`
113
+ */
114
+ handleUnload() {
115
+ __classPrivateFieldGet(this, _WindowManager_parent, "f").WindowManager.popThisWindowFromParent(__classPrivateFieldGet(this, _WindowManager_win, "f").name);
116
+ (0, lodash_1.forEach)(__classPrivateFieldGet(this, _WindowManager_onUnloadCallbacks, "f"), cb => cb());
117
+ }
118
+ }
119
+ _WindowManager_win = new WeakMap(), _WindowManager_newWindowDetails = new WeakMap(), _WindowManager_children = new WeakMap(), _WindowManager_windowManagerCache = new WeakMap(), _WindowManager_onUnloadCallbacks = new WeakMap(), _WindowManager_parent = new WeakMap();
120
+ const NewWindowManagerInstance = new WindowManager(window).init();
121
+ window.WindowManager = NewWindowManagerInstance;
122
+ exports.default = NewWindowManagerInstance;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "web-window-manager",
3
+ "version": "1.0.3",
4
+ "description": "A window manager for maintaining window parent child relationships",
5
+ "main": "lib/index.js",
6
+ "author": "Triston Armstrong",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "start": "tsc --watch",
10
+ "prepublishOnly": "npm run fix",
11
+ "publish": "npm run build",
12
+ "build": "npm run clean && tsc",
13
+ "fix": "npm run lint; npm run format",
14
+ "clean": "tsc --build --clean",
15
+ "format": "prettier --write \"src/**/*.ts\"",
16
+ "lint": "tslint -p tsconfig.json"
17
+ },
18
+ "dependencies": {
19
+ "@types/lodash": "^4.14.185",
20
+ "lodash": "^4.17.21"
21
+ },
22
+ "files": [
23
+ "lib/**/*"
24
+ ],
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/Tarmstrong95/WebWindowManager.git"
28
+ },
29
+ "keywords": [
30
+ "web",
31
+ "window",
32
+ "manager",
33
+ "helper"
34
+ ],
35
+ "bugs": {
36
+ "url": "https://github.com/Tarmstrong95/WebWindowManager/issues"
37
+ },
38
+ "homepage": "https://github.com/Tarmstrong95/WebWindowManager#readme",
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org"
41
+ },
42
+ "devDependencies": {
43
+ "prettier": "^2.8.0",
44
+ "tslint": "^6.1.3",
45
+ "tslint-config-prettier": "^1.18.0",
46
+ "typescript": "^4.9.3"
47
+ }
48
+ }