Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/elements/ve.ui.MWExpandableContentElement.js

raw symbols SCIP
1 /**
2 * Container for textual elements, which should be collapsed to one line by default.
3 *
4 * A "more / less" button is used to toggle additional lines.
5 *
6 * @class
7 * @extends OO.ui.Element
8 * @mixes OO.EventEmitter
9 *
10 * @constructor
11 * @param {Object} config
12 * @param {jQuery} config.$content
13 */
14 ve.ui.MWExpandableContentElement = function VeUiMWExpandableContentElement( config ) {
15 // Parent constructor
16 ve.ui.MWExpandableContentElement.super.call( this, config );
17
18 // Mixin constructors
19 OO.EventEmitter.call( this );
20
21 this.$content = config.$content;
22
23 this.collapsed = true;
24 this.toggle( false );
25 };
26
27 /* Inheritance */
28
29 OO.inheritClass( ve.ui.MWExpandableContentElement, OO.ui.Element );
30
31 OO.mixinClass( ve.ui.MWExpandableContentElement, OO.EventEmitter );
32
33 /* Methods */
34
35 /**
36 * @private
37 * @return {number}
38 */
39 ve.ui.MWExpandableContentElement.prototype.getLineHeight = function () {
40 return parseInt( this.$content.css( 'line-height' ) );
41 };
42
43 /**
44 * @private
45 * @return {number}
46 */
47 ve.ui.MWExpandableContentElement.prototype.calculateCurrentTextHeight = function () {
48 const currentHeight = this.$content.height(),
49 expandedHeight = this.$content.css( 'height', 'auto' ).height();
50 if ( expandedHeight !== currentHeight ) {
51 this.$content.css( 'height', currentHeight );
52 }
53 return expandedHeight;
54 };
55
56 /**
57 * @private
58 */
59 ve.ui.MWExpandableContentElement.prototype.makeCollapsible = function () {
60 this.button = new OO.ui.ButtonWidget( {
61 framed: false,
62 flags: [ 'progressive' ],
63 label: ve.msg( 'visualeditor-expandable-more' ),
64 classes: [ 've-ui-expandableContent-toggle' ],
65 invisibleLabel: ve.ui.MWTransclusionDialog.static.isSmallScreen(),
66 icon: 'expand'
67 } ).on( 'click', this.onButtonClick.bind( this ) );
68
69 this.$content.on( 'click', this.onDescriptionClick.bind( this ) )
70 .addClass( 've-ui-expandableContent-collapsible' );
71
72 this.$container = $( '<div>' )
73 .addClass( 've-ui-expandableContent-container' )
74 .append(
75 $( '<div>' ).addClass( 've-ui-expandableContent-fade' ),
76 this.button.$element
77 )
78 .appendTo( this.$element );
79 };
80
81 /**
82 * @private
83 */
84 ve.ui.MWExpandableContentElement.prototype.recalculateVisuals = function () {
85 const height = this.calculateCurrentTextHeight() + this.button.$element.height(),
86 collapsedHeight = this.getLineHeight(),
87 label = this.collapsed ? 'visualeditor-expandable-more' : 'visualeditor-expandable-less';
88
89 this.$content.css( 'height', this.collapsed ? collapsedHeight : height );
90 this.$container.removeClass( 'oo-ui-element-hidden' )
91 .height( collapsedHeight );
92 // The following messages are used here:
93 // * visualeditor-expandable-more
94 // * visualeditor-expandable-less
95 this.button.setLabel( ve.msg( label ) )
96 .setInvisibleLabel( ve.ui.MWTransclusionDialog.static.isSmallScreen() )
97 .setIcon( this.collapsed ? 'expand' : 'collapse' );
98 };
99
100 /**
101 * @private
102 */
103 ve.ui.MWExpandableContentElement.prototype.reset = function () {
104 this.$content.css( 'height', 'auto' );
105 this.$container.addClass( 'oo-ui-element-hidden' );
106 this.button.setInvisibleLabel( false );
107 };
108
109 /**
110 * @private
111 */
112 ve.ui.MWExpandableContentElement.prototype.onButtonClick = function () {
113 this.collapsed = !this.collapsed;
114 this.recalculateVisuals();
115 };
116
117 /**
118 * @private
119 */
120 ve.ui.MWExpandableContentElement.prototype.onDescriptionClick = function () {
121 if ( this.button.invisibleLabel ) {
122 // Don't toggle the description if the user is trying to select the text.
123 if ( window.getSelection().toString() === '' ) {
124 this.onButtonClick();
125 }
126 }
127 };
128
129 ve.ui.MWExpandableContentElement.prototype.updateSize = function () {
130 this.toggle( true );
131
132 if ( Math.floor( this.calculateCurrentTextHeight() / this.getLineHeight() ) > 3 ) {
133 if ( !this.$container ) {
134 this.makeCollapsible();
135 }
136 this.recalculateVisuals();
137 } else {
138 if ( this.$container ) {
139 this.reset();
140 }
141 }
142 };