mediawiki-extensions-Visual.../modules/ve/ui/widgets/ve.ui.MWLinkTargetInputWidget.js

361 lines
8.9 KiB
JavaScript
Raw Normal View History

(bug 43841) Major ve.ui rewrite, especially ve.ui.LinkInspector Objectives: * Make the link inspector easier to use * Try to resolve a few bugs (bug 43841, bug 43063, bug 42986) * Stop using jquery.multiSuggest (which didn't really understand annotations) * Better divide MediaWiki specifics from generic implementations Changes: VisualEditor.php, modules/ve/test/index.php, demos/ve/index.php * Updated links to files ve.Registry * Fixed mistake where registry was initialized as an array - this didn't cause any errors because you can add arbitrary properties to an array and use it like any other object ve.Factory * Removed duplicate initialization of registry property * Added entries property, which is an array that's appended to for tracking the order of registrations ve.CommandRegistry * Added mwLink command which opens the mwLink inspector ve.ui.TextInputWidget * Added basic widget class for text inputs ve.ui.TextInputMenuWidget * Added widget that provides a menu of options for a text input widget ve.ui.MWLinkTargetInputWidget * Added MediaWiki specific link target widget ve.ui.MenuWidget * Converted ve.ui.Menu into a widget * Moved the body of onSelect to onMouseUp ve.ui.LinkTargetInputWidget * Added link target widget which adds link annotation functionality to a normal text input ve.ui.InputWidget * Added generic input widget which emits reliable and instant change events and synchronizes a value property with the DOM value ve.ui.Widget * Added base widget class * Widgets can be used in any frame ve.ui.Tool * Fixed line length issues ve.ui.InspectorFactory * Made use of new entries property for factories to select the most recently added inspector if more than one match a given annotation ve.ui.Inspector * Added auto-focus on the first visible input element on open * Moved afterClose event to after re-focus on document on close * Added documentation ve.ui.Frame * Adjusted documentation * Added binding of $$ to the frame context so it can be passed around * Added documentation ve.ui.Context * Added ve.ui.Widget.css to iframes * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget * Removed unused positionBelowOverlay method * Added CSS settings to set overlay left and width properties according to context size * Added documentation ve.ui.DropdownTool * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget ve.ui.FormatDropdownTool * Added documentation ve.ui.MWLinkButtonTool * Added MediaWiki specific version of ve.ui.LinkButtonTool, which opens the mwLink inspector ve.ui.Widget.css * Added styles for all widgets ve.ui.Tool.css, ve.init.sa.css, ve.init.mw.ViewPageTarget.css, ve.init.mw.ViewPageTarget-apex.css * Updated code as per moving of ve.ui.Menu to ve.ui.MenuWidget ve.ui.Menu.css * Deleted (merged into ve.ui.Widget.css) ve.ui.Menu.css * Deleted suggest styles (no longer used) pending.gif, pending.psd * Added diagonal stripe animation to indicate a pending request to the API ve.ui.MWLinkInspector * Added MediaWiki specific inspector which uses MediaWiki specific annotations and widgets ve.ui.LinkInspector * Removed mw global hint (not needed anymore) * Switched from comparing targets to annotations (since the target text is ambiguous in some situations) * Switched to using input widget, which is configured using a static property * Removed use of jquery.multiSuggest * Moved MediaWiki specifics to their own class (ve.ui.MWLinkInspector) ve.init.mw.ViewPageTarget * Added MediaWiki specific toolbar and command options Change-Id: I859b5871a9d2f17d970c002067c8ff24f3513e9f
2013-01-09 21:34:23 +00:00
/*global mw*/
/**
* VisualEditor user interface MWLinkTargetInputWidget class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Creates an ve.ui.MWLinkTargetInputWidget object.
*
* @class
* @constructor
* @extends ve.ui.LinkTargetInputWidget
* @param {Function} $$ jQuery for the frame the widget is in
* @param {jQuery} $overlay DOM element to add menu to
* @param {string} [name] Input name, used by HTML forms
* @param {string} [value] Input value
*/
ve.ui.MWLinkTargetInputWidget = function VeUiMWLinkTargetInputWidget( $$, $overlay, name, value ) {
// Parent constructor
ve.ui.LinkTargetInputWidget.call( this, $$, name, value );
// Properties
this.menu = new ve.ui.TextInputMenuWidget( $, $overlay );
this.annotation = null;
this.existingPages = {};
this.matchingPages = {};
this.existingPagesRequest = null;
this.matchingPagesRequest = null;
this.waiting = 0;
this.previousMatches = null;
// Events
this.$input.on( {
'click': ve.bind( this.onClick, this ),
'focus': ve.bind( this.onFocus, this ),
'blur': ve.bind( this.onBlur, this ),
'keydown': ve.bind( this.onKeyDown, this )
} );
this.menu.on( 'select', ve.bind( this.onMenuItemSelect, this ) );
// Initialization
this.$.addClass( 've-ui-mwLinkTargetInputWidget' );
};
/* Inheritance */
ve.inheritClass( ve.ui.MWLinkTargetInputWidget, ve.ui.LinkTargetInputWidget );
/* Methods */
/**
* Handles change events.
*
* @method
* @param {jQuery.Event} e Event
*/
ve.ui.MWLinkTargetInputWidget.prototype.onChange = function ( value, origin ) {
if ( origin !== 'annotation' ) {
this.annotation = null;
this.setValue( value );
this.openMenu();
}
};
/**
* Handles click events.
*
* @method
* @param {jQuery.Event} e Event
*/
ve.ui.MWLinkTargetInputWidget.prototype.onClick = function () {
this.openMenu();
};
/**
* Handles focus events.
*
* @method
* @param {jQuery.Event} e Event
*/
ve.ui.MWLinkTargetInputWidget.prototype.onFocus = function () {
this.openMenu();
};
/**
* Handles blur events.
*
* @method
* @param {jQuery.Event} e Event
*/
ve.ui.MWLinkTargetInputWidget.prototype.onBlur = function () {
this.menu.hide();
};
/**
* Handles key down events.
*
* @method
* @param {jQuery.Event} e Event
*/
ve.ui.MWLinkTargetInputWidget.prototype.onKeyDown = function ( e ) {
var handled = false;
switch ( e.keyCode ) {
// Up arrow
case 38:
this.menu.selectRelativeItem( -1 );
handled = true;
break;
// Down arrow
case 40:
this.menu.selectRelativeItem( 1 );
handled = true;
break;
}
if ( handled ) {
e.preventDefault();
}
};
/**
* Handles change events.
*
* @method
* @param {ve.dm.LinkAnnotation} annotation Link annotation
*/
ve.ui.MWLinkTargetInputWidget.prototype.onMenuItemSelect = function ( annotation ) {
this.setAnnotation( annotation );
};
/**
* Opens the menu.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.openMenu = function () {
this.populateMenu();
this.queryPageExistence();
this.queryMatchingPages();
if ( !this.menu.isVisible() ) {
this.menu.show();
this.menu.setPosition( this.$input );
}
};
/**
* Populates the menu.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.populateMenu = function () {
var i, len,
externalLink = this.getExternalLinkAnnotationFromUrl( this.value ),
internalLink = this.getInternalLinkAnnotationFromTitle( this.value ),
pageExists = this.existingPages[this.value],
matchingPages = this.matchingPages[this.value];
this.menu.clearItems();
// Hide on empty target
if ( !this.value.length ) {
this.menu.hide();
return;
}
// External links
this.menu.addGroup( 'externalLink', 'External link' );
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( this.value ) ) {
this.menu.addItem( 'externalLink', this.value, externalLink );
}
// Internal links
this.menu.addGroup( 'newPage', 'New page' );
this.menu.addGroup( 'existingPage', 'Existing page' );
this.menu.addGroup( 'matchingPage', 'Matching page' );
if ( !pageExists && ( !matchingPages || matchingPages.indexOf( this.value ) === -1 ) ) {
this.menu.addItem( 'newPage', this.value, internalLink );
}
if ( matchingPages ) {
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
internalLink = new ve.dm.MWInternalLinkAnnotation();
internalLink.data.title = matchingPages[i];
this.menu.addItem(
this.value === matchingPages[i] ? 'existingPage' : 'matchingPage',
matchingPages[i],
internalLink
);
}
this.previousMatches = matchingPages;
}
// Auto-select
this.menu.selectItemByData( this.annotation );
if ( !this.menu.getSelectedItem() ) {
this.menu.selectItemByIndex( 0 );
}
};
/**
* Signals that an response is pending.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.pushPending = function () {
this.pending++;
this.$.addClass( 've-ui-mwLinkTargetInputWidget-pending' );
};
/**
* Signals that an response is complete.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.popPending = function () {
this.pending--;
this.$.removeClass( 've-ui-mwLinkTargetInputWidget-pending' );
};
/**
* 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, annotation;
try {
title = new mw.Title( target );
if ( title.getNamespaceId() === 6 || title.getNamespaceId() === 14 ) {
target = ':' + target;
}
} catch ( e ) { }
annotation = new ve.dm.MWInternalLinkAnnotation();
annotation.data.title = target;
return annotation;
};
/**
* Gets an external link annotation.
*
* @method
* @param {string} target Web address
* @returns {ve.dm.MWExternalLinkAnnotation}
*/
ve.ui.MWLinkTargetInputWidget.prototype.getExternalLinkAnnotationFromUrl = function ( target ) {
var annotation = new ve.dm.MWExternalLinkAnnotation();
annotation.data.href = target;
return annotation;
};
/**
* 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.data.href;
} else if ( annotation instanceof ve.dm.MWInternalLinkAnnotation ) {
return annotation.data.title;
}
return '';
};
/**
* Checks page existence for the current value.
*
* {ve.ui.MWLinkTargetInputWidget.populateMenu} will be called immediately if the page existence has
* been cached, or as soon as the API returns a result.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.queryPageExistence = function () {
if ( this.existingPagesRequest ) {
this.existingPagesRequest.abort();
this.existingPagesRequest = null;
}
if ( this.value in this.existingPages ) {
this.populateMenu();
} else {
this.pushPending();
this.existingPagesRequest = $.ajax( {
'url': mw.util.wikiScript( 'api' ),
'data': {
'format': 'json',
'action': 'query',
'indexpageids': '',
'titles': this.value,
'converttitles': ''
},
'dataType': 'json',
'success': ve.bind( function ( data ) {
this.existingPagesRequest = null;
var page,
exists = false;
if ( data.query ) {
page = data.query.pages[data.query.pageids[0]];
exists = ( page.missing === undefined && page.invalid === undefined );
// Cache result for normalized title
this.existingPages[page.title] = exists;
}
// Cache result for original input
this.existingPages[this.value] = exists;
this.populateMenu();
}, this ),
'complete': ve.bind( function () {
this.popPending();
}, this )
} );
}
};
/**
* Checks matching pages for the current value.
*
* {ve.ui.MWLinkTargetInputWidget.populateMenu} will be called immediately if matching pages have
* been cached, or as soon as the API returns a result.
*
* @method
*/
ve.ui.MWLinkTargetInputWidget.prototype.queryMatchingPages = function () {
if ( this.matchingPagesRequest ) {
this.matchingPagesRequest.abort();
this.matchingPagesRequest = null;
}
if ( this.value in this.matchingPages ) {
this.populateMenu();
} else {
this.pushPending();
this.matchingPagesRequest = $.ajax( {
'url': mw.util.wikiScript( 'api' ),
'data': {
'format': 'json',
'action': 'opensearch',
'search': this.value,
'namespace': 0,
'suggest': ''
},
'dataType': 'json',
'success': ve.bind( function ( data ) {
this.matchingPagesRequest = null;
if ( ve.isArray( data ) && data.length ) {
// Cache the matches to the query
this.matchingPages[this.value] = data[1];
this.populateMenu();
}
}, this ),
'complete': ve.bind( function () {
this.popPending();
}, this )
} );
}
};