mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
FIX: Search was clearing data when you closed it
This commit is contained in:
parent
baae2a4b7c
commit
468af7f0d7
1 changed files with 36 additions and 42 deletions
|
@ -3,6 +3,14 @@ import { createWidget } from 'discourse/widgets/widget';
|
||||||
import { h } from 'virtual-dom';
|
import { h } from 'virtual-dom';
|
||||||
import DiscourseURL from 'discourse/lib/url';
|
import DiscourseURL from 'discourse/lib/url';
|
||||||
|
|
||||||
|
const searchData = {
|
||||||
|
loading: false,
|
||||||
|
results: {},
|
||||||
|
noResults: false,
|
||||||
|
term: undefined,
|
||||||
|
typeFilter: null
|
||||||
|
};
|
||||||
|
|
||||||
// Helps with debouncing and cancelling promises
|
// Helps with debouncing and cancelling promises
|
||||||
const SearchHelper = {
|
const SearchHelper = {
|
||||||
_activeSearch: null,
|
_activeSearch: null,
|
||||||
|
@ -29,23 +37,22 @@ const SearchHelper = {
|
||||||
this._activeSearch = null;
|
this._activeSearch = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { state } = widget;
|
const { term, typeFilter, contextEnabled } = searchData;
|
||||||
const { term, typeFilter, contextEnabled } = state;
|
|
||||||
const searchContext = contextEnabled ? widget.searchContext() : null;
|
const searchContext = contextEnabled ? widget.searchContext() : null;
|
||||||
const fullSearchUrl = widget.fullSearchUrl();
|
const fullSearchUrl = widget.fullSearchUrl();
|
||||||
|
|
||||||
if (!isValidSearchTerm(term)) {
|
if (!isValidSearchTerm(term)) {
|
||||||
state.noResults = true;
|
searchData.noResults = true;
|
||||||
state.results = [];
|
searchData.results = [];
|
||||||
state.loading = false;
|
searchData.loading = false;
|
||||||
widget.scheduleRerender();
|
widget.scheduleRerender();
|
||||||
} else {
|
} else {
|
||||||
this._activeSearch = searchForTerm(term, { typeFilter, searchContext, fullSearchUrl });
|
this._activeSearch = searchForTerm(term, { typeFilter, searchContext, fullSearchUrl });
|
||||||
this._activeSearch.then(content => {
|
this._activeSearch.then(content => {
|
||||||
state.noResults = content.resultTypes.length === 0;
|
searchData.noResults = content.resultTypes.length === 0;
|
||||||
state.results = content;
|
searchData.results = content;
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
state.loading = false;
|
searchData.loading = false;
|
||||||
widget.scheduleRerender();
|
widget.scheduleRerender();
|
||||||
this._activeSearch = null;
|
this._activeSearch = null;
|
||||||
});
|
});
|
||||||
|
@ -55,19 +62,9 @@ const SearchHelper = {
|
||||||
|
|
||||||
export default createWidget('search-menu', {
|
export default createWidget('search-menu', {
|
||||||
tagName: 'div.search-menu',
|
tagName: 'div.search-menu',
|
||||||
buildKey: () => 'search-menu',
|
|
||||||
|
|
||||||
defaultState() {
|
|
||||||
return { loading: false,
|
|
||||||
results: {},
|
|
||||||
noResults: false,
|
|
||||||
term: null,
|
|
||||||
typeFilter: null };
|
|
||||||
},
|
|
||||||
|
|
||||||
fullSearchUrl() {
|
fullSearchUrl() {
|
||||||
const state = this.state;
|
const contextEnabled = searchData.contextEnabled;
|
||||||
const contextEnabled = state.contextEnabled;
|
|
||||||
|
|
||||||
const ctx = contextEnabled ? this.searchContext() : null;
|
const ctx = contextEnabled ? this.searchContext() : null;
|
||||||
const type = Ember.get(ctx, 'type');
|
const type = Ember.get(ctx, 'type');
|
||||||
|
@ -76,7 +73,7 @@ export default createWidget('search-menu', {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = '/search?q=' + encodeURIComponent(state.term);
|
let url = '/search?q=' + encodeURIComponent(searchData.term);
|
||||||
if (contextEnabled) {
|
if (contextEnabled) {
|
||||||
if (ctx.id.toString().toLowerCase() === this.currentUser.username_lower &&
|
if (ctx.id.toString().toLowerCase() === this.currentUser.username_lower &&
|
||||||
type === "private_messages") {
|
type === "private_messages") {
|
||||||
|
@ -90,18 +87,17 @@ export default createWidget('search-menu', {
|
||||||
},
|
},
|
||||||
|
|
||||||
panelContents() {
|
panelContents() {
|
||||||
const { state } = this;
|
const contextEnabled = searchData.contextEnabled;
|
||||||
const contextEnabled = state.contextEnabled;
|
|
||||||
|
|
||||||
const results = [this.attach('search-term', { value: state.term, contextEnabled }),
|
const results = [this.attach('search-term', { value: searchData.term, contextEnabled }),
|
||||||
this.attach('search-context', { contextEnabled })];
|
this.attach('search-context', { contextEnabled })];
|
||||||
|
|
||||||
if (state.loading) {
|
if (searchData.loading) {
|
||||||
results.push(h('div.searching', h('div.spinner')));
|
results.push(h('div.searching', h('div.spinner')));
|
||||||
} else {
|
} else {
|
||||||
results.push(this.attach('search-menu-results', { term: state.term,
|
results.push(this.attach('search-menu-results', { term: searchData.term,
|
||||||
noResults: state.noResults,
|
noResults: searchData.noResults,
|
||||||
results: state.results }));
|
results: searchData.results }));
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
@ -121,8 +117,8 @@ export default createWidget('search-menu', {
|
||||||
return this._searchContext;
|
return this._searchContext;
|
||||||
},
|
},
|
||||||
|
|
||||||
html(attrs, state) {
|
html(attrs) {
|
||||||
state.contextEnabled = attrs.contextEnabled;
|
searchData.contextEnabled = attrs.contextEnabled;
|
||||||
|
|
||||||
return this.attach('menu-panel', { maxWidth: 500, contents: () => this.panelContents() });
|
return this.attach('menu-panel', { maxWidth: 500, contents: () => this.panelContents() });
|
||||||
},
|
},
|
||||||
|
@ -132,38 +128,36 @@ export default createWidget('search-menu', {
|
||||||
},
|
},
|
||||||
|
|
||||||
triggerSearch() {
|
triggerSearch() {
|
||||||
const { state } = this;
|
searchData.noResults = false;
|
||||||
|
if (isValidSearchTerm(searchData.term)) {
|
||||||
state.noResults = false;
|
this.searchService().set('highlightTerm', searchData.term);
|
||||||
if (isValidSearchTerm(state.term)) {
|
searchData.loading = true;
|
||||||
this.searchService().set('highlightTerm', state.term);
|
|
||||||
state.loading = true;
|
|
||||||
Ember.run.debounce(SearchHelper, SearchHelper.perform, this, 400);
|
Ember.run.debounce(SearchHelper, SearchHelper.perform, this, 400);
|
||||||
} else {
|
} else {
|
||||||
state.results = [];
|
searchData.results = [];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
moreOfType(type) {
|
moreOfType(type) {
|
||||||
this.state.typeFilter = type;
|
searchData.typeFilter = type;
|
||||||
this.triggerSearch();
|
this.triggerSearch();
|
||||||
},
|
},
|
||||||
|
|
||||||
searchContextChanged(enabled) {
|
searchContextChanged(enabled) {
|
||||||
this.state.typeFilter = null;
|
searchData.typeFilter = null;
|
||||||
this.sendWidgetAction('searchMenuContextChanged', enabled);
|
this.sendWidgetAction('searchMenuContextChanged', enabled);
|
||||||
this.state.contextEnabled = enabled;
|
searchData.contextEnabled = enabled;
|
||||||
this.triggerSearch();
|
this.triggerSearch();
|
||||||
},
|
},
|
||||||
|
|
||||||
searchTermChanged(term) {
|
searchTermChanged(term) {
|
||||||
this.state.typeFilter = null;
|
searchData.typeFilter = null;
|
||||||
this.state.term = term;
|
searchData.term = term;
|
||||||
this.triggerSearch();
|
this.triggerSearch();
|
||||||
},
|
},
|
||||||
|
|
||||||
fullSearch() {
|
fullSearch() {
|
||||||
if (!isValidSearchTerm(this.state.term)) { return; }
|
if (!isValidSearchTerm(searchData.term)) { return; }
|
||||||
|
|
||||||
SearchHelper.cancel();
|
SearchHelper.cancel();
|
||||||
const url = this.fullSearchUrl();
|
const url = this.fullSearchUrl();
|
||||||
|
|
Loading…
Reference in a new issue