[MediaWiki-commits] [Gerrit] Extract some methods in SearchApi - change (mediawiki...MobileFrontend)
jenkins-bot has submitted this change and it was merged. Change subject: Extract some methods in SearchApi .. Extract some methods in SearchApi This improves the readability of this module. Bug: T110069 Change-Id: Id39125366afbf06c8cc0d798ec4171435c341005 --- M resources/mobile.search/SearchApi.js M tests/qunit/mobile.search/test_SearchApi.js 2 files changed, 102 insertions(+), 95 deletions(-) Approvals: Jonas Kress (WMDE): Looks good to me, but someone else must approve Florianschmidtwelzow: Looks good to me, but someone else must approve Jdlrobson: Looks good to me, approved jenkins-bot: Verified diff --git a/resources/mobile.search/SearchApi.js b/resources/mobile.search/SearchApi.js index 86b553e..f3fa82b 100644 --- a/resources/mobile.search/SearchApi.js +++ b/resources/mobile.search/SearchApi.js @@ -10,34 +10,6 @@ Api = M.require( 'api' ).Api; /** -* Escapes regular expression wildcards (metacharacters) by adding a \\ prefix -* @method -* @ignore -* @param {String} str a string -* @return {Object} a regular expression that can be used to search for that str -*/ - function createSearchRegEx( str ) { - str = str.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$' ); - return new RegExp( '^(' + str + ')', 'ig' ); - } - - /** -* Takes a label potentially beginning with term -* and highlights term if it is present with strong -* @method -* @private -* @param {String} label a piece of text -* @param {String} term a string to search for from the start -* @return {String} safe html string with matched terms encapsulated in strong tags -*/ - function highlightSearchTerm( label, term ) { - label = $( 'span' ).text( label ).html(); - term = $( 'span' ).text( term ).html(); - - return label.replace( createSearchRegEx( term ), 'strong$1/strong' ); - } - - /** * @class SearchApi * @extends Api */ @@ -79,16 +51,42 @@ }, /** +* Escapes regular expression wildcards (metacharacters) by adding a \\ prefix +* @param {String} str a string +* @return {Object} a regular expression that can be used to search for that str +* @private +*/ + _createSearchRegEx: function ( str ) { + str = str.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$' ); + return new RegExp( '^(' + str + ')', 'ig' ); + }, + + /** +* Takes a label potentially beginning with term +* and highlights term if it is present with strong +* @param {String} label a piece of text +* @param {String} term a string to search for from the start +* @return {String} safe html string with matched terms encapsulated in strong tags +* @private +*/ + _highlightSearchTerm: function ( label, term ) { + label = $( 'span' ).text( label ).html(); + term = $( 'span' ).text( term ).html(); + + return label.replace( this._createSearchRegEx( term ), 'strong$1/strong' ); + }, + + /** * Return data used for creating {Page} objects * @param {String} query to search for * @param {Object} info page info from the API -* @returns {Object} data needed to create a {Page} +* @return {Object} data needed to create a {Page} * @private */ _getPageData: function ( query, info ) { return { id: info.pageid, - displayTitle: highlightSearchTerm( info.title, query ), + displayTitle: this._highlightSearchTerm( info.title, query ), title: info.title, url: mw.util.getUrl( info.title ), thumbnail: info.thumbnail @@ -96,9 +94,79 @@ }, /** -* Perform a search for the given query. +* Process the data returned by the api call. * FIXME: remove filtering of redirects once the upstream bug has been fixed: * https://bugzilla.wikimedia.org/show_bug.cgi?id=73673 +* @param {String} query to search for +* @param {Object} data from api +* @return {Array} +* @private +*/ + _processData: function ( query, data ) { +
[MediaWiki-commits] [Gerrit] Extract some methods in SearchApi - change (mediawiki...MobileFrontend)
Bene has uploaded a new change for review. https://gerrit.wikimedia.org/r/233479 Change subject: Extract some methods in SearchApi .. Extract some methods in SearchApi This makes it easier to reuse code in this module. Bug: T110069 Change-Id: Id39125366afbf06c8cc0d798ec4171435c341005 --- M resources/mobile.search/SearchApi.js 1 file changed, 101 insertions(+), 94 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/79/233479/1 diff --git a/resources/mobile.search/SearchApi.js b/resources/mobile.search/SearchApi.js index 86b553e..d0ad1b4 100644 --- a/resources/mobile.search/SearchApi.js +++ b/resources/mobile.search/SearchApi.js @@ -10,34 +10,6 @@ Api = M.require( 'api' ).Api; /** -* Escapes regular expression wildcards (metacharacters) by adding a \\ prefix -* @method -* @ignore -* @param {String} str a string -* @return {Object} a regular expression that can be used to search for that str -*/ - function createSearchRegEx( str ) { - str = str.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$' ); - return new RegExp( '^(' + str + ')', 'ig' ); - } - - /** -* Takes a label potentially beginning with term -* and highlights term if it is present with strong -* @method -* @private -* @param {String} label a piece of text -* @param {String} term a string to search for from the start -* @return {String} safe html string with matched terms encapsulated in strong tags -*/ - function highlightSearchTerm( label, term ) { - label = $( 'span' ).text( label ).html(); - term = $( 'span' ).text( term ).html(); - - return label.replace( createSearchRegEx( term ), 'strong$1/strong' ); - } - - /** * @class SearchApi * @extends Api */ @@ -79,16 +51,42 @@ }, /** +* Escapes regular expression wildcards (metacharacters) by adding a \\ prefix +* @param {String} str a string +* @return {Object} a regular expression that can be used to search for that str +* @private +*/ + _createSearchRegEx: function ( str ) { + str = str.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$'); + return new RegExp('^(' + str + ')', 'ig'); + }, + + /** +* Takes a label potentially beginning with term +* and highlights term if it is present with strong +* @param {String} label a piece of text +* @param {String} term a string to search for from the start +* @return {String} safe html string with matched terms encapsulated in strong tags +* @private +*/ + _highlightSearchTerm: function ( label, term ) { + label = $( 'span' ).text( label ).html(); + term = $( 'span' ).text( term ).html(); + + return label.replace( this._createSearchRegEx( term ), 'strong$1/strong' ); + }, + + /** * Return data used for creating {Page} objects * @param {String} query to search for * @param {Object} info page info from the API -* @returns {Object} data needed to create a {Page} +* @return {Object} data needed to create a {Page} * @private */ _getPageData: function ( query, info ) { return { id: info.pageid, - displayTitle: highlightSearchTerm( info.title, query ), + displayTitle: this._highlightSearchTerm( info.title, query ), title: info.title, url: mw.util.getUrl( info.title ), thumbnail: info.thumbnail @@ -96,9 +94,79 @@ }, /** -* Perform a search for the given query. +* Process the data returned by the api call. * FIXME: remove filtering of redirects once the upstream bug has been fixed: * https://bugzilla.wikimedia.org/show_bug.cgi?id=73673 +* @param {String} query to search for +* @param {Object} data from api +* @return {Array} +* @private +*/ + _processData: function( query, data ) { + var self = this, + results = [], + pages = {}, +