Debian Codegraph @

extensions/TemplateData/modules/ext.templateDataGenerator.editTemplatePage/Target.js

raw symbols SCIP
1 const Dialog = require( './Dialog.js' ),
2 DataModule = require( 'ext.templateDataGenerator.data' ),
3 Model = DataModule.Model,
4 SourceHandler = DataModule.SourceHandler;
5
6 /**
7 * Template data edit ui target
8 *
9 * @class
10 * @extends OO.ui.Element
11 * @mixes OO.EventEmitter
12 *
13 * @constructor
14 * @param {jQuery} $textarea Editor textarea
15 * @param {Object} config
16 */
17 function Target( $textarea, config ) {
18 // Parent constructor
19 Target.super.call( this, config );
20
21 // Mixin constructor
22 OO.EventEmitter.call( this );
23
24 this.pageName = config.pageName;
25 this.parentPage = config.parentPage;
26 this.isPageSubLevel = !!config.isPageSubLevel;
27 this.isDocPage = !!config.isDocPage;
28 this.docSubpage = config.docSubpage;
29 this.$textarea = $textarea;
30
31 this.editOpenDialogButton = new OO.ui.ButtonWidget( {
32 label: mw.msg( 'templatedata-editbutton' )
33 } );
34
35 this.editNoticeMessage = new OO.ui.MessageWidget( {
36 classes: [ 'tdg-editscreen-edit-notice' ]
37 } )
38 .toggle( false );
39
40 const $helpLink = $( '<a>' )
41 .attr( {
42 href: mw.msg( 'templatedata-helplink-target' ),
43 target: '_blank'
44 } )
45 .addClass( 'tdg-editscreen-main-helplink' )
46 .text( mw.msg( 'templatedata-helplink' ) );
47
48 this.windowManager = new OO.ui.WindowManager();
49 // Ensure OOUI's window manager is on top of ours, because we use OO.ui.confirm() elsewhere.
50 // This is a bit silly...
51 OO.ui.getWindowManager().$element.before( this.windowManager.$element );
52
53 // Dialog
54 this.tdgDialog = new Dialog( config );
55 this.windowManager.addWindows( [ this.tdgDialog ] );
56
57 this.sourceHandler = new SourceHandler( {
58 fullPageName: this.pageName,
59 parentPage: this.parentPage,
60 isPageSubLevel: this.isPageSubLevel
61 } );
62
63 // Check if there's already a templatedata in a related page
64 const relatedPage = this.isDocPage ? this.parentPage : this.pageName + '/' + this.docSubpage;
65 this.sourceHandler.getApi( relatedPage )
66 .then( ( result ) => {
67 const response = result.query.pages[ result.query.pageids[ 0 ] ];
68 // HACK: When checking whether a related page (parent for /doc page or
69 // vice versa) already has a templatedata string, we shouldn't
70 // ask for the 'templatedata' action but rather the actual content
71 // of the related page, otherwise we get embedded templatedata and
72 // wrong information is presented.
73 if ( response.missing === undefined ) {
74 const content = response.revisions[ 0 ][ '*' ];
75 // There's a templatedata string
76 if ( /<templatedata>/i.test( content ) ) {
77 // HACK: Setting a link in the messages doesn't work. The bug report offers
78 // a somewhat hacky work around that includes setting a separate message
79 // to be parsed.
80 // https://phabricator.wikimedia.org/T49395#490610
81 let msg = mw.message( 'templatedata-exists-on-related-page', relatedPage ).plain();
82 mw.messages.set( { 'templatedata-string-exists-hack-message': msg } );
83 msg = new OO.ui.HtmlSnippet(
84 mw.message( 'templatedata-string-exists-hack-message' ).parse()
85 );
86
87 this.setEditNoticeMessage( msg, 'warning' );
88 }
89 }
90 } );
91
92 // Events
93 this.editOpenDialogButton.connect( this, { click: 'onEditOpenDialogButton' } );
94 this.tdgDialog.connect( this, { apply: 'onDialogApply' } );
95
96 this.$element
97 .addClass( 'tdg-editscreen-main' )
98 .append(
99 this.editOpenDialogButton.$element,
100 $helpLink,
101 this.editNoticeMessage.$element
102 );
103 }
104
105 /* Inheritance */
106
107 OO.inheritClass( Target, OO.ui.Element );
108
109 OO.mixinClass( Target, OO.EventEmitter );
110
111 /* Methods */
112
113 /**
114 * Destroy the target
115 */
116 Target.prototype.destroy = function () {
117 this.windowManager.destroy();
118 this.$element.remove();
119 };
120
121 /**
122 * Display error message in the edit window
123 *
124 * @method setNoticeMessage
125 * @param {jQuery|string|OO.ui.HtmlSnippet|Function|null} label Message to display
126 * @param {string} type Message type 'notice', 'error', 'warning' or 'success'
127 */
128 Target.prototype.setEditNoticeMessage = function ( label, type ) {
129 this.editNoticeMessage.setLabel( label );
130 this.editNoticeMessage.setType( type );
131 this.editNoticeMessage.toggle( true );
132 };
133
134 /**
135 * Open the templatedata edit dialog
136 *
137 * @method openEditDialog
138 * @param {Model} dataModel The data model
139 * associated with this edit dialog.
140 */
141 Target.prototype.openEditDialog = function ( dataModel ) {
142 // Open the edit dialog
143 this.windowManager.openWindow( 'TemplateDataDialog', {
144 model: dataModel,
145 editNoticeMessage: this.editNoticeMessage
146 } );
147 };
148
149 /**
150 * Respond to edit dialog button click.
151 *
152 * @method onEditOpenDialogButton
153 */
154 Target.prototype.onEditOpenDialogButton = function () {
155 this.originalWikitext = this.$textarea.textSelection( 'getContents' );
156
157 // Build the model
158 this.sourceHandler.buildModel( this.originalWikitext )
159 .then(
160 // Success
161 ( model ) => {
162 this.openEditDialog( model );
163 },
164 // Failure
165 () => {
166 // Open a message dialog
167 OO.ui.getWindowManager().openWindow( 'message', {
168 title: mw.msg( 'templatedata-modal-title' ),
169 message: mw.msg( 'templatedata-errormsg-jsonbadformat' ),
170 verbose: true,
171 actions: [
172 {
173 action: 'accept',
174 label: mw.msg( 'templatedata-modal-json-error-replace' ),
175 flags: [ 'primary', 'destructive' ]
176 },
177 {
178 action: 'reject',
179 label: OO.ui.deferMsg( 'ooui-dialog-message-reject' ),
180 flags: 'safe'
181 }
182 ]
183 } ).closed.then( ( data ) => {
184 if ( data && data.action === 'accept' ) {
185 // Open the dialog with an empty model
186 const model = Model.static.newFromObject(
187 null,
188 this.sourceHandler.getTemplateSourceCodeParams()
189 );
190 this.openEditDialog( model );
191 }
192 } );
193 }
194 );
195 };
196
197 /**
198 * Replace the old templatedata string with the new one, or
199 * insert the new one into the page if an old one doesn't exist
200 *
201 * @method replaceTemplateData
202 * @param {Object} newTemplateData New templatedata
203 */
204 Target.prototype.replaceTemplateData = function ( newTemplateData ) {
205 const templateDataJSON = JSON.stringify( newTemplateData, null, '\t' ),
206 templatedataPattern = /(<templatedata>\s*)([\s\S]*?)\s*<\/templatedata>/i;
207
208 let matches, templateDataOutput;
209 if ( ( matches = this.originalWikitext.match( templatedataPattern ) ) ) {
210 // Move cursor to select withing existing <templatedata> and whitespace
211 this.$textarea.textSelection( 'setSelection', {
212 start: matches.index + matches[ 1 ].length,
213 end: matches.index + matches[ 1 ].length + matches[ 2 ].length
214 } );
215 templateDataOutput = templateDataJSON;
216 } else {
217 this.$textarea.textSelection( 'setSelection', { start: this.originalWikitext.length } );
218
219 templateDataOutput = '<templatedata>\n' + templateDataJSON + '\n</templatedata>';
220
221 if ( !this.isPageSubLevel ) {
222 if ( ( matches = this.originalWikitext.match( /<\/noinclude>\s*$/ ) ) ) {
223 // Move cursor inside </noinclude>
224 this.$textarea.textSelection( 'setSelection', { start: matches.index } );
225 } else {
226 // Wrap in new <noinclude>s
227 templateDataOutput = '<noinclude>\n' + templateDataOutput + '\n</noinclude>\n';
228 }
229 }
230
231 if ( this.originalWikitext.slice( -1 ) !== '\n' ) {
232 templateDataOutput = '\n' + templateDataOutput;
233 }
234 }
235 this.$textarea.textSelection( 'replaceSelection', templateDataOutput );
236 };
237
238 /**
239 * Respond to edit dialog apply event
240 *
241 * @method onDialogApply
242 * @param {Object} templateData New templatedata
243 */
244 Target.prototype.onDialogApply = function ( templateData ) {
245 if (
246 Object.keys( templateData ).length > 1 ||
247 Object.keys( templateData.params ).length > 0
248 ) {
249 this.replaceTemplateData( templateData );
250 } else {
251 this.windowManager.closeWindow( this.windowManager.getCurrentWindow() );
252 OO.ui.getWindowManager().openWindow( 'message', {
253 title: mw.msg( 'templatedata-modal-title' ),
254 message: mw.msg( 'templatedata-errormsg-insertblank' ),
255 actions: [
256 {
257 label: mw.msg( 'templatedata-modal-button-cancel' ),
258 flags: [ 'primary', 'safe' ]
259 },
260 {
261 action: 'apply',
262 label: mw.msg( 'templatedata-modal-button-apply' )
263 }
264 ]
265 } ).closed.then( ( data ) => {
266 if ( data && data.action === 'apply' ) {
267 this.replaceTemplateData( templateData );
268 }
269 } );
270 }
271
272 // TODO: Remove when not needed any more, see T267926
273 // eslint-disable-next-line no-jquery/no-global-selector
274 if ( !$( 'input[name="TemplateDataGeneratorUsed"]' ).length ) {
275 $( '<input>' ).attr( {
276 type: 'hidden',
277 value: 1,
278 name: 'TemplateDataGeneratorUsed'
279 } ).insertAfter( '#wpTextbox1' );
280 }
281 };
282
283 module.exports = Target;