mediawiki-extensions-Visual.../modules/ve-mw/ui/widgets/ve.ui.MWTitleInputWidget.js
Trevor Parscal db3da5d16b Link inspector bug fixes
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
2013-07-31 17:42:14 +00:00

133 lines
3.2 KiB
JavaScript

/*!
* VisualEditor UserInterface MWTitleInputWidget 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.MWTitleInputWidget object.
*
* @class
* @extends ve.ui.TextInputWidget
* @mixins ve.ui.LookupInputWidget
*
* @constructor
* @param {Object} [config] Config options
* @param {number} [namespace] Namespace to prepend to queries not prefixed with ':'
*/
ve.ui.MWTitleInputWidget = function VeUiMWTitleInputWidget( config ) {
// Config intialization
config = config || {};
// Parent constructor
ve.ui.TextInputWidget.call( this, config );
// Mixin constructors
ve.ui.LookupInputWidget.call( this, this, config );
// Properties
this.namespace = config.namespace || null;
// Events
this.lookupMenu.connect( this, { 'select': 'onLookupMenuItemSelect' } );
// Initialization
this.$.addClass( 've-ui-mwTitleInputWidget' );
this.lookupMenu.$.addClass( 've-ui-mwTitleInputWidget-menu' );
};
/* Inheritance */
ve.inheritClass( ve.ui.MWTitleInputWidget, ve.ui.TextInputWidget );
ve.mixinClass( ve.ui.MWTitleInputWidget, ve.ui.LookupInputWidget );
/* Methods */
/**
* Handle menu item select event.
*
* @method
* @param {ve.ui.MenuItemWidget} item Selected item
*/
ve.ui.MWTitleInputWidget.prototype.onLookupMenuItemSelect = function ( item ) {
if ( item ) {
this.setValue( item.getData() );
}
};
/**
* Gets a new request object of the current lookup query value.
*
* @method
* @returns {jQuery.Deferred} Deferred object with success and fail handlers already attached
*/
ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
var value = this.value;
// Prefix with default namespace name
if ( this.namespace !== null && value.charAt( 0 ) !== ':' ) {
value = mw.config.get( 'wgFormattedNamespaces' )[this.namespace] + ':' + value;
}
// Dont send leading ':' to open search
if ( value.charAt( 0 ) === ':' ) {
value = value.substr( 1 );
}
return $.ajax( {
'url': mw.util.wikiScript( 'api' ),
'data': {
'format': 'json',
'action': 'opensearch',
'search': value,
'suggest': ''
},
'dataType': 'json'
} );
};
/**
* Get lookup cache item from server response data.
*
* @method
* @param {Mixed} data Response from server
*/
ve.ui.MWTitleInputWidget.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.MWTitleInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
var i, len, title, value,
menu$$ = this.lookupMenu.$$,
items = [],
matchingPages = data;
// Matching pages
if ( matchingPages && matchingPages.length ) {
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
title = new mw.Title( matchingPages[i] );
if ( this.namespace !== null ) {
value = title.getNamespaceId() === this.namespace ?
title.getNameText() : ':' + title.getPrefixedText();
} else {
value = title.getPrefixedText();
}
items.push( new ve.ui.MenuItemWidget(
value, { '$$': menu$$, 'label': value }
) );
}
}
return items;
};