Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWMediaInfoFieldWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWMediaDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /* global moment */
9
10 /**
11 * MWMediaInfoFieldWidget widget for displaying media information from the API.
12 *
13 * @class
14 * @extends OO.ui.Widget
15 * @mixes OO.ui.mixin.IconElement
16 * @mixes OO.ui.mixin.TitledElement
17 *
18 * @constructor
19 * @param {jQuery|string|OO.ui.HtmlSnippet} content API response data from which to build the display
20 * @param {Object} [config] Configuration options
21 * @param {string} [config.href] A url encapsulating the field text. If a label is attached it will include the label.
22 * @param {string} [config.labelMsg] A ve.msg() label string for the field.
23 * @param {boolean} [config.isDate=false] Field text is a date that will be converted to 'fromNow' string.
24 * @param {string} [config.type='attribute'] Field type, either 'description' or 'attribute'
25 * @param {string} [config.descriptionHeight='4em'] Height limit for description fields
26 */
27 ve.ui.MWMediaInfoFieldWidget = function VeUiMWMediaInfoFieldWidget( content, config ) {
28 // Configuration initialization
29 config = config || {};
30
31 // Parent constructor
32 ve.ui.MWMediaInfoFieldWidget.super.call( this, config );
33
34 // Mixin constructors
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 // Initialization
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 // Messages defined in ve.ui.MWMediaDialog#buildMediaInfoPanel
51 // eslint-disable-next-line mediawiki/msg-doc
52 content = ve.msg( config.labelMsg, content );
53 }
54
55 if ( config.href ) {
56 // This variable may contain either jQuery objects or strings
57 // eslint-disable-next-line no-jquery/variable-pattern
58 content = $( '<a>' )
59 .attr( 'href',
60 // For the cases where we get urls that are "local"
61 // without http(s) prefix, we will add that prefix
62 // ourselves
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 // eslint-disable-next-line no-jquery/no-html
75 this.$text.html( content.toString() );
76 } else if ( content instanceof $ ) {
77 // eslint-disable-next-line no-jquery/no-append-html
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 // The following classes are used here:
87 // * ve-ui-mwMediaInfoFieldWidget-description
88 // * ve-ui-mwMediaInfoFieldWidget-attribute
89 .addClass( 've-ui-mwMediaInfoFieldWidget-' + this.type );
90 this.$icon.addClass( 've-ui-mwMediaInfoFieldWidget-icon' );
91
92 if ( this.type === 'description' ) {
93 // Limit height
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 /* Setup */
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 /* Static Properties */
120
121 ve.ui.MWMediaInfoFieldWidget.static.tagName = 'div';
122
123 /**
124 * Define a height threshold for the description fields.
125 * If the rendered field's height is under the defined limit
126 * (max-height + threshold) we should remove the max-height
127 * and display the field as-is.
128 * This prevents cases where "read more" appears but only
129 * exposes only a few pixels or a line extra.
130 *
131 * @property {number} Threshold in pixels
132 */
133 ve.ui.MWMediaInfoFieldWidget.static.threshold = 24;
134
135 /**
136 * Toggle the read more button according to whether it should be
137 * visible or not.
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 // The contained result is big enough to show. Remove the maximum height
146 this.$text
147 .css( 'maxHeight', '' );
148 } else {
149 // Only show the readMore button if it should be shown
150 this.readMoreButton.toggle( containerHeight < actualHeight );
151 }
152 }
153 };
154
155 /**
156 * Respond to read more button click event.
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 * Get field type; 'attribute' or 'description'
165 *
166 * @return {string} Field type
167 */
168 ve.ui.MWMediaInfoFieldWidget.prototype.getType = function () {
169 return this.type;
170 };