Debian Codegraph @

extensions/DiscussionTools/modules/NewTopicController.js

raw symbols SCIP
1 const
2 CommentController = require( './CommentController.js' ),
3 HeadingItem = require( './HeadingItem.js' );
4
5 /**
6 * Handles setup, save and teardown of new topic widget
7 *
8 * @param {jQuery} $pageContainer Page container
9 * @param {HeadingItem} threadItem
10 * @param {ThreadItemSet} threadItemSet
11 * @param {MemoryStorage} storage Storage object for autosave
12 */
13 function NewTopicController( $pageContainer, threadItem, threadItemSet, storage ) {
14 this.container = new OO.ui.PanelLayout( {
15 classes: [ 'ext-discussiontools-ui-newTopic' ],
16 expanded: false,
17 padded: true,
18 framed: true
19 } );
20 this.$notices = $( '<div>' ).addClass( 'ext-discussiontools-ui-newTopic-notices' );
21
22 this.sectionTitle = new OO.ui.TextInputWidget( {
23 // Wrap in a <h2> element to inherit heading font styles
24 $element: $( '<h2>' ),
25 classes: [ 'ext-discussiontools-ui-newTopic-sectionTitle' ],
26 placeholder: mw.msg( 'discussiontools-newtopic-placeholder-title' ),
27 spellcheck: true
28 } );
29 this.sectionTitle.$input.attr( 'aria-label', mw.msg( 'discussiontools-newtopic-placeholder-title' ) );
30 this.sectionTitleField = new OO.ui.FieldLayout( this.sectionTitle, {
31 classes: [ 'ext-discussiontools-ui-newTopic-sectionTitle-field' ],
32 align: 'top'
33 } );
34 this.prevTitleText = '';
35
36 this.container.$element.append( this.$notices, this.sectionTitleField.$element );
37
38 // HeadingItem representing the heading being added, so that we can pretend we're replying to it
39 threadItem.range.startContainer = this.sectionTitleField.$element[ 0 ];
40 threadItem.range.startOffset = 0;
41 threadItem.range.endContainer = this.sectionTitleField.$element[ 0 ];
42 threadItem.range.endOffset = this.sectionTitleField.$element[ 0 ].childNodes.length;
43
44 NewTopicController.super.call( this, $pageContainer, threadItem, threadItemSet, storage );
45 }
46
47 OO.inheritClass( NewTopicController, CommentController );
48
49 /* Static properties */
50
51 NewTopicController.static.initType = 'section';
52
53 NewTopicController.static.suppressedEditNotices = [
54 // Ignored because we have a custom warning for non-logged-in users.
55 'anoneditwarning',
56 // Ignored because it contains mostly instructions for signing comments using tildes.
57 // (Does not appear in VE notices right now, but just in case.)
58 'talkpagetext',
59 // Ignored because the empty state takeover has already explained
60 // that this is a new article.
61 'newarticletext',
62 'newarticletextanon'
63 ];
64
65 /* Methods */
66
67 /**
68 * @inheritdoc
69 */
70 NewTopicController.prototype.setup = function () {
71 const rootScrollable = OO.ui.Element.static.getRootScrollableElement( document.body );
72
73 // Insert directly after the page content on already existing pages
74 // (.mw-parser-output is missing on non-existent pages)
75 const $parserOutput = this.$pageContainer.find( '.mw-parser-output' ).first();
76 const $mobileAddTopicWrapper = this.$pageContainer.find( '.ext-discussiontools-init-new-topic' );
77 if ( $parserOutput.length ) {
78 $parserOutput.after( this.container.$element );
79 } else if ( $mobileAddTopicWrapper.length ) {
80 $mobileAddTopicWrapper.before( this.container.$element );
81 } else {
82 this.$pageContainer.append( this.container.$element );
83 }
84
85 NewTopicController.super.prototype.setup.apply( this, arguments );
86
87 if ( this.threadItem.preloadtitle ) {
88 this.sectionTitle.setValue( this.threadItem.preloadtitle );
89 }
90
91 // The section title field is added to the page immediately, we can scroll to the bottom and focus
92 // it while the content field is still loading.
93 rootScrollable.scrollTop = rootScrollable.scrollHeight;
94 this.focus();
95 };
96
97 /**
98 * @inheritdoc
99 */
100 NewTopicController.prototype.setupReplyWidget = function ( replyWidget, data ) {
101 if ( replyWidget.commentDetails.preloadContent && ( !data || data.value === undefined ) ) {
102 if ( replyWidget.commentDetails.preloadContentMode !== replyWidget.getMode() ) {
103 // This should never happen
104 throw new Error( 'Preload content was loaded for wrong mode' );
105 }
106 data = Object.assign( {}, data, {
107 value: replyWidget.commentDetails.preloadContent
108 } );
109 }
110 NewTopicController.super.prototype.setupReplyWidget.apply( this, arguments );
111
112 this.$notices.empty();
113 for ( const noticeName in this.replyWidget.commentDetails.notices ) {
114 if ( this.constructor.static.suppressedEditNotices.indexOf( noticeName ) !== -1 ) {
115 continue;
116 }
117 const noticeItem = this.replyWidget.commentDetails.notices[ noticeName ];
118 const $noticeElement = $( '<div>' )
119 .addClass( 'ext-discussiontools-ui-replyWidget-notice' )
120 .html( typeof noticeItem === 'string' ? noticeItem : noticeItem.message );
121 this.$notices.append( $noticeElement );
122 }
123 mw.hook( 'wikipage.content' ).fire( this.$notices );
124
125 const title = this.replyWidget.storage.get( 'title' );
126 if ( title && !this.sectionTitle.getValue() ) {
127 // Don't overwrite if the user has already typed something in while the widget was loading.
128 // TODO This should happen immediately rather than waiting for the reply widget to load,
129 // then we wouldn't need this check, but the autosave code is in ReplyWidget.
130 this.sectionTitle.setValue( title );
131 this.prevTitleText = title;
132
133 if ( this.replyWidget.storage.get( 'summary' ) === null ) {
134 const generatedSummary = this.generateSummary( title );
135 this.replyWidget.editSummaryInput.setValue( generatedSummary );
136 }
137 }
138 this.replyWidget.storage.set( 'title', this.sectionTitle.getValue() );
139
140 if ( this.replyWidget.modeTabSelect ) {
141 // Start with the mode-select widget not-tabbable so focus will go from the title to the body
142 this.replyWidget.modeTabSelect.$element.attr( {
143 tabindex: '-1'
144 } );
145 }
146
147 this.sectionTitle.connect( this, { change: 'onSectionTitleChange' } );
148 this.replyWidget.connect( this, { bodyFocus: 'onBodyFocus' } );
149
150 replyWidget.connect( this, {
151 clear: 'onReplyWidgetClear',
152 clearStorage: 'onReplyWidgetClearStorage'
153 } );
154 };
155
156 /**
157 * @inheritdoc
158 */
159 NewTopicController.prototype.focus = function () {
160 this.sectionTitle.focus();
161 };
162
163 /**
164 * Restore the widget to its original state
165 */
166 NewTopicController.prototype.onReplyWidgetClear = function () {
167 // This is going to get called as part of the teardown chain from replywidget
168 this.sectionTitle.setValue( '' );
169 this.sectionTitleField.setWarnings( [] );
170 };
171
172 /**
173 * Remove any storage that the widget is using
174 */
175 NewTopicController.prototype.onReplyWidgetClearStorage = function () {
176 // This is going to get called as part of the teardown chain from replywidget
177 if ( this.replyWidget ) {
178 this.replyWidget.storage.remove( 'title' );
179 }
180 };
181
182 NewTopicController.prototype.storeEditSummary = function () {
183 if ( this.replyWidget ) {
184 const currentSummary = this.replyWidget.editSummaryInput.getValue();
185 const generatedSummary = this.generateSummary( this.sectionTitle.getValue() );
186 if ( currentSummary === generatedSummary ) {
187 // Do not store generated summaries (T315730)
188 this.replyWidget.storage.remove( 'summary' );
189 return;
190 }
191 }
192
193 NewTopicController.super.prototype.storeEditSummary.call( this );
194 };
195
196 /**
197 * @inheritdoc
198 */
199 NewTopicController.prototype.onReplyWidgetTeardown = function ( abandoned ) {
200 // Parent method
201 NewTopicController.super.prototype.onReplyWidgetTeardown.call( this, abandoned );
202
203 this.container.$element.detach();
204
205 if ( mw.config.get( 'wgDiscussionToolsStartNewTopicTool' ) ) {
206 const url = new URL( location.href );
207 url.searchParams.delete( 'action' );
208 url.searchParams.delete( 'veaction' );
209 url.searchParams.delete( 'section' );
210 url.searchParams.delete( 'dtpreload' );
211 url.searchParams.delete( 'editintro' );
212 url.searchParams.delete( 'preload' );
213 url.searchParams.delete( 'preloadparams[]' );
214 url.searchParams.delete( 'preloadtitle' );
215 history.replaceState( null, '', url );
216 mw.config.set( 'wgDiscussionToolsStartNewTopicTool', false );
217 }
218 };
219
220 /**
221 * @inheritdoc
222 */
223 NewTopicController.prototype.doIndentReplacements = function ( wikitext ) {
224 // No indent replacements when posting new topics
225 return wikitext;
226 };
227
228 /**
229 * @inheritdoc
230 */
231 NewTopicController.prototype.undoIndentReplacements = function ( wikitext ) {
232 // No indent replacements when posting new topics
233 return wikitext;
234 };
235
236 /**
237 * @inheritdoc
238 */
239 NewTopicController.prototype.getUnsupportedNodeSelectors = function () {
240 // No unsupported nodes when posting new topics
241 return {};
242 };
243
244 /**
245 * @inheritdoc
246 */
247 NewTopicController.prototype.getApiQuery = function ( pageName, checkboxes, extraParams ) {
248 let data = NewTopicController.super.prototype.getApiQuery.call( this, pageName, checkboxes, extraParams );
249
250 // Rebuild the tags array and remove the reply tag
251 const tags = ( data.dttags || '' ).split( ',' );
252 const replyTag = tags.indexOf( 'discussiontools-reply' );
253 if ( replyTag !== -1 ) {
254 tags.splice( replyTag, 1 );
255 }
256 // Add the newtopic tag
257 tags.push( 'discussiontools-newtopic' );
258
259 data = Object.assign( {}, data, {
260 paction: 'addtopic',
261 sectiontitle: this.sectionTitle.getValue(),
262 dttags: tags.join( ',' )
263 } );
264
265 // Allow MediaWiki to generate the summary if it wasn't modified by the user. This avoids
266 // inconsistencies in how wiki markup is stripped from section titles when they're used in
267 // automatic summaries. (T275178)
268 if ( data.summary === this.generateSummary( this.sectionTitle.getValue() ) ) {
269 delete data.summary;
270 }
271
272 return data;
273 };
274
275 /**
276 * @inheritdoc
277 */
278 NewTopicController.prototype.saveFail = function ( code ) {
279 if ( code === 'discussiontools-newtopic-missing-title' ) {
280 OO.ui.confirm( mw.msg( 'discussiontools-newtopic-missing-title-prompt' ) ).then( ( confirmed ) => {
281 if ( confirmed ) {
282 this.onReplySubmit( { allownosectiontitle: true } );
283 } else {
284 this.sectionTitle.focus();
285 }
286 } );
287 return;
288 }
289
290 NewTopicController.super.prototype.saveFail.apply( this, arguments );
291 };
292
293 /**
294 * Generate a default edit summary based on the section title.
295 *
296 * @param {string} titleText Section title
297 * @return {string}
298 */
299 NewTopicController.prototype.generateSummary = function ( titleText ) {
300 return titleText ? mw.msg( 'newsectionsummary', titleText ) : '';
301 };
302
303 /**
304 * Handle 'change' events for the section title input.
305 *
306 * @private
307 */
308 NewTopicController.prototype.onSectionTitleChange = function () {
309 const titleText = this.sectionTitle.getValue();
310 const prevTitleText = this.prevTitleText;
311
312 if ( prevTitleText !== titleText ) {
313 this.replyWidget.storage.set( 'title', titleText );
314
315 const generatedSummary = this.generateSummary( titleText );
316 const generatedPrevSummary = this.generateSummary( prevTitleText );
317
318 const currentSummary = this.replyWidget.editSummaryInput.getValue();
319
320 // Fill in edit summary if it was not modified by the user yet
321 if ( currentSummary === generatedPrevSummary ) {
322 this.replyWidget.editSummaryInput.setValue( generatedSummary );
323 }
324 }
325
326 this.prevTitleText = titleText;
327
328 this.checkSectionTitleValidity();
329 };
330
331 /**
332 * Handle 'focus' events for the description field (regardless of mode).
333 *
334 * @private
335 */
336 NewTopicController.prototype.onBodyFocus = function () {
337 const offsetBefore = this.replyWidget.$element.offset().top;
338 const rootScrollable = OO.ui.Element.static.getRootScrollableElement( document.body );
339 const scrollBefore = rootScrollable.scrollTop;
340
341 this.checkSectionTitleValidity();
342
343 const offsetChange = this.replyWidget.$element.offset().top - offsetBefore;
344 // Ensure the rest of the widget doesn't move when the validation
345 // message is triggered by a focus. (T275923)
346 // Browsers sometimes also scroll in response to focus events,
347 // so use the old scrollTop value for consistent results.
348 rootScrollable.scrollTop = scrollBefore + offsetChange;
349
350 if ( this.replyWidget.modeTabSelect ) {
351 // Return normal tabbable status to the mode select widget so shift-tab will move focus to it
352 // (Similar to how the other toolbar elements only become tabbable once the body has focus)
353 this.replyWidget.modeTabSelect.$element.attr( {
354 tabindex: '0'
355 } );
356 }
357 };
358
359 /**
360 * Check if the section title is valid, and display a warning message.
361 *
362 * @private
363 */
364 NewTopicController.prototype.checkSectionTitleValidity = function () {
365 if ( !this.sectionTitle.getValue() ) {
366 // Show warning about missing title
367 this.sectionTitleField.setWarnings( [
368 mw.msg( 'discussiontools-newtopic-missing-title' )
369 ] );
370 } else {
371 this.sectionTitleField.setWarnings( [] );
372 }
373 };
374
375 module.exports = NewTopicController;