| 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 |
ve.ui.MWMediaInfoFieldWidget = function VeUiMWMediaInfoFieldWidget( content, config ) { |
| 28 |
|
| 29 |
config = config || {}; |
| 30 |
|
| 31 |
|
| 32 |
ve.ui.MWMediaInfoFieldWidget.super.call( this, config ); |
| 33 |
|
| 34 |
|
| 35 |
OO.ui.mixin.IconElement.call( this, config ); |
| 36 |
OO.ui.mixin.LabelElement.call( this, ve.extendObject( { $label: $( '<div>' ) }, config ) ); |
| 37 |
|
| 38 |
this.$text = $( '<div>' ) |
| 39 |
.addClass( 've-ui-mwMediaInfoFieldWidget-text' ); |
| 40 |
this.type = config.type || 'attribute'; |
| 41 |
|
| 42 |
|
| 43 |
if ( typeof content === 'string' ) { |
| 44 |
let datetime; |
| 45 |
if ( config.isDate && ( datetime = moment( content ) ).isValid() ) { |
| 46 |
content = datetime.fromNow(); |
| 47 |
} |
| 48 |
|
| 49 |
if ( config.labelMsg ) { |
| 50 |
|
| 51 |
|
| 52 |
content = ve.msg( config.labelMsg, content ); |
| 53 |
} |
| 54 |
|
| 55 |
if ( config.href ) { |
| 56 |
|
| 57 |
|
| 58 |
content = $( '<a>' ) |
| 59 |
.attr( 'href', |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
!/^(https?:)?\/\//.test( config.href ) ? |
| 64 |
'//' + config.href : |
| 65 |
config.href |
| 66 |
) |
| 67 |
.text( content ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if ( typeof content === 'string' ) { |
| 72 |
this.$text.text( content ); |
| 73 |
} else if ( content instanceof OO.ui.HtmlSnippet ) { |
| 74 |
|
| 75 |
this.$text.html( content.toString() ); |
| 76 |
} else if ( content instanceof $ ) { |
| 77 |
|
| 78 |
this.$text.append( content ); |
| 79 |
} else { |
| 80 |
throw new Error( 'Unexpected metadata field content' ); |
| 81 |
} |
| 82 |
|
| 83 |
this.$element |
| 84 |
.append( this.$icon, this.$label ) |
| 85 |
.addClass( 've-ui-mwMediaInfoFieldWidget' ) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
.addClass( 've-ui-mwMediaInfoFieldWidget-' + this.type ); |
| 90 |
this.$icon.addClass( 've-ui-mwMediaInfoFieldWidget-icon' ); |
| 91 |
|
| 92 |
if ( this.type === 'description' ) { |
| 93 |
|
| 94 |
this.readMoreButton = new OO.ui.ButtonWidget( { |
| 95 |
framed: false, |
| 96 |
icon: 'expand', |
| 97 |
label: ve.msg( 'visualeditor-dialog-media-info-readmore' ), |
| 98 |
classes: [ 've-ui-mwMediaInfoFieldWidget-readmore' ] |
| 99 |
} ); |
| 100 |
this.readMoreButton.toggle( false ); |
| 101 |
this.readMoreButton.connect( this, { click: 'onReadMoreClick' } ); |
| 102 |
|
| 103 |
this.$text |
| 104 |
.css( 'maxHeight', config.descriptionHeight || '4em' ); |
| 105 |
|
| 106 |
this.$element |
| 107 |
.append( this.readMoreButton.$element ); |
| 108 |
} |
| 109 |
|
| 110 |
this.setLabel( this.$text ); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
OO.inheritClass( ve.ui.MWMediaInfoFieldWidget, OO.ui.Widget ); |
| 116 |
OO.mixinClass( ve.ui.MWMediaInfoFieldWidget, OO.ui.mixin.IconElement ); |
| 117 |
OO.mixinClass( ve.ui.MWMediaInfoFieldWidget, OO.ui.mixin.LabelElement ); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
ve.ui.MWMediaInfoFieldWidget.static.tagName = 'div'; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ve.ui.MWMediaInfoFieldWidget.static.threshold = 24; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ve.ui.MWMediaInfoFieldWidget.prototype.initialize = function () { |
| 140 |
if ( this.getType() === 'description' ) { |
| 141 |
const actualHeight = this.$text.prop( 'scrollHeight' ); |
| 142 |
const containerHeight = this.$text.outerHeight( true ); |
| 143 |
|
| 144 |
if ( actualHeight < containerHeight + this.constructor.static.threshold ) { |
| 145 |
|
| 146 |
this.$text |
| 147 |
.css( 'maxHeight', '' ); |
| 148 |
} else { |
| 149 |
|
| 150 |
this.readMoreButton.toggle( containerHeight < actualHeight ); |
| 151 |
} |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
ve.ui.MWMediaInfoFieldWidget.prototype.onReadMoreClick = function () { |
| 159 |
this.readMoreButton.toggle( false ); |
| 160 |
this.$text.css( 'maxHeight', this.$text.prop( 'scrollHeight' ) ); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
ve.ui.MWMediaInfoFieldWidget.prototype.getType = function () { |
| 169 |
return this.type; |
| 170 |
}; |