mediawiki-extensions-Visual.../modules/ve-mw/ui/elements/ve.ui.MWExpandableContentElement.js
Thiemo Kreuz 217885013a Refine height calculation of template parameter docs
Instead of using a potentially weird fractional value like 3.2 lets
use the floor(ed) value, e.g. 3. Reasoning: These fractions are not
from text lines but optional margins between some of the lines. These
margins should not be counted.

Also increase the cut point from >=3 to >3. This is much closer to
the original specification that talked about "2 lines plus x
characters". See T273426. These "x characters" are the 3rd line.

Bug: T310775
Change-Id: Ib5d1642d7967593dd5f13e556c6244fc44677af4
2022-07-06 10:40:51 +00:00

146 lines
3.7 KiB
JavaScript

/**
* Container for textual elements, which should be collapsed to one line by default.
*
* A "more / less" button is used to toggle additional lines.
*
* @class
* @extends OO.ui.Element
* @mixins OO.EventEmitter
*
* @constructor
* @param {Object} config
* @cfg {jQuery} $content
*/
ve.ui.MWExpandableContentElement = function VeUiMWExpandableContentElement( config ) {
// Parent constructor
ve.ui.MWExpandableContentElement.super.call( this, config );
// Mixin constructors
OO.EventEmitter.call( this );
this.$content = config.$content;
this.collapsed = true;
this.toggle( false );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWExpandableContentElement, OO.ui.Element );
OO.mixinClass( ve.ui.MWExpandableContentElement, OO.EventEmitter );
/* Methods */
/**
* @private
* @return {number}
*/
ve.ui.MWExpandableContentElement.prototype.getLineHeight = function () {
return parseInt( this.$content.css( 'line-height' ) );
};
/**
* @private
* @return {number}
*/
ve.ui.MWExpandableContentElement.prototype.getTextHeight = function () {
var currentHeight = this.$content.height(),
expandedHeight = this.$content.css( 'height', 'auto' ).height();
if ( expandedHeight !== currentHeight ) {
this.$content.css( { height: currentHeight } );
}
return expandedHeight;
};
/**
* @private
*/
ve.ui.MWExpandableContentElement.prototype.makeCollapsible = function () {
this.button = new OO.ui.ButtonWidget( {
framed: false,
flags: [ 'progressive' ],
label: ve.msg( 'visualeditor-expandable-more' ),
classes: [ 've-ui-expandableContent-toggle' ],
invisibleLabel: ve.ui.MWTransclusionDialog.static.isSmallScreen(),
icon: 'expand'
} ).on( 'click', this.onButtonClick.bind( this ) );
this.$content.on( 'click', this.onDescriptionClick.bind( this ) )
.addClass( 've-ui-expandableContent-collapsible' );
this.$container = $( '<div>' )
.addClass( 've-ui-expandableContent-container' )
.append(
$( '<div>' )
.addClass( 've-ui-expandableContent-fade' )
)
.append( this.button.$element )
.appendTo( this.$element );
};
/**
* @private
* @param {boolean} collapse
*/
ve.ui.MWExpandableContentElement.prototype.collapse = function ( collapse ) {
var collapsedHeight = this.getLineHeight();
if ( collapse ) {
this.button.setLabel( ve.msg( 'visualeditor-expandable-more' ) );
this.$content.css( { height: collapsedHeight } );
this.button.setIcon( 'expand' );
} else {
this.button.setLabel( ve.msg( 'visualeditor-expandable-less' ) );
this.$content.css( 'height', this.getTextHeight() + this.button.$element.height() );
this.button.setIcon( 'collapse' );
}
this.$container.removeClass( 'oo-ui-element-hidden' );
this.$container.height( collapsedHeight );
this.button.setInvisibleLabel( ve.ui.MWTransclusionDialog.static.isSmallScreen() );
};
/**
* @private
*/
ve.ui.MWExpandableContentElement.prototype.reset = function () {
this.$content.css( 'height', 'auto' );
this.$container.addClass( 'oo-ui-element-hidden' );
this.button.setInvisibleLabel( false );
};
/**
* @private
*/
ve.ui.MWExpandableContentElement.prototype.onButtonClick = function () {
this.collapse( !this.collapsed );
this.collapsed = !this.collapsed;
};
/**
* @private
*/
ve.ui.MWExpandableContentElement.prototype.onDescriptionClick = function () {
if ( this.button.invisibleLabel ) {
// Don't toggle the description if the user is trying to select the text.
if ( window.getSelection().toString() === '' ) {
this.onButtonClick();
}
}
};
ve.ui.MWExpandableContentElement.prototype.updateSize = function () {
this.toggle( true );
if ( Math.floor( this.getTextHeight() / this.getLineHeight() ) > 3 ) {
if ( !this.$container ) {
this.makeCollapsible();
}
this.collapse( this.collapsed );
} else {
if ( this.$container ) {
this.reset();
}
}
};