X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js new file mode 100644 index 00000000..5ba9481a --- /dev/null +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleSearchWidget.js @@ -0,0 +1,105 @@ +/*! + * MediaWiki Widgets - TitleSearchWidget class. + * + * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt + * @license The MIT License (MIT); see LICENSE.txt + */ +( function ( $, mw ) { + + /** + * Creates an mw.widgets.TitleSearchWidget object. + * + * @class + * @extends OO.ui.SearchWidget + * @mixins OO.ui.mixin.RequestManager + * @mixins mw.widgets.TitleWidget + * + * @constructor + * @param {Object} [config] Configuration options + */ + mw.widgets.TitleSearchWidget = function MwWidgetsTitleSearchWidget( config ) { + config = config || {}; + + // Parent constructor + mw.widgets.TitleSearchWidget.parent.call( this, config ); + + // Mixin constructors + mw.widgets.TitleWidget.call( this, config ); + OO.ui.mixin.RequestManager.call( this, config ); + + this.query.setValidation( this.isQueryValid.bind( this ) ); + + // Events + this.results.connect( this, { choose: 'onTitleSearchResultsChoose' } ); + + // Initialization + this.$element.addClass( 'mw-widget-titleSearchWidget' ); + this.results.$element.addClass( 'mw-widget-titleWidget-menu' ); + if ( this.showImages ) { + this.results.$element.addClass( 'mw-widget-titleWidget-menu-withImages' ); + } + if ( this.showDescriptions ) { + this.results.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' ); + } + if ( this.maxLength !== undefined ) { + this.getQuery().$input.attr( 'maxlength', this.maxLength ); + } + }; + + /* Setup */ + + OO.inheritClass( mw.widgets.TitleSearchWidget, OO.ui.SearchWidget ); + OO.mixinClass( mw.widgets.TitleSearchWidget, OO.ui.mixin.RequestManager ); + OO.mixinClass( mw.widgets.TitleSearchWidget, mw.widgets.TitleWidget ); + + /* Methods */ + + /** + * @inheritdoc mw.widgets.TitleWidget + */ + mw.widgets.TitleSearchWidget.prototype.getQueryValue = function () { + return this.getQuery().getValue(); + }; + + /** + * Handle choose events from the result widget + * + * @param {OO.ui.OptionWidget} item Chosen item + */ + mw.widgets.TitleSearchWidget.prototype.onTitleSearchResultsChoose = function ( item ) { + this.getQuery().setValue( item.getData() ); + }; + + /** + * @inheritdoc + */ + mw.widgets.TitleSearchWidget.prototype.onQueryChange = function () { + var widget = this; + + this.getRequestData().done( function ( data ) { + // Parent method + mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget ); + widget.results.addItems( widget.getOptionsFromData( data ) ); + } ); + }; + + /** + * @inheritdoc OO.ui.mixin.RequestManager + */ + mw.widgets.TitleSearchWidget.prototype.getRequestQuery = function () { + return this.getQueryValue(); + }; + /** + * @inheritdoc OO.ui.mixin.RequestManager + */ + mw.widgets.TitleSearchWidget.prototype.getRequest = function () { + return this.getSuggestionsPromise(); + }; + /** + * @inheritdoc OO.ui.mixin.RequestManager + */ + mw.widgets.TitleSearchWidget.prototype.getRequestCacheDataFromResponse = function ( response ) { + return response.query || {}; + }; + +}( jQuery, mediaWiki ) );