Ricordisamoa has uploaded a new change for review. https://gerrit.wikimedia.org/r/282455
Change subject: WIP CategorySelector: use classes instead of switch for category search types ...................................................................... WIP CategorySelector: use classes instead of switch for category search types Change-Id: I1cecdad3049115852afdf9ae78dc894d28b0d1b4 --- M resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js 1 file changed, 150 insertions(+), 130 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/55/282455/1 diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js index 5d7d115..14f0b31 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js @@ -9,6 +9,139 @@ NS_CATEGORY = mw.config.get( 'wgNamespaceIds' ).category; /** + * @class mw.widgets.CategorySelector.CategorySearchType + * @uses mw.Api + * + * @constructor + * @param {Object} config Configuration options + * @cfg {mw.Api} api Instance of mw.Api (or subclass thereof) to use for queries + */ + function CategorySearchType( config ) { + this.api = config.api; + } + CategorySearchType.prototype.mustBeTheOnlySearchType = false; + + function OpenSearchCategorySearchType( config ) { + CategorySearchType.call( this, config ); + } + OO.inheritClass( OpenSearchCategorySearchType, CategorySearchType ); + OpenSearchCategorySearchType.prototype.searchCategories = function ( input, limit ) { + return this.api.get( { + formatversion: 2, + action: 'opensearch', + namespace: NS_CATEGORY, + limit: limit, + search: input + } ).then( function ( res ) { + return res[ 1 ]; + } ); + }; + + function InternalSearchCategorySearchType( config ) { + CategorySearchType.call( this, config ); + } + OO.inheritClass( InternalSearchCategorySearchType, CategorySearchType ); + InternalSearchCategorySearchType.prototype.searchCategories = function ( input, limit ) { + return this.api.get( { + formatversion: 2, + action: 'query', + list: 'allpages', + apnamespace: NS_CATEGORY, + aplimit: limit, + apfrom: input, + apprefix: input + } ).then( function ( res ) { + return res.query.allpages.map( function ( page ) { + return page.title; + } ); + } ); + }; + + function ExistsCategorySearchType( config ) { + CategorySearchType.call( this, config ); + } + OO.inheritClass( ExistsCategorySearchType, CategorySearchType ); + ExistsCategorySearchType.prototype.searchCategories = function ( input ) { + if ( input.indexOf( '|' ) > -1 ) { + return $.Deferred().resolve( [] ); + } + + return this.api.get( { + formatversion: 2, + action: 'query', + prop: 'info', + titles: 'Category:' + input + } ).then( function ( res ) { + var categories = []; + + $.each( res.query.pages, function ( index, page ) { + if ( !page.missing ) { + categories.push( page.title ); + } + } ); + + return categories; + } ); + }; + + function SubCategoriesCategorySearchType( config ) { + CategorySearchType.call( this, config ); + } + OO.inheritClass( SubCategoriesCategorySearchType, CategorySearchType ); + SubCategoriesCategorySearchType.prototype.mustBeTheOnlySearchType = true; + SubCategoriesCategorySearchType.prototype.searchCategories = function ( input, limit ) { + if ( input.indexOf( '|' ) > -1 ) { + return $.Deferred().resolve( [] ); + } + + return this.api.get( { + formatversion: 2, + action: 'query', + list: 'categorymembers', + cmtype: 'subcat', + cmlimit: limit, + cmtitle: 'Category:' + input + } ).then( function ( res ) { + return res.query.categorymembers.map( function ( category ) { + return category.title; + } ); + } ); + }; + + function ParentCategoriesCategorySearchType( config ) { + CategorySearchType.call( this, config ); + } + OO.inheritClass( ParentCategoriesCategorySearchType, CategorySearchType ); + ParentCategoriesCategorySearchType.prototype.mustBeTheOnlySearchType = true; + ParentCategoriesCategorySearchType.prototype.searchCategories = function ( input, limit ) { + if ( input.indexOf( '|' ) > -1 ) { + return $.Deferred().resolve( [] ); + } + + return this.api.get( { + formatversion: 2, + action: 'query', + prop: 'categories', + cllimit: limit, + titles: 'Category:' + input + } ).then( function ( res ) { + var categories = []; + + $.each( res.query.pages, function ( index, page ) { + if ( !page.missing ) { + if ( $.isArray( page.categories ) ) { + categories.push.apply( categories, page.categories.map( function ( category ) { + return category.title; + } ) ); + } + } + } ); + + return categories; + } ); + }; + + /** * Category selector widget. Displays an OO.ui.CapsuleMultiSelectWidget * and autocompletes with available categories. * @@ -209,32 +342,24 @@ * @return {boolean} */ CSP.validateSearchTypes = function () { - var validSearchTypes = false, - searchTypeEnumCount = Object.keys( CategorySelector.SearchType ).length; + var validSearchTypes = false; - // Check if all values are in the SearchType enum + // Check if all values are instances of CategorySearchType validSearchTypes = this.searchTypes.every( function ( searchType ) { - return searchType > -1 && searchType < searchTypeEnumCount; + return searchType instanceof CategorySearchType; } ); if ( validSearchTypes === false ) { throw new Error( 'Unknown searchType in searchTypes' ); } - // If the searchTypes has CategorySelector.SearchType.SubCategories - // it can be the only search type. - if ( this.searchTypes.indexOf( CategorySelector.SearchType.SubCategories ) > -1 && - this.searchTypes.length > 1 - ) { - throw new Error( 'Can\'t have additional search types with CategorySelector.SearchType.SubCategories' ); - } - - // If the searchTypes has CategorySelector.SearchType.ParentCategories - // it can be the only search type. - if ( this.searchTypes.indexOf( CategorySelector.SearchType.ParentCategories ) > -1 && - this.searchTypes.length > 1 - ) { - throw new Error( 'Can\'t have additional search types with CategorySelector.SearchType.ParentCategories' ); + // Check whether it can be the only search type. + if ( this.searchTypes.length > 1 ) { + this.searchTypes.forEach( function ( searchType ) { + if ( searchType.mustBeTheOnlySearchType === true ) { + throw new Error( 'Can\'t have additional search types with ' + searchType.constructor.name ); + } + } ); } return true; @@ -260,118 +385,7 @@ * @return {jQuery.Promise} Resolves with an array of categories */ CSP.searchCategories = function ( input, searchType ) { - var deferred = $.Deferred(); - - switch ( searchType ) { - case CategorySelector.SearchType.OpenSearch: - this.api.get( { - formatversion: 2, - action: 'opensearch', - namespace: NS_CATEGORY, - limit: this.limit, - search: input - } ).done( function ( res ) { - var categories = res[ 1 ]; - deferred.resolve( categories ); - } ).fail( deferred.reject.bind( deferred ) ); - break; - - case CategorySelector.SearchType.InternalSearch: - this.api.get( { - formatversion: 2, - action: 'query', - list: 'allpages', - apnamespace: NS_CATEGORY, - aplimit: this.limit, - apfrom: input, - apprefix: input - } ).done( function ( res ) { - var categories = res.query.allpages.map( function ( page ) { - return page.title; - } ); - deferred.resolve( categories ); - } ).fail( deferred.reject.bind( deferred ) ); - break; - - case CategorySelector.SearchType.Exists: - if ( input.indexOf( '|' ) > -1 ) { - deferred.resolve( [] ); - break; - } - - this.api.get( { - formatversion: 2, - action: 'query', - prop: 'info', - titles: 'Category:' + input - } ).done( function ( res ) { - var categories = []; - - $.each( res.query.pages, function ( index, page ) { - if ( !page.missing ) { - categories.push( page.title ); - } - } ); - - deferred.resolve( categories ); - } ).fail( deferred.reject.bind( deferred ) ); - break; - - case CategorySelector.SearchType.SubCategories: - if ( input.indexOf( '|' ) > -1 ) { - deferred.resolve( [] ); - break; - } - - this.api.get( { - formatversion: 2, - action: 'query', - list: 'categorymembers', - cmtype: 'subcat', - cmlimit: this.limit, - cmtitle: 'Category:' + input - } ).done( function ( res ) { - var categories = res.query.categorymembers.map( function ( category ) { - return category.title; - } ); - deferred.resolve( categories ); - } ).fail( deferred.reject.bind( deferred ) ); - break; - - case CategorySelector.SearchType.ParentCategories: - if ( input.indexOf( '|' ) > -1 ) { - deferred.resolve( [] ); - break; - } - - this.api.get( { - formatversion: 2, - action: 'query', - prop: 'categories', - cllimit: this.limit, - titles: 'Category:' + input - } ).done( function ( res ) { - var categories = []; - - $.each( res.query.pages, function ( index, page ) { - if ( !page.missing ) { - if ( $.isArray( page.categories ) ) { - categories.push.apply( categories, page.categories.map( function ( category ) { - return category.title; - } ) ); - } - } - } ); - - deferred.resolve( categories ); - } ).fail( deferred.reject.bind( deferred ) ); - break; - - default: - throw new Error( 'Unknown searchType' ); - } - - return deferred.promise(); + return searchType.searchCategories( input, this.limit ).promise(); }; /** @@ -396,4 +410,10 @@ }; mw.widgets.CategorySelector = CategorySelector; + mw.widgets.CategorySelector.CategorySearchType = CategorySearchType; + mw.widgets.CategorySelector.OpenSearchCategorySearchType = OpenSearchCategorySearchType; + mw.widgets.CategorySelector.InternalSearchCategorySearchType = InternalSearchCategorySearchType; + mw.widgets.CategorySelector.ExistsCategorySearchType = ExistsCategorySearchType; + mw.widgets.CategorySelector.SubCategoriesCategorySearchType = SubCategoriesCategorySearchType; + mw.widgets.CategorySelector.ParentCategoriesCategorySearchType = ParentCategoriesCategorySearchType; }( jQuery, mediaWiki ) ); -- To view, visit https://gerrit.wikimedia.org/r/282455 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1cecdad3049115852afdf9ae78dc894d28b0d1b4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits