2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryInputWidget class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright 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
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes 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
|
2024-05-27 04:59:02 +00:00
|
|
|
* @param {jQuery} [config.$overlay] Overlay to render dropdowns in
|
|
|
|
* @param {mw.Api} [config.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 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A category was chosen
|
2024-04-30 09:14:36 +00:00
|
|
|
*
|
|
|
|
* @event ve.ui.MWCategoryInputWidget#choose
|
2015-03-27 10:51:06 +00:00
|
|
|
* @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 () {
|
2024-05-21 14:22:56 +00:00
|
|
|
let title = mw.Title.newFromText( this.value );
|
2015-06-25 19:10:40 +00:00
|
|
|
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 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const result = [],
|
2015-01-31 00:41:37 +00:00
|
|
|
linkCacheUpdate = {},
|
|
|
|
query = data.query || {};
|
2014-09-09 19:49:27 +00:00
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
( query.pages || [] ).forEach( ( 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
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
( query.redirects || [] ).forEach( ( 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 ) {
|
2024-05-21 16:40:36 +00:00
|
|
|
const 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(),
|
2024-05-21 16:40:36 +00:00
|
|
|
linkCacheUpdate = {};
|
|
|
|
|
|
|
|
let canonicalQueryValue = mw.Title.newFromText( this.value ),
|
2014-10-15 20:07:39 +00:00
|
|
|
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
|
|
|
|
2024-05-21 16:40:36 +00:00
|
|
|
let exactMatch = false;
|
2024-04-30 16:44:25 +00:00
|
|
|
data.forEach( ( suggestedCategory ) => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const suggestedCategoryTitle = mw.Title.newFromText(
|
2014-10-15 20:07:39 +00:00
|
|
|
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
|
2024-04-30 16:44:25 +00:00
|
|
|
existingCategories.forEach( ( 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
|
|
|
}
|
2024-04-30 16:44:25 +00:00
|
|
|
].forEach( ( 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
|
|
|
|
} ) );
|
2024-04-30 16:44:25 +00:00
|
|
|
sectionData.items.forEach( ( categoryItem ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
itemWidgets.push( this.getCategoryWidgetFromName( categoryItem ) );
|
2014-12-16 21:14:01 +00:00
|
|
|
} );
|
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
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.ui.MWCategoryInputWidget#choose
|
2015-01-15 22:07:34 +00:00
|
|
|
*/
|
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 ) {
|
2024-05-21 16:40:36 +00:00
|
|
|
const cachedData = ve.init.platform.linkCache.getCached( mw.Title.newFromText(
|
|
|
|
name,
|
|
|
|
mw.config.get( 'wgNamespaceIds' ).category
|
|
|
|
).getPrefixedText() );
|
|
|
|
let 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>' ),
|
2021-10-25 15:51:29 +00:00
|
|
|
$( document.createTextNode( '↳ ' ) ),
|
2019-10-02 23:54:54 +00:00
|
|
|
$( '<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
|
|
|
};
|