Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor user interface FragmentWindow class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Mixin for window for working with fragments of content.
9 *
10 * @class
11 * @abstract
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.FragmentWindow = function VeUiFragmentWindow() {
17 // Properties
18 this.fragment = null;
19 };
20
21 /* Inheritance */
22
23 OO.initClass( ve.ui.FragmentWindow );
24
25 /* Methods */
26
27 /**
28 * Get the surface fragment the window is for.
29 *
30 * @return {ve.dm.SurfaceFragment|null} Surface fragment the window is for, null if the
31 * window is closed
32 */
33 ve.ui.FragmentWindow.prototype.getFragment = function () {
34 return this.fragment;
35 };
36
37 // eslint-disable-next-line jsdoc/require-param, jsdoc/require-returns
38 /**
39 * @see OO.ui.Dialog
40 */
41 ve.ui.FragmentWindow.prototype.getActionWidgetConfig = function ( config ) {
42 if ( config.action === 'done' && OO.ui.isMobile() ) {
43 // Use label-less check icon on mobile (T228230)
44 config = ve.extendObject( {
45 icon: 'check',
46 invisibleLabel: true
47 }, config );
48 }
49 return config;
50 };
51
52 // eslint-disable-next-line jsdoc/require-param, jsdoc/require-returns
53 /**
54 * @see OO.ui.Window
55 * @throws {Error} If fragment was not provided through data parameter
56 */
57 ve.ui.FragmentWindow.prototype.getSetupProcess = function ( data, process ) {
58 data = data || {};
59 return process.first( () => {
60 if ( !( data.fragment instanceof ve.dm.SurfaceFragment ) ) {
61 throw new Error( 'Cannot open dialog: opening data must contain a fragment' );
62 }
63 this.fragment = data.fragment;
64 this.initialFragment = data.fragment;
65 this.selectFragmentOnClose = data.selectFragmentOnClose !== false;
66 // Prefer this.initialFragment.getSelection() to this.previousSelection
67 this.previousSelection = this.fragment.getSelection();
68 } ).next( () => {
69 // Don't allow windows to be opened for insertion in readonly mode
70 if ( !this.isEditing() && this.isReadOnly() ) {
71 return ve.createDeferred().reject().promise();
72 }
73 this.actions.setMode( this.getMode() );
74 } );
75 };
76
77 // eslint-disable-next-line jsdoc/require-param, jsdoc/require-returns
78 /**
79 * @see OO.ui.Window
80 */
81 ve.ui.FragmentWindow.prototype.getTeardownProcess = function ( data, process ) {
82 ve.track( 'activity.' + this.constructor.static.name, { action: 'dialog-' + ( data && data.action || 'abort' ) } );
83 return process.next( () => {
84 this.fragment = null;
85 this.initialFragment = null;
86 this.previousSelection = null;
87 } );
88 };
89
90 /**
91 * Check if the fragment's surface is readOnly
92 *
93 * @return {boolean} Fragment's surface is readOnly
94 */
95 ve.ui.FragmentWindow.prototype.isReadOnly = function () {
96 const fragment = this.getFragment(),
97 surface = fragment && fragment.getSurface();
98
99 return surface && surface.isReadOnly();
100 };
101
102 /**
103 * Get a symbolic mode name.
104 *
105 * By default will return 'edit' if #isEditing is true, and 'insert' otherwise.
106 *
107 * If the surface model is in read-only mode, will return 'readonly'.
108 *
109 * @return {string} Symbolic mode name
110 */
111 ve.ui.FragmentWindow.prototype.getMode = function () {
112 if ( this.isReadOnly() ) {
113 return 'readonly';
114 }
115 if ( this.getFragment() ) {
116 return this.isEditing() ? 'edit' : 'insert';
117 }
118 return '';
119 };
120
121 /**
122 * Check if the current fragment is editable by this window.
123 *
124 * @localdoc Returns true if the fragment being edited selects at least one model,
125 *
126 * @return {boolean} Fragment is editable by this window
127 */
128 ve.ui.FragmentWindow.prototype.isEditing = function () {
129 return !!this.fragment.getSelectedModels().length;
130 };