| 1 | const CommentItem = require( './CommentItem.js' ); |
| 2 | const HeadingItem = require( './HeadingItem.js' ); |
| 3 | const ThreadItem = require( './ThreadItem.js' ); |
| 4 | |
| 5 | /** |
| 6 | * Groups thread items (headings and comments) generated by parsing a discussion page. |
| 7 | * |
| 8 | * @class ThreadItemSet |
| 9 | */ |
| 10 | function ThreadItemSet() { |
| 11 | this.threadItems = []; |
| 12 | this.commentItems = []; |
| 13 | this.threadItemsByName = {}; |
| 14 | this.threadItemsById = {}; |
| 15 | this.threads = []; |
| 16 | } |
| 17 | |
| 18 | OO.initClass( ThreadItemSet ); |
| 19 | |
| 20 | /** |
| 21 | * Create a new ThreadItemSet from a JSON serialization |
| 22 | * |
| 23 | * @param {Array} threads Hash object |
| 24 | * @param {HTMLElement} rootNode |
| 25 | * @param {mw.dt.Parser} parser |
| 26 | * @return {ThreadItemSet} |
| 27 | */ |
| 28 | ThreadItemSet.static.newFromJSON = function ( threads, rootNode, parser ) { |
| 29 | const result = new ThreadItemSet(); |
| 30 | |
| 31 | function infuse( itemHash, parent ) { |
| 32 | const item = ThreadItem.static.newFromJSON( itemHash, rootNode ); |
| 33 | |
| 34 | result.addThreadItem( item ); |
| 35 | |
| 36 | item.replies = itemHash.replies.map( ( replyHash ) => infuse( replyHash, item ) ); |
| 37 | item.parent = parent; |
| 38 | |
| 39 | return item; |
| 40 | } |
| 41 | |
| 42 | threads.forEach( ( itemHash ) => { |
| 43 | infuse( itemHash, null ); |
| 44 | } ); |
| 45 | |
| 46 | // Calculate names (currently not stored in the metadata) - can't be done |
| 47 | // in the above loop because we have to wait until we have all the |
| 48 | // replies. |
| 49 | parser.computeIdsAndNames( result ); |
| 50 | |
| 51 | return result; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * @param {ThreadItem} item |
| 56 | */ |
| 57 | ThreadItemSet.prototype.addThreadItem = function ( item ) { |
| 58 | this.threadItems.push( item ); |
| 59 | if ( item instanceof CommentItem ) { |
| 60 | this.commentItems.push( item ); |
| 61 | } |
| 62 | if ( item instanceof HeadingItem ) { |
| 63 | this.threads.push( item ); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * @return {boolean} |
| 69 | */ |
| 70 | ThreadItemSet.prototype.isEmpty = function () { |
| 71 | return this.threadItems.length === 0; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * @param {ThreadItem} item |
| 76 | */ |
| 77 | ThreadItemSet.prototype.updateIdAndNameMaps = function ( item ) { |
| 78 | if ( !this.threadItemsByName[ item.name ] ) { |
| 79 | this.threadItemsByName[ item.name ] = []; |
| 80 | } |
| 81 | this.threadItemsByName[ item.name ].push( item ); |
| 82 | |
| 83 | this.threadItemsById[ item.id ] = item; |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Get all discussion comments (and headings) within a DOM subtree. |
| 88 | * |
| 89 | * This returns a flat list, use #getThreads to get a tree structure starting at section headings. |
| 90 | * |
| 91 | * For example, for a MediaWiki discussion like this (we're dealing with HTML DOM here, the wikitext |
| 92 | * syntax is just for illustration): |
| 93 | * |
| 94 | * == A == |
| 95 | * B. ~~~~ |
| 96 | * : C. |
| 97 | * : C. ~~~~ |
| 98 | * :: D. ~~~~ |
| 99 | * ::: E. ~~~~ |
| 100 | * ::: F. ~~~~ |
| 101 | * : G. ~~~~ |
| 102 | * H. ~~~~ |
| 103 | * : I. ~~~~ |
| 104 | * |
| 105 | * This function would return a structure like: |
| 106 | * |
| 107 | * [ |
| 108 | * HeadingItem( { level: 0, range: (h2: A) } ), |
| 109 | * CommentItem( { level: 1, range: (p: B) } ), |
| 110 | * CommentItem( { level: 2, range: (li: C, li: C) } ), |
| 111 | * CommentItem( { level: 3, range: (li: D) } ), |
| 112 | * CommentItem( { level: 4, range: (li: E) } ), |
| 113 | * CommentItem( { level: 4, range: (li: F) } ), |
| 114 | * CommentItem( { level: 2, range: (li: G) } ), |
| 115 | * CommentItem( { level: 1, range: (p: H) } ), |
| 116 | * CommentItem( { level: 2, range: (li: I) } ) |
| 117 | * ] |
| 118 | * |
| 119 | * @return {ThreadItem[]} Thread items |
| 120 | */ |
| 121 | ThreadItemSet.prototype.getThreadItems = function () { |
| 122 | return this.threadItems; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * Same as getFlatThreadItems, but only returns the CommentItems |
| 127 | * |
| 128 | * @return {CommentItem[]} Comment items |
| 129 | */ |
| 130 | ThreadItemSet.prototype.getCommentItems = function () { |
| 131 | return this.commentItems; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * Find ThreadItems by their name |
| 136 | * |
| 137 | * This will usually return a single-element array, but it may return multiple comments if they're |
| 138 | * indistinguishable by name. In that case, use their IDs to disambiguate. |
| 139 | * |
| 140 | * @param {string} name Name |
| 141 | * @return {ThreadItem[]} Thread items, empty array if not found |
| 142 | */ |
| 143 | ThreadItemSet.prototype.findCommentsByName = function ( name ) { |
| 144 | return this.threadItemsByName[ name ] || []; |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * Find a ThreadItem by its ID |
| 149 | * |
| 150 | * @param {string} id ID |
| 151 | * @return {ThreadItem|null} Thread item, null if not found |
| 152 | */ |
| 153 | ThreadItemSet.prototype.findCommentById = function ( id ) { |
| 154 | return this.threadItemsById[ id ] || null; |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * Group discussion comments into threads and associate replies to original messages. |
| 159 | * |
| 160 | * Each thread must begin with a heading. Original messages in the thread are treated as replies to |
| 161 | * its heading. Other replies are associated based on the order and indentation level. |
| 162 | * |
| 163 | * Note that the objects in `comments` are extended in-place with the additional data. |
| 164 | * |
| 165 | * For example, for a MediaWiki discussion like this (we're dealing with HTML DOM here, the wikitext |
| 166 | * syntax is just for illustration): |
| 167 | * |
| 168 | * == A == |
| 169 | * B. ~~~~ |
| 170 | * : C. |
| 171 | * : C. ~~~~ |
| 172 | * :: D. ~~~~ |
| 173 | * ::: E. ~~~~ |
| 174 | * ::: F. ~~~~ |
| 175 | * : G. ~~~~ |
| 176 | * H. ~~~~ |
| 177 | * : I. ~~~~ |
| 178 | * |
| 179 | * This function would return a structure like: |
| 180 | * |
| 181 | * [ |
| 182 | * HeadingItem( { level: 0, range: (h2: A), replies: [ |
| 183 | * CommentItem( { level: 1, range: (p: B), replies: [ |
| 184 | * CommentItem( { level: 2, range: (li: C, li: C), replies: [ |
| 185 | * CommentItem( { level: 3, range: (li: D), replies: [ |
| 186 | * CommentItem( { level: 4, range: (li: E), replies: [] } ), |
| 187 | * CommentItem( { level: 4, range: (li: F), replies: [] } ), |
| 188 | * ] } ), |
| 189 | * ] } ), |
| 190 | * CommentItem( { level: 2, range: (li: G), replies: [] } ), |
| 191 | * ] } ), |
| 192 | * CommentItem( { level: 1, range: (p: H), replies: [ |
| 193 | * CommentItem( { level: 2, range: (li: I), replies: [] } ), |
| 194 | * ] } ), |
| 195 | * ] } ) |
| 196 | * ] |
| 197 | * |
| 198 | * @return {HeadingItem[]} Tree structure of comments, top-level items are the headings. |
| 199 | */ |
| 200 | ThreadItemSet.prototype.getThreads = function () { |
| 201 | return this.threads; |
| 202 | }; |
| 203 | |
| 204 | module.exports = ThreadItemSet; |