web-window-manager 1.0.13 → 1.0.18

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 CHANGED
@@ -1,9 +1,3 @@
1
- <!-- PSA SECTION _____________________________ -->
2
-
3
- # PSA
4
-
5
- THIS PACKAGE IS NOT READY FOR USE IN YOUR PROJECTS
6
-
7
1
  <!-- TODO SECTION _____________________________ -->
8
2
 
9
3
  # TODO
@@ -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,93 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _WindowManagerCache_dataKey, _WindowManagerCache_win, _WindowManagerCache_cache;
13
+ import { isEmpty, get } from 'lodash';
14
+ class WindowManagerCache extends Object {
15
+ constructor(win) {
16
+ super();
17
+ _WindowManagerCache_dataKey.set(this, void 0);
18
+ _WindowManagerCache_win.set(this, void 0);
19
+ _WindowManagerCache_cache.set(this, void 0);
20
+ __classPrivateFieldSet(this, _WindowManagerCache_dataKey, 'window_manager_cache', "f");
21
+ __classPrivateFieldSet(this, _WindowManagerCache_win, win, "f");
22
+ __classPrivateFieldSet(this, _WindowManagerCache_cache, new Proxy({ [__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name]: [] }, {
23
+ get: (target, property) => target[property],
24
+ set: (target, property, value) => {
25
+ target[property] = value;
26
+ this.updateStorage(target);
27
+ return true;
28
+ }
29
+ }), "f");
30
+ }
31
+ removeItemFromCache(item) {
32
+ if (typeof item !== 'string')
33
+ throw new TypeError('WindowManagerCache.removeItemFromCache() item must be a string');
34
+ const cache = this.getCache();
35
+ if (isEmpty(cache))
36
+ return;
37
+ this.hardSetCache(cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name].filter(name => item !== name));
38
+ }
39
+ addItemToCache(item) {
40
+ if (typeof item !== 'string')
41
+ throw new TypeError('WindowManagerCache.addItemFromCache() item must be a string');
42
+ const cache = this.getCache();
43
+ const temp = cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name];
44
+ cache[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name] = [...temp, item];
45
+ }
46
+ getCache() {
47
+ return __classPrivateFieldGet(this, _WindowManagerCache_cache, "f");
48
+ }
49
+ init() {
50
+ const cacheData = this.getFromStorage();
51
+ cacheData
52
+ ? this.hardSetCache(get(cacheData, __classPrivateFieldGet(this, _WindowManagerCache_win, "f").name, []))
53
+ : this.clearCache();
54
+ return this;
55
+ }
56
+ /**
57
+ * Takes in an array of window names and sets the key of `this.#win.name` to the new array
58
+ * @param data an array of window names
59
+ */
60
+ hardSetCache(data) {
61
+ if (typeof data !== 'object')
62
+ throw new TypeError('WindowManagerCache.hardSetCache() data must be an array of string');
63
+ __classPrivateFieldGet(this, _WindowManagerCache_cache, "f")[__classPrivateFieldGet(this, _WindowManagerCache_win, "f").name] = data;
64
+ }
65
+ /**
66
+ * sets the cache to an empty array
67
+ */
68
+ clearCache() {
69
+ this.hardSetCache([]);
70
+ }
71
+ /**
72
+ * Adds a CacheObject to session storage for window name persistance
73
+ * @param data the CacheObject to update the browser cache to
74
+ */
75
+ updateStorage(data) {
76
+ if (typeof data !== 'object')
77
+ throw new TypeError('WindowManagerCache.updateStorage() data must be an object');
78
+ sessionStorage.setItem(__classPrivateFieldGet(this, _WindowManagerCache_dataKey, "f"), JSON.stringify(data));
79
+ }
80
+ /**
81
+ * pulls the cache from session storage. If there is no cache, returns a falsy value
82
+ * @returns a CacheObject or undefined
83
+ */
84
+ getFromStorage() {
85
+ const data = sessionStorage.getItem(__classPrivateFieldGet(this, _WindowManagerCache_dataKey, "f"));
86
+ if (!data)
87
+ return;
88
+ const cache = JSON.parse(data);
89
+ return cache;
90
+ }
91
+ }
92
+ _WindowManagerCache_dataKey = new WeakMap(), _WindowManagerCache_win = new WeakMap(), _WindowManagerCache_cache = new WeakMap();
93
+ export 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: (windowProperties: {
38
+ link: string;
39
+ name?: string;
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,133 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ 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");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _WindowManager_win, _WindowManager_newWindowDetails, _WindowManager_children, _WindowManager_windowManagerCache, _WindowManager_onUnloadCallbacks, _WindowManager_parent;
13
+ import WindowManagerCache from './WindowManagerCache';
14
+ import { remove, forEachRight, forEach } from 'lodash';
15
+ /**
16
+ * A window manager that keeps track of all open child windows, opened by this instance.
17
+ */
18
+ class WindowManager {
19
+ constructor(win) {
20
+ _WindowManager_win.set(this, void 0);
21
+ _WindowManager_newWindowDetails.set(this, void 0);
22
+ _WindowManager_children.set(this, void 0);
23
+ _WindowManager_windowManagerCache.set(this, void 0);
24
+ _WindowManager_onUnloadCallbacks.set(this, void 0);
25
+ _WindowManager_parent.set(this, void 0);
26
+ this.openWindow = (windowProperties) => {
27
+ var _a, _b, _c;
28
+ const { link, name } = windowProperties;
29
+ if (typeof link !== 'string')
30
+ throw new TypeError(`WindowManager.openWindow requires link to be given as a String`);
31
+ if (!['undefined', 'string'].includes(typeof name))
32
+ throw new TypeError(`WindowManager.openWindow requires name to be given as String or undefined`);
33
+ const childWindow = __classPrivateFieldGet(this, _WindowManager_win, "f").open(link, name || link, `
34
+ width=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").WIDTH},
35
+ height=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").HEIGHT},
36
+ left=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").LEFT},
37
+ top=${__classPrivateFieldGet(this, _WindowManager_newWindowDetails, "f").TOP},
38
+ resizable=yes,
39
+ scrollbars=yes
40
+ `);
41
+ if (!childWindow)
42
+ return;
43
+ this.setChild(childWindow);
44
+ if (link)
45
+ return (_a = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _a === void 0 ? void 0 : _a.addItemToCache(childWindow.name);
46
+ if (((_b = childWindow.document) === null || _b === void 0 ? void 0 : _b.URL) !== 'about:blank')
47
+ return;
48
+ childWindow.close();
49
+ (_c = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _c === void 0 ? void 0 : _c.removeItemFromCache(name);
50
+ };
51
+ this.popThisWindowFromParent = (childWindowName) => {
52
+ var _a;
53
+ if (typeof childWindowName !== 'string')
54
+ throw new TypeError('WindowManager.popThisWindowFromParent requires childWindowName to be given as a string');
55
+ __classPrivateFieldSet(this, _WindowManager_children, remove(__classPrivateFieldGet(this, _WindowManager_children, "f"), kid => kid.name !== childWindowName), "f");
56
+ (_a = __classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f")) === null || _a === void 0 ? void 0 : _a.removeItemFromCache(childWindowName);
57
+ };
58
+ this.recursivelyCloseChildren = (id = 0) => {
59
+ if (typeof id !== 'number')
60
+ throw new TypeError('WindowManager.recursivelyCloseChildren requires id to be given as a number');
61
+ if (this.hasChildren) {
62
+ const copy = [...__classPrivateFieldGet(this, _WindowManager_children, "f")];
63
+ forEachRight(copy, child => {
64
+ var _a;
65
+ (_a = child === null || child === void 0 ? void 0 : child.WindowManager) === null || _a === void 0 ? void 0 : _a.recursivelyCloseChildren(id + 1);
66
+ });
67
+ }
68
+ if (!id)
69
+ return;
70
+ this.close();
71
+ };
72
+ this.init = () => {
73
+ window.addEventListener('beforeunload', e => this.handleUnload());
74
+ __classPrivateFieldGet(this, _WindowManager_win, "f").WindowManager = this;
75
+ if (!__classPrivateFieldGet(this, _WindowManager_win, "f").name)
76
+ __classPrivateFieldGet(this, _WindowManager_win, "f").name = 'main';
77
+ __classPrivateFieldSet(this, _WindowManager_windowManagerCache, new WindowManagerCache(__classPrivateFieldGet(this, _WindowManager_win, "f")).init(), "f");
78
+ forEach(__classPrivateFieldGet(this, _WindowManager_windowManagerCache, "f").getCache()[__classPrivateFieldGet(this, _WindowManager_win, "f").name], name => this.openWindow({ link: '', name }));
79
+ return this;
80
+ };
81
+ /**
82
+ * Calls `this.#win.close()` on this instance to close itself
83
+ */
84
+ this.close = () => {
85
+ const p = __classPrivateFieldGet(this, _WindowManager_parent, "f");
86
+ if (!!p)
87
+ p.WindowManager.popThisWindowFromParent(__classPrivateFieldGet(this, _WindowManager_win, "f").name);
88
+ __classPrivateFieldGet(this, _WindowManager_win, "f").close();
89
+ };
90
+ __classPrivateFieldSet(this, _WindowManager_win, win, "f");
91
+ __classPrivateFieldSet(this, _WindowManager_parent, win.opener, "f");
92
+ __classPrivateFieldSet(this, _WindowManager_children, [], "f");
93
+ __classPrivateFieldSet(this, _WindowManager_onUnloadCallbacks, [], "f");
94
+ __classPrivateFieldSet(this, _WindowManager_newWindowDetails, {
95
+ WIDTH: 900,
96
+ HEIGHT: 600,
97
+ LEFT: 0,
98
+ RIGHT: 0
99
+ }, "f");
100
+ }
101
+ newUnloadCallback(cb) {
102
+ if (typeof cb !== 'function')
103
+ throw new TypeError('Windowmanager.newUnloadCallback requires cb to be given as a function');
104
+ __classPrivateFieldGet(this, _WindowManager_onUnloadCallbacks, "f").push(cb);
105
+ }
106
+ /**
107
+ * Gets the length of `this.#children` and type casts it to boolean
108
+ * @returns boolean
109
+ */
110
+ get hasChildren() {
111
+ return !!__classPrivateFieldGet(this, _WindowManager_children, "f").length;
112
+ }
113
+ /**
114
+ * Takes a window reference and adds it
115
+ */
116
+ setChild(child) {
117
+ if (typeof child !== 'object' || !child)
118
+ throw new TypeError('WindowManager.setChild requires child to be a Window object');
119
+ __classPrivateFieldSet(this, _WindowManager_children, [...new Set([...__classPrivateFieldGet(this, _WindowManager_children, "f"), child])].filter(kid => !kid.closed), "f");
120
+ }
121
+ /**
122
+ * Removes this window from the parent window's window manager.
123
+ * It calls each callback assigned to `this.#onUnloadCallbacks`
124
+ */
125
+ handleUnload() {
126
+ __classPrivateFieldGet(this, _WindowManager_parent, "f").WindowManager.popThisWindowFromParent(__classPrivateFieldGet(this, _WindowManager_win, "f").name);
127
+ forEach(__classPrivateFieldGet(this, _WindowManager_onUnloadCallbacks, "f"), cb => cb());
128
+ }
129
+ }
130
+ _WindowManager_win = new WeakMap(), _WindowManager_newWindowDetails = new WeakMap(), _WindowManager_children = new WeakMap(), _WindowManager_windowManagerCache = new WeakMap(), _WindowManager_onUnloadCallbacks = new WeakMap(), _WindowManager_parent = new WeakMap();
131
+ const NewWindowManagerInstance = new WindowManager(window).init();
132
+ window.WindowManager = NewWindowManagerInstance;
133
+ export default NewWindowManagerInstance;
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "web-window-manager",
3
- "version": "1.0.13",
3
+ "version": "1.0.18",
4
4
  "description": "A window manager for maintaining window parent child relationships",
5
5
  "main": "lib/index.js",
6
6
  "author": "Triston Armstrong",
7
7
  "license": "MIT",
8
8
  "scripts": {
9
9
  "start": "tsc --watch",
10
- "prepublishOnly": "npm run fix",
11
- "publish": "npm run build",
10
+ "prepublishOnly": "npm run fix; npm run build",
12
11
  "build": "npm run clean && tsc",
13
12
  "fix": "npm run lint; npm run format",
14
13
  "clean": "tsc --build --clean",