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