/**
* The abstract base class of all local commands bound to a mouse event.
*/
export class OnMouseEventCmd extends Command {
/**
* Constructor.
*
* @param {boolean} alwaysConsumeEvent - if <code>true</code>,
* the mouse event which triggers this command will be
* automatically be consumed.
*/
constructor(alwaysConsumeEvent) {
super();
this._alwaysConsumeEvent = alwaysConsumeEvent;
}
execute(mode, docView, params, event) {
let canExecute = this.canExecuteImpl(docView, params, event);
if (mode === EXECUTE_TEST) {
return Promise.resolve(canExecute);
} else {
if (!canExecute) {
Command.consumeEvent(event);
return Promise.resolve(null); // Not CommandResult.FAILED.
}
}
// ---
if (this._alwaysConsumeEvent) {
Command.consumeEvent(event);
}
// Otherwise it is executeImpl which will do that.
// For an OnMouseEventCmd implementation, don't care about mode.
return this.executeImpl(docView, params, event)
.catch((error) => {
// Do not display an error dialog for a command triggered by a
// mouse event.
console.error(`Command "${this.commandName}" has failed: \
${error}`);
return null; // Not CommandResult.FAILED.
});
}
/**
* Invoked by <code>OnMouseEventCmd</code> redefinition of
* {@link Command.execute} when execution mode is
* {@link Command.EXECUTE_NORMAL} or {@link Command.EXECUTE_HELPER}.
* <p><strong>Must be redefined by subclass</strong>.
* Default implementation returns a <code>Promise</code>
* containing {@link CommandResult.FAILED)}.
*
* @param {DocumentView} docView - the target of this command.
* @param {string} params - parameterizes the command.
* May be <code>null</code>.
* @param {UIEvent} event - the mouse event which triggered this command.
* @returns {Promise} A Promise containing {@link CommandResult} or
* <code>null</code> if, for any reason, this command has not been executed.
*/
executeImpl(docView, params, event) {
if (!this._alwaysConsumeEvent) {
Command.consumeEvent(event);
}
console.error(`OnMouseEventCmd.executeImpl: NOT IMPLEMENTED: \
command "${this.commandName}" \
(params="${params}", event=${event})`);
return Promise.resolve(CommandResult.FAILED);
}
/**
* Invoked by <code>OnMouseEventCmd</code> redefinition of
* {@link Command.execute} when execution mode is
* {@link Command.EXECUTE_TEST}.
* <p><strong>May be redefined by subclass</strong>.
* Default implementation always returns <code>true</code>.
*
* @param {DocumentView} docView - the target of this command.
* @param {string} params - parameterizes the command.
* May be <code>null</code>.
* @param {UIEvent} event - the mouse event which triggered this command.
* @returns {boolean} <code>true</code> if the command can be executed,
* <code>false</code> otherwise.
*/
canExecuteImpl(docView, params, event) {
return (event !== null && (event instanceof MouseEvent));
}
}