| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MultimediaViewer. |
| 3 | * |
| 4 | * MultimediaViewer is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * MultimediaViewer is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | const HtmlUtils = require( '../mmv.HtmlUtils.js' ); |
| 19 | const UiElement = require( './mmv.ui.js' ); |
| 20 | |
| 21 | /** |
| 22 | * Represents any text field that might need to be truncated to be readable. Text will be adjusted to |
| 23 | * fit into its container. |
| 24 | * |
| 25 | * More specifically, TruncatableTextField should be invoked with a fixed-height container as the first |
| 26 | * parameter and a flexible-width content (which gets its size from the text inside it) as the second |
| 27 | * one. The container gets overflow: hidden, and the content is placed inside it; if the content |
| 28 | * overflows the container, TruncatableTextField will cycle through a set of styles and apply to the |
| 29 | * container the first one that makes the content not overflow anymore. If none of the styles do that, |
| 30 | * the last one is applied anyway. |
| 31 | * |
| 32 | * The list of styles can be customized; by default, they set progressively smaller font size, and the |
| 33 | * last one adds an ellipsis to the end. (An ellipsis element is automatically appended to the end of |
| 34 | * the container to help with this, but it is hidden unless made visible by one of the styles.) |
| 35 | * |
| 36 | * grow() and shrink() can be used to show full text (by making the container flexible-height) and hiding |
| 37 | * them again; TruncatableTextField will not call them automatically (the caller class should e.g. set up |
| 38 | * a click handler on the ellipsis). |
| 39 | * |
| 40 | * repaint() should be called after layout changes to keep the truncation accurate. |
| 41 | */ |
| 42 | class TruncatableTextField extends UiElement { |
| 43 | /** |
| 44 | * @param {jQuery} $container The container for the element. |
| 45 | * @param {jQuery} $element The element where we should put the text. |
| 46 | * @param {Object} [options] |
| 47 | * @param {string[]} [options.styles] a list of styles to try if the text does not fit into the container. |
| 48 | * Will stop at the first which makes the text fit; the last one will be used even if it does not make |
| 49 | * the text fit. |
| 50 | */ |
| 51 | constructor( $container, $element, options ) { |
| 52 | super( $container ); |
| 53 | |
| 54 | /** @property {jQuery} $element The DOM element that holds text for this element. */ |
| 55 | this.$element = $element; |
| 56 | |
| 57 | /** @property {Object} options - */ |
| 58 | this.options = Object.assign( { |
| 59 | styles: [ 'mw-mmv-ttf-small', 'mw-mmv-ttf-smaller', 'mw-mmv-ttf-smallest' ] |
| 60 | }, options ); |
| 61 | |
| 62 | /** @property {boolean} expanded true if the text is long enough to be truncated but the full text is shown */ |
| 63 | this.expanded = false; |
| 64 | |
| 65 | /** @property {jQuery} ellipsis the element which marks that the text was truncated */ |
| 66 | this.$ellipsis = null; |
| 67 | |
| 68 | /** @property {string} normalTitle title attribute to show when the text is not truncated */ |
| 69 | this.normalTitle = null; |
| 70 | |
| 71 | /** @property {string} truncatedTitle title attribute to show when the text is not truncated */ |
| 72 | this.truncatedTitle = null; |
| 73 | |
| 74 | this.init(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Initializes the DOM. |
| 79 | * |
| 80 | * @private |
| 81 | */ |
| 82 | init() { |
| 83 | this.$ellipsis = $( '<span>' ) |
| 84 | .text( '…' ) |
| 85 | .hide() |
| 86 | .addClass( 'mw-mmv-ttf-ellipsis' ); |
| 87 | |
| 88 | this.$container |
| 89 | .addClass( 'mw-mmv-ttf-container empty' ) |
| 90 | .append( this.$element, this.$ellipsis ); |
| 91 | } |
| 92 | |
| 93 | attach() { |
| 94 | $( window ).on( 'resize.mmv-ttf', mw.util.debounce( this.repaint.bind( this ), 100 ) ); |
| 95 | } |
| 96 | |
| 97 | unattach() { |
| 98 | $( window ).off( 'resize.mmv-ttf' ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets the string for the element. |
| 103 | * |
| 104 | * @param {string} value Warning - unsafe HTML is allowed here. |
| 105 | */ |
| 106 | set( value ) { |
| 107 | this.$element.empty().append( HtmlUtils.htmlToTextWithTags( value ) ); |
| 108 | this.changeStyle(); |
| 109 | this.$container.toggleClass( 'empty', !value ); |
| 110 | this.$ellipsis.hide(); |
| 111 | this.shrink(); |
| 112 | } |
| 113 | |
| 114 | empty() { |
| 115 | this.$element.empty(); |
| 116 | // eslint-disable-next-line mediawiki/class-doc |
| 117 | this.$container |
| 118 | .removeClass( this.options.styles.concat( [ 'mw-mmv-ttf-untruncated', 'mw-mmv-ttf-truncated' ] ) ) |
| 119 | .addClass( 'empty' ); |
| 120 | this.$ellipsis.hide(); |
| 121 | this.setTitle( '', '' ); |
| 122 | this.expanded = false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Recalculate truncation after layout changes (such as resize) |
| 127 | */ |
| 128 | repaint() { |
| 129 | this.changeStyle(); |
| 130 | this.$ellipsis.hide(); |
| 131 | this.shrink(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Allows setting different titles for fully visible and for truncated text. |
| 136 | * |
| 137 | * @param {string} normal |
| 138 | * @param {string} truncated |
| 139 | */ |
| 140 | setTitle( normal, truncated ) { |
| 141 | this.normalTitle = normal; |
| 142 | this.truncatedTitle = truncated; |
| 143 | this.updateTitle(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Selects the right title to use (for full or for truncated version). The title can be set with setTitle(). |
| 148 | */ |
| 149 | updateTitle() { |
| 150 | const $elementsWithTitle = this.$element.add( this.$ellipsis ); |
| 151 | $elementsWithTitle.attr( 'original-title', this.isTruncated() ? this.truncatedTitle : this.normalTitle ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns true if the text is long enough that it needs to be truncated. |
| 156 | * |
| 157 | * @return {boolean} |
| 158 | */ |
| 159 | isTruncatable() { |
| 160 | // height calculation logic does not work for expanded state since the container expands |
| 161 | // to envelop the element, but we never go into expanded state for non-truncatable elements anyway |
| 162 | return this.$container.height() < this.$element.height() || this.expanded; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns true if the text is truncated at the moment. |
| 167 | * |
| 168 | * @return {boolean} |
| 169 | */ |
| 170 | isTruncated() { |
| 171 | return this.isTruncatable() && !this.expanded; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Makes the container fixed-width, clipping the text. |
| 176 | * This will only add a .mw-mmv-ttf-truncated class; it's the caller's responsibility to define the fixed |
| 177 | * height for that class. |
| 178 | */ |
| 179 | shrink() { |
| 180 | if ( this.isTruncatable() ) { |
| 181 | this.expanded = false; |
| 182 | this.$container.addClass( 'mw-mmv-ttf-truncated' ).removeClass( 'mw-mmv-ttf-untruncated' ); |
| 183 | this.$ellipsis.show(); |
| 184 | this.updateTitle(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Makes the container flexible-width, thereby restoring the full text. |
| 190 | */ |
| 191 | grow() { |
| 192 | if ( this.isTruncatable() ) { |
| 193 | this.expanded = true; |
| 194 | this.$container.removeClass( 'mw-mmv-ttf-truncated' ).addClass( 'mw-mmv-ttf-untruncated' ); |
| 195 | this.$ellipsis.hide(); |
| 196 | this.updateTitle(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Changes the element style if a certain length is reached. |
| 202 | */ |
| 203 | changeStyle() { |
| 204 | let newClass = 'mw-mmv-ttf-normal'; |
| 205 | |
| 206 | // eslint-disable-next-line mediawiki/class-doc |
| 207 | this.$container |
| 208 | .removeClass( this.options.styles.concat( [ 'mw-mmv-ttf-untruncated', 'mw-mmv-ttf-truncated' ] ) ) |
| 209 | .addClass( newClass ); |
| 210 | this.expanded = false; |
| 211 | |
| 212 | for ( const v of this.options.styles ) { |
| 213 | if ( !this.isTruncatable() ) { |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | const oldClass = newClass; |
| 218 | newClass = v; |
| 219 | // eslint-disable-next-line mediawiki/class-doc |
| 220 | this.$container.removeClass( oldClass ).addClass( newClass ); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | module.exports = TruncatableTextField; |