diff --git a/extension.json b/extension.json index 77602d862..d76217c2f 100644 --- a/extension.json +++ b/extension.json @@ -63,6 +63,7 @@ "ThreadItem.js", "CommentItem.js", "HeadingItem.js", + "CommentDetails.js", "lib/moment-timezone/moment-timezone-with-data-1970-2030.js", { "name": "parser/data.json", diff --git a/modules/CommentController.js b/modules/CommentController.js index 80dd18309..79103d7f8 100644 --- a/modules/CommentController.js +++ b/modules/CommentController.js @@ -1,5 +1,6 @@ /** * @external CommentItem + * @external CommentDetails * @external ThreadItem */ @@ -74,7 +75,7 @@ function getLatestRevId( pageName ) { * and then follows transclusions to determine the source page where it is written. * * @param {CommentItem} comment Comment - * @return {jQuery.Promise} Promise which resolves with pageName+oldId, or rejects with an error + * @return {jQuery.Promise} Promise which resolves with a CommentDetails object, or rejects with an error */ CommentController.prototype.getTranscludedFromSource = function ( comment ) { var pageName = mw.config.get( 'wgRelevantPageName' ), @@ -161,8 +162,8 @@ CommentController.prototype.setup = function ( mode, hideErrors ) { this.$replyLinkButtons.addClass( 'ext-discussiontools-init-replylink-active' ); if ( !this.replyWidgetPromise ) { - this.replyWidgetPromise = this.getTranscludedFromSource( comment ).then( function ( pageData ) { - return commentController.createReplyWidget( comment, pageData.pageName, pageData.oldId, { mode: mode } ); + this.replyWidgetPromise = this.getTranscludedFromSource( comment ).then( function ( commentDetails ) { + return commentController.createReplyWidget( comment, commentDetails, { mode: mode } ); }, function ( code, data ) { commentController.teardown(); @@ -213,10 +214,16 @@ CommentController.prototype.getReplyWidgetClass = function ( visual ) { } ); }; -CommentController.prototype.createReplyWidget = function ( comment, pageName, oldId, config ) { +/** + * @param {CommentItem} comment + * @param {CommentDetails} commentDetails + * @param {Object} config + * @return {jQuery.Promise} Promise resolved with a ReplyWidget + */ +CommentController.prototype.createReplyWidget = function ( comment, commentDetails, config ) { var commentController = this; return this.getReplyWidgetClass( config.mode === 'visual' ).then( function ( ReplyWidget ) { - return new ReplyWidget( commentController, comment, pageName, oldId, config ); + return new ReplyWidget( commentController, comment, commentDetails, config ); } ); }; @@ -381,8 +388,7 @@ CommentController.prototype.switchToWikitext = function () { var wikitextPromise = target.getWikitextFragment( target.getSurface().getModel().getDocument() ); this.replyWidgetPromise = this.createReplyWidget( oldWidget.comment, - oldWidget.pageName, - oldWidget.oldId, + oldWidget.commentDetails, { mode: 'source' } ); @@ -503,8 +509,7 @@ CommentController.prototype.switchToVisual = function () { } this.replyWidgetPromise = this.createReplyWidget( oldWidget.comment, - oldWidget.pageName, - oldWidget.oldId, + oldWidget.commentDetails, { mode: 'visual' } ); diff --git a/modules/CommentDetails.js b/modules/CommentDetails.js new file mode 100644 index 000000000..9629c3d69 --- /dev/null +++ b/modules/CommentDetails.js @@ -0,0 +1,16 @@ +/** + * More information about a comment obtained from various APIs, rather than parsed from the page. + * + * @class CommentDetails + * @constructor + * @param {string} pageName Page name the reply is being saved to + * @param {number} oldId Revision ID of page at time of editing + */ +function CommentDetails( pageName, oldId ) { + this.pageName = pageName; + this.oldId = oldId; +} + +OO.initClass( CommentDetails ); + +module.exports = CommentDetails; diff --git a/modules/controller.js b/modules/controller.js index 72458508d..d2d545d00 100644 --- a/modules/controller.js +++ b/modules/controller.js @@ -2,6 +2,7 @@ /** * @external CommentItem + * @external CommentDetails */ var @@ -10,6 +11,7 @@ var featuresEnabled = mw.config.get( 'wgDiscussionToolsFeaturesEnabled' ) || {}, Parser = require( './Parser.js' ), ThreadItem = require( './ThreadItem.js' ), + CommentDetails = require( './CommentDetails.js' ), logger = require( './logger.js' ), utils = require( './utils.js' ), pageDataCache = {}; @@ -130,7 +132,7 @@ function getPageData( pageName, oldId, isNewTopic ) { * @param {string} pageName Page title * @param {number} oldId Revision ID * @param {CommentItem} comment Comment - * @return {jQuery.Promise} Resolves with the pageName+oldId if the comment appears on the page. + * @return {jQuery.Promise} Resolved with a CommentDetails object if the comment appears on the page. * Rejects with error data if the comment is transcluded, or there are lint errors on the page. */ function checkCommentOnPage( pageName, oldId, comment ) { @@ -206,10 +208,7 @@ function checkCommentOnPage( pageName, oldId, comment ) { } ] } ).promise(); } - return { - pageName: pageName, - oldId: oldId - }; + return new CommentDetails( pageName, oldId ); } ); } diff --git a/modules/dt.ui.ReplyWidget.js b/modules/dt.ui.ReplyWidget.js index 1c3821bec..99b1cf247 100644 --- a/modules/dt.ui.ReplyWidget.js +++ b/modules/dt.ui.ReplyWidget.js @@ -13,6 +13,7 @@ require( './AbandonTopicDialog.js' ); /** * @external CommentController * @external CommentItem + * @external CommentDetails */ /** @@ -23,12 +24,11 @@ require( './AbandonTopicDialog.js' ); * @constructor * @param {CommentController} commentController Comment controller * @param {CommentItem} comment Comment item - * @param {string} pageName Page name the reply is being saved to - * @param {number} oldId Revision ID of page at time of editing + * @param {CommentDetails} commentDetails * @param {Object} [config] Configuration options * @param {Object} [config.input] Configuration options for the comment input widget */ -function ReplyWidget( commentController, comment, pageName, oldId, config ) { +function ReplyWidget( commentController, comment, commentDetails, config ) { var widget = this; config = config || {}; @@ -39,9 +39,10 @@ function ReplyWidget( commentController, comment, pageName, oldId, config ) { this.pending = false; this.commentController = commentController; this.comment = comment; + this.commentDetails = commentDetails; this.isNewTopic = !!comment.isNewTopic; - this.pageName = pageName; - this.oldId = oldId; + this.pageName = commentDetails.pageName; + this.oldId = commentDetails.oldId; var contextNode = utils.closestElement( comment.range.endContainer, [ 'dl', 'ul', 'ol' ] ); this.context = contextNode ? contextNode.nodeName.toLowerCase() : 'dl'; // TODO: Should storagePrefix include pageName? diff --git a/modules/dt.ui.ReplyWidgetPlain.js b/modules/dt.ui.ReplyWidgetPlain.js index 2d6837fce..1d08b19be 100644 --- a/modules/dt.ui.ReplyWidgetPlain.js +++ b/modules/dt.ui.ReplyWidgetPlain.js @@ -1,6 +1,7 @@ /** * @external CommentController * @external CommentItem + * @external CommentDetails */ var utils = require( 'ext.discussionTools.init' ).utils; @@ -13,8 +14,7 @@ var utils = require( 'ext.discussionTools.init' ).utils; * @constructor * @param {CommentController} commentController * @param {CommentItem} comment - * @param {string} pageName - * @param {number} oldId + * @param {CommentDetails} commentDetails * @param {Object} [config] */ function ReplyWidgetPlain() { diff --git a/modules/dt.ui.ReplyWidgetVisual.js b/modules/dt.ui.ReplyWidgetVisual.js index b09b33d89..4fd1799cb 100644 --- a/modules/dt.ui.ReplyWidgetVisual.js +++ b/modules/dt.ui.ReplyWidgetVisual.js @@ -11,6 +11,7 @@ require( './dt-ve/dt.ce.PingNode.js' ); /** * @external CommentController * @external CommentItem + * @external CommentDetails */ /** @@ -21,11 +22,10 @@ require( './dt-ve/dt.ce.PingNode.js' ); * @constructor * @param {CommentController} commentController * @param {CommentItem} comment - * @param {string} pageName - * @param {number} oldId + * @param {CommentDetails} commentDetails * @param {Object} [config] */ -function ReplyWidgetVisual( commentController, comment, pageName, oldId, config ) { +function ReplyWidgetVisual( commentController, comment, commentDetails, config ) { this.defaultMode = config.mode; // Parent constructor