2021-04-22 16:59:30 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-09-11 08:05:31 +00:00
|
|
|
* @param {Object} config
|
|
|
|
* @cfg {jQuery} $content
|
2021-04-22 16:59:30 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
this.collapsed = true;
|
2021-04-22 16:59:30 +00:00
|
|
|
this.toggle( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWExpandableContentElement, OO.ui.Element );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ui.MWExpandableContentElement, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @return {number}
|
|
|
|
*/
|
2021-04-22 16:59:30 +00:00
|
|
|
ve.ui.MWExpandableContentElement.prototype.getLineHeight = function () {
|
|
|
|
return parseInt( this.$content.css( 'line-height' ) );
|
|
|
|
};
|
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @return {number}
|
|
|
|
*/
|
2022-08-04 06:54:54 +00:00
|
|
|
ve.ui.MWExpandableContentElement.prototype.calculateCurrentTextHeight = function () {
|
2022-06-13 12:55:34 +00:00
|
|
|
var currentHeight = this.$content.height(),
|
|
|
|
expandedHeight = this.$content.css( 'height', 'auto' ).height();
|
2022-06-14 13:59:04 +00:00
|
|
|
if ( expandedHeight !== currentHeight ) {
|
2022-08-04 06:54:54 +00:00
|
|
|
this.$content.css( 'height', currentHeight );
|
2022-06-14 13:59:04 +00:00
|
|
|
}
|
2022-06-13 12:55:34 +00:00
|
|
|
return expandedHeight;
|
2022-05-27 13:01:47 +00:00
|
|
|
};
|
2022-05-12 10:43:50 +00:00
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.ui.MWExpandableContentElement.prototype.makeCollapsible = function () {
|
2022-05-16 10:43:10 +00:00
|
|
|
this.button = new OO.ui.ButtonWidget( {
|
|
|
|
framed: false,
|
|
|
|
flags: [ 'progressive' ],
|
|
|
|
label: ve.msg( 'visualeditor-expandable-more' ),
|
|
|
|
classes: [ 've-ui-expandableContent-toggle' ],
|
2022-05-12 10:43:50 +00:00
|
|
|
invisibleLabel: ve.ui.MWTransclusionDialog.static.isSmallScreen(),
|
2022-05-16 10:43:10 +00:00
|
|
|
icon: 'expand'
|
|
|
|
} ).on( 'click', this.onButtonClick.bind( this ) );
|
2021-04-22 16:59:30 +00:00
|
|
|
|
2022-05-12 10:43:50 +00:00
|
|
|
this.$content.on( 'click', this.onDescriptionClick.bind( this ) )
|
2022-05-27 13:01:47 +00:00
|
|
|
.addClass( 've-ui-expandableContent-collapsible' );
|
2022-05-12 10:43:50 +00:00
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
this.$container = $( '<div>' )
|
2021-06-26 13:27:52 +00:00
|
|
|
.addClass( 've-ui-expandableContent-container' )
|
|
|
|
.append(
|
2022-08-04 06:54:54 +00:00
|
|
|
$( '<div>' ).addClass( 've-ui-expandableContent-fade' ),
|
|
|
|
this.button.$element
|
2021-06-26 13:27:52 +00:00
|
|
|
)
|
2021-04-22 16:59:30 +00:00
|
|
|
.appendTo( this.$element );
|
|
|
|
};
|
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2022-08-04 06:54:54 +00:00
|
|
|
ve.ui.MWExpandableContentElement.prototype.recalculateVisuals = function () {
|
|
|
|
var height = this.calculateCurrentTextHeight() + this.button.$element.height(),
|
|
|
|
collapsedHeight = this.getLineHeight(),
|
|
|
|
label = this.collapsed ? 'visualeditor-expandable-more' : 'visualeditor-expandable-less';
|
|
|
|
|
|
|
|
this.$content.css( 'height', this.collapsed ? collapsedHeight : height );
|
|
|
|
this.$container.removeClass( 'oo-ui-element-hidden' )
|
|
|
|
.height( collapsedHeight );
|
|
|
|
// The following messages are used here:
|
|
|
|
// * visualeditor-expandable-more
|
|
|
|
// * visualeditor-expandable-less
|
|
|
|
this.button.setLabel( ve.msg( label ) )
|
|
|
|
.setInvisibleLabel( ve.ui.MWTransclusionDialog.static.isSmallScreen() )
|
|
|
|
.setIcon( this.collapsed ? 'expand' : 'collapse' );
|
2022-05-27 13:01:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.ui.MWExpandableContentElement.prototype.reset = function () {
|
2022-06-14 13:59:04 +00:00
|
|
|
this.$content.css( 'height', 'auto' );
|
2022-05-27 13:01:47 +00:00
|
|
|
this.$container.addClass( 'oo-ui-element-hidden' );
|
|
|
|
this.button.setInvisibleLabel( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.ui.MWExpandableContentElement.prototype.onButtonClick = function () {
|
2022-05-16 10:43:10 +00:00
|
|
|
this.collapsed = !this.collapsed;
|
2022-08-04 06:54:54 +00:00
|
|
|
this.recalculateVisuals();
|
2022-05-16 10:43:10 +00:00
|
|
|
};
|
|
|
|
|
2022-05-27 13:01:47 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2022-05-12 10:43:50 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-22 16:59:30 +00:00
|
|
|
ve.ui.MWExpandableContentElement.prototype.updateSize = function () {
|
|
|
|
this.toggle( true );
|
|
|
|
|
2022-08-04 06:54:54 +00:00
|
|
|
if ( Math.floor( this.calculateCurrentTextHeight() / this.getLineHeight() ) > 3 ) {
|
2022-05-27 13:01:47 +00:00
|
|
|
if ( !this.$container ) {
|
|
|
|
this.makeCollapsible();
|
|
|
|
}
|
2022-08-04 06:54:54 +00:00
|
|
|
this.recalculateVisuals();
|
2022-05-27 13:01:47 +00:00
|
|
|
} else {
|
|
|
|
if ( this.$container ) {
|
|
|
|
this.reset();
|
|
|
|
}
|
2021-04-22 16:59:30 +00:00
|
|
|
}
|
|
|
|
};
|