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 ) {
|
|
|
|
// Config intialization
|
|
|
|
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',
|
|
|
|
list: 'allcategories',
|
2014-08-26 09:17:50 +00:00
|
|
|
acmin: 1,
|
2014-08-22 20:50:48 +00:00
|
|
|
acprefix: this.value,
|
2014-09-09 19:49:27 +00:00
|
|
|
acprop: 'hidden'
|
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-10-31 19:08:02 +00:00
|
|
|
$.each( query.allcategories || [], function ( index, category ) {
|
2014-09-09 19:49:27 +00:00
|
|
|
result.push( category['*'] );
|
2014-11-05 20:18:24 +00:00
|
|
|
linkCacheUpdate['Category:' + category['*']] = { missing: false, hidden: 'hidden' in category };
|
2014-09-09 19:49:27 +00:00
|
|
|
}.bind( this ) );
|
|
|
|
|
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 (
|
|
|
|
ve.indexOf( suggestedCategory, existingCategories ) === -1 &&
|
|
|
|
suggestedCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0
|
|
|
|
) {
|
|
|
|
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 ) {
|
|
|
|
itemWidgets.push( new OO.ui.MenuSectionItemWidget(
|
|
|
|
sectionData.id, { $: this.lookupMenu.$, label: sectionData.label }
|
|
|
|
) );
|
|
|
|
$.each( sectionData.items, function ( index, categoryItem ) {
|
|
|
|
itemWidgets.push( new OO.ui.MenuItemWidget( categoryItem, { $: this.lookupMenu.$, label: categoryItem } ) );
|
|
|
|
}.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
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|