2013-04-29 21:01:56 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWCategoryInputWidget class.
|
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 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
|
2018-03-16 11:30:57 +00:00
|
|
|
* @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance 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-03-16 11:30:57 +00:00
|
|
|
this.api = config.api || new mw.Api();
|
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
|
|
|
|
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
|
|
|
|
2014-09-25 19:59:41 +00:00
|
|
|
$.each( query.pages || [], function ( pageId, categoryPage ) {
|
|
|
|
result.push( mw.Title.newFromText( categoryPage.title ).getMainText() );
|
2015-08-19 17:33:02 +00:00
|
|
|
linkCacheUpdate[ categoryPage.title ] = {
|
2014-09-25 19:59:41 +00:00
|
|
|
missing: categoryPage.hasOwnProperty( 'missing' ),
|
|
|
|
hidden: categoryPage.categoryinfo && categoryPage.categoryinfo.hasOwnProperty( 'hidden' )
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
$.each( query.redirects || [], function ( index, redirect ) {
|
|
|
|
if ( !linkCacheUpdate.hasOwnProperty( 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
|
|
|
|
2014-09-09 19:49:27 +00:00
|
|
|
$.each( data, function ( index, 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
|
|
|
|
$.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
|
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
|
|
|
|
$.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( {
|
|
|
|
data: sectionData.id,
|
|
|
|
label: sectionData.label
|
|
|
|
} ) );
|
2014-09-09 19:49:27 +00:00
|
|
|
$.each( sectionData.items, function ( index, 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
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @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 )
|
2014-09-25 19:59:41 +00:00
|
|
|
.append( '<br>↳ ' )
|
2015-04-09 23:47:15 +00:00
|
|
|
.append( $( '<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
|
|
|
};
|