add the onclick event seperately
This commit is contained in:
parent
f5cf3f09a2
commit
bdd8eda819
1 changed files with 78 additions and 59 deletions
|
@ -1,73 +1,92 @@
|
||||||
/* context_menus.js */
|
/* context_menus.js */
|
||||||
|
|
||||||
export default class Menu {
|
export default class Menu {
|
||||||
#options;
|
#options;
|
||||||
|
|
||||||
constructor (ID, title, contexts, event, type, icon) {
|
constructor (ID, title, contexts, event, type, icon) {
|
||||||
if ((typeof ID).includes(`obj`) && !Array.isArray(ID)) {
|
if ((typeof ID).includes(`obj`) && !Array.isArray(ID)) {
|
||||||
ID.ID = ((ID.hasOwnProperty(`ID`)) ? ID.ID : false) ? ID.ID : String(Math.random() / Math.random() * 100);
|
// Create the ID if it doesn't exist.
|
||||||
|
ID.ID = ((ID.hasOwnProperty(`ID`)) ? ID.ID : false) ? ID.ID : String(Math.random() / Math.random() * 100);
|
||||||
|
|
||||||
(Object.keys(ID)).forEach((key) => {
|
(Object.keys(ID)).forEach((key) => {
|
||||||
this[key] = ID[key];
|
this[key] = ID[key];
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
this.ID = String((ID) ? ID : (Math.random() / Math.random() * 100));
|
this.ID = String((ID) ? ID : (Math.random() / Math.random() * 100));
|
||||||
this.title = (title) ? title : `Menu`;
|
this.title = (title) ? title : `Menu`;
|
||||||
this.contexts = (Array.isArray(contexts)) ? contexts : [`all`];
|
this.contexts = (Array.isArray(contexts)) ? contexts : [`all`];
|
||||||
this.event = (event) ? event : function() {};
|
this.events = (event) ? event : {"onClicked" : function() {}};
|
||||||
this.type = (((typeof type).includes(`str`) && type) ? type.trim() : false) ? type : `normal`;
|
this.type = (((typeof type).includes(`str`) && type) ? type.trim() : false) ? type : `normal`;
|
||||||
|
|
||||||
if (icon) {
|
if (icon) {
|
||||||
this.icon = icon;
|
this.icon = icon;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
this.#options = {
|
this.#options = {
|
||||||
id: this.ID,
|
id: this.ID,
|
||||||
title: this.title,
|
title: this.title,
|
||||||
contexts: this.contexts,
|
contexts: this.contexts,
|
||||||
type: this.type,
|
type: this.type
|
||||||
onclick: this.event
|
};
|
||||||
};
|
(this.icon) ? this.#options.icon = this.icon : null;
|
||||||
(this.icon) ? this.#options.icon = this.icon : null;
|
((this.hidden != null) ? (!this.hidden) : true) ? this.show() : null;
|
||||||
((this.hidden != null) ? (!this.hidden) : true) ? this.show() : null;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
remove() {
|
remove() {
|
||||||
this.hidden = true;
|
this.hidden = true;
|
||||||
chrome.contextMenus.remove(this.ID);
|
chrome.contextMenus.remove(this.ID);
|
||||||
};
|
};
|
||||||
|
|
||||||
show() {
|
show() {
|
||||||
this.hidden = false;
|
this.hidden = false;
|
||||||
chrome.contextMenus.create(this.#options);
|
this.ID = chrome.contextMenus.create(this.#options);
|
||||||
}
|
|
||||||
|
|
||||||
/* Update the context menu.
|
if (((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(events) > 0 : false) {
|
||||||
|
(this.events.onClicked)
|
||||||
|
? chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||||
|
((info.menuItemId) ? info.menuItemId == this.ID : false)
|
||||||
|
? this.events.onClicked(info, tab)
|
||||||
|
: false;
|
||||||
|
})
|
||||||
|
: false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@param {Object} options The new options for the context menu.
|
/* Update the context menu.
|
||||||
*/
|
|
||||||
update(options) {
|
|
||||||
if ((typeof options).includes(`obj`) && options != null && !Array.isArray(options)) {
|
|
||||||
(Object.keys(options)).forEach((key) => {
|
|
||||||
(options[key] != null && options[key] != undefined) ? this[key] = options[key] : delete this[key];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#options = {
|
@param {Object} options The new options for the context menu.
|
||||||
title: this.title,
|
*/
|
||||||
contexts: this.contexts,
|
update(options) {
|
||||||
type: this.type,
|
if ((typeof options).includes(`obj`) && options != null && !Array.isArray(options)) {
|
||||||
onclick: this.event
|
(Object.keys(options)).forEach((key) => {
|
||||||
};
|
(options[key] != null && options[key] != undefined) ? this[key] = options[key] : delete this[key];
|
||||||
(this.icon) ? this.#options.icon = this.icon : null;
|
});
|
||||||
|
}
|
||||||
|
|
||||||
chrome.contextMenus.update(this.ID, this.#options);
|
this.#options = {
|
||||||
}
|
title: this.title,
|
||||||
|
contexts: this.contexts,
|
||||||
|
type: this.type
|
||||||
|
};
|
||||||
|
(this.icon) ? this.#options.icon = this.icon : null;
|
||||||
|
|
||||||
/* Run a new function when triggered. */
|
chrome.contextMenus.update(this.ID, this.#options);
|
||||||
onclick(callback) {
|
|
||||||
this.event = callback;
|
(((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(events) > 0 : false)
|
||||||
this.update();
|
? ((this.events.onClicked)
|
||||||
}
|
? chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||||
|
((info.menuItemId) ? info.menuItemId == this.ID : false)
|
||||||
|
? this.events.onClicked(info, tab)
|
||||||
|
: false;
|
||||||
|
})
|
||||||
|
: false)
|
||||||
|
: false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Run a new function when triggered. */
|
||||||
|
onclick(callback) {
|
||||||
|
this.event = {"onClicked": callback};
|
||||||
|
this.update();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue