Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface ContextItem class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Item in a context.
9 *
10 * @class
11 * @extends OO.ui.Widget
12 *
13 * @constructor
14 * @param {ve.ui.LinearContext} context Context the item is in
15 * @param {ve.dm.Model} [model] Model the item is related to
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.ContextItem = function VeUiContextItem( context, model, config ) {
19 // Parent constructor
20 ve.ui.ContextItem.super.call( this, config );
21
22 // Properties
23 this.context = context;
24 this.model = model;
25 this.fragment = null;
26
27 // Events
28 this.$element.on( 'mousedown', () => {
29 // Deactivate so context is not automatically closed
30 // by null selection
31 context.getSurface().getView().deactivate();
32 } );
33 this.$element.on( 'keydown', ( e ) => {
34 // Pressing escape while focus is in the context should
35 // return focus to the surface
36 if ( e.keyCode === OO.ui.Keys.ESCAPE && context.getSurface().getView().isDeactivated() ) {
37 context.getSurface().getView().activate();
38 return false;
39 }
40 } );
41
42 // Initialization
43 this.$element.addClass( 've-ui-contextItem' );
44 };
45
46 /* Inheritance */
47
48 OO.inheritClass( ve.ui.ContextItem, OO.ui.Widget );
49
50 /* Events */
51
52 /**
53 * The context executed a ve.ui.Command
54 *
55 * @event ve.ui.ContextItem#command
56 */
57
58 /* Static Properties */
59
60 /**
61 * Whether this item exclusively handles any model class
62 *
63 * @static
64 * @property {boolean}
65 * @inheritable
66 */
67 ve.ui.ContextItem.static.exclusive = true;
68
69 ve.ui.ContextItem.static.commandName = null;
70
71 /**
72 * Sort order of the context item within the context
73 *
74 * Items are sorted top to bottom in ascending order. Negative values are allowed.
75 *
76 * @static
77 * @property {number}
78 * @inheritable
79 */
80 ve.ui.ContextItem.static.sortOrder = 0;
81
82 /**
83 * Annotation or node models this item is related to.
84 *
85 * Used by #isCompatibleWith.
86 *
87 * @static
88 * @property {Function[]}
89 * @inheritable
90 */
91 ve.ui.ContextItem.static.modelClasses = [];
92
93 /**
94 * Context items (by name) which this context item suppresses.
95 *
96 * See ve.ui.ModeledFactory.
97 *
98 * @static
99 * @property {string[]}
100 * @inheritable
101 */
102 ve.ui.ContextItem.static.suppresses = [];
103
104 /* Methods */
105
106 /**
107 * Check if this item is compatible with a given model.
108 *
109 * @static
110 * @inheritable
111 * @param {ve.dm.Model} model Model to check
112 * @return {boolean} Item can be used with model
113 */
114 ve.ui.ContextItem.static.isCompatibleWith = function ( model ) {
115 return ve.isInstanceOfAny( model, this.modelClasses );
116 };
117
118 /**
119 * Check if model is a node
120 *
121 * @return {boolean} Model is a node
122 */
123 ve.ui.ContextItem.prototype.isNode = function () {
124 return this.model && this.model instanceof ve.dm.Node;
125 };
126
127 /**
128 * Get the command for this item.
129 *
130 * @return {ve.ui.Command}
131 */
132 ve.ui.ContextItem.prototype.getCommand = function () {
133 return this.context.getSurface().commandRegistry.lookup( this.constructor.static.commandName );
134 };
135
136 /**
137 * Get a surface fragment covering the related model node, or the current selection otherwise
138 *
139 * @return {ve.dm.SurfaceFragment} Surface fragment
140 */
141 ve.ui.ContextItem.prototype.getFragment = function () {
142 if ( !this.fragment ) {
143 const surfaceModel = this.context.getSurface().getModel();
144 this.fragment = this.isNode() ?
145 surfaceModel.getLinearFragment( this.model.getOuterRange() ) :
146 surfaceModel.getFragment();
147 }
148 return this.fragment;
149 };
150
151 /**
152 * Check if the context's surface is readOnly
153 *
154 * @return {boolean} Context's surface is readOnly
155 */
156 ve.ui.ContextItem.prototype.isReadOnly = function () {
157 return this.context.getSurface().isReadOnly();
158 };
159
160 /**
161 * Check whether this context item represents the same content as another
162 *
163 * @param {ve.ui.ContextItem} other
164 * @return {boolean}
165 */
166 ve.ui.ContextItem.prototype.equals = function ( other ) {
167 return this.constructor.static.name === other.constructor.static.name &&
168 this.getFragment().getSelection().equals( other.getFragment().getSelection() );
169 };
170
171 /**
172 * Setup the item.
173 *
174 * @param {boolean} refreshing If this is a reconstruction/refresh of a context
175 * @return {ve.ui.ContextItem}
176 * @chainable
177 */
178 ve.ui.ContextItem.prototype.setup = function () {
179 return this;
180 };
181
182 /**
183 * Teardown the item.
184 *
185 * @return {ve.ui.ContextItem}
186 * @chainable
187 */
188 ve.ui.ContextItem.prototype.teardown = function () {
189 return this;
190 };