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
|
|
|
|
*/
|
|
|
|
|
2013-05-21 01:27:38 +00:00
|
|
|
/*global mw */
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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( {
|
2013-05-25 10:01:32 +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' );
|
2014-03-11 19:02:16 +00:00
|
|
|
this.categoryRedirects = [];
|
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 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a new request object of the current lookup query value.
|
|
|
|
*
|
|
|
|
* @method
|
2013-06-18 17:56:56 +00:00
|
|
|
* @returns {jqXHR} AJAX object without success or fail handlers attached
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
|
2014-02-28 18:17:11 +00:00
|
|
|
var propsJqXhr,
|
|
|
|
searchJqXhr = ve.init.mw.Target.static.apiRequest( {
|
|
|
|
'action': 'opensearch',
|
|
|
|
'search': this.categoryPrefix + this.value,
|
|
|
|
'suggest': ''
|
|
|
|
} );
|
|
|
|
|
|
|
|
return searchJqXhr.then( function ( data ) {
|
|
|
|
propsJqXhr = ve.init.mw.Target.static.apiRequest( {
|
|
|
|
'action': 'query',
|
|
|
|
'prop': 'pageprops',
|
|
|
|
'titles': ( data[1] || [] ).join( '|' ),
|
2014-03-11 19:02:16 +00:00
|
|
|
'ppprop': 'hiddencat',
|
|
|
|
'redirects': ''
|
2014-02-28 18:17:11 +00:00
|
|
|
} );
|
|
|
|
return propsJqXhr;
|
|
|
|
} ).promise( { abort: function () {
|
|
|
|
searchJqXhr.abort();
|
|
|
|
|
|
|
|
if ( propsJqXhr ) {
|
|
|
|
propsJqXhr.abort();
|
|
|
|
}
|
|
|
|
} } );
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lookup cache item from server response data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Mixed} data Response from server
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
|
2014-03-11 19:02:16 +00:00
|
|
|
var categoryInputWidget = this, result = {};
|
2014-02-28 18:17:11 +00:00
|
|
|
if ( data.query && data.query.pages ) {
|
|
|
|
$.each( data.query.pages, function ( pageId, pageInfo ) {
|
|
|
|
var title = mw.Title.newFromText( pageInfo.title );
|
2013-10-09 21:20:51 +00:00
|
|
|
if ( title ) {
|
2014-02-28 18:17:11 +00:00
|
|
|
result[title.getMainText()] = !!( pageInfo.pageprops && pageInfo.pageprops.hiddencat !== undefined );
|
2014-03-11 19:02:16 +00:00
|
|
|
categoryInputWidget.categoryWidget.categoryHiddenStatus[pageInfo.title] = result[title.getMainText()];
|
2014-02-28 18:17:11 +00:00
|
|
|
} else {
|
|
|
|
mw.log.warning( '"' + pageInfo.title + '" is an invalid title!' );
|
2013-10-09 21:20:51 +00:00
|
|
|
}
|
2014-02-28 18:17:11 +00:00
|
|
|
} );
|
2013-06-11 18:54:39 +00:00
|
|
|
}
|
2014-03-11 19:02:16 +00:00
|
|
|
if ( data.query && data.query.redirects ) {
|
|
|
|
$.each( data.query.redirects, function ( index, redirectInfo ) {
|
|
|
|
var foundIdentical = false;
|
|
|
|
$.each( categoryInputWidget.categoryRedirects, function ( index, existingRedirectInfo ) {
|
|
|
|
if ( existingRedirectInfo.from === redirectInfo.from && existingRedirectInfo.to === redirectInfo.to ) {
|
|
|
|
foundIdentical = true;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
if ( !foundIdentical ) {
|
|
|
|
categoryInputWidget.categoryRedirects.push( redirectInfo );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2013-06-11 18:54:39 +00:00
|
|
|
return result;
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of menu items from a server response.
|
|
|
|
*
|
|
|
|
* @param {Object} data Query result
|
2013-10-09 20:09:59 +00:00
|
|
|
* @returns {OO.ui.MenuItemWidget[]} Menu items
|
2013-04-29 21:01:56 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
|
|
|
|
var i, len, item,
|
|
|
|
exactMatch = false,
|
|
|
|
newCategoryItems = [],
|
|
|
|
existingCategoryItems = [],
|
|
|
|
matchingCategoryItems = [],
|
2014-02-28 18:17:11 +00:00
|
|
|
hiddenCategoryItems = [],
|
2013-04-29 21:01:56 +00:00
|
|
|
items = [],
|
2013-11-01 19:45:59 +00:00
|
|
|
menu$ = this.lookupMenu.$,
|
2013-04-29 21:01:56 +00:00
|
|
|
category = this.getCategoryItemFromValue( this.value ),
|
|
|
|
existingCategories = this.categoryWidget.getCategories(),
|
2014-02-28 18:17:11 +00:00
|
|
|
matchingCategories = [],
|
2014-03-11 19:02:16 +00:00
|
|
|
hiddenCategories = [],
|
|
|
|
redirectStorage = {},
|
|
|
|
itemTitle,
|
|
|
|
searchForQueryWithinRedirectInfo = function ( element ) {
|
|
|
|
return element.lastIndexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText(), 0 ) === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
$.each( this.categoryRedirects, function () {
|
|
|
|
if ( redirectStorage.hasOwnProperty( this.to ) && redirectStorage[this.to].indexOf( this.from ) === -1 ) {
|
|
|
|
redirectStorage[this.to].push( this.from );
|
|
|
|
} else {
|
|
|
|
redirectStorage[this.to] = [this.from];
|
|
|
|
}
|
|
|
|
} );
|
2014-02-28 18:17:11 +00:00
|
|
|
|
|
|
|
$.each( data, function ( title, hiddenStatus ) {
|
|
|
|
if ( hiddenStatus ) {
|
|
|
|
hiddenCategories.push( title );
|
|
|
|
} else {
|
|
|
|
matchingCategories.push( title );
|
|
|
|
}
|
|
|
|
} );
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
// Existing categories
|
2013-07-01 23:01:34 +00:00
|
|
|
for ( i = 0, len = existingCategories.length - 1; i < len; i++ ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
item = existingCategories[i];
|
2013-06-11 18:54:39 +00:00
|
|
|
// Verify that item starts with category.value
|
|
|
|
if ( item.lastIndexOf( category.value, 0 ) === 0 ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
if ( item === category.value ) {
|
|
|
|
exactMatch = true;
|
|
|
|
}
|
|
|
|
existingCategoryItems.push( item );
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
// Matching categories
|
|
|
|
for ( i = 0, len = matchingCategories.length; i < len; i++ ) {
|
|
|
|
item = matchingCategories[i];
|
2014-03-11 19:02:16 +00:00
|
|
|
itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText();
|
2013-04-29 21:01:56 +00:00
|
|
|
if (
|
2013-07-01 23:01:34 +00:00
|
|
|
ve.indexOf( item, existingCategories ) === -1 &&
|
2014-03-11 19:02:16 +00:00
|
|
|
item.lastIndexOf( category.value, 0 ) === 0 || (
|
|
|
|
redirectStorage[itemTitle] !== undefined &&
|
|
|
|
$.grep( redirectStorage[itemTitle], searchForQueryWithinRedirectInfo ).length
|
|
|
|
)
|
2013-04-29 21:01:56 +00:00
|
|
|
) {
|
2014-03-11 19:02:16 +00:00
|
|
|
if ( ( item === category.value ) || (
|
|
|
|
redirectStorage[itemTitle] !== undefined &&
|
|
|
|
redirectStorage[itemTitle].indexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText() ) !== -1
|
|
|
|
) ) {
|
2013-04-29 21:01:56 +00:00
|
|
|
exactMatch = true;
|
|
|
|
}
|
|
|
|
matchingCategoryItems.push( item );
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 18:17:11 +00:00
|
|
|
// Hidden categories
|
|
|
|
for ( i = 0, len = hiddenCategories.length; i < len; i++ ) {
|
|
|
|
item = hiddenCategories[i];
|
2014-03-11 19:02:16 +00:00
|
|
|
itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText();
|
2014-02-28 18:17:11 +00:00
|
|
|
if (
|
|
|
|
ve.indexOf( item, existingCategories ) === -1 &&
|
2014-03-11 19:02:16 +00:00
|
|
|
item.lastIndexOf( category.value, 0 ) === 0 || (
|
|
|
|
redirectStorage[itemTitle] !== undefined &&
|
|
|
|
$.grep( redirectStorage[itemTitle], searchForQueryWithinRedirectInfo ).length
|
|
|
|
)
|
2014-02-28 18:17:11 +00:00
|
|
|
) {
|
2014-03-11 19:02:16 +00:00
|
|
|
if ( ( item === category.value ) || (
|
|
|
|
redirectStorage[itemTitle] !== undefined &&
|
|
|
|
redirectStorage[itemTitle].indexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText() ) !== -1
|
|
|
|
) ) {
|
2014-02-28 18:17:11 +00:00
|
|
|
exactMatch = true;
|
|
|
|
}
|
|
|
|
hiddenCategoryItems.push( item );
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 19:02:16 +00:00
|
|
|
|
2013-04-29 21:01:56 +00:00
|
|
|
// New category
|
|
|
|
if ( !exactMatch ) {
|
|
|
|
newCategoryItems.push( category.value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add sections for non-empty groups
|
|
|
|
if ( newCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-11-01 19:45:59 +00:00
|
|
|
'newCategory', { '$': menu$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
|
|
|
for ( i = 0, len = newCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = newCategoryItems[i];
|
2013-11-01 19:45:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget( item, { '$': menu$, 'label': item } ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( existingCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-11-01 19:45:59 +00:00
|
|
|
'inArticle', { '$': menu$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
|
|
|
for ( i = 0, len = existingCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = existingCategoryItems[i];
|
2013-11-01 19:45:59 +00:00
|
|
|
items.push( new OO.ui.MenuItemWidget( item, { '$': menu$, 'label': item } ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( matchingCategoryItems.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
2013-11-01 19:45:59 +00:00
|
|
|
'matchingCategories', { '$': menu$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ) }
|
2013-04-29 21:01:56 +00:00
|
|
|
) );
|
2013-06-12 01:52:39 +00:00
|
|
|
for ( i = 0, len = matchingCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = matchingCategoryItems[i];
|
2014-03-11 19:02:16 +00:00
|
|
|
items.push( this.getMenuItemWidgetFromCategoryName( item, menu$ ) );
|
2013-04-29 21:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 18:17:11 +00:00
|
|
|
if ( hiddenCategoryItems.length ) {
|
|
|
|
items.push( new OO.ui.MenuSectionItemWidget(
|
|
|
|
'hiddenCategories', { '$': menu$, 'label': ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ) }
|
|
|
|
) );
|
|
|
|
for ( i = 0, len = hiddenCategoryItems.length; i < len; i++ ) {
|
|
|
|
item = hiddenCategoryItems[i];
|
2014-03-11 19:02:16 +00:00
|
|
|
items.push( this.getMenuItemWidgetFromCategoryName( item, menu$ ) );
|
2014-02-28 18:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-29 21:01:56 +00:00
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
2014-03-11 19:02:16 +00:00
|
|
|
/**
|
|
|
|
* Get a OO.ui.MenuSectionItemWidget object for a given category name.
|
|
|
|
* Deals with redirects.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} item Category name
|
|
|
|
* @param {jQuery} menu$ Lookup menu jQuery
|
|
|
|
* @returns {OO.ui.MenuSectionItemWidget} Menu item
|
|
|
|
*/
|
|
|
|
ve.ui.MWCategoryInputWidget.prototype.getMenuItemWidgetFromCategoryName = function ( item, menu$ ) {
|
|
|
|
var itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText(),
|
|
|
|
redirectInfo = $.grep( this.categoryRedirects, function ( redirectInfo ) {
|
|
|
|
return redirectInfo.to === itemTitle;
|
|
|
|
} );
|
|
|
|
if ( redirectInfo.length ) {
|
|
|
|
return new OO.ui.MenuItemWidget( item, {
|
|
|
|
'$': menu$,
|
|
|
|
'autoFitLabel': false,
|
|
|
|
'label': this.$( '<span>' )
|
|
|
|
.text( new mw.Title( redirectInfo[0].from ).getMainText() )
|
|
|
|
.append( '<br>↳ ' )
|
|
|
|
.append( $( '<span>' ).text( new mw.Title( item ).getMainText() ) )
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
return new OO.ui.MenuItemWidget( item, { '$': menu$, 'label': item } );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
'name': title.getPrefixedText(),
|
2013-07-10 22:47:37 +00:00
|
|
|
'value': title.getMainText(),
|
2013-04-29 21:01:56 +00:00
|
|
|
'metaItem': {}
|
|
|
|
};
|
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 {
|
|
|
|
'name': this.categoryPrefix + value,
|
|
|
|
'value': value,
|
|
|
|
'metaItem': {}
|
|
|
|
};
|
2013-04-29 21:01:56 +00:00
|
|
|
};
|