| 1 |
const |
| 2 |
|
| 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 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 50 |
if ( featuresEnabled.newtopictool ) { |
| 51 |
|
| 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 |
|
| 62 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
ReplyLinksController.prototype.onReplyLinkClick = function ( e ) { |
| 81 |
if ( !this.isActivationEvent( e ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
e.preventDefault(); |
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
$( e.target ).closest( '#ca-addsection' ).off( '.ve-target' ); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
}; |
| 112 |
|
| 113 |
ReplyLinksController.prototype.onAnyLinkClick = function ( e ) { |
| 114 |
if ( $( e.currentTarget ).closest( '[data-mw-thread-id]' ).length ) { |
| 115 |
|
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 153 |
if ( |
| 154 |
|
| 155 |
title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).special && |
| 156 |
title.getMainText().split( '/' )[ 0 ] === parserData.specialNewSectionName |
| 157 |
) { |
| 158 |
|
| 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 |
|
| 167 |
|
| 168 |
( searchParams.get( 'action' ) === 'edit' || searchParams.get( 'veaction' ) === 'editsource' ) && |
| 169 |
searchParams.get( 'section' ) === 'new' && |
| 170 |
searchParams.get( 'dtenable' ) !== '0' |
| 171 |
) { |
| 172 |
|
| 173 |
|
| 174 |
} else { |
| 175 |
|
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
if ( title.getPrefixedDb() !== mw.config.get( 'wgRelevantPageName' ) ) { |
| 180 |
|
| 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 |
|
| 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 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
if ( e.type === 'keypress' && e.which !== OO.ui.Keys.ENTER && e.which !== OO.ui.Keys.SPACE ) { |
| 212 |
|
| 213 |
return false; |
| 214 |
} |
| 215 |
if ( e.type === 'click' && !utils.isUnmodifiedLeftClick( e ) ) { |
| 216 |
|
| 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 |
|
| 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 |
|
| 244 |
$( '#ca-addsection' ).addClass( 'selected' ); |
| 245 |
|
| 246 |
$( '#ca-addsection-sticky-header' ).addClass( 'ext-discussiontools-fake-disabled' ); |
| 247 |
|
| 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 |
|
| 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 |
|
| 286 |
|
| 287 |
|
| 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 |
|
| 298 |
$( '#ca-addsection' ).removeClass( 'selected' ); |
| 299 |
|
| 300 |
$( '#ca-addsection-sticky-header' ).removeClass( 'ext-discussiontools-fake-disabled' ); |
| 301 |
|
| 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 |
|
| 325 |
|
| 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 |
|
| 361 |
|
| 362 |
return featuresEnabled.newtopictool && document.getElementById( 'ca-addsection' ); |
| 363 |
}; |
| 364 |
|
| 365 |
module.exports = ReplyLinksController; |