From adfd9733c6475f6e1a5da68c1c60b9a9f798c3f0 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 25 Oct 2016 11:46:50 +0800 Subject: [PATCH] FIX: Advanced search should retain search term. --- .../widgets/search-menu-controls.js.es6 | 9 ++-- .../discourse/widgets/search-menu.js.es6 | 43 ++++++++++++++----- .../javascripts/acceptance/search-test.js.es6 | 7 +++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 b/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 index dd07753bad3..5be3ce35430 100644 --- a/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 +++ b/app/assets/javascripts/discourse/widgets/search-menu-controls.js.es6 @@ -44,9 +44,12 @@ createWidget('search-context', { ])); } - result.push(this.attach('link', { href: Discourse.getURL('/search?expanded=true'), - label: 'show_help', - className: 'show-help' })); + if (!attrs.contextEnabled) { + result.push(this.attach('link', { href: attrs.url, + label: 'show_help', + className: 'show-help' })); + } + result.push(h('div.clearfix')); return result; }, diff --git a/app/assets/javascripts/discourse/widgets/search-menu.js.es6 b/app/assets/javascripts/discourse/widgets/search-menu.js.es6 index 69631028ae6..23f86461b44 100644 --- a/app/assets/javascripts/discourse/widgets/search-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/search-menu.js.es6 @@ -67,7 +67,7 @@ const SearchHelper = { export default createWidget('search-menu', { tagName: 'div.search-menu', - fullSearchUrl() { + fullSearchUrl(opts) { const contextEnabled = searchData.contextEnabled; const ctx = contextEnabled ? this.searchContext() : null; @@ -77,15 +77,31 @@ export default createWidget('search-menu', { return; } - let url = '/search?q=' + encodeURIComponent(searchData.term); - if (contextEnabled) { - if (this.currentUser && - ctx.id.toString().toLowerCase() === this.currentUser.username_lower && - type === "private_messages") { - url += ' in:private'; - } else { - url += encodeURIComponent(" " + type + ":" + ctx.id); + let url = '/search'; + const params = []; + + if (searchData.term) { + let query = ''; + + query += `q=${encodeURIComponent(searchData.term)}`; + + if (contextEnabled) { + if (this.currentUser && + ctx.id.toString().toLowerCase() === this.currentUser.username_lower && + type === "private_messages") { + query += ' in:private'; + } else { + query += encodeURIComponent(" " + type + ":" + ctx.id); + } } + + if (query) params.push(query); + } + + if (opts && opts.expanded) params.push('expanded=true'); + + if (params.length > 0) { + url = `${url}?${params.join("&")}`; } return Discourse.getURL(url); @@ -94,8 +110,13 @@ export default createWidget('search-menu', { panelContents() { const contextEnabled = searchData.contextEnabled; - const results = [this.attach('search-term', { value: searchData.term, contextEnabled }), - this.attach('search-context', { contextEnabled })]; + const results = [ + this.attach('search-term', { value: searchData.term, contextEnabled }), + this.attach('search-context', { + contextEnabled, + url: this.fullSearchUrl({ expanded: true }) + }) + ]; if (searchData.term) { if (searchData.loading) { diff --git a/test/javascripts/acceptance/search-test.js.es6 b/test/javascripts/acceptance/search-test.js.es6 index 7b0c8dc9b76..12243b05c30 100644 --- a/test/javascripts/acceptance/search-test.js.es6 +++ b/test/javascripts/acceptance/search-test.js.es6 @@ -16,4 +16,11 @@ test("search", (assert) => { andThen(() => { assert.ok(exists('.search-menu .results ul li'), 'it shows results'); }); + + click('.show-help'); + + andThen(() => { + assert.equal(find('.full-page-search').val(), 'dev', 'it shows the search term'); + assert.ok(exists('.search-advanced-options'), 'advanced search is expanded'); + }); });