Phuedx has uploaded a new change for review. https://gerrit.wikimedia.org/r/207761
Change subject: Add new events to SearchOverlay ...................................................................... Add new events to SearchOverlay SearchOverlay currently emits the search-results event after the search API request completes and the response has been processed. Add the following events: * search-show - emitted immediately after the search overlay is shown * search-start - emitted immediately before the search API request is * search-result-click - emitted when the user clicks a search result Bug: T96326 Change-Id: I63d73687ea233e404638f44aaf65c625889e69e1 --- M javascripts/modules/search/SearchOverlay.js M javascripts/modules/search/init.js 2 files changed, 60 insertions(+), 16 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/61/207761/1 diff --git a/javascripts/modules/search/SearchOverlay.js b/javascripts/modules/search/SearchOverlay.js index 5204a39..96e1e90 100644 --- a/javascripts/modules/search/SearchOverlay.js +++ b/javascripts/modules/search/SearchOverlay.js @@ -61,7 +61,8 @@ 'click .overlay-content': 'onClickOverlayContent', 'click .overlay-content > div': 'onClickOverlayContentDiv', 'touchstart .results': 'hideKeyboardOnScroll', - 'mousedown .results': 'hideKeyboardOnScroll' + 'mousedown .results': 'hideKeyboardOnScroll', + 'click .results li': 'onClickResult' } ), /** @@ -156,6 +157,30 @@ this.$input.blur(); }, + /** + * Handle the user clicking a result. + * + * @param {jQuery.Event} ev + */ + onClickResult: function ( ev ) { + var $result = $( ev.target ).closest( 'li' ); + + /** + * @event search-result-click Fired when the user clicks a search result + * @type {Object} + * @property {jQuery} result The jQuery-wrapped DOM element that + * the user clicked + * @property {Number} resultIndex The zero-based index of the + * result in the set of results + * @property {jQuery.Event} originalEvent The original event + */ + M.emit( 'search-result-click', { + result: $result, + resultIndex: this.$results.index( $result ), + originalEvent: ev + } ); + }, + /** @inheritdoc */ postRender: function ( options ) { var self = this; @@ -188,6 +213,11 @@ if ( this.$input[0].setSelectionRange ) { this.$input[0].setSelectionRange( len, len ); } + + /** + * @event search-show Fired after the search overlay is shown + */ + M.emit( 'search-show' ); }, /** @@ -217,7 +247,7 @@ self = this, pageList, query = this.$input.val(), - $results = this.$( '.results' ); + $resultContainer = this.$( '.results' ); // it seems the input event can be fired when virtual keyboard is closed // (Chrome for Android) @@ -225,12 +255,20 @@ this.api.abort(); clearTimeout( this.timer ); self.$searchContent.hide(); - $results.empty(); + $resultContainer.empty(); if ( query.length ) { this.$( '.spinner' ).show(); this.timer = setTimeout( function () { + + // FIXME: The query might be useful here, bit it ain't necessary right now. + /** + * @event search-start Fired immediately before the search API request is + * sent + */ + M.emit( 'search-start' ); + self.api.search( query ).done( function ( data ) { // check if we're getting the rights response in case of out of // order responses (need to get the current value of the input) @@ -245,13 +283,20 @@ pageList = new WatchstarPageList( { funnel: 'search', pages: data.results, - el: $results + el: $resultContainer } ); + + self.$results = $resultContainer.find( 'li' ); + /** - * @event search-results - * Fired when search API returns results + * @event search-results Fired when search API returns results + * @type {Object} + * @property {Object[]} results The results returned by the search + * API */ - M.emit( 'search-results', self, data.results ); + M.emit( 'search-results', { + results: data.results + } ); } } ); }, this.api.isCached( query ) ? 0 : SEARCH_DELAY ); diff --git a/javascripts/modules/search/init.js b/javascripts/modules/search/init.js index c97a55e..ef59b81 100644 --- a/javascripts/modules/search/init.js +++ b/javascripts/modules/search/init.js @@ -45,15 +45,14 @@ // to search results (we can't rely on History API yet) // alpha does it differently in lazyload.js if ( !context.isAlphaGroupMember() ) { - M.on( 'search-results', function ( overlay ) { - overlay.$( '.results a' ).on( 'click', function () { - var href = $( this ).attr( 'href' ); - router.back().done( function () { - window.location.href = href; - } ); - // Prevent the link from working and prevent the closing of the overlay - // by an event upstream which would trigger browser back on the clicked link - return false; + M.on( 'search-result-click', function ( ev ) { + var href = $( ev.result ).find( 'a' ) + .attr( 'href' ); + + ev.originalEvent.preventDefault(); + + router.back().done( function () { + window.location.href = href; } ); } ); } -- To view, visit https://gerrit.wikimedia.org/r/207761 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I63d73687ea233e404638f44aaf65c625889e69e1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Phuedx <g...@samsmith.io> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits