Don't check namespace in SpecialWantedtemplates
[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 * Creates an mw.widgets.TitleInputWidget object.
10 *
11 * @class
12 * @extends OO.ui.TextInputWidget
13 * @mixins OO.ui.mixin.LookupElement
14 *
15 * @constructor
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [namespace] Namespace to prepend to queries
18 */
19 mw.widgets.TitleInputWidget = function MWWTitleInputWidget( config ) {
20 // Config initialization
21 config = config || {};
22
23 // Parent constructor
24 OO.ui.TextInputWidget.call( this, config );
25
26 // Mixin constructors
27 OO.ui.mixin.LookupElement.call( this, config );
28
29 // Properties
30 this.namespace = config.namespace || null;
31
32 // Initialization
33 this.$element.addClass( 'mw-widget-TitleInputWidget' );
34 this.lookupMenu.$element.addClass( 'mw-widget-TitleInputWidget-menu' );
35 };
36
37 /* Inheritance */
38
39 OO.inheritClass( mw.widgets.TitleInputWidget, OO.ui.TextInputWidget );
40
41 OO.mixinClass( mw.widgets.TitleInputWidget, OO.ui.mixin.LookupElement );
42
43 /* Methods */
44
45 /**
46 * @inheritdoc
47 */
48 mw.widgets.TitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
49 this.closeLookupMenu();
50 this.setLookupsDisabled( true );
51 this.setValue( item.getData() );
52 this.setLookupsDisabled( false );
53 };
54
55 /**
56 * @inheritdoc
57 */
58 mw.widgets.TitleInputWidget.prototype.getLookupRequest = function () {
59 var value = this.value;
60
61 // Prefix with default namespace name
62 if ( this.namespace !== null && mw.Title.newFromText( value, this.namespace ) ) {
63 value = mw.Title.newFromText( value, this.namespace ).getPrefixedText();
64 }
65
66 // Dont send leading ':' to open search
67 if ( value.charAt( 0 ) === ':' ) {
68 value = value.slice( 1 );
69 }
70
71 return new mw.Api().get( {
72 action: 'opensearch',
73 search: value,
74 suggest: ''
75 } );
76 };
77
78 /**
79 * @inheritdoc
80 */
81 mw.widgets.TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
82 return data[1] || [];
83 };
84
85 /**
86 * @inheritdoc
87 */
88 mw.widgets.TitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
89 var i, len, title, value,
90 items = [],
91 matchingPages = data;
92
93 // Matching pages
94 if ( matchingPages && matchingPages.length ) {
95 for ( i = 0, len = matchingPages.length; i < len; i++ ) {
96 title = new mw.Title( matchingPages[i] );
97 if ( this.namespace !== null ) {
98 value = title.getRelativeText( this.namespace );
99 } else {
100 value = title.getPrefixedText();
101 }
102 items.push( new OO.ui.MenuOptionWidget( {
103 data: value,
104 label: value
105 } ) );
106 }
107 }
108
109 return items;
110 };
111
112 /**
113 * Get title object corresponding to #getValue
114 *
115 * @returns {mw.Title|null} Title object, or null if value is invalid
116 */
117 mw.widgets.TitleInputWidget.prototype.getTitle = function () {
118 var title = this.getValue(),
119 // mw.Title doesn't handle null well
120 titleObj = mw.Title.newFromText( title, this.namespace !== null ? this.namespace : undefined );
121
122 return titleObj;
123 };
124
125 /**
126 * @inheritdoc
127 */
128 mw.widgets.TitleInputWidget.prototype.isValid = function () {
129 return $.Deferred().resolve( !!this.getTitle() ).promise();
130 };
131
132 }( jQuery, mediaWiki ) );