Merge "Use interwiki cache directly to resolve transwiki import sources"
[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 */
20 mw.widgets.TitleInputWidget = function MwWidgetsTitleInputWidget( config ) {
21 config = config || {};
22
23 // Parent constructor
24 mw.widgets.TitleInputWidget.parent.call( this, $.extend( {}, config, {
25 validate: this.isQueryValid.bind( this ),
26 autocomplete: false
27 } ) );
28
29 // Mixin constructors
30 mw.widgets.TitleWidget.call( this, config );
31 OO.ui.mixin.LookupElement.call( this, config );
32
33 // Properties
34 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
35
36 // Initialization
37 this.$element.addClass( 'mw-widget-titleInputWidget' );
38 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu' );
39 if ( this.showImages ) {
40 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withImages' );
41 }
42 if ( this.showDescriptions ) {
43 this.lookupMenu.$element.addClass( 'mw-widget-titleWidget-menu-withDescriptions' );
44 }
45 this.setLookupsDisabled( !this.suggestions );
46 };
47
48 /* Setup */
49
50 OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
51 OO.mixinClass( mw.widgets.TitleInputWidget, mw.widgets.TitleWidget );
52 OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
53
54 /* Methods */
55
56 /**
57 * @inheritdoc mw.widgets.TitleWidget
58 */
59 mw.widgets.TitleInputWidget.prototype.getQueryValue = function () {
60 return this.getValue();
61 };
62
63 /**
64 * @inheritdoc mw.widgets.TitleWidget
65 */
66 mw.widgets.TitleInputWidget.prototype.setNamespace = function ( namespace ) {
67 // Mixin method
68 mw.widgets.TitleWidget.prototype.setNamespace.call( this, namespace );
69
70 this.lookupCache = {};
71 this.closeLookupMenu();
72 };
73
74 /**
75 * @inheritdoc
76 */
77 mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
78 return this.getSuggestionsPromise();
79 };
80
81 /**
82 * @inheritdoc OO.ui.mixin.LookupElement
83 */
84 mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
85 return response.query || {};
86 };
87
88 /**
89 * @inheritdoc OO.ui.mixin.LookupElement
90 */
91 mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( response ) {
92 return this.getOptionsFromData( response );
93 };
94
95 /**
96 * @inheritdoc
97 */
98 mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
99 this.closeLookupMenu();
100 this.setLookupsDisabled( true );
101 this.setValue( item.getData() );
102 this.setLookupsDisabled( !this.suggestions );
103 };
104
105 /**
106 * @inheritdoc
107 */
108 mw.widgets.TitleInputWidget.prototype.focus = function () {
109 var retval;
110
111 // Prevent programmatic focus from opening the menu
112 this.setLookupsDisabled( true );
113
114 // Parent method
115 retval = mw.widgets.TitleInputWidget.parent.prototype.focus.apply( this, arguments );
116
117 this.setLookupsDisabled( !this.suggestions );
118
119 return retval;
120 };
121
122 /**
123 * @inheritdoc
124 */
125 mw.widgets.TitleInputWidget.prototype.cleanUpValue = function ( value ) {
126 var widget = this;
127
128 // Parent method
129 value = mw.widgets.TitleInputWidget.parent.prototype.cleanUpValue.call( this, value );
130
131 return $.trimByteLength( this.value, value, this.maxLength, function ( value ) {
132 var title = widget.getTitle( value );
133 return title ? title.getMain() : value;
134 } ).newVal;
135 };
136
137 }( jQuery, mediaWiki ) );