mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 10:35:48 +00:00
db3da5d16b
Formerly known as "The greatest commit in the history of the world*". * Within a 3 block radius of Drayton Park and Auburt Park, starting from July 30th at about 9pm or so. Bugs: * (bug 51404) Allow escaping out of the link inspector when in creation mode (no text is selected, text will be inserted based on link target) and the text input is empty * (bug 51065 and bug 51415) Keep model and view in sync when changing the link inspector's text input value and showing options in a menu * (bug 51523) Either restore selection at the time of close to what it was before opening the inspector (when using back) or to what it was before closing (might be changed by transactions processed during the close method) - this makes it simpler and more natural when clicking away from the link inspector, even when there are changes that must be saved by the link inspector on close Bonus: * Use only the light blue highlight color for menu widget items - the checkmark already displays the selected item, the dark blue is just masking the current highlight position and confusing the peoples * Remove links when the user deletes everything from the link inspector's text input and then closes the link inspector * Replace select menu's evil "silent" selectItem/highlightItem argument with a new method called initializeSelection which sets both selection and highlighting to an item without emitting events - this is needed when synchronizing the view with the model so the model isn't immediately told to change to a value it already has * Make the MWTitle lookup menu not flash like crazy as you type (this was caused by a copy-paste oversight overriding initializeLookupMenuSelection unnecessarily) Bug: 51404 Bug: 51065 Bug: 51415 Bug: 51523 Change-Id: I339d9253ad472c2f42c3179edc84a83d27561270
233 lines
6.3 KiB
JavaScript
233 lines
6.3 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface MWLinkTargetInputWidget class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/*global mw*/
|
|
|
|
/**
|
|
* Creates an ve.ui.MWLinkTargetInputWidget object.
|
|
*
|
|
* @class
|
|
* @extends ve.ui.LinkTargetInputWidget
|
|
* @mixins ve.ui.LookupInputWidget
|
|
*
|
|
* @constructor
|
|
* @param {Object} [config] Config options
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget = function VeUiMWLinkTargetInputWidget( config ) {
|
|
// Config intialization
|
|
config = config || {};
|
|
|
|
// Parent constructor
|
|
ve.ui.LinkTargetInputWidget.call( this, config );
|
|
|
|
// Mixin constructors
|
|
ve.ui.LookupInputWidget.call( this, this, config );
|
|
|
|
// Events
|
|
this.lookupMenu.connect( this, { 'select': 'onLookupMenuItemSelect' } );
|
|
|
|
// Initialization
|
|
this.$.addClass( 've-ui-mwLinkTargetInputWidget' );
|
|
this.lookupMenu.$.addClass( 've-ui-mwLinkTargetInputWidget-menu' );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ui.MWLinkTargetInputWidget, ve.ui.LinkTargetInputWidget );
|
|
|
|
ve.mixinClass( ve.ui.MWLinkTargetInputWidget, ve.ui.LookupInputWidget );
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Handle menu item select event.
|
|
*
|
|
* @method
|
|
* @param {ve.ui.MenuItemWidget} item Selected item
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.onLookupMenuItemSelect = function ( item ) {
|
|
if ( item ) {
|
|
this.setAnnotation( item.getData() );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Gets a new request object of the current lookup query value.
|
|
*
|
|
* @method
|
|
* @returns {jqXHR} AJAX object without success or fail handlers attached
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getLookupRequest = function () {
|
|
return $.ajax( {
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
'data': {
|
|
'format': 'json',
|
|
'action': 'opensearch',
|
|
'search': this.value,
|
|
'namespace': 0,
|
|
'suggest': ''
|
|
},
|
|
'dataType': 'json'
|
|
} );
|
|
};
|
|
|
|
/**
|
|
* Get lookup cache item from server response data.
|
|
*
|
|
* @method
|
|
* @param {Mixed} data Response from server
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
|
|
return ve.isArray( data ) && data.length ? data[1] : [];
|
|
};
|
|
|
|
/**
|
|
* Get list of menu items from a server response.
|
|
*
|
|
* @param {Object} data Query result
|
|
* @returns {ve.ui.MenuItemWidget[]} Menu items
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
|
|
var i, len,
|
|
menu$$ = this.lookupMenu.$$,
|
|
items = [],
|
|
matchingPages = data,
|
|
// If not found, run value through mw.Title to avoid treating a match as a
|
|
// mismatch where normalisation would make them matching (bug 48476)
|
|
pageExists =
|
|
ve.indexOf( this.value, matchingPages ) !== -1 ||
|
|
ve.indexOf( new mw.Title( this.value ).getPrefixedText(), matchingPages ) !== -1;
|
|
|
|
// External link
|
|
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( this.value ) ) {
|
|
items.push( new ve.ui.MenuSectionItemWidget(
|
|
'externalLink',
|
|
{ '$$': menu$$, 'label': ve.msg( 'visualeditor-linkinspector-suggest-external-link' ) }
|
|
) );
|
|
items.push( new ve.ui.MenuItemWidget(
|
|
this.getExternalLinkAnnotationFromUrl( this.value ),
|
|
{ '$$': menu$$, 'rel': 'externalLink', 'label': this.value }
|
|
) );
|
|
}
|
|
|
|
// Internal link
|
|
if ( !pageExists ) {
|
|
items.push( new ve.ui.MenuSectionItemWidget(
|
|
'newPage',
|
|
{ '$$': menu$$, 'label': ve.msg( 'visualeditor-linkinspector-suggest-new-page' ) }
|
|
) );
|
|
items.push( new ve.ui.MenuItemWidget(
|
|
this.getInternalLinkAnnotationFromTitle( this.value ),
|
|
{ '$$': menu$$, 'rel': 'newPage', 'label': this.value }
|
|
) );
|
|
}
|
|
|
|
// Matching pages
|
|
if ( matchingPages && matchingPages.length ) {
|
|
items.push( new ve.ui.MenuSectionItemWidget(
|
|
'matchingPages',
|
|
{ '$$': menu$$, 'label': ve.msg( 'visualeditor-linkinspector-suggest-matching-page' ) }
|
|
) );
|
|
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
|
|
items.push( new ve.ui.MenuItemWidget(
|
|
this.getInternalLinkAnnotationFromTitle( matchingPages[i] ),
|
|
{ '$$': menu$$, 'rel': 'matchingPage', 'label': matchingPages[i] }
|
|
) );
|
|
}
|
|
}
|
|
|
|
return items;
|
|
};
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.initializeLookupMenuSelection = function () {
|
|
var item;
|
|
|
|
// Parent method
|
|
ve.ui.LookupInputWidget.prototype.initializeLookupMenuSelection.call( this );
|
|
|
|
// Update annotation to match selected item
|
|
item = this.lookupMenu.getSelectedItem();
|
|
if ( item ) {
|
|
this.setAnnotation( item.getData() );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Set the value of the input.
|
|
*
|
|
* Overrides setValue to keep annotations in sync.
|
|
*
|
|
* @method
|
|
* @param {string} value New value
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.setValue = function ( value ) {
|
|
// Keep annotation in sync with value by skipping parent and calling grandparent method
|
|
ve.ui.TextInputWidget.prototype.setValue.call( this, value );
|
|
};
|
|
|
|
/**
|
|
* Gets an internal link annotation.
|
|
*
|
|
* File: or Category: links will be prepended with a colon so they are interpreted as a links rather
|
|
* than image inclusions or categorizations.
|
|
*
|
|
* @method
|
|
* @param {string} target Page title
|
|
* @returns {ve.dm.MWInternalLinkAnnotation}
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getInternalLinkAnnotationFromTitle = function ( target ) {
|
|
var title;
|
|
try {
|
|
title = new mw.Title( target );
|
|
if ( title.getNamespaceId() === 6 || title.getNamespaceId() === 14 ) {
|
|
target = ':' + target;
|
|
}
|
|
} catch ( e ) { }
|
|
return new ve.dm.MWInternalLinkAnnotation( {
|
|
'type': 'link/mwInternal',
|
|
'attributes': {
|
|
'title': target,
|
|
'normalizedTitle': ve.dm.MWInternalLinkAnnotation.static.normalizeTitle( target )
|
|
}
|
|
} );
|
|
};
|
|
|
|
/**
|
|
* Gets an external link annotation.
|
|
*
|
|
* @method
|
|
* @param {string} target Web address
|
|
* @returns {ve.dm.MWExternalLinkAnnotation}
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getExternalLinkAnnotationFromUrl = function ( target ) {
|
|
return new ve.dm.MWExternalLinkAnnotation( {
|
|
'type': 'link/mwExternal',
|
|
'attributes': {
|
|
'href': target
|
|
}
|
|
} );
|
|
};
|
|
|
|
/**
|
|
* Gets a target from an annotation.
|
|
*
|
|
* @method
|
|
* @param {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation} annotation Annotation
|
|
* @returns {string} Target
|
|
*/
|
|
ve.ui.MWLinkTargetInputWidget.prototype.getTargetFromAnnotation = function ( annotation ) {
|
|
if ( annotation instanceof ve.dm.MWExternalLinkAnnotation ) {
|
|
return annotation.getAttribute( 'href' );
|
|
} else if ( annotation instanceof ve.dm.MWInternalLinkAnnotation ) {
|
|
return annotation.getAttribute( 'title' );
|
|
}
|
|
return '';
|
|
};
|