Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/dialogs/ve.ui.TableDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface TableDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Dialog for table properties.
9 *
10 * @class
11 * @extends ve.ui.FragmentDialog
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.TableDialog = function VeUiTableDialog( config ) {
17 // Parent constructor
18 ve.ui.TableDialog.super.call( this, config );
19
20 this.$element.addClass( 've-ui-tableDialog' );
21 };
22
23 /* Inheritance */
24
25 OO.inheritClass( ve.ui.TableDialog, ve.ui.FragmentDialog );
26
27 /* Static Properties */
28
29 ve.ui.TableDialog.static.name = 'table';
30
31 ve.ui.TableDialog.static.size = 'medium';
32
33 ve.ui.TableDialog.static.title = OO.ui.deferMsg( 'visualeditor-dialog-table-title' );
34
35 /* Methods */
36
37 /**
38 * @inheritdoc
39 */
40 ve.ui.TableDialog.prototype.initialize = function () {
41 // Parent method
42 ve.ui.TableDialog.super.prototype.initialize.call( this );
43
44 this.initialValues = null;
45
46 this.panel = new OO.ui.PanelLayout( {
47 padded: true,
48 expanded: false,
49 classes: [ 've-ui-tableDialog-panel' ]
50 } );
51
52 this.captionToggle = new OO.ui.ToggleSwitchWidget();
53 this.captionField = new OO.ui.FieldLayout( this.captionToggle, {
54 align: 'left',
55 label: ve.msg( 'visualeditor-dialog-table-caption' )
56 } );
57
58 this.captionToggle.connect( this, { change: 'updateActions' } );
59
60 this.panel.$element.append( this.captionField.$element );
61
62 this.$body.append( this.panel.$element );
63 };
64
65 /**
66 * Update the 'done' action according to whether there are changes
67 */
68 ve.ui.TableDialog.prototype.updateActions = function () {
69 this.actions.setAbilities( { done: !ve.compare( this.getValues(), this.initialValues ) } );
70 };
71
72 /**
73 * Get object describing current form values.
74 *
75 * To be compared against this.initialValues
76 *
77 * @return {Object} Current form values
78 */
79 ve.ui.TableDialog.prototype.getValues = function () {
80 return {
81 caption: this.captionToggle.getValue()
82 };
83 };
84
85 /**
86 * @inheritdoc
87 */
88 ve.ui.TableDialog.prototype.getSetupProcess = function ( data ) {
89 return ve.ui.TableDialog.super.prototype.getSetupProcess.call( this, data )
90 .next( () => {
91 const isReadOnly = this.isReadOnly();
92 this.initialValues = {
93 caption: !!this.getFragment().getSelection().getTableNode(
94 this.getFragment().getDocument()
95 ).getCaptionNode()
96 };
97 this.captionToggle.setValue( this.initialValues.caption ).setDisabled( isReadOnly );
98 this.closingFragment = null;
99 this.updateActions();
100 } );
101 };
102
103 /**
104 * @inheritdoc
105 */
106 ve.ui.TableDialog.prototype.getActionProcess = function ( action ) {
107 return ve.ui.TableDialog.super.prototype.getActionProcess.call( this, action )
108 .next( () => {
109 if ( action === 'done' ) {
110 const surfaceModel = this.getFragment().getSurface();
111 const selection = surfaceModel.getSelection();
112 const captionNode = this.getFragment().getSelection().getTableNode(
113 this.getFragment().getDocument()
114 ).getCaptionNode();
115 if ( this.captionToggle.getValue() !== this.initialValues.caption ) {
116 let fragment;
117 if ( this.initialValues.caption ) {
118 fragment = surfaceModel.getLinearFragment( captionNode.getOuterRange(), true );
119 fragment.removeContent();
120 } else {
121 fragment = surfaceModel.getLinearFragment( new ve.Range( selection.tableRange.start + 1 ), true );
122
123 fragment.insertContent( [
124 { type: 'tableCaption' },
125 { type: 'paragraph', internal: { generated: 'wrapper' } },
126 { type: '/paragraph' },
127 { type: '/tableCaption' }
128 ], false );
129 // Don't change this.fragment immediately, wait until teardown process, as child
130 // dialogs my want access to the original fragment
131 this.closingFragment = fragment.collapseToStart().adjustLinearSelection( 2, 2 );
132 }
133 }
134 this.close( { action: 'done' } );
135 }
136 } );
137 };
138
139 /**
140 * @inheritdoc
141 */
142 ve.ui.TableDialog.prototype.getTeardownProcess = function ( action ) {
143 return ve.ui.TableDialog.super.prototype.getTeardownProcess.call( this, action )
144 .first( () => {
145 this.fragment = this.closingFragment || this.fragment;
146 } );
147 };
148
149 /* Registration */
150
151 ve.ui.windowFactory.register( ve.ui.TableDialog );