2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryInputWidget class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-04-29 21:01:56 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2014-11-21 13:00:50 +00:00
|
|
|
// Config initialization
|
2013-04-29 21:01:56 +00:00
|
|
|
config = ve.extendObject( {
|
2014-08-22 20:50:48 +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
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.addClass( 've-ui-mwCategoryInputWidget' );
|
|
|
|
this.lookupMenu.$element.addClass( 've-ui-mwCategoryInputWidget-menu' );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
|
2014-05-23 09:57:58 +00:00
|
|
|
return ve.init.target.constructor.static.apiRequest( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'query',
|
2014-09-25 19:59:41 +00:00
|
|
|
generator: 'allcategories',
|
|
|
|
gacmin: 1,
|
|
|
|
gacprefix: this.value,
|
|
|
|
prop: 'categoryinfo',
|
|
|
|
redirects: ''
|
2014-04-29 12:24:56 +00:00
|
|
|
} );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
|
2014-10-31 19:08:02 +00:00
|
|
|
var result = [], linkCacheUpdate = {}, query = data.query || {};
|
2014-09-09 19:49:27 +00:00
|
|
|
|
2014-09-25 19:59:41 +00:00
|
|
|
$.each( query.pages || [], function ( pageId, categoryPage ) {
|
|
|
|
result.push( mw.Title.newFromText( categoryPage.title ).getMainText() );
|
|
|
|
linkCacheUpdate[categoryPage.title] = {
|
|
|
|
missing: categoryPage.hasOwnProperty( 'missing' ),
|
|
|
|
hidden: categoryPage.categoryinfo && categoryPage.categoryinfo.hasOwnProperty( 'hidden' )
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
$.each( query.redirects || [], function ( index, redirect ) {
|
|
|
|
if ( !linkCacheUpdate.hasOwnProperty( redirect.to ) ) {
|
|
|
|
linkCacheUpdate[redirect.to] = ve.init.platform.linkCache.getCached( redirect.to ) ||
|
|
|
|
{ missing: false, redirectFrom: [redirect.from] };
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
linkCacheUpdate[redirect.to].redirectFrom &&
|
|
|
|
linkCacheUpdate[redirect.to].redirectFrom.indexOf( redirect.from ) === -1
|
|
|
|
) {
|
|
|
|
linkCacheUpdate[redirect.to].redirectFrom.push( redirect.from );
|
|
|
|
} else {
|
|
|
|
linkCacheUpdate[redirect.to].redirectFrom = [redirect.from];
|
|
|
|
}
|
|
|
|
} );
|
2014-09-09 19:49:27 +00:00
|
|
|
|
2014-09-20 00:59:22 +00:00
|
|
|
ve.init.platform.linkCache.set( linkCacheUpdate );
|
|
|
|
|
2013-06-11 18:54:39 +00:00
|
|
|
return result;
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-10-31 19:08:02 +00:00
|
|
|
* @inheritdoc
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
var exactMatch = false,
|
|
|
|
itemWidgets = [],
|
|
|
|
existingCategoryItems = [], matchingCategoryItems = [],
|
|
|
|
hiddenCategoryItems = [], newCategoryItems = [],
|
2013-04-29 21:01:56 +00:00
|
|
|
existingCategories = this.categoryWidget.getCategories(),
|
2014-09-09 19:49:27 +00:00
|
|
|
linkCacheUpdate = {},
|
|
|
|
canonicalQueryValue = mw.Title.newFromText( this.value );
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( canonicalQueryValue ) {
|
|
|
|
canonicalQueryValue = canonicalQueryValue.getMainText();
|
|
|
|
} // Invalid titles just end up with canonicalQueryValue being null.
|
2014-02-28 18:17:11 +00:00
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
$.each( data, function ( index, suggestedCategory ) {
|
2014-09-20 00:59:22 +00:00
|
|
|
var suggestedCacheEntry = ve.init.platform.linkCache.getCached( 'Category:' + suggestedCategory );
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( canonicalQueryValue === suggestedCategory ) {
|
|
|
|
exactMatch = true;
|
2014-02-28 18:17:11 +00:00
|
|
|
}
|
2014-09-20 00:59:22 +00:00
|
|
|
if ( !suggestedCacheEntry ) {
|
|
|
|
linkCacheUpdate['Category:' + suggestedCategory] = { missing: false };
|
|
|
|
}
|
|
|
|
if (
|
2014-09-25 19:59:41 +00:00
|
|
|
ve.indexOf( suggestedCategory, existingCategories ) === -1
|
2014-09-20 00:59:22 +00:00
|
|
|
) {
|
|
|
|
if ( suggestedCacheEntry && suggestedCacheEntry.hidden ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
hiddenCategoryItems.push( suggestedCategory );
|
|
|
|
} else {
|
|
|
|
matchingCategoryItems.push( suggestedCategory );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-09 19:49:27 +00:00
|
|
|
}.bind( this ) );
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
// Existing categories
|
|
|
|
$.each( existingCategories, function ( index, existingCategory ) {
|
|
|
|
if ( existingCategory === canonicalQueryValue ) {
|
|
|
|
exactMatch = true;
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( index < existingCategories.length - 1 && existingCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) {
|
|
|
|
// Verify that item starts with category.value
|
|
|
|
existingCategoryItems.push( existingCategory );
|
2014-02-28 18:17:11 +00:00
|
|
|
}
|
2014-09-09 19:49:27 +00:00
|
|
|
} );
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
// New category
|
|
|
|
if ( !exactMatch ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
newCategoryItems.push( canonicalQueryValue );
|
2014-09-20 00:59:22 +00:00
|
|
|
linkCacheUpdate['Category:' + canonicalQueryValue] = { missing: true };
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
ve.init.platform.linkCache.set( linkCacheUpdate );
|
|
|
|
|
|
|
|
// Add sections for non-empty groups. Each section consists of an id, a label and items
|
|
|
|
$.each( [
|
|
|
|
{
|
|
|
|
id: 'newCategory',
|
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ),
|
|
|
|
items: newCategoryItems
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'inArticle',
|
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ),
|
|
|
|
items: existingCategoryItems
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'matchingCategories',
|
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ),
|
|
|
|
items: matchingCategoryItems
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'hiddenCategories',
|
|
|
|
label: ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ),
|
|
|
|
items: hiddenCategoryItems
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2014-09-09 19:49:27 +00:00
|
|
|
], function ( index, sectionData ) {
|
|
|
|
if ( sectionData.items.length ) {
|
2014-11-22 01:40:00 +00:00
|
|
|
itemWidgets.push( new OO.ui.MenuSectionOptionWidget( {
|
|
|
|
$: this.lookupMenu.$,
|
|
|
|
data: sectionData.id,
|
|
|
|
label: sectionData.label
|
|
|
|
} ) );
|
2014-09-09 19:49:27 +00:00
|
|
|
$.each( sectionData.items, function ( index, categoryItem ) {
|
2014-09-25 19:59:41 +00:00
|
|
|
itemWidgets.push( this.getCategoryWidgetFromName( categoryItem ) );
|
2014-09-09 19:49:27 +00:00
|
|
|
}.bind( this ) );
|
2014-02-28 18:17:11 +00:00
|
|
|
}
|
2014-09-09 19:49:27 +00:00
|
|
|
}.bind( this ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
return itemWidgets;
|
2014-03-11 19:02:16 +00:00
|
|
|
};
|
|
|
|
|
2014-09-25 19:59:41 +00:00
|
|
|
/**
|
|
|
|
* Take a category name and turn it into a menu item widget, following redirects.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Category name
|
2014-11-20 01:02:44 +00:00
|
|
|
* @returns {OO.ui.MenuOptionWidget} Menu item widget to be shown
|
2014-09-25 19:59:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getCategoryWidgetFromName = function ( name ) {
|
|
|
|
var cachedData = ve.init.platform.linkCache.getCached(
|
|
|
|
mw.Title.newFromText( name, mw.config.get( 'wgNamespaceIds' ).category ).getPrefixedText()
|
|
|
|
);
|
|
|
|
if ( cachedData && cachedData.redirectFrom ) {
|
2014-11-22 01:40:00 +00:00
|
|
|
return new OO.ui.MenuOptionWidget( {
|
2014-09-25 19:59:41 +00:00
|
|
|
$: this.lookupMenu.$,
|
2014-11-22 01:40:00 +00:00
|
|
|
data: name,
|
2014-09-25 19:59:41 +00:00
|
|
|
autoFitLabel: false,
|
|
|
|
label: this.$( '<span>' )
|
|
|
|
.text( mw.Title.newFromText( cachedData.redirectFrom[0] ).getMainText() )
|
|
|
|
.append( '<br>↳ ' )
|
|
|
|
.append( this.$( '<span>' ).text( mw.Title.newFromText( name ).getMainText() ) )
|
|
|
|
} );
|
|
|
|
} else {
|
2014-11-22 01:40:00 +00:00
|
|
|
return new OO.ui.MenuOptionWidget( {
|
|
|
|
$: this.lookupMenu.$,
|
|
|
|
data: name,
|
|
|
|
label: name
|
|
|
|
} );
|
2014-09-25 19:59:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2014-08-22 20:50:48 +00:00
|
|
|
name: title.getPrefixedText(),
|
|
|
|
value: title.getMainText(),
|
|
|
|
metaItem: {}
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
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 {
|
2014-08-22 20:50:48 +00:00
|
|
|
name: this.categoryPrefix + value,
|
|
|
|
value: value,
|
|
|
|
metaItem: {}
|
2013-10-09 21:20:51 +00:00
|
|
|
};
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|