2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryInputWidget class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 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
|
2015-06-10 19:28:09 +00:00
|
|
|
* @mixins OO.ui.mixin.LookupElement
|
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
|
2021-09-11 08:05:31 +00:00
|
|
|
* @cfg {jQuery} [$overlay] Overlay to render dropdowns in
|
2018-05-04 13:30:10 +00:00
|
|
|
* @cfg {mw.Api} [api] API object to use, uses Target#getContentApi if not specified
|
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
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWCategoryInputWidget.super.call( this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2015-06-10 19:28:09 +00:00
|
|
|
OO.ui.mixin.LookupElement.call( this, config );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.categoryWidget = categoryWidget;
|
2018-05-04 13:30:10 +00:00
|
|
|
this.api = config.api || ve.init.target.getContentApi();
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2020-03-06 19:33:08 +00:00
|
|
|
this.$input.attr( 'aria-label', ve.msg( 'visualeditor-dialog-meta-categories-input-placeholder' ) );
|
|
|
|
|
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' );
|
2020-03-06 19:33:08 +00:00
|
|
|
this.lookupMenu.$element.attr( 'aria-label', ve.msg( 'visualeditor-dialog-meta-categories-data-label' ) );
|
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
|
|
|
|
2015-06-10 19:28:09 +00:00
|
|
|
OO.mixinClass( ve.ui.MWCategoryInputWidget, OO.ui.mixin.LookupElement );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
2015-03-27 10:51:06 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event choose
|
|
|
|
* A category was chosen
|
|
|
|
* @param {OO.ui.MenuOptionWidget} item Chosen item
|
|
|
|
*/
|
|
|
|
|
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 () {
|
2015-06-25 19:10:40 +00:00
|
|
|
var title = mw.Title.newFromText( this.value );
|
|
|
|
if ( title && title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).category ) {
|
|
|
|
title = title.getMainText();
|
|
|
|
} else {
|
|
|
|
title = this.value;
|
|
|
|
}
|
2018-03-16 11:30:57 +00:00
|
|
|
return this.api.get( {
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'query',
|
2014-09-25 19:59:41 +00:00
|
|
|
generator: 'allcategories',
|
|
|
|
gacmin: 1,
|
2015-06-25 19:10:40 +00:00
|
|
|
gacprefix: title,
|
2014-09-25 19:59:41 +00:00
|
|
|
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
|
|
|
*/
|
2015-01-15 22:07:34 +00:00
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
|
2015-01-31 00:41:37 +00:00
|
|
|
var result = [],
|
|
|
|
linkCacheUpdate = {},
|
|
|
|
query = data.query || {};
|
2014-09-09 19:49:27 +00:00
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
( query.pages || [] ).forEach( function ( categoryPage ) {
|
2014-09-25 19:59:41 +00:00
|
|
|
result.push( mw.Title.newFromText( categoryPage.title ).getMainText() );
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ categoryPage.title ] = {
|
2018-07-06 00:10:20 +00:00
|
|
|
missing: Object.prototype.hasOwnProperty.call( categoryPage, 'missing' ),
|
|
|
|
hidden: (
|
|
|
|
categoryPage.categoryinfo &&
|
|
|
|
Object.prototype.hasOwnProperty.call( categoryPage.categoryinfo, 'missing' )
|
|
|
|
)
|
2014-09-25 19:59:41 +00:00
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
( query.redirects || [] ).forEach( function ( redirect ) {
|
2018-07-06 00:10:20 +00:00
|
|
|
if ( !Object.prototype.hasOwnProperty.call( linkCacheUpdate, redirect.to ) ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ redirect.to ] = ve.init.platform.linkCache.getCached( redirect.to ) ||
|
2015-07-22 22:13:09 +00:00
|
|
|
{ missing: false, redirectFrom: [ redirect.from ] };
|
2014-09-25 19:59:41 +00:00
|
|
|
}
|
|
|
|
if (
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ redirect.to ].redirectFrom &&
|
|
|
|
linkCacheUpdate[ redirect.to ].redirectFrom.indexOf( redirect.from ) === -1
|
2014-09-25 19:59:41 +00:00
|
|
|
) {
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ redirect.to ].redirectFrom.push( redirect.from );
|
2014-09-25 19:59:41 +00:00
|
|
|
} else {
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ redirect.to ].redirectFrom = [ redirect.from ];
|
2014-09-25 19:59:41 +00:00
|
|
|
}
|
|
|
|
} );
|
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
|
|
|
*/
|
2015-01-15 22:07:34 +00:00
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
|
2014-12-16 21:14:01 +00:00
|
|
|
var widget = this,
|
|
|
|
exactMatch = false,
|
2014-09-09 19:49:27 +00:00
|
|
|
itemWidgets = [],
|
2015-01-31 00:41:37 +00:00
|
|
|
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 = {},
|
2014-10-15 20:07:39 +00:00
|
|
|
canonicalQueryValue = mw.Title.newFromText( this.value ),
|
|
|
|
prefixedCanonicalQueryValue = mw.Title.newFromText(
|
|
|
|
this.value,
|
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
);
|
|
|
|
|
|
|
|
prefixedCanonicalQueryValue = prefixedCanonicalQueryValue && prefixedCanonicalQueryValue.getPrefixedText();
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2014-12-05 20:07:57 +00:00
|
|
|
// Invalid titles end up with canonicalQueryValue being null.
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( canonicalQueryValue ) {
|
|
|
|
canonicalQueryValue = canonicalQueryValue.getMainText();
|
2014-12-05 20:07:57 +00:00
|
|
|
}
|
2014-02-28 18:17:11 +00:00
|
|
|
|
2020-08-18 12:16:49 +00:00
|
|
|
data.forEach( function ( suggestedCategory ) {
|
2014-10-15 20:07:39 +00:00
|
|
|
var suggestedCategoryTitle = mw.Title.newFromText(
|
|
|
|
suggestedCategory,
|
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
).getPrefixedText(),
|
|
|
|
suggestedCacheEntry = ve.init.platform.linkCache.getCached( suggestedCategoryTitle );
|
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 ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ suggestedCategoryTitle ] = { missing: false };
|
2014-09-20 00:59:22 +00:00
|
|
|
}
|
|
|
|
if (
|
2015-03-10 12:50:42 +00:00
|
|
|
existingCategories.indexOf( suggestedCategory ) === -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-12-16 21:14:01 +00:00
|
|
|
} );
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
// Existing categories
|
2020-08-18 12:16:49 +00:00
|
|
|
existingCategories.forEach( function ( existingCategory, i ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( existingCategory === canonicalQueryValue ) {
|
|
|
|
exactMatch = true;
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
2020-08-18 12:16:49 +00:00
|
|
|
if ( i < existingCategories.length - 1 && existingCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
// 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
|
2014-12-05 20:07:57 +00:00
|
|
|
if ( !exactMatch && canonicalQueryValue ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
newCategoryItems.push( canonicalQueryValue );
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ prefixedCanonicalQueryValue ] = { 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
|
2020-08-18 12:16:49 +00:00
|
|
|
[
|
2014-09-09 19:49:27 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2020-08-18 12:16:49 +00:00
|
|
|
].forEach( function ( sectionData ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
if ( sectionData.items.length ) {
|
2014-11-22 01:40:00 +00:00
|
|
|
itemWidgets.push( new OO.ui.MenuSectionOptionWidget( {
|
|
|
|
data: sectionData.id,
|
|
|
|
label: sectionData.label
|
|
|
|
} ) );
|
2020-08-18 12:16:49 +00:00
|
|
|
sectionData.items.forEach( function ( categoryItem ) {
|
2014-12-16 21:14:01 +00:00
|
|
|
itemWidgets.push( widget.getCategoryWidgetFromName( categoryItem ) );
|
|
|
|
} );
|
2014-02-28 18:17:11 +00:00
|
|
|
}
|
2014-12-16 21:14:01 +00:00
|
|
|
} );
|
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
|
|
|
};
|
|
|
|
|
2015-01-15 22:07:34 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2019-12-12 01:03:40 +00:00
|
|
|
ve.ui.MWCategoryInputWidget.prototype.onLookupMenuChoose = function ( item ) {
|
2015-01-15 22:07:34 +00:00
|
|
|
this.emit( 'choose', item );
|
|
|
|
|
|
|
|
// Reset input
|
|
|
|
this.setValue( '' );
|
|
|
|
};
|
|
|
|
|
2014-09-25 19:59:41 +00:00
|
|
|
/**
|
|
|
|
* Take a category name and turn it into a menu item widget, following redirects.
|
|
|
|
*
|
|
|
|
* @param {string} name Category name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {OO.ui.MenuOptionWidget} Menu item widget to be shown
|
2014-09-25 19:59:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getCategoryWidgetFromName = function ( name ) {
|
2016-07-14 22:16:35 +00:00
|
|
|
var cachedData = ve.init.platform.linkCache.getCached( mw.Title.newFromText(
|
|
|
|
name,
|
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
).getPrefixedText() ),
|
|
|
|
optionWidget, labelText;
|
2014-09-25 19:59:41 +00:00
|
|
|
if ( cachedData && cachedData.redirectFrom ) {
|
2016-07-14 22:16:35 +00:00
|
|
|
labelText = mw.Title.newFromText( cachedData.redirectFrom[ 0 ] ).getMainText();
|
2017-05-19 09:57:17 +00:00
|
|
|
optionWidget = new OO.ui.MenuOptionWidget( {
|
2014-11-22 01:40:00 +00:00
|
|
|
data: name,
|
2014-09-25 19:59:41 +00:00
|
|
|
autoFitLabel: false,
|
2015-04-09 23:47:15 +00:00
|
|
|
label: $( '<span>' )
|
2016-07-14 22:16:35 +00:00
|
|
|
.text( labelText )
|
2019-10-02 23:54:54 +00:00
|
|
|
.append(
|
|
|
|
$( '<br>' ),
|
|
|
|
document.createTextNode( '↳ ' ),
|
|
|
|
$( '<span>' ).text( mw.Title.newFromText( name ).getMainText() )
|
|
|
|
)
|
2014-09-25 19:59:41 +00:00
|
|
|
} );
|
|
|
|
} else {
|
2016-07-14 22:16:35 +00:00
|
|
|
labelText = name;
|
|
|
|
optionWidget = new OO.ui.MenuOptionWidget( {
|
2014-11-22 01:40:00 +00:00
|
|
|
data: name,
|
|
|
|
label: name
|
|
|
|
} );
|
2014-09-25 19:59:41 +00:00
|
|
|
}
|
2016-07-14 22:16:35 +00:00
|
|
|
optionWidget.$element.attr( 'title', labelText );
|
|
|
|
return optionWidget;
|
2014-09-25 19:59:41 +00:00
|
|
|
};
|