Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.Command.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Command class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Command that executes an action.
9 *
10 * @class
11 *
12 * @constructor
13 * @param {string} name Symbolic name for the command
14 * @param {string} action Action to execute when command is triggered
15 * @param {string} method Method to call on action when executing
16 * @param {Object} [options]
17 * @param {string[]|null} [options.supportedSelections] List of supported selection types, or null for all
18 * @param {Array} [options.args] Additional arguments to pass to the action when executing
19 */
20 ve.ui.Command = function VeUiCommand( name, action, method, options ) {
21 options = options || {};
22 this.name = name;
23 this.action = action;
24 this.method = method;
25 this.supportedSelections = options.supportedSelections || null;
26 this.args = options.args || [];
27 };
28
29 /* Methods */
30
31 /**
32 * Execute command on a surface.
33 *
34 * @param {ve.ui.Surface} surface Surface to execute command on
35 * @param {Array} [args] Custom arguments to override defaults
36 * @param {string} [source] Label for the source of the command.
37 * One of 'trigger', 'sequence', 'tool', or 'context'
38 * @return {boolean} Command was executed
39 */
40 ve.ui.Command.prototype.execute = function ( surface, args, source ) {
41 args = args || this.args;
42 if ( this.isExecutable( surface.getModel().getFragment() ) ) {
43 return surface.executeWithSource( this.action, this.method, source, ...args );
44 } else {
45 return false;
46 }
47 };
48
49 /**
50 * Check if this command is executable on a given surface fragment
51 *
52 * @param {ve.dm.SurfaceFragment} fragment Surface fragment
53 * @return {boolean} The command can execute on this fragment
54 */
55 ve.ui.Command.prototype.isExecutable = function ( fragment ) {
56 return !this.supportedSelections ||
57 this.supportedSelections.indexOf( fragment.getSelection().getName() ) !== -1;
58 };
59
60 /**
61 * Get command action.
62 *
63 * @return {string} action Action to execute when command is triggered
64 */
65 ve.ui.Command.prototype.getAction = function () {
66 return this.action;
67 };
68
69 /**
70 * Get command method.
71 *
72 * @return {string} method Method to call on action when executing
73 */
74 ve.ui.Command.prototype.getMethod = function () {
75 return this.method;
76 };
77
78 /**
79 * Get command name.
80 *
81 * @return {string} name The symbolic name of the command.
82 */
83 ve.ui.Command.prototype.getName = function () {
84 return this.name;
85 };
86
87 /**
88 * Get command arguments.
89 *
90 * @return {Array} args Additional arguments to pass to the action when executing
91 */
92 ve.ui.Command.prototype.getArgs = function () {
93 return this.args;
94 };