Debian Codegraph @

extensions/Math/modules/ve-math/ve.ui.MWLatexInspector.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWLatexInspector class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license MIT
6 */
7
8 /**
9 * MediaWiki abstract inspector for quick editing of different formulas
10 * that the Math extension provides.
11 *
12 * @abstract
13 * @class
14 * @extends ve.ui.MWLiveExtensionInspector
15 *
16 * @constructor
17 * @param {Object} [config] Configuration options
18 */
19 ve.ui.MWLatexInspector = function VeUiMWLatexInspector( config ) {
20 // Parent constructor
21 ve.ui.MWLatexInspector.super.call( this, config );
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.ui.MWLatexInspector, ve.ui.MWLiveExtensionInspector );
27
28 /* Static properties */
29
30 ve.ui.MWLatexInspector.static.dir = 'ltr';
31
32 /* Methods */
33
34 /**
35 * @inheritdoc
36 */
37 ve.ui.MWLatexInspector.prototype.initialize = function () {
38 // Parent method
39 ve.ui.MWLatexInspector.super.prototype.initialize.call( this );
40
41 this.displaySelect = new OO.ui.ButtonSelectWidget( {
42 items: [
43 new OO.ui.ButtonOptionWidget( {
44 data: 'default',
45 icon: 'mathematicsDisplayDefault',
46 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-default' )
47 } ),
48 new OO.ui.ButtonOptionWidget( {
49 data: 'inline',
50 icon: 'mathematicsDisplayInline',
51 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-inline' )
52 } ),
53 new OO.ui.ButtonOptionWidget( {
54 data: 'block',
55 icon: 'mathematicsDisplayBlock',
56 label: ve.msg( 'math-visualeditor-mwlatexinspector-display-block' )
57 } )
58 ]
59 } );
60
61 this.idInput = new OO.ui.TextInputWidget();
62 this.qidInput = new mw.widgets.MathWbEntitySelector();
63
64 const inputField = new OO.ui.FieldLayout( this.input, {
65 align: 'top',
66 label: ve.msg( 'math-visualeditor-mwlatexdialog-card-formula' )
67 } );
68 const displayField = new OO.ui.FieldLayout( this.displaySelect, {
69 align: 'top',
70 label: ve.msg( 'math-visualeditor-mwlatexinspector-display' )
71 } );
72 const idField = new OO.ui.FieldLayout( this.idInput, {
73 align: 'top',
74 label: ve.msg( 'math-visualeditor-mwlatexinspector-id' )
75 } );
76 const qidField = new OO.ui.FieldLayout( this.qidInput, {
77 align: 'top',
78 label: ve.msg( 'math-visualeditor-mwlatexinspector-qid' )
79 } );
80
81 // Initialization
82 this.$content.addClass( 've-ui-mwLatexInspector-content' );
83 this.form.$element.append(
84 inputField.$element,
85 this.generatedContentsError.$element,
86 displayField.$element,
87 idField.$element,
88 qidField.$element
89 );
90 };
91
92 /**
93 * @inheritdoc
94 */
95 ve.ui.MWLatexInspector.prototype.getSetupProcess = function ( data ) {
96 return ve.ui.MWLatexInspector.super.prototype.getSetupProcess.call( this, data )
97 .next( () => {
98 const display = this.selectedNode.getAttribute( 'mw' ).attrs.display || 'default';
99 const attributes = this.selectedNode && this.selectedNode.getAttribute( 'mw' ).attrs,
100 id = attributes && attributes.id || '',
101 qid = attributes && attributes.qid || '',
102 isReadOnly = this.isReadOnly();
103
104 // Populate form
105 // TODO: This widget is not readable when disabled
106 this.idInput.setValue( id ).setReadOnly( isReadOnly );
107 this.qidInput.setValue( qid ).setReadOnly( isReadOnly );
108
109 // Add event handlers
110 this.input.on( 'change', this.onChangeHandler );
111 this.displaySelect.on( 'choose', this.onChangeHandler );
112 this.idInput.on( 'change', this.onChangeHandler );
113 this.qidInput.on( 'change', this.onChangeHandler );
114 this.displaySelect.selectItemByData( display );
115 this.displaySelect.on( 'choose', this.onChangeHandler );
116 } );
117 };
118
119 /**
120 * @inheritdoc
121 */
122 ve.ui.MWLatexInspector.prototype.getTeardownProcess = function ( data ) {
123 return ve.ui.MWLatexInspector.super.prototype.getTeardownProcess.call( this, data )
124 .first( () => {
125 this.displaySelect.off( 'choose', this.onChangeHandler );
126 } );
127 };
128
129 /**
130 * @inheritdoc
131 */
132 ve.ui.MWLatexInspector.prototype.updateMwData = function ( mwData ) {
133 // Parent method
134 ve.ui.MWLatexInspector.super.prototype.updateMwData.call( this, mwData );
135
136 const display = this.displaySelect.findSelectedItem().getData();
137 const id = this.idInput.getValue();
138 const qid = this.qidInput.getValue();
139
140 mwData.attrs.display = display !== 'default' ? display : undefined;
141 mwData.attrs.id = id || undefined;
142 mwData.attrs.qid = qid || undefined;
143 };
144
145 /**
146 * @inheritdoc
147 */
148 ve.ui.MWLatexInspector.prototype.formatGeneratedContentsError = function ( $element ) {
149 return $element.text().trim();
150 };