|
|
|
@ -8,13 +8,6 @@
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @todo Work out a better system for providing subcommands to make it much easier and less
|
|
|
|
|
* painful; preferably getting rid of the autocomplete property and adding a subcommands array
|
|
|
|
|
* property that would contain an array of VizalityCommand and would be processed like normal
|
|
|
|
|
* commands, but as subcommands.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Vizality command object.
|
|
|
|
|
* @typedef VizalityCommand
|
|
|
|
@ -50,17 +43,19 @@ import { Events } from '@vizality/constants';
|
|
|
|
|
import { Icon } from '@vizality/components';
|
|
|
|
|
import { API } from '@vizality/entities';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* All currently registered commands.
|
|
|
|
|
* Accessed with `getAllCommands` below.
|
|
|
|
|
*/
|
|
|
|
|
let commands = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @extends API
|
|
|
|
|
* @extends Events
|
|
|
|
|
*/
|
|
|
|
|
export default class Commands extends API {
|
|
|
|
|
constructor () {
|
|
|
|
|
super();
|
|
|
|
|
/**
|
|
|
|
|
* All currently registered actions.
|
|
|
|
|
*/
|
|
|
|
|
this.commands = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shuts down the API, removing all listeners and stored objects.
|
|
|
|
|
*/
|
|
|
|
@ -113,7 +108,7 @@ export default class Commands extends API {
|
|
|
|
|
command.aliases = command.aliases.map(alias => alias.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
command.caller = getCaller();
|
|
|
|
|
commands.push(command);
|
|
|
|
|
this.commands.push(command);
|
|
|
|
|
this.emit(Events.VIZALITY_COMMAND_ADD, command.command);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('registerCommand'), err);
|
|
|
|
@ -176,7 +171,7 @@ export default class Commands extends API {
|
|
|
|
|
getCommand (filter) {
|
|
|
|
|
try {
|
|
|
|
|
if (!filter?.length) return null;
|
|
|
|
|
return commands.find(filter);
|
|
|
|
|
return this.commands.find(filter);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('getCommand'), err);
|
|
|
|
|
}
|
|
|
|
@ -190,7 +185,7 @@ export default class Commands extends API {
|
|
|
|
|
getCommandByName (commandName) {
|
|
|
|
|
try {
|
|
|
|
|
assertString(commandName);
|
|
|
|
|
return commands.find(command => command.command === commandName);
|
|
|
|
|
return this.commands.find(command => command.command === commandName);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('getCommandByName'), err);
|
|
|
|
|
}
|
|
|
|
@ -204,7 +199,7 @@ export default class Commands extends API {
|
|
|
|
|
getCommands (filter) {
|
|
|
|
|
try {
|
|
|
|
|
if (!filter?.length) return null;
|
|
|
|
|
return commands.filter(filter);
|
|
|
|
|
return this.commands.filter(filter);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('getCommands'), err);
|
|
|
|
|
}
|
|
|
|
@ -218,7 +213,7 @@ export default class Commands extends API {
|
|
|
|
|
getCommandsByCaller (addonId) {
|
|
|
|
|
try {
|
|
|
|
|
assertString(addonId);
|
|
|
|
|
return commands.filter(command => command.caller?.id === addonId);
|
|
|
|
|
return this.commands.filter(command => command.caller?.id === addonId);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('getCommandsByCaller'), err);
|
|
|
|
|
}
|
|
|
|
@ -230,7 +225,7 @@ export default class Commands extends API {
|
|
|
|
|
*/
|
|
|
|
|
getAllCommands () {
|
|
|
|
|
try {
|
|
|
|
|
return commands;
|
|
|
|
|
return this.commands;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('getAllCommands'), err);
|
|
|
|
|
}
|
|
|
|
@ -247,7 +242,7 @@ export default class Commands extends API {
|
|
|
|
|
if (!this.isCommand(commandName)) {
|
|
|
|
|
throw new Error(`Command "${commandName}" is not registered, so it cannot be unregistered!`);
|
|
|
|
|
}
|
|
|
|
|
commands = this.getCommands(command => command.command !== commandName);
|
|
|
|
|
this.commands = this.getCommands(command => command.command !== commandName);
|
|
|
|
|
this.emit(Events.VIZALITY_COMMAND_REMOVE, commandName);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('unregisterCommand'), err);
|
|
|
|
@ -262,7 +257,7 @@ export default class Commands extends API {
|
|
|
|
|
unregisterCommandsByCaller (addonId) {
|
|
|
|
|
try {
|
|
|
|
|
assertString(addonId);
|
|
|
|
|
commands = commands.filter(command => command.caller?.id !== addonId);
|
|
|
|
|
this.commands = this.commands.filter(command => command.caller?.id !== addonId);
|
|
|
|
|
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL_BY_CALLER, addonId);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('unregisterCommandsByCaller'), err);
|
|
|
|
@ -275,7 +270,7 @@ export default class Commands extends API {
|
|
|
|
|
*/
|
|
|
|
|
unregisterAllCommands () {
|
|
|
|
|
try {
|
|
|
|
|
commands = [];
|
|
|
|
|
this.commands = [];
|
|
|
|
|
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return this.error(this._labels.concat('unregisterAllCommands'), err);
|
|
|
|
|