Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Context class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * UserInterface context.
9 *
10 * @class
11 * @abstract
12 * @extends OO.ui.Element
13 * @mixes OO.EventEmitter
14 * @mixes OO.ui.mixin.GroupElement
15 *
16 * @constructor
17 * @param {ve.ui.Surface} surface
18 * @param {Object} [config] Configuration options
19 */
20 ve.ui.Context = function VeUiContext( surface, config ) {
21 // Parent constructor
22 ve.ui.Context.super.call( this, config );
23
24 // Mixin constructors
25 OO.EventEmitter.call( this );
26 OO.ui.mixin.GroupElement.call( this, config );
27
28 // Properties
29 this.surface = surface;
30 this.visible = false;
31 this.choosing = false;
32
33 this.$focusTrapBefore = $( '<div>' ).prop( 'tabIndex', 0 );
34 this.$focusTrapAfter = $( '<div>' ).prop( 'tabIndex', 0 );
35 this.$focusTrapBefore.add( this.$focusTrapAfter ).on( 'focus', () => {
36 surface.getView().activate();
37 } );
38
39 // Initialization
40 // Hide element using a class, not this.toggle, as child implementations
41 // of toggle may require the instance to be fully constructed before running.
42 this.$group.addClass( 've-ui-context-menu' );
43 this.$element
44 .addClass( 've-ui-context ve-ui-context-hidden' )
45 .append( this.$focusTrapBefore, this.$group, this.$focusTrapAfter );
46 };
47
48 /* Inheritance */
49
50 OO.inheritClass( ve.ui.Context, OO.ui.Element );
51
52 OO.mixinClass( ve.ui.Context, OO.EventEmitter );
53
54 OO.mixinClass( ve.ui.Context, OO.ui.mixin.GroupElement );
55
56 /* Events */
57
58 /**
59 * @event ve.ui.Context#resize
60 */
61
62 /* Static Properties */
63
64 /**
65 * Context is for mobile devices.
66 *
67 * @static
68 * @inheritable
69 * @property {boolean}
70 */
71 ve.ui.Context.static.isMobile = false;
72
73 /* Methods */
74
75 /**
76 * Check if context is for mobile devices
77 *
78 * @return {boolean} Context is for mobile devices
79 */
80 ve.ui.Context.prototype.isMobile = function () {
81 return this.constructor.static.isMobile;
82 };
83
84 /**
85 * Check if context is visible.
86 *
87 * @return {boolean} Context is visible
88 */
89 ve.ui.Context.prototype.isVisible = function () {
90 return this.visible;
91 };
92
93 /**
94 * Get related item sources.
95 *
96 * Result is cached, and cleared when the model or selection changes.
97 *
98 * @abstract
99 * @return {Object[]} List of objects containing `type`, `name`, and `model` or `data` properties,
100 * `type` is either `item`, `tool` or `persistent`
101 * `name` is the symbolic name of the item or tool
102 * `model` is the model the item or tool is compatible with (for `item` or `tool`)
103 * `data` is additional data, for `persistent` context items
104 */
105 ve.ui.Context.prototype.getRelatedSources = null;
106
107 /**
108 * Get the surface the context is being used with.
109 *
110 * @return {ve.ui.Surface}
111 */
112 ve.ui.Context.prototype.getSurface = function () {
113 return this.surface;
114 };
115
116 /**
117 * Hide the context while it has valid items in the menu
118 *
119 * This could be triggered by clicking the close button on
120 * mobile or by pressing escape.
121 */
122 ve.ui.Context.prototype.hide = function () {
123 const surfaceModel = this.surface.getModel();
124 this.toggleMenu( false );
125 this.toggle( false );
126 // Desktop: Ensure the next cursor movement re-evaluates the context,
127 // e.g. if moving within a link, the context is re-shown.
128 surfaceModel.once( 'select', () => {
129 surfaceModel.emitContextChange();
130 } );
131 // Mobile: Clear last-known contexedAnnotations so that clicking the annotation
132 // again just brings up this context item. (T232172)
133 this.getSurface().getView().contexedAnnotations = [];
134 };
135
136 /**
137 * Toggle the menu.
138 *
139 * @param {boolean} [show] Show the menu, omit to toggle
140 * @return {ve.ui.Context}
141 * @chainable
142 */
143 ve.ui.Context.prototype.toggleMenu = function ( show ) {
144 show = show === undefined ? !this.choosing : !!show;
145
146 if ( show !== this.choosing ) {
147 this.choosing = show;
148 this.$element.toggleClass( 've-ui-context-choosing', show );
149 if ( show ) {
150 this.setupMenuItems();
151 } else {
152 this.teardownMenuItems();
153 }
154 }
155
156 return this;
157 };
158
159 /**
160 * Setup menu items.
161 *
162 * @protected
163 * @param {ve.ui.ContextItem[]} [previousItems] if a context is being refreshed, this will
164 * be the previously-open items for comparison
165 * @return {ve.ui.Context}
166 * @chainable
167 */
168 ve.ui.Context.prototype.setupMenuItems = function ( previousItems ) {
169 const sources = this.getRelatedSources(),
170 items = [];
171
172 let i, len;
173 for ( i = 0, len = sources.length; i < len; i++ ) {
174 const source = sources[ i ];
175 if ( source.type === 'item' ) {
176 items.push( ve.ui.contextItemFactory.create(
177 sources[ i ].name, this, sources[ i ].model
178 ) );
179 } else if ( source.type === 'tool' ) {
180 items.push( new ve.ui.ToolContextItem(
181 this, sources[ i ].model, ve.ui.toolFactory.lookup( sources[ i ].name )
182 ) );
183 } else if ( source.type === 'persistent' ) {
184 items.push( ve.ui.contextItemFactory.create(
185 sources[ i ].name, this, sources[ i ].data
186 ) );
187 }
188 }
189
190 items.sort( ( a, b ) => a.constructor.static.sortOrder - b.constructor.static.sortOrder );
191
192 this.addItems( items );
193 for ( i = 0, len = items.length; i < len; i++ ) {
194 const item = items[ i ];
195 const isRefreshing = previousItems && previousItems.some(
196 // We treat it as refreshing if they're exactly equal, or if either is null.
197 // Null here probably means we're dealing with a persistent fragment that's
198 // between text-selections currently.
199 ( oldItem ) => oldItem.equals( item ) ||
200 oldItem.getFragment().isNull() ||
201 item.getFragment().isNull()
202 );
203 item.connect( this, { command: 'onContextItemCommand' } );
204 item.setup( isRefreshing );
205 }
206
207 return this;
208 };
209
210 /**
211 * Teardown menu items.
212 *
213 * @protected
214 * @return {ve.ui.Context}
215 * @chainable
216 */
217 ve.ui.Context.prototype.teardownMenuItems = function () {
218 for ( let i = 0, len = this.items.length; i < len; i++ ) {
219 this.items[ i ].teardown();
220 }
221 this.clearItems();
222
223 return this;
224 };
225
226 /**
227 * Handle command events from context items
228 */
229 ve.ui.Context.prototype.onContextItemCommand = function () {};
230
231 /**
232 * Toggle the visibility of the context.
233 *
234 * @param {boolean} [show] Show the context, omit to toggle
235 * @return {jQuery.Promise} Promise resolved when context is finished showing/hiding
236 * @fires ve.ui.Context#resize
237 */
238 ve.ui.Context.prototype.toggle = function ( show ) {
239 show = show === undefined ? !this.visible : !!show;
240 if ( show !== this.visible ) {
241 this.visible = show;
242 this.$element.toggleClass( 've-ui-context-hidden', !this.visible );
243 }
244 this.emit( 'resize' );
245 return ve.createDeferred().resolve().promise();
246 };
247
248 /**
249 * Update the size and position of the context.
250 *
251 * @return {ve.ui.Context}
252 * @chainable
253 * @fires ve.ui.Context#resize
254 */
255 ve.ui.Context.prototype.updateDimensions = function () {
256 // Override in subclass if context is positioned relative to content
257 this.emit( 'resize' );
258 return this;
259 };
260
261 /**
262 * Destroy the context, removing all DOM elements.
263 *
264 * @return {ve.ui.Context}
265 * @chainable
266 */
267 ve.ui.Context.prototype.destroy = function () {
268 // Disconnect events
269 this.surface.getModel().disconnect( this );
270
271 this.$element.remove();
272 return this;
273 };
274
275 /**
276 * Get an object describing the amount of padding the context adds to the surface.
277 *
278 * For example the mobile context, which is fixed to the bottom of the viewport,
279 * will add bottom padding, whereas the floating desktop context will add none.
280 *
281 * @return {ve.ui.Surface.Padding|null} Padding object, or null
282 */
283 ve.ui.Context.prototype.getSurfacePadding = function () {
284 return null;
285 };