set options as part of instantiating a window
This commit is contained in:
parent
378970097e
commit
0a79f64528
1 changed files with 69 additions and 21 deletions
|
@ -5,36 +5,39 @@ export default class Window {
|
|||
|
||||
constructor(url, options) {
|
||||
this.url = url;
|
||||
|
||||
if ((typeof options).includes(`obj`) && options != null) {
|
||||
this.#options = options;
|
||||
(Object.keys(options)).forEach((OPTION) => {
|
||||
this[OPTION] = options[OPTION];
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the URL starts with a valid protocol. If not, it is most likely an extension page.
|
||||
if (!(this.url.startsWith(`http`) && this.url.contains(`://`))) {
|
||||
this.url = chrome.runtime.getURL(this.url);
|
||||
}
|
||||
|
||||
this.#options = (this.#options) ? this.#options : {};
|
||||
this.#options[`url`] = (this.url != this.#options[`url`]) ? this.url : this.#options[`url`];
|
||||
|
||||
// Remove options only readable here and not in the API.
|
||||
(this.hidden != null) ? delete this.#options.hidden : this.hidden = false;
|
||||
this.update(options);
|
||||
|
||||
// Show the window if it's not hidden.
|
||||
(!this.hidden) ? this.show() : false;
|
||||
}
|
||||
|
||||
/*
|
||||
Check this window's state.
|
||||
*/
|
||||
#check() {
|
||||
// Determine if this window is still open.
|
||||
(this.ID)
|
||||
? chrome.windows.get(this.ID, (window) => {
|
||||
if (window == null) {
|
||||
delete this.ID;
|
||||
this.hidden = true;
|
||||
}
|
||||
})
|
||||
: false;
|
||||
}
|
||||
|
||||
/*
|
||||
Show the window.
|
||||
*/
|
||||
show() {
|
||||
chrome.windows.create(this.#options, (window) => {
|
||||
this.ID = window.id;
|
||||
});
|
||||
this.#check();
|
||||
|
||||
(this.hidden || this.hidden == null || !(this.ID))
|
||||
? chrome.windows.create(this.#options, (window) => {
|
||||
this.hidden = false;
|
||||
this.ID = window.id;
|
||||
})
|
||||
: false;
|
||||
};
|
||||
|
||||
|
||||
|
@ -42,11 +45,14 @@ export default class Window {
|
|||
Hide the window.
|
||||
*/
|
||||
hide() {
|
||||
this.#check();
|
||||
|
||||
// Close the window if it is still defined.
|
||||
(this.ID) ? chrome.windows.remove(this.ID) : false;
|
||||
|
||||
// Remove the ID.
|
||||
delete this.ID;
|
||||
this.hidden = true;
|
||||
}
|
||||
|
||||
/* Create an action listener.
|
||||
|
@ -61,4 +67,46 @@ export default class Window {
|
|||
// Add the event.
|
||||
chrome.windows[event].addListener(callback);
|
||||
}
|
||||
|
||||
/*
|
||||
Set the options of the window.
|
||||
*/
|
||||
update(options) {
|
||||
if ((typeof options).includes(`obj`) && options != null) {
|
||||
// Merge the options if defined. If not, set it to the options itself.
|
||||
this.#options = (this.#options)
|
||||
? Object.assign(this.#options, options)
|
||||
: options;
|
||||
|
||||
(Object.keys(options)).forEach((OPTION) => {
|
||||
this[OPTION] = options[OPTION];
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the URL starts with a valid protocol. If not, it is most likely an extension page.
|
||||
(!(this.url.startsWith(`http`) && this.url.contains(`://`)))
|
||||
? this.url = chrome.runtime.getURL(this.url)
|
||||
: false;
|
||||
|
||||
this.#options[`url`] = (this.url != this.#options[`url`]) ? this.url : this.#options[`url`];
|
||||
|
||||
// Make sure height and width are integers.
|
||||
[`height`, `width`].forEach((DIMENSION) => {
|
||||
if (this[DIMENSION]) {
|
||||
this[DIMENSION] = Math.abs(parseInt(this[DIMENSION]));
|
||||
this.#options[DIMENSION] = this[DIMENSION];
|
||||
}
|
||||
});
|
||||
|
||||
// Remove options only readable here and not in the API.
|
||||
(this.hidden != null)
|
||||
? delete this.#options.hidden
|
||||
: this.hidden = ((this.hidden != null)
|
||||
? this.hidden
|
||||
: false);
|
||||
|
||||
// Update windows already open.
|
||||
this.#check();
|
||||
(this.ID) ? chrome.windows.update(this.ID, options) : false;
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue