Merge "Allow easy suppression of multiple deleted revs"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.TitleInputWidget.js
1 /*!
2 * MediaWiki Widgets - TitleInputWidget class.
3 *
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function ( $, mw ) {
8
9 /**
10 * Creates an mw.widgets.TitleInputWidget object.
11 *
12 * @class
13 * @extends OO.ui.TextInputWidget
14 * @mixins mw.widgets.TitleWidget
15 * @mixins OO.ui.mixin.LookupElement
16 *
17 * @constructor
18 * @cfg {boolean} [suggestions=true] Display search suggestions
19 * @cfg {RegExp|Function|string} [validate] Perform title validation
20 */
21 mw.widgets.TitleInputWidget = function MwWidgetsTitleInputWidget( config ) {
22 config = config || {};
23
24 // Parent constructor
25 mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, {
26 validate: config.validate !== undefined ? config.validate : this.isQueryValid.bind( this ),
27 autocomplete: false
28 } ) );
29
30 // Mixin constructors
31 mw.widgets.TitleWidget.call( this, config );
32 OO.ui.mixin.LookupElement.call( this, config );
33
34 // Properties
35 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
36
37 // Initialization
38 this.$element.addClass( 'mw-widget-titleInputWidget' );
39 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
40 if ( this.showImages ) {
41 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
42 }
43 if ( this.showDescriptions ) {
44 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
45 }
46 this.setLookupsDisabled( !this.suggestions );
47 };
48
49 /* Setup */
50
51 OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
52 OO.mixinClass( mw.widgets.TitleInputWidget, mw.widgets.TitleWidget );
53 OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
54
55 /* Methods */
56
57 /**
58 * @inheritdoc mw.widgets.TitleWidget
59 */
60 mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
61 return this.getValue();
62 };
63
64 /**
65 * @inheritdoc mw.widgets.TitleWidget
66 */
67 mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) {
68 // Mixin method
69 mw.widgets.TitleWidget.prototype.setNamespace.call( this, namespace );
70
71 this.lookupCache = {};
72 this.closeLookupMenu();
73 };
74
75 /**
76 * @inheritdoc
77 */
78 mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
79 return this.getSuggestionsPromise();
80 };
81
82 /**
83 * @inheritdoc OO.ui.mixin.LookupElement
84 */
85 mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
86 return response.query || {};
87 };
88
89 /**
90 * @inheritdoc OO.ui.mixin.LookupElement
91 */
92 mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
93 return this.getOptionsFromData( response );
94 };
95
96 /**
97 * @inheritdoc
98 */
99 mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
100 this.closeLookupMenu();
101 this.setLookupsDisabled( true );
102 this.setValue( item.getData() );
103 this.setLookupsDisabled( !this.suggestions );
104 };
105
106 /**
107 * @inheritdoc
108 */
109 mw.widgets.TitleInputWidget.prototype.focus = function () {
110 var retval;
111
112 // Prevent programmatic focus from opening the menu
113 this.setLookupsDisabled( true );
114
115 // Parent method
116 retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments );
117
118 this.setLookupsDisabled( !this.suggestions );
119
120 return retval;
121 };
122
123 /**
124 * @inheritdoc
125 */
126 mw.widgets.TitleInputWidget.prototype.cleanUpValue = function ( value ) {
127 var widget = this;
128
129 // Parent method
130 value = mw.widgets.TitleInputWidget.parent.prototype.cleanUpValue.call( this, value );
131
132 return $.trimByteLength( this.value, value, this.maxLength, function ( value ) {
133 var title = widget.getTitle( value );
134 return title ? title.getMain() : value;
135 } ).newVal;
136 };
137
138 }( jQuery, mediaWiki ) );