2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryInputWidget class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
/*global mw */
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWCategoryInputWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.TextInputWidget
|
|
|
|
* @mixins OO.ui.LookupInputWidget
|
2013-04-29 21:01:56 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-05-21 01:27:38 +00:00
|
|
|
* @param {ve.ui.MWCategoryWidget} categoryWidget
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget = function VeUiMWCategoryInputWidget( categoryWidget, config ) {
|
|
|
|
// Config intialization
|
|
|
|
config = ve.extendObject( {
|
2013-05-25 10:01:32 +00:00
|
|
|
'placeholder': ve.msg( 'visualeditor-dialog-meta-categories-input-placeholder' )
|
2013-04-29 21:01:56 +00:00
|
|
|
}, config );
|
|
|
|
|
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.TextInputWidget.call( this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.LookupInputWidget.call( this, this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.categoryWidget = categoryWidget;
|
|
|
|
this.forceCapitalization = mw.config.get( 'wgCaseSensitiveNamespaces' ).indexOf( 14 ) === -1;
|
2013-06-11 18:54:39 +00:00
|
|
|
this.categoryPrefix = mw.config.get( 'wgFormattedNamespaces' )['14'] + ':';
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$.addClass( 've-ui-mwCategoryInputWidget' );
|
|
|
|
this.lookupMenu.$.addClass( 've-ui-mwCategoryInputWidget-menu' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWCategoryInputWidget, OO.ui.TextInputWidget );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.mixinClass( ve.ui.MWCategoryInputWidget, OO.ui.LookupInputWidget );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a new request object of the current lookup query value.
|
|
|
|
*
|
|
|
|
* @method
|
2013-06-18 17:56:56 +00:00
|
|
|
* @returns {jqXHR} AJAX object without success or fail handlers attached
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
|
|
|
|
return $.ajax( {
|
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'data': {
|
|
|
|
'format': 'json',
|
|
|
|
'action': 'opensearch',
|
|
|
|
'search': this.categoryPrefix + this.value,
|
|
|
|
'suggest': ''
|
|
|
|
},
|
|
|
|
'dataType': 'json'
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lookup cache item from server response data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Mixed} data Response from server
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
|
2013-06-11 18:54:39 +00:00
|
|
|
var i, len, title, result = [];
|
|
|
|
if ( ve.isArray( data ) && data.length ) {
|
|
|
|
for ( i = 0, len = data[1].length; i < len; i++ ) {
|
2013-10-09 21:20:51 +00:00
|
|
|
title = mw.Title.newFromText( data[1][i] );
|
|
|
|
if ( title ) {
|
2013-07-10 22:47:37 +00:00
|
|
|
result.push( title.getMainText() );
|
2013-10-09 21:20:51 +00:00
|
|
|
}
|
2013-06-11 18:54:39 +00:00
|
|
|
// If the received title isn't valid, just ignore it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of menu items from a server response.
|
|
|
|
*
|
|
|
|
* @param {Object} data Query result
|
2013-10-09 20:09:59 +00:00
|
|
|
* @returns {OO.ui.MenuItemWidget[]} Menu items
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
|
|
|
|
var i, len, item,
|
|
|
|
exactMatch = false,
|
|
|
|
newCategoryItems = [],
|
|
|
|
existingCategoryItems = [],
|
|
|
|
matchingCategoryItems = [],
|
|
|
|
items = [],
|
|
|
|
menu$$ = this.lookupMenu.$$,
|
|
|
|
category = this.getCategoryItemFromValue( this.value ),
|
|
|
|
existingCategories = this.categoryWidget.getCategories(),
|
2013-06-11 18:54:39 +00:00
|
|
|
matchingCategories = data || [];
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Existing categories
|
2013-07-01 23:01:34 +00:00
|
|
|
for ( i = 0, len = existingCategories.length - 1; i < len; i++ ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
item = existingCategories[i];
|
2013-06-11 18:54:39 +00:00
|
|
|
// Verify that item starts with category.value
|
|
|
|
if ( item.lastIndexOf( category.value, 0 ) === 0 ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
if ( item === category.value ) {
|
|
|
|
exactMatch = true;
|
|
|
|
}
|
|
|
|
existingCategoryItems.push( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Matching categories
|
|
|
|
for ( i = 0, len = matchingCategories.length; i < len; i++ ) {
|
|
|
|
item = matchingCategories[i];
|
|
|
|
if (
|
2013-07-01 23:01:34 +00:00
|
|
|
ve.indexOf( item, existingCategories ) === -1 &&
|
2013-06-11 18:54:39 +00:00
|
|
|
item.lastIndexOf( category.value, 0 ) === 0
|
2013-04-29 21:01:56 +00:00
|
|
|
) {
|
|
|
|
if ( item === category.value ) {
|
|
|
|
exactMatch = true;
|
|
|
|
}
|
|
|
|
matchingCategoryItems.push( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// New category
|
|
|
|
if ( !exactMatch ) {
|
|
|
|
newCategoryItems.push( category.value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add sections for non-empty groups
|
|
|
|
if ( newCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-05-25 10:01:32 +00:00
|
|
|
'newCategory', { '$$': menu$$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
|
|
|
for ( i = 0, len = newCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = newCategoryItems[i];
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget( item, { '$$': menu$$, 'label': item } ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( existingCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-05-25 10:01:32 +00:00
|
|
|
'inArticle', { '$$': menu$$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
|
|
|
for ( i = 0, len = existingCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = existingCategoryItems[i];
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget( item, { '$$': menu$$, 'label': item } ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( matchingCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-05-25 10:01:32 +00:00
|
|
|
'matchingCategories', { '$$': menu$$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
2013-06-12 01:52:39 +00:00
|
|
|
for ( i = 0, len = matchingCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = matchingCategoryItems[i];
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget( item, { '$$': menu$$, 'label': item } ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a category item.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} value Category name
|
|
|
|
* @returns {Object} Category item with name, value and metaItem properties
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getCategoryItemFromValue = function ( value ) {
|
|
|
|
var title;
|
|
|
|
|
|
|
|
// Normalize
|
2013-10-09 21:20:51 +00:00
|
|
|
title = mw.Title.newFromText( this.categoryPrefix + value );
|
|
|
|
if ( title ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
return {
|
|
|
|
'name': title.getPrefixedText(),
|
2013-07-10 22:47:37 +00:00
|
|
|
'value': title.getMainText(),
|
2013-04-29 21:01:56 +00:00
|
|
|
'metaItem': {}
|
|
|
|
};
|
2013-10-09 21:20:51 +00:00
|
|
|
}
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
if ( this.forceCapitalization ) {
|
|
|
|
value = value.substr( 0, 1 ).toUpperCase() + value.substr( 1 );
|
|
|
|
}
|
|
|
|
|
2013-10-09 21:20:51 +00:00
|
|
|
return {
|
|
|
|
'name': this.categoryPrefix + value,
|
|
|
|
'value': value,
|
|
|
|
'metaItem': {}
|
|
|
|
};
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|