| 1 |
|
| 2 |
|
| 3 |
const utils = require( './utils.js' ); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
function ThreadItem( type, level, range ) { |
| 18 |
this.type = type; |
| 19 |
this.level = level; |
| 20 |
this.range = range; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
this.name = null; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
this.id = null; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
this.replies = []; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
this.warnings = []; |
| 40 |
|
| 41 |
this.rootNode = null; |
| 42 |
|
| 43 |
this.authors = null; |
| 44 |
this.commentCount = null; |
| 45 |
this.oldestReply = null; |
| 46 |
this.latestReply = null; |
| 47 |
} |
| 48 |
|
| 49 |
OO.initClass( ThreadItem ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
ThreadItem.static.newFromJSON = function ( json, rootNode ) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
const hash = typeof json === 'string' ? JSON.parse( json ) : json; |
| 65 |
|
| 66 |
let item; |
| 67 |
switch ( hash.type ) { |
| 68 |
case 'comment': { |
| 69 |
|
| 70 |
const CommentItem = require( './CommentItem.js' ); |
| 71 |
item = new CommentItem( |
| 72 |
hash.level, |
| 73 |
hash.range, |
| 74 |
hash.signatureRanges, |
| 75 |
hash.timestampRanges, |
| 76 |
moment.utc( hash.timestamp, [ |
| 77 |
|
| 78 |
'YYYYMMDDHHmmss', |
| 79 |
moment.ISO_8601 |
| 80 |
], true ), |
| 81 |
hash.author, |
| 82 |
hash.displayName |
| 83 |
); |
| 84 |
break; |
| 85 |
} |
| 86 |
case 'heading': { |
| 87 |
const HeadingItem = require( './HeadingItem.js' ); |
| 88 |
|
| 89 |
|
| 90 |
if ( hash.headingLevel === 99 ) { |
| 91 |
hash.headingLevel = null; |
| 92 |
} |
| 93 |
item = new HeadingItem( |
| 94 |
hash.range, |
| 95 |
hash.headingLevel |
| 96 |
); |
| 97 |
break; |
| 98 |
} |
| 99 |
default: |
| 100 |
throw new Error( 'Unknown ThreadItem type ' + hash.name ); |
| 101 |
} |
| 102 |
item.name = hash.name; |
| 103 |
item.id = hash.id; |
| 104 |
|
| 105 |
item.rootNode = rootNode; |
| 106 |
|
| 107 |
const idEscaped = $.escapeSelector( item.id ); |
| 108 |
const startMarker = document.getElementById( item.id ); |
| 109 |
const endMarker = document.querySelector( '[data-mw-comment-end="' + idEscaped + '"]' ); |
| 110 |
|
| 111 |
item.range = { |
| 112 |
|
| 113 |
startContainer: startMarker.parentNode, |
| 114 |
startOffset: utils.childIndexOf( startMarker ) + 1, |
| 115 |
|
| 116 |
endContainer: endMarker, |
| 117 |
endOffset: 0 |
| 118 |
}; |
| 119 |
|
| 120 |
if ( item.type === 'comment' ) { |
| 121 |
const sigMarker = document.querySelector( '[data-mw-comment-sig="' + idEscaped + '"]' ); |
| 122 |
if ( sigMarker ) { |
| 123 |
item.signatureRanges = [ { |
| 124 |
startContainer: sigMarker.parentNode, |
| 125 |
startOffset: utils.childIndexOf( sigMarker ) + 1, |
| 126 |
endContainer: item.range.endContainer, |
| 127 |
endOffset: item.range.endOffset |
| 128 |
} ]; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return item; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
ThreadItem.prototype.calculateThreadSummary = function () { |
| 139 |
if ( this.authors ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
const authors = {}; |
| 143 |
let commentCount = 0; |
| 144 |
let oldestReply = null; |
| 145 |
let latestReply = null; |
| 146 |
function threadScan( comment ) { |
| 147 |
if ( comment.type === 'comment' ) { |
| 148 |
authors[ comment.author ] = authors[ comment.author ] || { |
| 149 |
username: comment.author, |
| 150 |
displayNames: [] |
| 151 |
}; |
| 152 |
if ( |
| 153 |
comment.displayName && |
| 154 |
authors[ comment.author ].displayNames.indexOf( comment.displayName ) === -1 |
| 155 |
) { |
| 156 |
authors[ comment.author ].displayNames.push( comment.displayName ); |
| 157 |
} |
| 158 |
|
| 159 |
if ( |
| 160 |
!oldestReply || |
| 161 |
( comment.timestamp < oldestReply.timestamp ) |
| 162 |
) { |
| 163 |
oldestReply = comment; |
| 164 |
} |
| 165 |
if ( |
| 166 |
!latestReply || |
| 167 |
( latestReply.timestamp < comment.timestamp ) |
| 168 |
) { |
| 169 |
latestReply = comment; |
| 170 |
} |
| 171 |
commentCount++; |
| 172 |
} |
| 173 |
comment.replies.forEach( threadScan ); |
| 174 |
} |
| 175 |
this.replies.forEach( threadScan ); |
| 176 |
|
| 177 |
this.authors = Object.keys( authors ).sort().map( ( author ) => authors[ author ] ); |
| 178 |
this.commentCount = commentCount; |
| 179 |
this.oldestReply = oldestReply; |
| 180 |
this.latestReply = latestReply; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
ThreadItem.prototype.getAuthorsBelow = function () { |
| 191 |
this.calculateThreadSummary(); |
| 192 |
return this.authors; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
ThreadItem.prototype.getCommentCount = function () { |
| 201 |
this.calculateThreadSummary(); |
| 202 |
return this.commentCount; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
ThreadItem.prototype.getLatestReply = function () { |
| 211 |
this.calculateThreadSummary(); |
| 212 |
return this.latestReply; |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
ThreadItem.prototype.getOldestReply = function () { |
| 221 |
this.calculateThreadSummary(); |
| 222 |
return this.oldestReply; |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
ThreadItem.prototype.getThreadItemsBelow = function () { |
| 231 |
const threadItems = []; |
| 232 |
function getReplies( comment ) { |
| 233 |
threadItems.push( comment ); |
| 234 |
comment.replies.forEach( getReplies ); |
| 235 |
} |
| 236 |
|
| 237 |
this.replies.forEach( getReplies ); |
| 238 |
|
| 239 |
return threadItems; |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
ThreadItem.prototype.getRange = function () { |
| 248 |
const doc = this.range.startContainer.ownerDocument; |
| 249 |
const nativeRange = doc.createRange(); |
| 250 |
nativeRange.setStart( this.range.startContainer, this.range.startOffset ); |
| 251 |
nativeRange.setEnd( this.range.endContainer, this.range.endOffset ); |
| 252 |
return nativeRange; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
ThreadItem.prototype.getNativeRange = ThreadItem.prototype.getRange; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
module.exports = ThreadItem; |