| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
ve.ui.MWExpandableContentElement = function VeUiMWExpandableContentElement( config ) { |
| 15 |
|
| 16 |
ve.ui.MWExpandableContentElement.super.call( this, config ); |
| 17 |
|
| 18 |
|
| 19 |
OO.EventEmitter.call( this ); |
| 20 |
|
| 21 |
this.$content = config.$content; |
| 22 |
|
| 23 |
this.collapsed = true; |
| 24 |
this.toggle( false ); |
| 25 |
}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
OO.inheritClass( ve.ui.MWExpandableContentElement, OO.ui.Element ); |
| 30 |
|
| 31 |
OO.mixinClass( ve.ui.MWExpandableContentElement, OO.EventEmitter ); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ve.ui.MWExpandableContentElement.prototype.getLineHeight = function () { |
| 40 |
return parseInt( this.$content.css( 'line-height' ) ); |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
this.button.setLabel( ve.msg( label ) ) |
| 96 |
.setInvisibleLabel( ve.ui.MWTransclusionDialog.static.isSmallScreen() ) |
| 97 |
.setIcon( this.collapsed ? 'expand' : 'collapse' ); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 111 |
|
| 112 |
ve.ui.MWExpandableContentElement.prototype.onButtonClick = function () { |
| 113 |
this.collapsed = !this.collapsed; |
| 114 |
this.recalculateVisuals(); |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
ve.ui.MWExpandableContentElement.prototype.onDescriptionClick = function () { |
| 121 |
if ( this.button.invisibleLabel ) { |
| 122 |
|
| 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 |
}; |