2015-05-03 21:21:00 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWInternalLinkAnnotationWidget class.
|
|
|
|
*
|
2016-01-03 22:56:59 +00:00
|
|
|
* @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
|
2015-05-03 21:21:00 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWInternalLinkAnnotationWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.LinkAnnotationWidget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget = function VeUiMWInternalLinkAnnotationWidget() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.super.apply( this, arguments );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWInternalLinkAnnotationWidget, ve.ui.LinkAnnotationWidget );
|
|
|
|
|
2015-05-29 12:08:05 +00:00
|
|
|
/* Static Methods */
|
2015-05-03 21:21:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2015-05-29 12:08:05 +00:00
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.static.getAnnotationFromText = function ( value ) {
|
2015-08-25 15:39:32 +00:00
|
|
|
var title = mw.Title.newFromText( value.trim() );
|
2015-05-03 21:21:00 +00:00
|
|
|
|
2015-08-25 15:39:32 +00:00
|
|
|
if ( !title ) {
|
2015-05-03 21:21:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
2015-08-25 15:39:32 +00:00
|
|
|
return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
|
2015-05-03 21:21:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2015-05-29 12:08:05 +00:00
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.static.getTextFromAnnotation = function ( annotation ) {
|
2015-05-03 21:21:00 +00:00
|
|
|
return annotation ? annotation.getAttribute( 'title' ) : '';
|
|
|
|
};
|
2015-05-29 12:08:05 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a text input widget to be used by the annotation widget
|
|
|
|
*
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @return {OO.ui.TextInputWidget} Text input widget
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.prototype.createInputWidget = function ( config ) {
|
2016-03-08 10:26:31 +00:00
|
|
|
var input = new mw.widgets.TitleSearchWidget( ve.extendObject( {
|
2015-05-29 12:08:05 +00:00
|
|
|
icon: 'search',
|
2015-10-14 16:24:10 +00:00
|
|
|
showRedlink: true,
|
2016-09-30 19:40:07 +00:00
|
|
|
excludeCurrentPage: true,
|
2015-06-15 10:25:44 +00:00
|
|
|
showImages: mw.config.get( 'wgVisualEditor' ).usePageImages,
|
|
|
|
showDescriptions: mw.config.get( 'wgVisualEditor' ).usePageDescriptions,
|
|
|
|
cache: ve.init.platform.linkCache
|
2015-09-25 20:29:10 +00:00
|
|
|
}, config ) );
|
2016-03-08 10:26:31 +00:00
|
|
|
|
|
|
|
// Put query first in DOM
|
|
|
|
// TODO: Consider upstreaming this to SearchWidget
|
|
|
|
input.$element.prepend( input.$query );
|
|
|
|
|
|
|
|
return input;
|
2015-09-25 20:29:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.prototype.getTextInputWidget = function () {
|
|
|
|
return this.input.query;
|
2015-05-29 12:08:05 +00:00
|
|
|
};
|
2015-06-23 21:06:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.prototype.getHref = function () {
|
|
|
|
var title = ve.ui.MWInternalLinkAnnotationWidget.super.prototype.getHref.call( this );
|
|
|
|
return mw.util.getUrl( title );
|
|
|
|
};
|
2016-07-15 18:35:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWInternalLinkAnnotationWidget.prototype.onTextChange = function ( value ) {
|
|
|
|
var targetData,
|
|
|
|
htmlDoc = this.getElementDocument();
|
|
|
|
// Specific thing we want to check: has a valid URL for an internal page
|
|
|
|
// been pasted into here, in which case we want to convert it to just the
|
|
|
|
// page title. This has to happen /here/ because a URL can reference a
|
|
|
|
// valid page while not being a valid Title (e.g. if it contains a "%").
|
|
|
|
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
|
|
|
|
targetData = ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref(
|
|
|
|
value,
|
|
|
|
htmlDoc
|
|
|
|
);
|
|
|
|
if ( targetData.isInternal ) {
|
|
|
|
value = targetData.title;
|
|
|
|
this.input.query.setValue( targetData.title );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ve.ui.MWInternalLinkAnnotationWidget.super.prototype.onTextChange.call( this, value );
|
|
|
|
};
|