File: ve.ui.MWExpandableErrorElement.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 (131 lines) | stat: -rw-r--r-- 3,471 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
/*!
 * VisualEditor UserInterface MWExpandableErrorElement class.
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

/**
 * MediaWiki expandable error element.
 *
 * @class
 * @extends OO.ui.Element
 * @mixes OO.EventEmitter
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWExpandableErrorElement = function VeUiMWExpandableErrorElement( config ) {
	// Parent constructor
	ve.ui.MWExpandableErrorElement.super.call( this, config );

	// Mixin constructors
	OO.EventEmitter.call( this );

	// Interaction
	this.expanded = false;
	this.expandable = false;

	this.toggle( false );
	this.label = new OO.ui.LabelWidget( {
		classes: [ 've-ui-mwExpandableErrorElement-label' ]
	} );
	this.button = new OO.ui.ButtonWidget( {
		framed: false,
		classes: [ 've-ui-mwExpandableErrorElement-button' ],
		icon: 'expand'
	} ).toggle( false );

	this.$element.append(
		this.button.$element,
		this.label.$element
	).addClass( 've-ui-mwExpandableErrorElement' );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWExpandableErrorElement, OO.ui.Element );

OO.mixinClass( ve.ui.MWExpandableErrorElement, OO.EventEmitter );

/* Events */

/**
 * @event ve.ui.MWExpandableErrorElement#update
 */

/* Methods */

/**
 * Set the expandability of the error.
 *
 * @param {boolean} [expandable] Value to set the expandability to,
 * determine based on label size if omitted
 */
ve.ui.MWExpandableErrorElement.prototype.setExpandable = function ( expandable ) {
	if ( expandable !== undefined ) {
		this.expandable = expandable;
	} else {
		// Check if error fits when in not-expandable mode
		this.label.$element
			.addClass( 've-ui-mwExpandableErrorElement-label-not-expandable' );
		this.expandable = this.label.$element.prop( 'scrollWidth' ) >
			this.label.$element.innerWidth();
	}
	this.label.$element
		.toggleClass( 've-ui-mwExpandableErrorElement-label-not-expandable', !this.expandable );
};

/**
 * Show the error and set the label to contain the error text.
 *
 * @param {jQuery} [$element] Element containing the error
 */
ve.ui.MWExpandableErrorElement.prototype.show = function ( $element ) {
	this.label.setLabel( $element || null );
	this.toggle( true );

	this.setExpandable();

	if ( this.expandable ) {
		this.label.$element.addClass( 've-ui-mwExpandableErrorElement-label-collapsed' );
		this.button.toggle( true );
		this.button.connect( this, { click: 'toggleLabel' } );
	}

	this.emit( 'update' );
};

/**
 * Hide and collapse the error element, remove the label, and set expandable
 * to false.
 */
ve.ui.MWExpandableErrorElement.prototype.clear = function () {
	this.label.setLabel( null );
	this.toggle( false );

	this.button.toggle( false );
	this.button.disconnect( this );
	this.toggleLabel( false );

	this.emit( 'update' );
};

/**
 * Toggles the label between collapsed and expanded.
 *
 * @param {boolean} [expand] Expand if true, collapse if false, toggle if
 * omitted
 */
ve.ui.MWExpandableErrorElement.prototype.toggleLabel = function ( expand ) {
	// Set this.expanded to the new state
	this.expanded = expand === undefined ? !this.expanded : expand;

	// Update DOM based on the new state of this.expanded
	this.button.setIcon( this.expanded ? 'collapse' : 'expand' );
	this.label.$element
		.toggleClass( 've-ui-mwExpandableErrorElement-label-expanded', this.expanded )
		.toggleClass( 've-ui-mwExpandableErrorElement-label-collapsed', !this.expanded );

	this.emit( 'update' );
};