Debian Codegraph @

extensions/WikiEditor/modules/insertlink/TitleInputWidget.js

raw symbols SCIP
1 var InsertLinkTitleOptionWidget = require( './TitleOptionWidget.js' );
2
3 /**
4 * A custom TitleInputWidget that adds support for external links
5 * (any string that starts with a protocol or `www.` and doesn't exist as a page).
6 *
7 * @class
8 * @extends mw.widgets.TitleInputWidget
9 * @constructor
10 */
11 function TitleInputWidget() {
12 TitleInputWidget.super.call( this, {
13 showImages: true,
14 showDescriptions: true,
15 showDisambigsLast: true,
16 placeholder: mw.msg( 'wikieditor-toolbar-tool-link-int-target-tooltip' ),
17 $overlay: this.getOverlay(),
18 validateTitle: false,
19 showInterwikis: true,
20 required: true,
21 addQueryInput: true
22 } );
23 }
24
25 OO.inheritClass( TitleInputWidget, mw.widgets.TitleInputWidget );
26
27 /**
28 * Regular expression for determining what might be an external link.
29 *
30 * @static
31 * @property {RegExp}
32 */
33 TitleInputWidget.static.urlRegex = new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + '|www\\.)', 'i' );
34
35 /**
36 * When leaving the input without selecting a menu item,
37 * automatically select a matching item if there is one.
38 * Even though we specify addQueryInput=true in the config, the entered string
39 * is not always available to be selected. See T291056.
40 */
41 TitleInputWidget.prototype.onLookupInputBlur = function () {
42 TitleInputWidget.super.prototype.onLookupInputBlur.apply( this );
43 this.selectFirstMatch();
44 };
45
46 /**
47 * Select the first matching search result
48 * The first match might not be at the top of the list, nor an exact match.
49 *
50 * @public
51 */
52 TitleInputWidget.prototype.selectFirstMatch = function () {
53 var that = this;
54 this.getLookupMenuItems().done( function ( items ) {
55 // The matching item is not always the first,
56 // because disambiguation pages are moved to the end.
57 for ( var i = 0; i < items.length; i++ ) {
58 var item = items[ i ];
59 var queryVal = that.getQueryValue();
60 // Check for exact match, or a match with uppercase first character.
61 if ( item.getData() === queryVal ||
62 item.getData() === queryVal.charAt( 0 ).toUpperCase() + queryVal.slice( 1 )
63 ) {
64 // If a matching title is is found, fire an event and stop looking.
65 that.emit( 'select', item );
66 break;
67 }
68 }
69 } );
70 };
71
72 /**
73 * Get menu option widget data from the title and page data,
74 * adding an `external` property.
75 *
76 * @param {string} title Page title
77 * @param {Object} data Page data
78 * @return {Object} Data for option widget
79 */
80 TitleInputWidget.prototype.getOptionWidgetData = function ( title, data ) {
81 var widgetData = TitleInputWidget.super.prototype.getOptionWidgetData.call( this, title, data );
82 widgetData.external = data.originalData.external;
83 return widgetData;
84 };
85
86 /**
87 * Create a InsertLinkTitleOptionWidget.
88 *
89 * @param {Object} data Data for option widget
90 * @return {OO.ui.MenuOptionWidget} The option widget
91 */
92 TitleInputWidget.prototype.createOptionWidget = function ( data ) {
93 return new InsertLinkTitleOptionWidget( data );
94 };
95
96 /**
97 * Get pages' data from the API response, adding an `external` property for
98 * pages that do not exist and which look like external URLs.
99 *
100 * @param {Object} response
101 * @return {Object}
102 */
103 TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
104 var res = TitleInputWidget.super.prototype.getLookupCacheDataFromResponse( response );
105 // Guard against zero responses.
106 if ( res.pages === undefined ) {
107 return res;
108 }
109 for ( var pageId in res.pages ) {
110 if ( Object.prototype.hasOwnProperty.call( res.pages, pageId ) ) {
111 var page = res.pages[ pageId ];
112 page.external = page.missing !== undefined && this.looksLikeExternalLink( page.title );
113 }
114 }
115 return res;
116 };
117
118 /**
119 * Handle menu item 'choose' event, updating the text input value to the value of the clicked item.
120 *
121 * @param {OO.ui.MenuOptionWidget} item Selected item
122 */
123 TitleInputWidget.prototype.onLookupMenuChoose = function ( item ) {
124 TitleInputWidget.super.prototype.onLookupMenuChoose.call( this, item );
125 this.emit( 'select', item );
126 };
127
128 /**
129 * Get a custom overlay for the dropdown menu, so it's not contained within the jQuery UI dialog.
130 *
131 * @private
132 * @return {jQuery}
133 */
134 TitleInputWidget.prototype.getOverlay = function () {
135 // Overlay z-index must be greater than the jQuery UI dialog's of 1002.
136 return OO.ui.getDefaultOverlay()
137 .clone()
138 .css( 'z-index', '1010' )
139 .appendTo( document.body );
140 };
141
142 /**
143 * Determine if a given string is likely to be an external URL.
144 * External URLs start with either a valid protocol (from $wgUrlProtocols) or `www.`.
145 *
146 * @public
147 * @param {string} urlString The possible URL.
148 * @return {boolean}
149 */
150 TitleInputWidget.prototype.looksLikeExternalLink = function ( urlString ) {
151 return this.constructor.static.urlRegex.test( urlString );
152 };
153
154 module.exports = TitleInputWidget;