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;
|
|
|
|
|
|
|
|
this.collapsed = false;
|
|
|
|
this.toggle( false );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWExpandableContentElement, OO.ui.Element );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ui.MWExpandableContentElement, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.ui.MWExpandableContentElement.prototype.getLineHeight = function () {
|
|
|
|
return parseInt( this.$content.css( 'line-height' ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.MWExpandableContentElement.prototype.makeCollapsible = function () {
|
2022-05-16 10:43:10 +00:00
|
|
|
var collapsedHeight = this.getLineHeight();
|
2022-05-12 10:43:50 +00:00
|
|
|
|
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 ) )
|
|
|
|
.addClass( 've-ui-expandableContent-collapsible' )
|
|
|
|
.height( collapsedHeight );
|
|
|
|
|
2021-04-22 16:59:30 +00:00
|
|
|
$( '<div>' )
|
2021-06-26 13:27:52 +00:00
|
|
|
.addClass( 've-ui-expandableContent-container' )
|
|
|
|
.append(
|
|
|
|
$( '<div>' )
|
|
|
|
.addClass( 've-ui-expandableContent-fade' )
|
|
|
|
)
|
2022-05-16 10:43:10 +00:00
|
|
|
.append( this.button.$element )
|
2021-04-22 16:59:30 +00:00
|
|
|
.height( collapsedHeight )
|
|
|
|
.appendTo( this.$element );
|
|
|
|
};
|
|
|
|
|
2022-05-16 10:43:10 +00:00
|
|
|
ve.ui.MWExpandableContentElement.prototype.onButtonClick = function () {
|
|
|
|
if ( this.collapsed ) {
|
|
|
|
this.button.setLabel( ve.msg( 'visualeditor-expandable-more' ) );
|
|
|
|
this.$content.css( { height: this.getLineHeight() } );
|
|
|
|
this.button.setIcon( 'expand' );
|
|
|
|
} else {
|
|
|
|
this.button.setLabel( ve.msg( 'visualeditor-expandable-less' ) );
|
|
|
|
this.$content.css( { height: this.$content.prop( 'scrollHeight' ) + this.getLineHeight() } );
|
|
|
|
this.button.setIcon( 'collapse' );
|
|
|
|
}
|
|
|
|
this.collapsed = !this.collapsed;
|
|
|
|
};
|
|
|
|
|
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-05-12 10:43:50 +00:00
|
|
|
if ( this.button ) {
|
|
|
|
this.button.setInvisibleLabel( ve.ui.MWTransclusionDialog.static.isSmallScreen() );
|
|
|
|
} else if ( this.$content.outerHeight() / this.getLineHeight() >= 3 ) {
|
2021-04-22 16:59:30 +00:00
|
|
|
this.makeCollapsible();
|
|
|
|
}
|
|
|
|
};
|