all files / src/src/widgets/ MessageWidget.js

88.89% Statements 40/45
72.22% Branches 13/18
75% Functions 3/4
88.89% Lines 40/45
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                                3728×     3728×     3728× 3728× 3728× 3728×     3728× 3728×       3728× 212×     3728×                             3728×                                                                     3728×   3728× 3728× 3728×                   3728× 184×     3728×   3728× 3728×     3728× 3728× 3728×   3728× 2688× 2688×   1040× 1040×     3728×                          
/**
 * MessageWidget produces a visual component for sending a notice to the user
 * with an icon and distinct design noting its purpose. The MessageWidget changes
 * its visual presentation based on the type chosen, which also denotes its UX
 * purpose.
 *
 * @class
 * @extends OO.ui.Widget
 * @mixes OO.ui.mixin.IconElement
 * @mixes OO.ui.mixin.LabelElement
 * @mixes OO.ui.mixin.TitledElement
 * @mixes OO.ui.mixin.FlaggedElement
 *
 * @constructor
 * @param {Object} [config] Configuration options
 * @param {string} [config.type='notice'] The type of the notice widget. This will also
 *  impact the flags that the widget receives (and hence its CSS design) as well
 *  as the icon that appears. Available types:
 *  'notice', 'error', 'warning', 'success'
 * @param {boolean} [config.inline=false] Set the notice as an inline notice. The default
 *  is not inline, or 'boxed' style.
 * @param {boolean} [config.showClose] Show a close button. Can't be used with inline.
 */
OO.ui.MessageWidget = function OoUiMessageWidget( config ) {
	// Configuration initialization
	config = config || {};
 
	// Parent constructor
	OO.ui.MessageWidget.super.call( this, config );
 
	// Mixin constructors
	OO.ui.mixin.IconElement.call( this, config );
	OO.ui.mixin.LabelElement.call( this, config );
	OO.ui.mixin.TitledElement.call( this, config );
	OO.ui.mixin.FlaggedElement.call( this, config );
 
	// Set type
	this.setType( config.type );
	this.setInline( config.inline );
 
	// If an icon is passed in, set it again as setType will
	// have overridden the setIcon call in the IconElement constructor
	if ( config.icon ) {
		this.setIcon( config.icon );
	}
 
	Iif ( !this.inline && config.showClose ) {
		this.closeButton = new OO.ui.ButtonWidget( {
			classes: [ 'oo-ui-messageWidget-close' ],
			framed: false,
			icon: 'close',
			label: OO.ui.msg( 'ooui-popup-widget-close-button-aria-label' ),
			invisibleLabel: true
		} );
		this.closeButton.connect( this, {
			click: 'onCloseButtonClick'
		} );
		this.$element.addClass( 'oo-ui-messageWidget-showClose' );
	}
 
	// Build the widget
	this.$element
		.append( this.$icon, this.$label, this.closeButton && this.closeButton.$element )
		.addClass( 'oo-ui-messageWidget' );
};
 
/* Setup */
 
OO.inheritClass( OO.ui.MessageWidget, OO.ui.Widget );
OO.mixinClass( OO.ui.MessageWidget, OO.ui.mixin.IconElement );
OO.mixinClass( OO.ui.MessageWidget, OO.ui.mixin.LabelElement );
OO.mixinClass( OO.ui.MessageWidget, OO.ui.mixin.TitledElement );
OO.mixinClass( OO.ui.MessageWidget, OO.ui.mixin.FlaggedElement );
 
/* Events */
 
/**
 * @event OO.ui.MessageWidget#close
 */
 
/* Static Properties */
 
/**
 * An object defining the icon name per defined type.
 *
 * @static
 * @property {Object}
 */
OO.ui.MessageWidget.static.iconMap = {
	notice: 'infoFilled',
	error: 'error',
	warning: 'alert',
	success: 'success'
};
 
/* Methods */
 
/**
 * Set the inline state of the widget.
 *
 * @param {boolean} [inline=false] Widget is inline
 */
OO.ui.MessageWidget.prototype.setInline = function ( inline ) {
	inline = !!inline;
 
	Eif ( this.inline !== inline ) {
		this.inline = inline;
		this.$element
			.toggleClass( 'oo-ui-messageWidget-block', !this.inline );
	}
};
/**
 * Set the widget type. The given type must belong to the list of
 * legal types set by OO.ui.MessageWidget.static.iconMap
 *
 * @param {string} [type='notice']
 */
OO.ui.MessageWidget.prototype.setType = function ( type ) {
	if ( !this.constructor.static.iconMap[ type ] ) {
		type = 'notice';
	}
 
	Eif ( this.type !== type ) {
		// Flags
		this.clearFlags();
		this.setFlags( type );
 
		// Set the icon and its variant
		this.setIcon( this.constructor.static.iconMap[ type ] );
		this.$icon.removeClass( 'oo-ui-image-' + this.type );
		this.$icon.addClass( 'oo-ui-image-' + type );
 
		if ( type === 'error' ) {
			this.$element.attr( 'role', 'alert' );
			this.$element.removeAttr( 'aria-live' );
		} else {
			this.$element.removeAttr( 'role' );
			this.$element.attr( 'aria-live', 'polite' );
		}
 
		this.type = type;
	}
};
 
/**
 * Handle click events on the close button
 *
 * @param {jQuery} e jQuery event
 * @fires OO.ui.MessageWidget#close
 */
OO.ui.MessageWidget.prototype.onCloseButtonClick = function () {
	this.toggle( false );
	this.emit( 'close' );
};