| 1 | const ThreadItem = require( './ThreadItem.js' ); |
| 2 | // Placeholder headings must have a level higher than real headings (1-6) |
| 3 | const PLACEHOLDER_HEADING_LEVEL = 99; |
| 4 | |
| 5 | /** |
| 6 | * A heading item |
| 7 | * |
| 8 | * @class HeadingItem |
| 9 | * @extends ThreadItem |
| 10 | * @constructor |
| 11 | * @param {Object} range |
| 12 | * @param {number|null} headingLevel Heading level (1-6). Use null for a placeholder heading. |
| 13 | */ |
| 14 | function HeadingItem( range, headingLevel ) { |
| 15 | // Parent constructor |
| 16 | HeadingItem.super.call( this, 'heading', 0, range ); |
| 17 | |
| 18 | this.placeholderHeading = headingLevel === null; |
| 19 | this.headingLevel = this.placeholderHeading ? PLACEHOLDER_HEADING_LEVEL : headingLevel; |
| 20 | } |
| 21 | |
| 22 | OO.inheritClass( HeadingItem, ThreadItem ); |
| 23 | |
| 24 | HeadingItem.prototype.getLinkableTitle = function () { |
| 25 | let title = ''; |
| 26 | // If this comment is in 0th section, there's no section title for the edit summary |
| 27 | if ( !this.placeholderHeading ) { |
| 28 | const headline = this.range.startContainer; |
| 29 | const id = headline.getAttribute( 'id' ); |
| 30 | if ( id ) { |
| 31 | // Replace underscores with spaces to undo Sanitizer::escapeIdInternal(). |
| 32 | // This assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ], |
| 33 | // otherwise the escaped IDs are super garbled and can't be unescaped reliably. |
| 34 | title = id.replace( /_/g, ' ' ); |
| 35 | } |
| 36 | // else: Not a real section, probably just HTML markup in wikitext |
| 37 | } |
| 38 | return title; |
| 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * Check whether this heading can be used for topic subscriptions. |
| 43 | * |
| 44 | * @return {boolean} |
| 45 | */ |
| 46 | HeadingItem.prototype.isSubscribable = function () { |
| 47 | return ( |
| 48 | // Placeholder headings have nothing to attach the button to. |
| 49 | !this.placeholderHeading && |
| 50 | // We only allow subscribing to level 2 headings, because the user interface for sub-headings |
| 51 | // would be difficult to present. |
| 52 | this.headingLevel === 2 && |
| 53 | // Check if the name corresponds to a section that contain no comments (only sub-sections). |
| 54 | // They can't be distinguished from each other, so disallow subscribing. |
| 55 | this.name !== 'h-' |
| 56 | ); |
| 57 | }; |
| 58 | |
| 59 | module.exports = HeadingItem; |