2020-08-29 12:00:51 +00:00
|
|
|
var
|
|
|
|
CommentController = require( './CommentController.js' ),
|
|
|
|
HeadingItem = require( './HeadingItem.js' );
|
|
|
|
|
|
|
|
function NewTopicController( $pageContainer, $replyLink ) {
|
|
|
|
var comment;
|
|
|
|
|
2021-01-13 20:54:59 +00:00
|
|
|
this.container = new OO.ui.PanelLayout( {
|
|
|
|
classes: [ 'dt-ui-newTopic' ],
|
|
|
|
expanded: false,
|
|
|
|
padded: true,
|
|
|
|
framed: true
|
|
|
|
} );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
this.sectionTitle = new OO.ui.TextInputWidget( {
|
|
|
|
// Wrap in a <h2> element to inherit heading font styles
|
|
|
|
$element: $( '<h2>' ),
|
|
|
|
classes: [ 'dt-ui-newTopic-sectionTitle' ],
|
|
|
|
placeholder: mw.msg( 'discussiontools-newtopic-placeholder-title' ),
|
|
|
|
spellcheck: true
|
|
|
|
} );
|
|
|
|
this.sectionTitleField = new OO.ui.FieldLayout( this.sectionTitle, {
|
|
|
|
align: 'top'
|
|
|
|
} );
|
|
|
|
this.prevTitleText = '';
|
|
|
|
|
2021-01-13 20:54:59 +00:00
|
|
|
this.container.$element.append( this.sectionTitleField.$element );
|
2020-08-29 12:00:51 +00:00
|
|
|
|
|
|
|
// HeadingItem representing the heading being added, so that we can pretend we're replying to it
|
|
|
|
comment = new HeadingItem( {
|
|
|
|
startContainer: this.sectionTitleField.$element[ 0 ],
|
|
|
|
startOffset: 0,
|
|
|
|
endContainer: this.sectionTitleField.$element[ 0 ],
|
|
|
|
endOffset: this.sectionTitleField.$element[ 0 ].childNodes.length
|
|
|
|
} );
|
2021-02-17 12:30:03 +00:00
|
|
|
comment.id = 'new|' + mw.config.get( 'wgRelevantPageName' );
|
2020-08-29 12:00:51 +00:00
|
|
|
comment.isNewTopic = true;
|
|
|
|
|
|
|
|
NewTopicController.super.call( this, $pageContainer, $replyLink, comment );
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.inheritClass( NewTopicController, CommentController );
|
|
|
|
|
2021-01-12 06:34:24 +00:00
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
NewTopicController.static.initType = 'section';
|
|
|
|
|
2020-08-29 12:00:51 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.getTranscludedFromSource = function () {
|
|
|
|
var
|
|
|
|
pageName = mw.config.get( 'wgRelevantPageName' ),
|
|
|
|
oldId = mw.config.get( 'wgCurRevisionId' );
|
|
|
|
|
|
|
|
// Always post on the current page
|
|
|
|
return $.Deferred().resolve( {
|
|
|
|
pageName: pageName,
|
|
|
|
oldId: oldId
|
|
|
|
} ).promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.setup = function ( mode ) {
|
|
|
|
var rootScrollable = OO.ui.Element.static.getRootScrollableElement( document.body );
|
|
|
|
|
2021-01-13 20:54:59 +00:00
|
|
|
this.$pageContainer.append( this.container.$element );
|
2020-08-29 12:00:51 +00:00
|
|
|
NewTopicController.super.prototype.setup.call( this, mode );
|
|
|
|
|
|
|
|
// The section title field is added to the page immediately, we can scroll to the bottom and focus
|
|
|
|
// it while the content field is still loading.
|
|
|
|
rootScrollable.scrollTop = rootScrollable.scrollHeight;
|
|
|
|
this.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.setupReplyWidget = function ( replyWidget, data ) {
|
|
|
|
var title;
|
|
|
|
|
|
|
|
NewTopicController.super.prototype.setupReplyWidget.call( this, replyWidget, data );
|
|
|
|
|
|
|
|
title = this.replyWidget.storage.get( this.replyWidget.storagePrefix + '/title' );
|
|
|
|
if ( title && !this.sectionTitle.getValue() ) {
|
|
|
|
// Don't overwrite if the user has already typed something in while the widget was loading.
|
|
|
|
// TODO This should happen immediately rather than waiting for the reply widget to load,
|
|
|
|
// then we wouldn't need this check, but the autosave code is in ReplyWidget.
|
|
|
|
this.sectionTitle.setValue( title );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sectionTitle.connect( this, { change: 'onSectionTitleChange' } );
|
2021-01-29 22:01:26 +00:00
|
|
|
this.sectionTitle.$input.on( 'blur', this.onSectionTitleBlur.bind( this ) );
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.focus = function () {
|
|
|
|
this.sectionTitle.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.teardown = function ( abandoned ) {
|
|
|
|
NewTopicController.super.prototype.teardown.call( this, abandoned );
|
|
|
|
|
|
|
|
this.replyWidget.storage.remove( this.replyWidget.storagePrefix + '/title' );
|
2021-01-19 20:29:04 +00:00
|
|
|
this.sectionTitle.setValue( '' );
|
2021-01-29 22:01:26 +00:00
|
|
|
this.sectionTitleField.setWarnings( [] );
|
2021-01-13 20:54:59 +00:00
|
|
|
this.container.$element.detach();
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-02-17 23:42:57 +00:00
|
|
|
NewTopicController.prototype.doIndentReplacements = function ( wikitext ) {
|
|
|
|
// No indent replacements when posting new topics
|
2020-08-29 12:00:51 +00:00
|
|
|
return wikitext;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-02-17 23:42:57 +00:00
|
|
|
NewTopicController.prototype.undoIndentReplacements = function ( wikitext ) {
|
|
|
|
// No indent replacements when posting new topics
|
2020-08-29 12:00:51 +00:00
|
|
|
return wikitext;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.getUnsupportedNodeSelectors = function () {
|
|
|
|
// No unsupported nodes when posting new topics
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.getApiQuery = function ( comment, pageName, checkboxes ) {
|
|
|
|
var data = NewTopicController.super.prototype.getApiQuery.call( this, comment, pageName, checkboxes );
|
|
|
|
|
|
|
|
data = $.extend( {}, data, {
|
|
|
|
paction: 'addtopic',
|
|
|
|
sectiontitle: this.sectionTitle.getValue(),
|
|
|
|
dttags: [
|
|
|
|
'discussiontools',
|
|
|
|
'discussiontools-newtopic',
|
|
|
|
'discussiontools-' + this.replyWidget.getMode()
|
|
|
|
].join( ',' )
|
|
|
|
} );
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a default edit summary based on the section title.
|
|
|
|
*
|
|
|
|
* @param {string} titleText Section title
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.generateSummary = function ( titleText ) {
|
|
|
|
return titleText ? mw.msg( 'newsectionsummary', titleText ) : '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle 'change' events for the section title input.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.onSectionTitleChange = function () {
|
|
|
|
var titleText, prevTitleText, generatedSummary, generatedPrevSummary, currentSummary;
|
|
|
|
|
|
|
|
titleText = this.sectionTitle.getValue();
|
|
|
|
prevTitleText = this.prevTitleText;
|
|
|
|
|
|
|
|
if ( prevTitleText !== titleText ) {
|
|
|
|
this.replyWidget.storage.set( this.replyWidget.storagePrefix + '/title', titleText );
|
|
|
|
|
|
|
|
generatedSummary = this.generateSummary( titleText );
|
|
|
|
generatedPrevSummary = this.generateSummary( prevTitleText );
|
|
|
|
|
|
|
|
currentSummary = this.replyWidget.editSummaryInput.getValue();
|
|
|
|
|
|
|
|
// Fill in edit summary if it was not modified by the user yet
|
|
|
|
if ( currentSummary === generatedPrevSummary ) {
|
|
|
|
this.replyWidget.editSummaryInput.setValue( generatedSummary );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prevTitleText = titleText;
|
2021-01-29 22:01:26 +00:00
|
|
|
|
|
|
|
this.checkSectionTitleValidity();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle 'blur' events for the section title input.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.onSectionTitleBlur = function () {
|
|
|
|
this.checkSectionTitleValidity();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the section title is valid, and display a warning message.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
NewTopicController.prototype.checkSectionTitleValidity = function () {
|
|
|
|
if ( !this.sectionTitle.getValue() ) {
|
|
|
|
// Show warning about missing title
|
|
|
|
this.sectionTitleField.setWarnings( [
|
|
|
|
mw.msg( 'discussiontools-newtopic-missing-title' )
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
this.sectionTitleField.setWarnings( [] );
|
|
|
|
}
|
2020-08-29 12:00:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = NewTopicController;
|