| 1 |
var InsertLinkTitleOptionWidget = require( './TitleOptionWidget.js' ); |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
TitleInputWidget.static.urlRegex = new RegExp( '^(' + mw.config.get( 'wgUrlProtocols' ) + '|www\\.)', 'i' ); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
TitleInputWidget.prototype.onLookupInputBlur = function () { |
| 42 |
TitleInputWidget.super.prototype.onLookupInputBlur.apply( this ); |
| 43 |
this.selectFirstMatch(); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
TitleInputWidget.prototype.selectFirstMatch = function () { |
| 53 |
var that = this; |
| 54 |
this.getLookupMenuItems().done( function ( items ) { |
| 55 |
|
| 56 |
|
| 57 |
for ( var i = 0; i < items.length; i++ ) { |
| 58 |
var item = items[ i ]; |
| 59 |
var queryVal = that.getQueryValue(); |
| 60 |
|
| 61 |
if ( item.getData() === queryVal || |
| 62 |
item.getData() === queryVal.charAt( 0 ).toUpperCase() + queryVal.slice( 1 ) |
| 63 |
) { |
| 64 |
|
| 65 |
that.emit( 'select', item ); |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
} ); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
TitleInputWidget.prototype.createOptionWidget = function ( data ) { |
| 93 |
return new InsertLinkTitleOptionWidget( data ); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
TitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) { |
| 104 |
var res = TitleInputWidget.super.prototype.getLookupCacheDataFromResponse( response ); |
| 105 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
TitleInputWidget.prototype.onLookupMenuChoose = function ( item ) { |
| 124 |
TitleInputWidget.super.prototype.onLookupMenuChoose.call( this, item ); |
| 125 |
this.emit( 'select', item ); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
TitleInputWidget.prototype.getOverlay = function () { |
| 135 |
|
| 136 |
return OO.ui.getDefaultOverlay() |
| 137 |
.clone() |
| 138 |
.css( 'z-index', '1010' ) |
| 139 |
.appendTo( document.body ); |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
TitleInputWidget.prototype.looksLikeExternalLink = function ( urlString ) { |
| 151 |
return this.constructor.static.urlRegex.test( urlString ); |
| 152 |
}; |
| 153 |
|
| 154 |
module.exports = TitleInputWidget; |