2020-10-21 15:52:04 +00:00
|
|
|
/* global moment */
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* @external CommentItem
|
|
|
|
*/
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
var utils = require( './utils.js' );
|
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* A thread item, either a heading or a comment
|
|
|
|
*
|
|
|
|
* @class ThreadItem
|
|
|
|
* @constructor
|
|
|
|
* @param {string} type `heading` or `comment`
|
2020-10-01 19:36:11 +00:00
|
|
|
* @param {number} level Indentation level
|
2020-06-25 12:23:17 +00:00
|
|
|
* @param {Object} range Object describing the extent of the comment, including the
|
|
|
|
* signature and timestamp. It has the same properties as a Range object: `startContainer`,
|
|
|
|
* `startOffset`, `endContainer`, `endOffset` (we don't use a real Range because they change
|
|
|
|
* magically when the DOM structure changes).
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
function ThreadItem( type, level, range ) {
|
|
|
|
this.type = type;
|
|
|
|
this.level = level;
|
|
|
|
this.range = range;
|
|
|
|
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
|
|
|
* @member {string} Unique ID (within the page) for this comment, intended to be used to
|
|
|
|
* find this comment in other revisions of the same page
|
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
this.id = null;
|
2020-06-25 12:23:17 +00:00
|
|
|
/**
|
2020-12-12 12:52:17 +00:00
|
|
|
* @member {ThreadItem[]} Replies to this thread item
|
2020-06-25 12:23:17 +00:00
|
|
|
*/
|
2020-05-22 16:26:05 +00:00
|
|
|
this.replies = [];
|
2020-07-29 23:57:51 +00:00
|
|
|
|
2020-11-02 18:35:38 +00:00
|
|
|
/**
|
|
|
|
* @member {string[]} Warnings
|
|
|
|
*/
|
|
|
|
this.warnings = [];
|
|
|
|
|
2020-07-29 23:57:51 +00:00
|
|
|
this.rootNode = null;
|
2020-05-22 16:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OO.initClass( ThreadItem );
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
/**
|
|
|
|
* Create a new ThreadItem from a JSON serialization
|
|
|
|
*
|
|
|
|
* @param {string|Object} json JSON serialization or hash object
|
|
|
|
* @param {Object} commentsById Collection of comments by ID for building replies/parent pointers
|
|
|
|
* @return {ThreadItem}
|
|
|
|
* @throws {Error} Unknown ThreadItem type
|
|
|
|
*/
|
2020-10-27 12:18:50 +00:00
|
|
|
ThreadItem.static.newFromJSON = function ( json, commentsById ) {
|
2020-10-21 15:52:04 +00:00
|
|
|
// The page can be served from the HTTP cache (Varnish), and the JSON may be generated
|
|
|
|
// by an older version of our PHP code. Code below must be able to handle that.
|
|
|
|
// See ThreadItem::jsonSerialize() in PHP.
|
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
var CommentItem, HeadingItem, item, idEscaped,
|
|
|
|
hash = typeof json === 'string' ? JSON.parse( json ) : json;
|
2020-10-27 12:18:50 +00:00
|
|
|
|
2020-09-16 12:07:27 +00:00
|
|
|
switch ( hash.type ) {
|
|
|
|
case 'comment':
|
|
|
|
// Late require to avoid circular dependency
|
|
|
|
CommentItem = require( './CommentItem.js' );
|
|
|
|
item = new CommentItem(
|
|
|
|
hash.level,
|
|
|
|
hash.range,
|
|
|
|
hash.signatureRanges,
|
2020-10-21 15:52:04 +00:00
|
|
|
moment( hash.timestamp ),
|
2020-09-16 12:07:27 +00:00
|
|
|
hash.author
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'heading':
|
|
|
|
HeadingItem = require( './HeadingItem.js' );
|
|
|
|
item = new HeadingItem(
|
|
|
|
hash.range,
|
2020-10-01 19:36:11 +00:00
|
|
|
hash.headingLevel,
|
2020-09-16 12:07:27 +00:00
|
|
|
hash.placeholderHeading
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error( 'Unknown ThreadItem type ' + hash.name );
|
|
|
|
}
|
2020-10-01 19:36:11 +00:00
|
|
|
item.id = hash.id;
|
|
|
|
|
2020-10-27 12:18:50 +00:00
|
|
|
idEscaped = $.escapeSelector( item.id );
|
|
|
|
item.range = {
|
2021-03-10 17:25:52 +00:00
|
|
|
startContainer: document.getElementById( item.id ),
|
2020-10-27 12:18:50 +00:00
|
|
|
startOffset: 0,
|
|
|
|
endContainer: document.querySelector( '[data-mw-comment-end="' + idEscaped + '"]' ),
|
|
|
|
endOffset: 0
|
|
|
|
};
|
2020-09-16 12:07:27 +00:00
|
|
|
|
|
|
|
// Setup replies/parent pointers
|
|
|
|
item.replies = hash.replies.map( function ( id ) {
|
|
|
|
commentsById[ id ].parent = item;
|
|
|
|
return commentsById[ id ];
|
|
|
|
} );
|
|
|
|
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
|
2020-07-20 14:13:59 +00:00
|
|
|
/**
|
|
|
|
* Get the list of authors in the comment tree below this thread item.
|
|
|
|
*
|
|
|
|
* Usually called on a HeadingItem to find all authors in a thread.
|
|
|
|
*
|
|
|
|
* @return {string[]} Author usernames
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getAuthorsBelow = function () {
|
|
|
|
var authors = {};
|
|
|
|
function getAuthorSet( comment ) {
|
|
|
|
authors[ comment.author ] = true;
|
|
|
|
// Get the set of authors in the same format from each reply
|
|
|
|
comment.replies.map( getAuthorSet );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.replies.map( getAuthorSet );
|
|
|
|
|
|
|
|
return Object.keys( authors ).sort();
|
|
|
|
};
|
|
|
|
|
2020-07-20 14:48:41 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the page from which this thread item is transcluded (if any).
|
|
|
|
*
|
|
|
|
* @return {string|boolean} `false` if this item is not transcluded. A string if it's transcluded
|
|
|
|
* from a single page (the page title, in text form with spaces). `true` if it's transcluded, but
|
|
|
|
* we can't determine the source.
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getTranscludedFrom = function () {
|
2020-06-16 14:08:01 +00:00
|
|
|
var coveredNodes, i, node, dataMw;
|
|
|
|
|
|
|
|
// If some template is used within the comment (e.g. {{ping|…}} or {{tl|…}}, or a
|
|
|
|
// non-substituted signature template), that *does not* mean the comment is transcluded.
|
|
|
|
// We only want to consider comments to be transcluded if all wrapper elements (usually
|
|
|
|
// <li> or <p>) are marked as part of a single transclusion.
|
|
|
|
|
|
|
|
// If we can't find "exact" wrappers, using only the end container works out well
|
|
|
|
// (because the main purpose of this method is to decide on which page we should post
|
|
|
|
// replies to the given comment, and they'll go after the comment).
|
|
|
|
|
|
|
|
coveredNodes = utils.getFullyCoveredSiblings( this ) ||
|
|
|
|
[ this.range.endContainer ];
|
|
|
|
|
|
|
|
node = utils.getTranscludedFromElement( coveredNodes[ 0 ] );
|
|
|
|
for ( i = 1; i < coveredNodes.length; i++ ) {
|
|
|
|
if ( node !== utils.getTranscludedFromElement( coveredNodes[ i ] ) ) {
|
|
|
|
// Comment is only partially transcluded, that should be fine
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 14:48:41 +00:00
|
|
|
|
|
|
|
if ( !node ) {
|
|
|
|
// No mw:Transclusion node found, this item is not transcluded
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dataMw = JSON.parse( node.getAttribute( 'data-mw' ) );
|
|
|
|
|
|
|
|
// Only return a page name if this is a simple single-template transclusion.
|
|
|
|
if (
|
|
|
|
dataMw &&
|
|
|
|
dataMw.parts &&
|
|
|
|
dataMw.parts.length === 1 &&
|
|
|
|
dataMw.parts[ 0 ].template &&
|
|
|
|
dataMw.parts[ 0 ].template.target.href
|
|
|
|
) {
|
|
|
|
// Slice off the './' prefix and convert to text form (underscores to spaces, URL-decoded)
|
|
|
|
return mw.libs.ve.normalizeParsoidResourceName( dataMw.parts[ 0 ].template.target.href );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multi-template transclusion, or a parser function call, or template-affected wikitext outside
|
|
|
|
// of a template call, or a mix of the above
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-07-22 18:34:08 +00:00
|
|
|
/**
|
|
|
|
* Return a native Range object corresponding to the item's range.
|
|
|
|
*
|
|
|
|
* @return {Range}
|
|
|
|
*/
|
|
|
|
ThreadItem.prototype.getNativeRange = function () {
|
2020-08-11 04:22:57 +00:00
|
|
|
var
|
2020-07-22 18:34:08 +00:00
|
|
|
doc = this.range.startContainer.ownerDocument,
|
|
|
|
nativeRange = doc.createRange();
|
|
|
|
nativeRange.setStart( this.range.startContainer, this.range.startOffset );
|
2020-08-11 04:22:57 +00:00
|
|
|
nativeRange.setEnd( this.range.endContainer, this.range.endOffset );
|
2020-07-22 18:34:08 +00:00
|
|
|
return nativeRange;
|
|
|
|
};
|
|
|
|
|
2020-07-22 18:25:34 +00:00
|
|
|
// TODO: Implement getHTML/getText if required
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
module.exports = ThreadItem;
|