Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWExpandableErrorElement class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * MediaWiki expandable error element.
10 *
11 * @class
12 * @extends OO.ui.Element
13 * @mixes OO.EventEmitter
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.MWExpandableErrorElement = function VeUiMWExpandableErrorElement( config ) {
19 // Parent constructor
20 ve.ui.MWExpandableErrorElement.super.call( this, config );
21
22 // Mixin constructors
23 OO.EventEmitter.call( this );
24
25 // Interaction
26 this.expanded = false;
27 this.expandable = false;
28
29 this.toggle( false );
30 this.label = new OO.ui.LabelWidget( {
31 classes: [ 've-ui-mwExpandableErrorElement-label' ]
32 } );
33 this.button = new OO.ui.ButtonWidget( {
34 framed: false,
35 classes: [ 've-ui-mwExpandableErrorElement-button' ],
36 icon: 'expand'
37 } ).toggle( false );
38
39 this.$element.append(
40 this.button.$element,
41 this.label.$element
42 ).addClass( 've-ui-mwExpandableErrorElement' );
43 };
44
45 /* Inheritance */
46
47 OO.inheritClass( ve.ui.MWExpandableErrorElement, OO.ui.Element );
48
49 OO.mixinClass( ve.ui.MWExpandableErrorElement, OO.EventEmitter );
50
51 /* Events */
52
53 /**
54 * @event ve.ui.MWExpandableErrorElement#update
55 */
56
57 /* Methods */
58
59 /**
60 * Set the expandability of the error.
61 *
62 * @param {boolean} [expandable] Value to set the expandability to,
63 * determine based on label size if omitted
64 */
65 ve.ui.MWExpandableErrorElement.prototype.setExpandable = function ( expandable ) {
66 if ( expandable !== undefined ) {
67 this.expandable = expandable;
68 } else {
69 // Check if error fits when in not-expandable mode
70 this.label.$element
71 .addClass( 've-ui-mwExpandableErrorElement-label-not-expandable' );
72 this.expandable = this.label.$element.prop( 'scrollWidth' ) >
73 this.label.$element.innerWidth();
74 }
75 this.label.$element
76 .toggleClass( 've-ui-mwExpandableErrorElement-label-not-expandable', !this.expandable );
77 };
78
79 /**
80 * Show the error and set the label to contain the error text.
81 *
82 * @param {jQuery} [$element] Element containing the error
83 */
84 ve.ui.MWExpandableErrorElement.prototype.show = function ( $element ) {
85 this.label.setLabel( $element || null );
86 this.toggle( true );
87
88 this.setExpandable();
89
90 if ( this.expandable ) {
91 this.label.$element.addClass( 've-ui-mwExpandableErrorElement-label-collapsed' );
92 this.button.toggle( true );
93 this.button.connect( this, { click: 'toggleLabel' } );
94 }
95
96 this.emit( 'update' );
97 };
98
99 /**
100 * Hide and collapse the error element, remove the label, and set expandable
101 * to false.
102 */
103 ve.ui.MWExpandableErrorElement.prototype.clear = function () {
104 this.label.setLabel( null );
105 this.toggle( false );
106
107 this.button.toggle( false );
108 this.button.disconnect( this );
109 this.toggleLabel( false );
110
111 this.emit( 'update' );
112 };
113
114 /**
115 * Toggles the label between collapsed and expanded.
116 *
117 * @param {boolean} [expand] Expand if true, collapse if false, toggle if
118 * omitted
119 */
120 ve.ui.MWExpandableErrorElement.prototype.toggleLabel = function ( expand ) {
121 // Set this.expanded to the new state
122 this.expanded = expand === undefined ? !this.expanded : expand;
123
124 // Update DOM based on the new state of this.expanded
125 this.button.setIcon( this.expanded ? 'collapse' : 'expand' );
126 this.label.$element
127 .toggleClass( 've-ui-mwExpandableErrorElement-label-expanded', this.expanded )
128 .toggleClass( 've-ui-mwExpandableErrorElement-label-collapsed', !this.expanded );
129
130 this.emit( 'update' );
131 };