| 1 |
const |
| 2 |
CommentController = require( './CommentController.js' ), |
| 3 |
HeadingItem = require( './HeadingItem.js' ); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 50 |
|
| 51 |
NewTopicController.static.initType = 'section'; |
| 52 |
|
| 53 |
NewTopicController.static.suppressedEditNotices = [ |
| 54 |
|
| 55 |
'anoneditwarning', |
| 56 |
|
| 57 |
|
| 58 |
'talkpagetext', |
| 59 |
|
| 60 |
|
| 61 |
'newarticletext', |
| 62 |
'newarticletextanon' |
| 63 |
]; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
NewTopicController.prototype.setup = function () { |
| 71 |
const rootScrollable = OO.ui.Element.static.getRootScrollableElement( document.body ); |
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 92 |
|
| 93 |
rootScrollable.scrollTop = rootScrollable.scrollHeight; |
| 94 |
this.focus(); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 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 |
|
| 158 |
|
| 159 |
NewTopicController.prototype.focus = function () { |
| 160 |
this.sectionTitle.focus(); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
NewTopicController.prototype.onReplyWidgetClear = function () { |
| 167 |
|
| 168 |
this.sectionTitle.setValue( '' ); |
| 169 |
this.sectionTitleField.setWarnings( [] ); |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
NewTopicController.prototype.onReplyWidgetClearStorage = function () { |
| 176 |
|
| 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 |
|
| 188 |
this.replyWidget.storage.remove( 'summary' ); |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
NewTopicController.super.prototype.storeEditSummary.call( this ); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
NewTopicController.prototype.onReplyWidgetTeardown = function ( abandoned ) { |
| 200 |
|
| 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 |
|
| 222 |
|
| 223 |
NewTopicController.prototype.doIndentReplacements = function ( wikitext ) { |
| 224 |
|
| 225 |
return wikitext; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
NewTopicController.prototype.undoIndentReplacements = function ( wikitext ) { |
| 232 |
|
| 233 |
return wikitext; |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
NewTopicController.prototype.getUnsupportedNodeSelectors = function () { |
| 240 |
|
| 241 |
return {}; |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
NewTopicController.prototype.getApiQuery = function ( pageName, checkboxes, extraParams ) { |
| 248 |
let data = NewTopicController.super.prototype.getApiQuery.call( this, pageName, checkboxes, extraParams ); |
| 249 |
|
| 250 |
|
| 251 |
const tags = ( data.dttags || '' ).split( ',' ); |
| 252 |
const replyTag = tags.indexOf( 'discussiontools-reply' ); |
| 253 |
if ( replyTag !== -1 ) { |
| 254 |
tags.splice( replyTag, 1 ); |
| 255 |
} |
| 256 |
|
| 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 |
|
| 266 |
|
| 267 |
|
| 268 |
if ( data.summary === this.generateSummary( this.sectionTitle.getValue() ) ) { |
| 269 |
delete data.summary; |
| 270 |
} |
| 271 |
|
| 272 |
return data; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
|
| 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 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
NewTopicController.prototype.generateSummary = function ( titleText ) { |
| 300 |
return titleText ? mw.msg( 'newsectionsummary', titleText ) : ''; |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 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 |
|
| 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 |
|
| 333 |
|
| 334 |
|
| 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 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
rootScrollable.scrollTop = scrollBefore + offsetChange; |
| 349 |
|
| 350 |
if ( this.replyWidget.modeTabSelect ) { |
| 351 |
|
| 352 |
|
| 353 |
this.replyWidget.modeTabSelect.$element.attr( { |
| 354 |
tabindex: '0' |
| 355 |
} ); |
| 356 |
} |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
NewTopicController.prototype.checkSectionTitleValidity = function () { |
| 365 |
if ( !this.sectionTitle.getValue() ) { |
| 366 |
|
| 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; |