Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.ui.MWReferencesListDialog.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor user interface MWReferencesListDialog class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * Dialog for editing MediaWiki references lists.
12 *
13 * @constructor
14 * @extends ve.ui.NodeDialog
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWReferencesListDialog = function VeUiMWReferencesListDialog( config ) {
18 // Parent constructor
19 ve.ui.MWReferencesListDialog.super.call( this, config );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWReferencesListDialog, ve.ui.NodeDialog );
25
26 /* Static Properties */
27
28 ve.ui.MWReferencesListDialog.static.name = 'referencesList';
29
30 ve.ui.MWReferencesListDialog.static.title =
31 OO.ui.deferMsg( 'cite-ve-dialog-referenceslist-title' );
32
33 ve.ui.MWReferencesListDialog.static.modelClasses = [ ve.dm.MWReferencesListNode ];
34
35 ve.ui.MWReferencesListDialog.static.size = 'medium';
36
37 /* Methods */
38
39 /**
40 * @override
41 */
42 ve.ui.MWReferencesListDialog.prototype.getBodyHeight = function () {
43 return Math.max( 150, Math.ceil( this.editPanel.$element[ 0 ].scrollHeight ) );
44 };
45
46 /**
47 * @override
48 */
49 ve.ui.MWReferencesListDialog.prototype.initialize = function () {
50 // Parent method
51 ve.ui.MWReferencesListDialog.super.prototype.initialize.call( this );
52
53 // Properties
54 this.panels = new OO.ui.StackLayout();
55 this.editPanel = new OO.ui.PanelLayout( {
56 scrollable: true, padded: true
57 } );
58 this.optionsFieldset = new OO.ui.FieldsetLayout();
59
60 this.groupInput = new ve.ui.MWReferenceGroupInputWidget( {
61 $overlay: this.$overlay,
62 emptyGroupName: ve.msg( 'cite-ve-dialog-reference-options-group-placeholder' )
63 } );
64 const groupField = new OO.ui.FieldLayout( this.groupInput, {
65 align: 'top',
66 label: ve.msg( 'cite-ve-dialog-reference-options-group-label' )
67 } );
68
69 this.responsiveCheckbox = new OO.ui.CheckboxInputWidget();
70 const responsiveField = new OO.ui.FieldLayout( this.responsiveCheckbox, {
71 align: 'inline',
72 label: ve.msg( 'cite-ve-dialog-reference-options-responsive-label' )
73 } );
74
75 // Initialization
76 this.optionsFieldset.addItems( [ groupField, responsiveField ] );
77 this.editPanel.$element.append( this.optionsFieldset.$element );
78 this.panels.addItems( [ this.editPanel ] );
79 this.$body.append( this.panels.$element );
80 };
81
82 /**
83 * Handle change event.
84 */
85 ve.ui.MWReferencesListDialog.prototype.onChange = function () {
86 this.updateActions();
87 };
88
89 /**
90 * Update the 'done' action according to whether there are changes
91 */
92 ve.ui.MWReferencesListDialog.prototype.updateActions = function () {
93 this.actions.setAbilities( { done: this.isModified() } );
94 };
95
96 /**
97 * @return {boolean}
98 */
99 ve.ui.MWReferencesListDialog.prototype.isModified = function () {
100 if ( !this.selectedNode ) {
101 return true;
102 }
103
104 const refGroup = this.groupInput.getValue();
105 const listGroup = 'mwReference/' + refGroup;
106 const isResponsive = this.responsiveCheckbox.isSelected();
107
108 const oldListGroup = this.selectedNode.getAttribute( 'listGroup' );
109 const oldResponsive = this.selectedNode.getAttribute( 'isResponsive' );
110
111 return listGroup !== oldListGroup || isResponsive !== oldResponsive;
112 };
113
114 /**
115 * @override
116 */
117 ve.ui.MWReferencesListDialog.prototype.getActionProcess = function ( action ) {
118 if ( action === 'done' ) {
119 return new OO.ui.Process( () => {
120
121 // Save changes
122 const refGroup = this.groupInput.getValue();
123 const listGroup = 'mwReference/' + refGroup;
124 const isResponsive = this.responsiveCheckbox.isSelected();
125
126 if ( this.selectedNode ) {
127 // Edit existing model
128 const surfaceModel = this.getFragment().getSurface();
129 const doc = surfaceModel.getDocument();
130
131 if ( this.isModified() ) {
132 // newFromAttributeChanges doesn't do the smart-replacing
133 // for nested attributes, so make a copy of the mw
134 // attribute so we can disable autoGenerated now we've
135 // changed it.
136 const mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) ) || {};
137 delete mwData.autoGenerated;
138
139 const attrChanges = {
140 listGroup: listGroup,
141 refGroup: refGroup,
142 isResponsive: isResponsive,
143 mw: mwData
144 };
145 surfaceModel.change(
146 ve.dm.TransactionBuilder.static.newFromAttributeChanges(
147 doc, this.selectedNode.getOuterRange().start, attrChanges
148 )
149 );
150 }
151 }
152
153 this.close( { action: action } );
154 } );
155 }
156 // Parent method
157 return ve.ui.MWReferencesListDialog.super.prototype.getActionProcess.call( this, action );
158 };
159
160 /**
161 * @override
162 */
163 ve.ui.MWReferencesListDialog.prototype.getSetupProcess = function ( data ) {
164 return ve.ui.MWReferencesListDialog.super.prototype.getSetupProcess.call( this, data )
165 .next( () => {
166 if ( !( this.selectedNode instanceof ve.dm.MWReferencesListNode ) ) {
167 throw new Error( 'Cannot open dialog: references list must be selected' );
168 }
169
170 this.groupInput.setValue( this.selectedNode.getAttribute( 'refGroup' ) );
171 const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( this.getFragment().getDocument() );
172 this.groupInput.populateMenu( docRefs.getAllGroupNames() );
173
174 this.responsiveCheckbox.setSelected( this.selectedNode.getAttribute( 'isResponsive' ) );
175
176 const isReadOnly = this.isReadOnly();
177 this.groupInput.setReadOnly( isReadOnly );
178 this.responsiveCheckbox.setDisabled( isReadOnly );
179
180 this.groupInput.connect( this, { change: 'onChange' } );
181 this.responsiveCheckbox.connect( this, { change: 'onChange' } );
182
183 this.updateActions();
184 } );
185 };
186
187 /**
188 * @override
189 */
190 ve.ui.MWReferencesListDialog.prototype.getTeardownProcess = function ( data ) {
191 return ve.ui.MWReferencesListDialog.super.prototype.getTeardownProcess.call( this, data )
192 .next( () => {
193 this.groupInput.disconnect( this );
194 this.responsiveCheckbox.disconnect( this );
195 } );
196 };
197
198 /**
199 * @override
200 */
201 ve.ui.MWReferencesListDialog.prototype.getReadyProcess = function ( data ) {
202 return ve.ui.MWReferencesListDialog.super.prototype.getReadyProcess.call( this, data )
203 .next( () => {
204 this.groupInput.focus();
205 } );
206 };
207
208 /* Registration */
209
210 ve.ui.windowFactory.register( ve.ui.MWReferencesListDialog );