all files / src/src/widgets/ SearchInputWidget.js

74.29% Statements 26/35
55.56% Branches 5/9
62.5% Functions 5/8
74.29% Lines 26/35
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                            3780×         3780×     3780×     3780× 3780×     3780× 3780×     3780×                               3780×                                                                       7660× 7404×   256× 256×                                   100×           3780× 3780× 3780×    
/**
 * SearchInputWidgets are TextInputWidgets with `type="search"` assigned and feature a
 * {@link OO.ui.mixin.IconElement 'search' icon} as well as a functional
 * {@link OO.ui.mixin.IndicatorElement 'clear' indicator} by default.
 * Please see the [OOUI documentation on MediaWiki][1] for more information and examples.
 *
 * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Inputs#SearchInputWidget
 *
 * @class
 * @extends OO.ui.TextInputWidget
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
OO.ui.SearchInputWidget = function OoUiSearchInputWidget( config ) {
	config = $.extend( {
		icon: 'search'
	}, config );
 
	// Parent constructor
	OO.ui.SearchInputWidget.super.call( this, config );
 
	// Events
	this.connect( this, {
		change: 'onChange'
	} );
	this.$indicator.on( 'click', this.onIndicatorClick.bind( this ) );
	this.$indicator.on( 'keydown', this.onIndicatorKeyDown.bind( this ) );
 
	// Initialization
	this.updateSearchIndicator();
	this.connect( this, {
		disable: 'onDisable'
	} );
	this.$indicator
		.attr( {
			tabindex: -1,
			role: 'button'
		} );
};
 
/* Setup */
 
OO.inheritClass( OO.ui.SearchInputWidget, OO.ui.TextInputWidget );
 
/* Methods */
 
/**
 * @inheritdoc
 * @protected
 */
OO.ui.SearchInputWidget.prototype.getValidType = function () {
	return 'search';
};
 
/**
 * Clear and focus the input element when pressing enter on the 'clear' indicator.
 *
 * @param {jQuery.Event} e KeyDown event
 * @return {boolean}
 */
OO.ui.SearchInputWidget.prototype.onIndicatorKeyDown = function ( e ) {
	if ( e.keyCode === OO.ui.Keys.ENTER ) {
		// Clear the text field
		this.setValue( '' );
		this.focus();
		return false;
	}
};
 
/**
 * Clear and focus the input element when clicking on the 'clear' indicator.
 *
 * @param {jQuery.Event} e Click event
 * @return {boolean}
 */
OO.ui.SearchInputWidget.prototype.onIndicatorClick = function ( e ) {
	if ( e.which === OO.ui.MouseButtons.LEFT ) {
		// Clear the text field
		this.setValue( '' );
		this.focus();
		return false;
	}
};
 
/**
 * Update the 'clear' indicator displayed on type: 'search' text
 * fields, hiding it when the field is already empty or when it's not
 * editable.
 */
OO.ui.SearchInputWidget.prototype.updateSearchIndicator = function () {
	if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) {
		this.setIndicator( null );
	} else {
		this.setIndicator( 'clear' );
		this.$indicator.attr( 'aria-label', OO.ui.msg( 'ooui-item-remove' ) );
	}
};
 
/**
 * Handle change events.
 *
 * @private
 */
OO.ui.SearchInputWidget.prototype.onChange = function () {
	this.updateSearchIndicator();
};
 
/**
 * Handle disable events.
 *
 * @param {boolean} disabled Element is disabled
 * @private
 */
OO.ui.SearchInputWidget.prototype.onDisable = function () {
	this.updateSearchIndicator();
};
 
/**
 * @inheritdoc
 */
OO.ui.SearchInputWidget.prototype.setReadOnly = function ( state ) {
	OO.ui.SearchInputWidget.super.prototype.setReadOnly.call( this, state );
	this.updateSearchIndicator();
	return this;
};