Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/dialogs/ve.ui.MWMetaDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWMetaDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Dialog for editing MediaWiki page information.
10 *
11 * @class
12 * @extends ve.ui.FragmentDialog
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWMetaDialog = function VeUiMWMetaDialog( config ) {
18 // Parent constructor
19 ve.ui.MWMetaDialog.super.call( this, config );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWMetaDialog, ve.ui.FragmentDialog );
25
26 /* Static Properties */
27
28 ve.ui.MWMetaDialog.static.name = 'meta';
29
30 ve.ui.MWMetaDialog.static.title =
31 OO.ui.deferMsg( 'visualeditor-dialog-meta-title' );
32
33 ve.ui.MWMetaDialog.static.size = 'large';
34
35 /* Methods */
36
37 /**
38 * @inheritdoc
39 */
40 ve.ui.MWMetaDialog.prototype.getBodyHeight = function () {
41 return 400;
42 };
43
44 /**
45 * @inheritdoc
46 */
47 ve.ui.MWMetaDialog.prototype.initialize = function () {
48 // Parent method
49 ve.ui.MWMetaDialog.super.prototype.initialize.call( this );
50
51 // Properties
52 this.panels = new OO.ui.StackLayout();
53 this.bookletLayout = new OO.ui.BookletLayout( { outlined: true } );
54 this.categoriesPage = new ve.ui.MWCategoriesPage( 'categories', { $overlay: this.$overlay } );
55 this.settingsPage = new ve.ui.MWSettingsPage( 'settings', { $overlay: this.$overlay } );
56 this.advancedSettingsPage = new ve.ui.MWAdvancedSettingsPage( 'advancedSettings', { $overlay: this.$overlay } );
57 this.languagesPage = new ve.ui.MWLanguagesPage( 'languages', { $overlay: this.$overlay } );
58 this.templatesUsedPage = new ve.ui.MWTemplatesUsedPage( 'templatesUsed', { $overlay: this.$overlay } );
59
60 // Initialization
61 this.$body.append( this.panels.$element );
62 this.panels.addItems( [ this.bookletLayout ] );
63 this.bookletLayout.addPages( [
64 this.categoriesPage,
65 this.settingsPage,
66 this.advancedSettingsPage,
67 this.languagesPage,
68 this.templatesUsedPage
69 ] );
70
71 this.bookletLayout.$menu.find( '[role=listbox]' ).first().attr( 'aria-label', OO.ui.deferMsg( 'visualeditor-dialog-meta-title' ) );
72 this.oldSettings = null;
73 this.widgetList = this.getAllWidgets();
74 };
75
76 /**
77 * @return {boolean} Whether settings were changed.
78 */
79 ve.ui.MWMetaDialog.prototype.compareSettings = function () {
80 const newSettings = this.extractSettings();
81 return !ve.compare( newSettings, this.oldSettings );
82 };
83
84 /**
85 * @return {Object[]} An array of objects
86 * {
87 * widget: OO.ui.Widget,
88 * name: string,
89 * hasChildren: boolean
90 * }
91 */
92 ve.ui.MWMetaDialog.prototype.getAllWidgets = function () {
93 const widgetList = [];
94
95 // eslint-disable-next-line no-jquery/no-each-util
96 $.each( this.bookletLayout.pages, ( pageName, page ) => {
97 const fieldsets = page.getFieldsets();
98 fieldsets.forEach( ( fieldset, fieldsetIndex ) => {
99 fieldset.items.forEach( ( item, itemIndex ) => {
100 const widget = item.fieldWidget;
101 // we can recheck the value
102 widgetList.push( {
103 widget: widget,
104 name: pageName + '/' + fieldsetIndex + '/' + itemIndex,
105 hasChildren: widget.items !== undefined
106 } );
107 } );
108 } );
109 } );
110
111 return widgetList;
112 };
113
114 /**
115 * Assigns updateActions to all widget updates.
116 */
117 ve.ui.MWMetaDialog.prototype.assignEvents = function () {
118 const widgetList = this.getAllWidgets();
119
120 widgetList.forEach( ( value ) => {
121 value.widget.connect( this, {
122 change: 'updateActions',
123 select: 'updateActions'
124 } );
125 } );
126 };
127
128 /**
129 * @param {Object} field Widget
130 * @return {string|boolean} Value of the field
131 */
132 ve.ui.MWMetaDialog.prototype.extractValue = function ( field ) {
133 if ( field instanceof OO.ui.TextInputWidget ) {
134 return field.getValue();
135 } else if ( field instanceof OO.ui.CheckboxInputWidget ) {
136 return field.isSelected();
137 } else if ( field instanceof OO.ui.ButtonOptionWidget ) {
138 return field.selected;
139 } else if ( field instanceof ve.ui.MWCategoryItemWidget ) {
140 return {
141 value: field.value,
142 sortKey: field.sortKey };
143 } else {
144 throw new Error( 'Unhandled widget type', field );
145 }
146 };
147
148 /**
149 * @return {Object[]} An array of all widgets with their current value.
150 * {
151 * name:string,
152 * value:string|boolean
153 * }
154 */
155 ve.ui.MWMetaDialog.prototype.extractSettings = function () {
156 const ret = []; // return value
157
158 this.widgetList.forEach( ( value ) => {
159 if ( value.hasChildren ) {
160 value.widget.items.forEach( ( item, index ) => {
161 ret.push( {
162 name: item.name + '/' + index,
163 value: this.extractValue( item )
164 } );
165 } );
166 } else {
167 ret.push( {
168 name: value.name,
169 value: this.extractValue( value.widget )
170 } );
171 }
172 } );
173
174 return ret;
175 };
176
177 /**
178 * Compares oldSetting with new settings and toggles the apply button accordingly.
179 */
180 ve.ui.MWMetaDialog.prototype.updateActions = function () {
181 this.actions.setAbilities( {
182 done: this.settingsPage.checkValidRedirect() && this.compareSettings()
183 } );
184 };
185
186 /**
187 * @inheritdoc ve.ui.FragmentWindow
188 */
189 ve.ui.MWMetaDialog.prototype.isEditing = function () {
190 // Always in editing mode, used for setting 'done' button label.
191 return true;
192 };
193
194 /**
195 * @inheritdoc
196 */
197 ve.ui.MWMetaDialog.prototype.getActionProcess = function ( action ) {
198 const surfaceModel = this.getFragment().getSurface();
199
200 if ( action === 'done' ) {
201 return new OO.ui.Process( () => {
202 surfaceModel.applyStaging();
203 this.close( { action: action } );
204 } );
205 }
206
207 return ve.ui.MWMetaDialog.super.prototype.getActionProcess.call( this, action )
208 .next( () => {
209 surfaceModel.popStaging();
210 } );
211 };
212
213 /**
214 * @inheritdoc
215 */
216 ve.ui.MWMetaDialog.prototype.getSetupProcess = function ( data ) {
217 data = data || {};
218 return ve.ui.MWMetaDialog.super.prototype.getSetupProcess.call( this, data )
219 .next( () => {
220 const surfaceModel = this.getFragment().getSurface(),
221 promises = [],
222 selectWidget = this.bookletLayout.outlineSelectWidget,
223 visualOnlyPages = [ 'categories', 'settings', 'advancedSettings', 'languages' ],
224 isSource = ve.init.target.getSurface().getMode() === 'source';
225
226 visualOnlyPages.forEach( ( page ) => {
227 selectWidget.findItemFromData( page ).setDisabled( isSource );
228 } );
229
230 if ( isSource && visualOnlyPages.indexOf( data.page || 'categories' ) !== -1 ) {
231 data.page = 'templatesUsed';
232 }
233
234 // Force all previous transactions to be separate from this history state
235 surfaceModel.pushStaging();
236
237 const config = {
238 data: data,
239 isReadOnly: this.isReadOnly()
240 };
241
242 // Let each page set itself up ('languages' page doesn't need this yet)
243 promises.push( this.categoriesPage.setup( surfaceModel.getFragment(), config ) );
244 promises.push( this.settingsPage.setup( surfaceModel.getFragment(), config ) );
245 promises.push( this.advancedSettingsPage.setup( surfaceModel.getFragment(), config ) );
246 return ve.promiseAll( promises );
247 } )
248 .next( () => {
249 if ( data.page && this.bookletLayout.getPage( data.page ) ) {
250 // HACK: Prevent the setPage() call from focussing stuff in the selected page. For the
251 // 'categories' page, this causes a dropdown to appear, and if it's done in the setup
252 // process, the dropdown will be misaligned (T185944). We don't pass `autoFocus: false`
253 // in the config because we want the auto-focus behavior when the user changes the page
254 // after the dialog is open. We focus in getReadyProcess() anyway.
255 this.bookletLayout.autoFocus = false;
256 this.bookletLayout.setPage( data.page );
257 this.bookletLayout.autoFocus = true;
258 }
259
260 if ( this.oldSettings === null ) {
261 this.assignEvents();
262 }
263 this.oldSettings = this.extractSettings(); // setting that were just loaded
264
265 this.actions.setAbilities( { done: false } );
266 } );
267 };
268
269 /**
270 * @inheritdoc
271 */
272 ve.ui.MWMetaDialog.prototype.getReadyProcess = function ( data ) {
273 data = data || {};
274 return ve.ui.MWMetaDialog.super.prototype.getReadyProcess.call( this, data )
275 .next( () => {
276 if ( data.page && this.bookletLayout.getPage( data.page ) ) {
277 this.bookletLayout.getPage( data.page ).focus();
278 }
279 } );
280 };
281
282 /**
283 * @inheritdoc
284 */
285 ve.ui.MWMetaDialog.prototype.getTeardownProcess = function ( data ) {
286 data = data || {};
287 return ve.ui.MWMetaDialog.super.prototype.getTeardownProcess.call( this, data )
288 .first( () => {
289 // Let each page tear itself down ('languages' page doesn't need this yet)
290 this.categoriesPage.teardown( { action: data.action } );
291 this.settingsPage.teardown( { action: data.action } );
292 this.advancedSettingsPage.teardown( { action: data.action } );
293
294 this.bookletLayout.setPage( 'categories' );
295 this.bookletLayout.resetScroll();
296 } );
297 };
298
299 /* Registration */
300
301 ve.ui.windowFactory.register( ve.ui.MWMetaDialog );