File: ve.ui.MWExpandableContentElement.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (142 lines) | stat: -rw-r--r-- 3,855 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * 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
 * @mixes OO.EventEmitter
 *
 * @constructor
 * @param {Object} config
 * @param {jQuery} config.$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.calculateCurrentTextHeight = function () {
	const 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' ),
			this.button.$element
		)
		.appendTo( this.$element );
};

/**
 * @private
 */
ve.ui.MWExpandableContentElement.prototype.recalculateVisuals = function () {
	const 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' );
};

/**
 * @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.collapsed = !this.collapsed;
	this.recalculateVisuals();
};

/**
 * @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.calculateCurrentTextHeight() / this.getLineHeight() ) > 3 ) {
		if ( !this.$container ) {
			this.makeCollapsible();
		}
		this.recalculateVisuals();
	} else {
		if ( this.$container ) {
			this.reset();
		}
	}
};