Files
threshold-ui/ui/admin/app/services/window-manager.js
T
hashicorp-copywrite[bot]andCameron Perera 3d610427d0 [COMPLIANCE] Add Copyright and License Headers (#1617)
* [COMPLIANCE] Add Copyright and License Headers

* chore: 🤖 update Copyright headers to satisfy our lint rules

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
2023-03-01 09:39:41 -06:00

64 lines
1.3 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Service from '@ember/service';
import { inject as service } from '@ember/service';
/**
* The window manager service enables browser windows to open and close
* without the need to retain window references at the callsite.
*/
export default class WindowManagerService extends Service {
// =services
@service('browser/window') window;
// =attributes
/**
* An array of windows under management.
* @private
* @type {[Window]}
*/
#managedWindows = [];
// =methods
/**
* Opens a new window of the specified URL via browser services.
* @param {string} url
* @see `window.open`
*/
open(url) {
const newWindow = this.window.open(url);
if (newWindow) this.add(newWindow);
}
/**
* Closes all windows under management and purges their references.
* @see `window.close`
*/
closeAll() {
this.#managedWindows.forEach((window) => window.close());
this.purgeAll();
}
/**
* Adds a window to management.
* @param {Window} window
*/
add(window) {
this.#managedWindows.push(window);
}
/**
* Resets the managed windows array. As long as no array references are
* retained, this should result in the original array being GC'd.
*/
purgeAll() {
this.#managedWindows = [];
}
}