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