mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: category quick search on world page, theme version updates, #13255
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
define('forum/world', ['topicList', 'sort', 'hooks', 'alerts', 'api', 'bootbox'], function (topicList, sort, hooks, alerts, api, bootbox) {
|
||||
define('forum/world', ['topicList', 'search', 'sort', 'hooks', 'alerts', 'api', 'bootbox'], function (topicList, search, sort, hooks, alerts, api, bootbox) {
|
||||
const World = {};
|
||||
|
||||
World.init = function () {
|
||||
@@ -12,6 +12,21 @@ define('forum/world', ['topicList', 'sort', 'hooks', 'alerts', 'api', 'bootbox']
|
||||
handleIgnoreWatch(-1);
|
||||
handleHelp();
|
||||
|
||||
search.enableQuickSearch({
|
||||
searchElements: {
|
||||
inputEl: $('[component="category-search"]'),
|
||||
resultEl: $('.world .quick-search-container'),
|
||||
},
|
||||
searchOptions: {
|
||||
in: 'categories',
|
||||
},
|
||||
dropdown: {
|
||||
maxWidth: '400px',
|
||||
maxHeight: '350px',
|
||||
},
|
||||
hideOnNoMatches: false,
|
||||
});
|
||||
|
||||
hooks.fire('action:topics.loaded', { topics: ajaxify.data.topics });
|
||||
hooks.fire('action:category.loaded', { cid: ajaxify.data.cid });
|
||||
};
|
||||
|
||||
@@ -132,34 +132,56 @@ define('search', [
|
||||
options.searchOptions.searchOnly = 1;
|
||||
Search.api(options.searchOptions, function (data) {
|
||||
quickSearchResults.find('.loading-indicator').addClass('hidden');
|
||||
if (!data.posts || (options.hideOnNoMatches && !data.posts.length)) {
|
||||
return quickSearchResults.addClass('hidden').find('.quick-search-results-container').html('');
|
||||
}
|
||||
data.posts.forEach(function (p) {
|
||||
const text = $('<div>' + p.content + '</div>').text();
|
||||
const query = inputEl.val().toLowerCase().replace(/^in:topic-\d+/, '');
|
||||
const start = Math.max(0, text.toLowerCase().indexOf(query) - 40);
|
||||
p.snippet = utils.escapeHTML((start > 0 ? '...' : '') +
|
||||
text.slice(start, start + 80) +
|
||||
(text.length - start > 80 ? '...' : ''));
|
||||
});
|
||||
data.dropdown = { maxWidth: '400px', maxHeight: '500px', ...options.dropdown };
|
||||
app.parseAndTranslate('partials/quick-search-results', data, function (html) {
|
||||
if (html.length) {
|
||||
html.find('.timeago').timeago();
|
||||
|
||||
if (options.searchOptions.in === 'categories') {
|
||||
if (!data.categories || (options.hideOnNoMatches && !data.categories.length)) {
|
||||
return quickSearchResults.addClass('hidden').find('.quick-search-results-container').html('');
|
||||
}
|
||||
quickSearchResults.toggleClass('hidden', !html.length || !inputEl.is(':focus'))
|
||||
.find('.quick-search-results-container')
|
||||
.html(html.length ? html : '');
|
||||
const highlightEls = quickSearchResults.find(
|
||||
'.quick-search-results .quick-search-title, .quick-search-results .snippet'
|
||||
);
|
||||
Search.highlightMatches(options.searchOptions.term, highlightEls);
|
||||
hooks.fire('action:search.quick.complete', {
|
||||
data: data,
|
||||
options: options,
|
||||
|
||||
data.dropdown = { maxWidth: '400px', maxHeight: '500px', ...options.dropdown };
|
||||
app.parseAndTranslate('partials/quick-category-search-results', data, (html) => {
|
||||
if (html.length) {
|
||||
html.find('.timeago').timeago();
|
||||
}
|
||||
quickSearchResults.toggleClass('hidden', !html.length || !inputEl.is(':focus'))
|
||||
.find('.quick-search-results-container')
|
||||
.html(html.length ? html : '');
|
||||
|
||||
hooks.fire('action:search.quick.complete', {
|
||||
data: data,
|
||||
options: options,
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (!data.posts || (options.hideOnNoMatches && !data.posts.length)) {
|
||||
return quickSearchResults.addClass('hidden').find('.quick-search-results-container').html('');
|
||||
}
|
||||
data.posts.forEach(function (p) {
|
||||
const text = $('<div>' + p.content + '</div>').text();
|
||||
const query = inputEl.val().toLowerCase().replace(/^in:topic-\d+/, '');
|
||||
const start = Math.max(0, text.toLowerCase().indexOf(query) - 40);
|
||||
p.snippet = utils.escapeHTML((start > 0 ? '...' : '') +
|
||||
text.slice(start, start + 80) +
|
||||
(text.length - start > 80 ? '...' : ''));
|
||||
});
|
||||
data.dropdown = { maxWidth: '400px', maxHeight: '500px', ...options.dropdown };
|
||||
app.parseAndTranslate('partials/quick-search-results', data, function (html) {
|
||||
if (html.length) {
|
||||
html.find('.timeago').timeago();
|
||||
}
|
||||
quickSearchResults.toggleClass('hidden', !html.length || !inputEl.is(':focus'))
|
||||
.find('.quick-search-results-container')
|
||||
.html(html.length ? html : '');
|
||||
const highlightEls = quickSearchResults.find(
|
||||
'.quick-search-results .quick-search-title, .quick-search-results .snippet'
|
||||
);
|
||||
Search.highlightMatches(options.searchOptions.term, highlightEls);
|
||||
hooks.fire('action:search.quick.complete', {
|
||||
data: data,
|
||||
options: options,
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user