Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/inspectors/ve.ui.MWLiveExtensionInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWLiveExtensionInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Inspector for editing generic MediaWiki extensions with dynamic rendering.
10 *
11 * @class
12 * @abstract
13 * @extends ve.ui.MWExtensionInspector
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.MWLiveExtensionInspector = function VeUiMWLiveExtensionInspector() {
19 // Parent constructor
20 ve.ui.MWLiveExtensionInspector.super.apply( this, arguments );
21
22 this.updatePreviewDebounced = ve.debounce( this.updatePreview.bind( this ), 1000 );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ui.MWLiveExtensionInspector, ve.ui.MWExtensionInspector );
28
29 /* Methods */
30
31 /**
32 * @inheritdoc
33 */
34 ve.ui.MWLiveExtensionInspector.prototype.initialize = function () {
35 // Parent method
36 ve.ui.MWLiveExtensionInspector.super.prototype.initialize.call( this );
37
38 this.generatedContentsError = new ve.ui.MWExpandableErrorElement();
39 this.form.$element.append( this.generatedContentsError.$element );
40 };
41
42 /**
43 * @inheritdoc
44 */
45 ve.ui.MWLiveExtensionInspector.prototype.getSetupProcess = function ( data ) {
46 return ve.ui.MWLiveExtensionInspector.super.prototype.getSetupProcess.call( this, data )
47 .next( () => {
48 const element = this.getNewElement();
49 // Initialization
50 this.getFragment().getSurface().pushStaging();
51
52 if ( !this.selectedNode ) {
53 // Create a new node
54 // collapseToEnd returns a new fragment
55 this.fragment = this.getFragment().collapseToEnd().insertContent( [
56 element,
57 { type: '/' + element.type }
58 ] );
59 // Check if the node was inserted at a structural offset and
60 // wrapped in a paragraph
61 if ( this.getFragment().getSelection().getRange().getLength() === 4 ) {
62 this.fragment = this.getFragment().adjustLinearSelection( 1, -1 );
63 }
64 this.getFragment().select();
65 this.selectedNode = this.getFragment().getSelectedNode();
66 }
67 this.input.on( 'change', this.onChangeHandler );
68 this.generatedContentsError.connect( this, {
69 update: 'updateSize'
70 } );
71 this.selectedNode.connect( this, {
72 generatedContentsError: 'showGeneratedContentsError'
73 } );
74 } );
75 };
76
77 /**
78 * @inheritdoc
79 */
80 ve.ui.MWLiveExtensionInspector.prototype.getTeardownProcess = function ( data ) {
81 return ve.ui.MWLiveExtensionInspector.super.prototype.getTeardownProcess.call( this, data )
82 .first( () => {
83 this.input.off( 'change', this.onChangeHandler );
84 this.generatedContentsError.clear();
85 this.generatedContentsError.disconnect( this );
86 this.selectedNode.disconnect( this );
87 if ( data === undefined ) { // cancel
88 this.getFragment().getSurface().popStaging();
89 }
90 } );
91 };
92
93 /**
94 * @inheritdoc
95 */
96 ve.ui.MWLiveExtensionInspector.prototype.insertOrUpdateNode = function () {
97 // No need to call parent method as changes have already been made
98 // to the model in staging, just need to apply them.
99 this.updatePreview();
100 this.getFragment().getSurface().applyStaging();
101 // Force the selected node to re-render after staging has finished
102 this.selectedNode.emit( 'update', false );
103 };
104
105 /**
106 * @inheritdoc
107 */
108 ve.ui.MWLiveExtensionInspector.prototype.removeNode = function () {
109 this.getFragment().getSurface().popStaging();
110
111 // Parent method
112 ve.ui.MWLiveExtensionInspector.super.prototype.removeNode.call( this );
113 };
114
115 /**
116 * @inheritdoc
117 */
118 ve.ui.MWLiveExtensionInspector.prototype.onChange = function () {
119 // Parent method
120 ve.ui.MWLiveExtensionInspector.super.prototype.onChange.call( this );
121
122 this.updatePreviewDebounced();
123 };
124
125 /**
126 * Update the node rendering to reflect the current content in the inspector.
127 */
128 ve.ui.MWLiveExtensionInspector.prototype.updatePreview = function () {
129 if ( !this.selectedNode ) {
130 // Method is called debounced, so selectedNode may not still exist
131 return;
132 }
133 const mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) );
134
135 this.updateMwData( mwData );
136
137 this.hideGeneratedContentsError();
138
139 if ( this.visible ) {
140 this.getFragment().changeAttributes( { mw: mwData } );
141 }
142 };
143
144 /**
145 * Show the error container and set the error label to contain the error.
146 *
147 * @param {jQuery} $element Element containing the error
148 */
149 ve.ui.MWLiveExtensionInspector.prototype.showGeneratedContentsError = function ( $element ) {
150 this.generatedContentsError.show( this.formatGeneratedContentsError( $element ) );
151 };
152
153 /**
154 * Hide the error and collapse the error container.
155 */
156 ve.ui.MWLiveExtensionInspector.prototype.hideGeneratedContentsError = function () {
157 this.generatedContentsError.clear();
158 };
159
160 /**
161 * Format the error.
162 *
163 * Default behaviour returns the error with no modification.
164 *
165 * @param {jQuery} $element Element containing the error
166 * @return {jQuery} $element Element containing the error
167 */
168 ve.ui.MWLiveExtensionInspector.prototype.formatGeneratedContentsError = function ( $element ) {
169 return $element;
170 };