Debian Codegraph @

extensions/DiscussionTools/modules/ReplyLinksController.js

raw symbols SCIP
1 const
2 // LanguageData::getLocalData()
3 parserData = require( './parser/data.json' ),
4 utils = require( './utils.js' );
5
6 const featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {};
7
8 function tryInfuse( $element ) {
9 if ( $element.length ) {
10 let element = null;
11 // $.data() might have already been cleared by jQuery if the elements were removed, ignore
12 // TODO: We should keep references to the OO.ui.ButtonWidget objects instead of infusing again,
13 // which would avoid this issue too
14 try {
15 element = OO.ui.infuse( $element );
16 } catch ( e ) {}
17 return element;
18 }
19 return null;
20 }
21
22 function ReplyLinksController( $pageContainer ) {
23 // Mixin constructors
24 OO.EventEmitter.call( this );
25
26 this.$pageContainer = $pageContainer;
27 this.$body = $( document.body );
28 this.onReplyLinkClickHandler = this.onReplyLinkClick.bind( this );
29 this.onReplyButtonClickHandler = this.onReplyButtonClick.bind( this );
30 this.onAddSectionLinkClickHandler = this.onAddSectionLinkClick.bind( this );
31 this.onAnyLinkClickHandler = this.onAnyLinkClick.bind( this );
32
33 // Reply links
34 this.$replyLinkSets = $pageContainer.find( '.ext-discussiontools-init-replylink-buttons[ data-mw-thread-id ]:not( :empty )' );
35
36 this.$replyLinkSets.each( ( i, replyLinkContainer ) => {
37 const replyButton = tryInfuse( $( replyLinkContainer ).find( '.ext-discussiontools-init-replybutton' ) );
38 const $replyLink = $( replyLinkContainer ).find( '.ext-discussiontools-init-replylink-reply' );
39 $replyLink.on( 'click keypress', this.onReplyLinkClickHandler );
40 if ( replyButton ) {
41 replyButton.on( 'click', this.onReplyButtonClickHandler, [ replyButton ] );
42 }
43 } );
44
45 this.$replyLinkSets.on( 'focusin mouseover touchstart', () => {
46 this.emit( 'link-interact' );
47 } );
48
49 // "Add topic" link in the skin interface
50 if ( featuresEnabled.newtopictool ) {
51 // eslint-disable-next-line no-jquery/no-global-selector
52 const $addSectionTab = $( '#ca-addsection' );
53 if ( $addSectionTab.length ) {
54 this.$addSectionLink = $addSectionTab.find( 'a' );
55 this.$addSectionLink.on( 'click keypress', this.onAddSectionLinkClickHandler );
56
57 this.$addSectionLink.on( 'focusin mouseover touchstart', () => {
58 this.emit( 'link-interact' );
59 } );
60 }
61 // Handle events on all links that potentially open the new section interface,
62 // including links in the page content (from templates) or from gadgets.
63 this.$body.on( 'click keypress', 'a', this.onAnyLinkClickHandler );
64 }
65 }
66
67 OO.initClass( ReplyLinksController );
68 OO.mixinClass( ReplyLinksController, OO.EventEmitter );
69
70 /**
71 * @event link-click
72 * @param {string} id
73 * @param {jQuery} $linkSet
74 * @param {jQuery} $link
75 * @param {Object} [data]
76 */
77
78 /* Methods */
79
80 ReplyLinksController.prototype.onReplyLinkClick = function ( e ) {
81 if ( !this.isActivationEvent( e ) ) {
82 return;
83 }
84 e.preventDefault();
85
86 // Browser plugins (such as Google Translate) may add extra tags inside
87 // the link, so find the containing link tag with the data we need.
88 const $linkSet = $( e.target ).closest( '[data-mw-thread-id]' );
89 if ( !$linkSet.length ) {
90 return;
91 }
92 this.emit( 'link-click', $linkSet.data( 'mw-thread-id' ), $linkSet );
93 };
94
95 ReplyLinksController.prototype.onReplyButtonClick = function ( button ) {
96 const $linkSet = button.$element.closest( '[data-mw-thread-id]' );
97 this.emit( 'link-click', $linkSet.data( 'mw-thread-id' ), $linkSet );
98 };
99
100 ReplyLinksController.prototype.onAddSectionLinkClick = function ( e ) {
101 if ( !this.isActivationEvent( e ) ) {
102 return;
103 }
104 // Disable VisualEditor's new section editor (in wikitext mode / NWE), to allow our own.
105 // We do this on first click, because we don't control the order in which our code and NWE code
106 // runs, so its event handlers may not be registered yet.
107 $( e.target ).closest( '#ca-addsection' ).off( '.ve-target' );
108
109 // onAnyLinkClick() will also handle clicks on this element, so we don't emit() here to avoid
110 // doing it twice.
111 };
112
113 ReplyLinksController.prototype.onAnyLinkClick = function ( e ) {
114 if ( $( e.currentTarget ).closest( '[data-mw-thread-id]' ).length ) {
115 // Handled elsewhere
116 return;
117 }
118
119 // Check query parameters to see if this is really a new topic link
120 const href = e.currentTarget.href;
121 if ( !href ) {
122 return;
123 }
124
125 const data = this.parseNewTopicLink( href );
126 if ( !data ) {
127 return;
128 }
129
130 if ( !this.isActivationEvent( e ) ) {
131 return;
132 }
133 e.preventDefault();
134
135 this.emit( 'link-click', utils.NEW_TOPIC_COMMENT_ID, $( e.currentTarget ), data );
136 };
137
138 /**
139 * Check if the given URL is a new topic link, and if so, return parsed parameters.
140 *
141 * @param {string} href
142 * @return {Object|null} `null` if not a new topic link, parameters otherwise
143 */
144 ReplyLinksController.prototype.parseNewTopicLink = function ( href ) {
145 const searchParams = new URL( href ).searchParams;
146
147 let title = mw.Title.newFromText( utils.getTitleFromUrl( href ) || '' );
148 if ( !title ) {
149 return null;
150 }
151
152 // Recognize links to add a new topic:
153 if (
154 // Special:NewSection/...
155 title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).special &&
156 title.getMainText().split( '/' )[ 0 ] === parserData.specialNewSectionName
157 ) {
158 // Get the real title from the subpage parameter
159 const param = title.getMainText().slice( parserData.specialNewSectionName.length + 1 );
160 title = mw.Title.newFromText( param );
161 if ( !title ) {
162 return null;
163 }
164
165 } else if (
166 // ?title=...&action=edit&section=new
167 // ?title=...&veaction=editsource&section=new
168 ( searchParams.get( 'action' ) === 'edit' || searchParams.get( 'veaction' ) === 'editsource' ) &&
169 searchParams.get( 'section' ) === 'new' &&
170 searchParams.get( 'dtenable' ) !== '0'
171 ) {
172 // Do nothing
173
174 } else {
175 // Not a link to add a new topic
176 return null;
177 }
178
179 if ( title.getPrefixedDb() !== mw.config.get( 'wgRelevantPageName' ) ) {
180 // Link to add a section on another page, not supported yet (T282205)
181 return null;
182 }
183
184 const data = {};
185 if ( searchParams.get( 'editintro' ) ) {
186 data.editintro = searchParams.get( 'editintro' );
187 }
188 if ( searchParams.get( 'preload' ) ) {
189 data.preload = searchParams.get( 'preload' );
190 }
191 if ( mw.util.getArrayParam( 'preloadparams', searchParams ) ) {
192 data.preloadparams = mw.util.getArrayParam( 'preloadparams', searchParams );
193 }
194 if ( searchParams.get( 'preloadtitle' ) ) {
195 data.preloadtitle = searchParams.get( 'preloadtitle' );
196 }
197
198 // Handle new topic with preloaded text only when requested (T269310)
199 if ( !searchParams.get( 'dtpreload' ) && !$.isEmptyObject( data ) ) {
200 return null;
201 }
202
203 return data;
204 };
205
206 ReplyLinksController.prototype.isActivationEvent = function ( e ) {
207 if ( mw.config.get( 'wgAction' ) !== 'view' ) {
208 // Don't do anything when we're editing/previewing
209 return false;
210 }
211 if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) {
212 // Only handle keypresses on the "Enter" or "Space" keys
213 return false;
214 }
215 if ( e.type === 'click' && !utils.isUnmodifiedLeftClick( e ) ) {
216 // Only handle unmodified left clicks
217 return false;
218 }
219 return true;
220 };
221
222 ReplyLinksController.prototype.focusLink = function ( $linkSet ) {
223 if ( $linkSet.is( this.$replyLinkSets ) ) {
224 const button = tryInfuse( $linkSet.find( '.ext-discussiontools-init-replybutton' ) );
225 // Focus whichever is visible, the link or the button
226 if ( button ) {
227 button.focus();
228 }
229 $linkSet.find( '.ext-discussiontools-init-replylink-reply' ).trigger( 'focus' );
230 }
231 };
232
233 ReplyLinksController.prototype.setActiveLink = function ( $linkSet ) {
234 this.$activeLink = $linkSet;
235
236 let isNewTopic = false;
237 let activeButton;
238 if ( this.$activeLink.is( this.$replyLinkSets ) ) {
239 this.$activeLink.addClass( 'ext-discussiontools-init-replylink-active' );
240 activeButton = tryInfuse( this.$activeLink.find( '.ext-discussiontools-init-replybutton' ) );
241 } else if ( this.$addSectionLink && this.$activeLink.is( this.$addSectionLink ) ) {
242 isNewTopic = true;
243 // eslint-disable-next-line no-jquery/no-global-selector
244 $( '#ca-addsection' ).addClass( 'selected' );
245 // eslint-disable-next-line no-jquery/no-global-selector
246 $( '#ca-addsection-sticky-header' ).addClass( 'ext-discussiontools-fake-disabled' );
247 // eslint-disable-next-line no-jquery/no-global-selector
248 $( '#ca-view' ).removeClass( 'selected' );
249 }
250
251 const title = mw.Title.newFromText( mw.config.get( 'wgRelevantPageName' ) );
252 const pageTitleMsg = mw.message( 'pagetitle',
253 mw.msg(
254 isNewTopic ?
255 'discussiontools-pagetitle-newtopic' :
256 'discussiontools-pagetitle-reply',
257 title.getPrefixedText()
258 )
259 );
260
261 // T317600
262 if ( pageTitleMsg.isParseable() ) {
263 this.originalDocumentTitle = document.title;
264 document.title = pageTitleMsg.text();
265 } else {
266 mw.log.warn( 'DiscussionTools: MediaWiki:Pagetitle contains unsupported syntax. ' +
267 'https://www.mediawiki.org/wiki/Manual:Messages_API#Feature_support_in_JavaScript' );
268 }
269
270 $( document.body ).addClass( 'ext-discussiontools-init-replylink-open' );
271 this.$replyLinkSets.each( ( i, replyLinkContainer ) => {
272 const replyButton = tryInfuse( $( replyLinkContainer ).find( '.ext-discussiontools-init-replybutton' ) );
273 const $replyLink = $( replyLinkContainer ).find( '.ext-discussiontools-init-replylink-reply' );
274 $replyLink.attr( 'tabindex', -1 );
275 if ( !replyButton ) {
276 return;
277 }
278 if ( replyButton === activeButton ) {
279 replyButton.setFlags( { progressive: false } );
280 } else {
281 replyButton.setDisabled( true );
282 }
283 } );
284
285 // Suppress page takeover behavior for VE editing so that our unload
286 // handler can warn of data loss.
287 // eslint-disable-next-line no-jquery/no-global-selector
288 $( '#ca-edit, #ca-ve-edit, .mw-editsection a, #ca-addsection' ).off( '.ve-target' );
289 };
290
291 ReplyLinksController.prototype.clearActiveLink = function () {
292 let activeButton;
293 if ( this.$activeLink.is( this.$replyLinkSets ) ) {
294 this.$activeLink.removeClass( 'ext-discussiontools-init-replylink-active' );
295 activeButton = tryInfuse( this.$activeLink.find( '.ext-discussiontools-init-replybutton' ) );
296 } else if ( this.$addSectionLink && this.$activeLink.is( this.$addSectionLink ) ) {
297 // eslint-disable-next-line no-jquery/no-global-selector
298 $( '#ca-addsection' ).removeClass( 'selected' );
299 // eslint-disable-next-line no-jquery/no-global-selector
300 $( '#ca-addsection-sticky-header' ).removeClass( 'ext-discussiontools-fake-disabled' );
301 // eslint-disable-next-line no-jquery/no-global-selector
302 $( '#ca-view' ).addClass( 'selected' );
303 }
304
305 if ( this.originalDocumentTitle ) {
306 document.title = this.originalDocumentTitle;
307 }
308
309 $( document.body ).removeClass( 'ext-discussiontools-init-replylink-open' );
310 this.$replyLinkSets.each( ( i, replyLinkContainer ) => {
311 const $replyLink = $( replyLinkContainer ).find( '.ext-discussiontools-init-replylink-reply' );
312 $replyLink.attr( 'tabindex', 0 );
313 const replyButton = tryInfuse( $( replyLinkContainer ).find( '.ext-discussiontools-init-replybutton' ) );
314 if ( !replyButton ) {
315 return;
316 }
317 if ( replyButton === activeButton ) {
318 replyButton.setFlags( { progressive: true } );
319 } else {
320 replyButton.setDisabled( false );
321 }
322 } );
323
324 // We deliberately mangled edit links earlier so VE can't steal our page;
325 // have it redo setup to fix those.
326 if ( mw.libs.ve && mw.libs.ve.setupEditLinks ) {
327 mw.libs.ve.setupEditLinks();
328 }
329
330 this.$activeLink = null;
331 };
332
333 ReplyLinksController.prototype.teardown = function () {
334 if ( this.$activeLink ) {
335 this.clearActiveLink();
336 }
337
338 this.$replyLinkSets.each( ( i, replyLinkContainer ) => {
339 const replyButton = tryInfuse( $( replyLinkContainer ).find( '.ext-discussiontools-init-replybutton' ) );
340 if ( replyButton ) {
341 replyButton.off( 'click', this.onReplyButtonClickHandler );
342 }
343 const $replyLink = $( this ).find( '.ext-discussiontools-init-replylink-reply' );
344 $replyLink.off( 'click keypress', this.onReplyLinkClickHandler );
345 } );
346
347 if ( featuresEnabled.newtopictool ) {
348 if ( this.$addSectionLink ) {
349 this.$addSectionLink.off( 'click keypress', this.onAddSectionLinkClickHandler );
350 }
351 this.$body.off( 'click keypress', 'a', this.onAnyLinkClickHandler );
352 }
353 };
354
355 ReplyLinksController.prototype.pageHasReplyLinks = function () {
356 return this.$replyLinkSets.length > 0;
357 };
358
359 ReplyLinksController.prototype.pageHasNewTopicLink = function () {
360 // Note: this will miss if there are random on-page links that would
361 // trigger the new topic tool via onAnyLinkClick
362 return featuresEnabled.newtopictool && document.getElementById( 'ca-addsection' );
363 };
364
365 module.exports = ReplyLinksController;