This reverts commit d7cc1da280.
This commit is contained in:
parent
eabc51eac1
commit
d1fc988ed0
19 changed files with 169 additions and 146 deletions
|
|
@ -179,46 +179,40 @@ function fetchNextPodcastPage(el) {
|
|||
fetchNext(el, "/api/podcast_episodes", insertArticles)
|
||||
}
|
||||
|
||||
function paginate(tag, params, requiresApproval) {
|
||||
const searchHash = { ...{ per_page: 15, page: nextPage }, ...JSON.parse(params) };
|
||||
|
||||
function paginate(tag, requiresApproval) {
|
||||
var tagFilters = [];
|
||||
if (tag && tag.length > 0) {
|
||||
searchHash.tag_names = searchHash.tag_names || [];
|
||||
searchHash.tag_names.push(tag);
|
||||
tagFilters.push(tag);
|
||||
}
|
||||
|
||||
var searchObj = {
|
||||
hitsPerPage: 15,
|
||||
page: nextPage,
|
||||
attributesToHighlight: [],
|
||||
tagFilters,
|
||||
filters: (requiresApproval === 'true') ? 'approved:true' : ''
|
||||
}
|
||||
searchHash.approved = (requiresApproval === 'true') ? 'true' : '';
|
||||
|
||||
var homeEl = document.getElementById("index-container");
|
||||
var paginationArticlesIndex;
|
||||
if (homeEl.dataset.feed === "base-feed") {
|
||||
searchHash.class_name = "Article";
|
||||
paginationArticlesIndex = client.initIndex('ordered_articles_<%= Rails.env %>');
|
||||
} else if (homeEl.dataset.feed === "latest") {
|
||||
searchHash.class_name = "Article";
|
||||
searchHash.sort_by = "published_at";
|
||||
paginationArticlesIndex = client.initIndex('ordered_articles_by_published_at_<%= Rails.env %>');
|
||||
} else {
|
||||
searchHash.class_name = "Article";
|
||||
searchHash["published_at[gte]"] = homeEl.dataset.articlesSince;
|
||||
searchHash.sort_by = "positive_reactions_count";
|
||||
var preamble = (requiresApproval === 'true') ? ' AND ' : ''
|
||||
searchObj.filters += ( preamble +'published_at_int > ' + homeEl.dataset.articlesSince);
|
||||
paginationArticlesIndex = client.initIndex('ordered_articles_by_positive_reactions_count_<%= Rails.env %>');
|
||||
}
|
||||
|
||||
const searchParams = new URLSearchParams(searchHash).toString();
|
||||
|
||||
fetch(`/search/feed_content?${searchParams}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'X-CSRF-Token': window.csrfToken,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then((content) => {
|
||||
paginationArticlesIndex.search("*", searchObj)
|
||||
.then(function searchDone(content) {
|
||||
nextPage += 1;
|
||||
insertArticles(content.result);
|
||||
insertArticles(content.hits);
|
||||
const checkBlockedContentEvent = new CustomEvent('checkBlockedContent')
|
||||
window.dispatchEvent(checkBlockedContentEvent);
|
||||
initializeReadingListIcons();
|
||||
if (content.result.length === 0) {
|
||||
if (content.hits.length === 0) {
|
||||
document.getElementById("loading-articles").style.display = "none"
|
||||
done = true;
|
||||
}
|
||||
|
|
@ -259,11 +253,7 @@ function fetchNextPageIfNearBottom() {
|
|||
} else {
|
||||
scrollableElemId = "articles-list";
|
||||
fetchCallback = function fetch() {
|
||||
paginate(
|
||||
indexContainer.dataset.tag,
|
||||
indexContainer.dataset.params,
|
||||
indexContainer.dataset.requiresApproval
|
||||
);
|
||||
paginate(indexContainer.dataset.tag, indexContainer.dataset.requiresApproval);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -276,6 +266,9 @@ function fetchNextPageIfNearBottom() {
|
|||
}
|
||||
|
||||
function checkIfNearBottomOfPage() {
|
||||
var publicSearchKey = '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>';
|
||||
client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', publicSearchKey);
|
||||
|
||||
if (document.getElementsByClassName("single-article").length < 2 || window.location.search.indexOf("q=") > -1 ) {
|
||||
document.getElementById("loading-articles").style.display = "none";
|
||||
done = true;
|
||||
|
|
|
|||
|
|
@ -37,44 +37,42 @@ function buildArticleHTML(article) {
|
|||
if (article.class_name == "PodcastEpisode"){
|
||||
flareTag = "<span class='tag-identifier'>podcast</span>"
|
||||
}
|
||||
if (article.class_name == "Comment"){
|
||||
flareTag = "<span class='tag-identifier'>comment</span>"
|
||||
}
|
||||
if (article.class_name == "User"){
|
||||
flareTag = "<span class='tag-identifier' style='background:#5874d9;color:white;'>person</span>"
|
||||
}
|
||||
var rc = article.positive_reactions_count
|
||||
var rc = article.positive_reactions_count || article.reactions_count
|
||||
var reactionsCountHTML = ''
|
||||
if ((rc || '0') > 0 && article.class_name != "User") {
|
||||
var reactionsCountHTML = '<div class="article-engagement-count reactions-count"><a href="'+article.path+'"><img src="<%= asset_path("reactions-stack.png") %>" alt="heart" /><span id="engagement-count-number-'+article.id+'" class="engagement-count-number">'+(rc || '0')+'</span></a></div>'
|
||||
}
|
||||
var picUrl;
|
||||
var profileUsername;
|
||||
var userName;
|
||||
if (article.class_name == "PodcastEpisode"){
|
||||
picUrl = article.main_image;
|
||||
profileUsername = article.slug;
|
||||
userName = article.title;
|
||||
} else {
|
||||
picUrl = article.user.profile_image_90;
|
||||
profileUsername = article.user.username;
|
||||
userName = article.user.name;
|
||||
}
|
||||
var picUrl = article.user.profile_image_90
|
||||
var profileUsername = article.user.username
|
||||
var orgHeadline = "";
|
||||
if (article.organization && !document.getElementById("organization-article-index")) {
|
||||
orgHeadline = '<div class="article-organization-headline"><a class="org-headline-filler" href="/'+article.organization.slug+'"><img alt="'+article.organization.name+' logo" src="'+article.organization.profile_image_90+'" loading="lazy">'+article.organization.name+'</a></div>'
|
||||
}
|
||||
var bodyTextSnippet = "";
|
||||
var commentsBlobSnippet = "";
|
||||
var searchSnippetHTML = "";
|
||||
if (article.highlight && article.highlight.body_text.length > 0){
|
||||
var firstSnippetChar = article.highlight.body_text[0];
|
||||
var startingEllipsis = ""
|
||||
if (firstSnippetChar.toLowerCase() != firstSnippetChar.toUpperCase()){
|
||||
startingEllipsis = "…"
|
||||
if (article._snippetResult && article._snippetResult.body_text){
|
||||
if (article._snippetResult.body_text.matchLevel != "none"){
|
||||
var firstSnippetChar = article._snippetResult.body_text.value[0];
|
||||
var startingEllipsis = ""
|
||||
if (firstSnippetChar.toLowerCase() != firstSnippetChar.toUpperCase()){
|
||||
startingEllipsis = "…"
|
||||
}
|
||||
bodyTextSnippet = startingEllipsis+article._snippetResult.body_text.value + "…"
|
||||
}
|
||||
bodyTextSnippet = startingEllipsis + article.highlight.body_text.join("...") + "…";
|
||||
if (bodyTextSnippet.length > 0){
|
||||
searchSnippetHTML = '<div class="search-snippet"><span>' + bodyTextSnippet + '</span></div>'
|
||||
if (article._snippetResult.comments_blob.matchLevel != "none" && bodyTextSnippet === ""){
|
||||
var firstSnippetChar = article._snippetResult.comments_blob.value[0];
|
||||
var startingEllipsis = ""
|
||||
if (firstSnippetChar.toLowerCase() != firstSnippetChar.toUpperCase()){
|
||||
startingEllipsis = "…"
|
||||
}
|
||||
commentsBlobSnippet = startingEllipsis+article._snippetResult.comments_blob.value + "… <i>(comments)</i>"
|
||||
}
|
||||
if ((bodyTextSnippet.length > 0 || commentsBlobSnippet.length > 0) && article.class_name == "Article"){
|
||||
searchSnippetHTML = '<div class="search-snippet"><span>'+bodyTextSnippet+commentsBlobSnippet+'</span></div>'
|
||||
}
|
||||
}
|
||||
var saveButton = '';
|
||||
|
|
@ -129,7 +127,7 @@ function buildArticleHTML(article) {
|
|||
'+searchSnippetHTML+'\
|
||||
</div>\
|
||||
</a>\
|
||||
<h4><a href="/'+profileUsername+'">'+filterXSS(userName)+publishDate+timeAgoInWords+'</a></h4>\
|
||||
<h4><a href="/'+article.user.username+'">'+filterXSS(article.user.name)+publishDate+timeAgoInWords+'</a></h4>\
|
||||
<div class="tags">'+tagString+'</div>\
|
||||
'+commentsCountHTML+reactionsCountHTML+readingTimeHTML+'\
|
||||
'+saveButton+'</div>';
|
||||
|
|
|
|||
|
|
@ -17,15 +17,15 @@ class SearchController < ApplicationController
|
|||
per_page
|
||||
].freeze
|
||||
|
||||
FEED_PARAMS = [
|
||||
:class_name,
|
||||
:page,
|
||||
:per_page,
|
||||
:search_fields,
|
||||
:sort_by,
|
||||
:tag_names,
|
||||
:user_id,
|
||||
{ published_at: [:gte] },
|
||||
FEED_PARAMS = %i[
|
||||
page
|
||||
per_page
|
||||
published_at
|
||||
search_fields
|
||||
sort_by
|
||||
tag_names
|
||||
user_id
|
||||
class_name
|
||||
].freeze
|
||||
|
||||
def tags
|
||||
|
|
@ -65,7 +65,7 @@ class SearchController < ApplicationController
|
|||
# No need to check for articles or podcast episodes if we know we only want users
|
||||
user_search
|
||||
else
|
||||
# if params[:class_name] == PodcastEpisode, Article, or Comment then skip user lookup
|
||||
# if params[:class_name] == PodcastEpisode or Article then skip user lookup
|
||||
feed_content_search
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import render from 'preact-render-to-json';
|
|||
import { shallow, deep } from 'preact-render-spy';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import ArticleForm from '../articleForm';
|
||||
import algoliasearch from '../elements/__mocks__/algoliasearch';
|
||||
|
||||
const dummyArticleUpdatedAt = new Date();
|
||||
const getArticleForm = () => (
|
||||
|
|
@ -37,6 +38,8 @@ describe('<ArticleForm />', () => {
|
|||
|
||||
global.document.body.innerHTML = "<div id='editor-help-guide'></div>";
|
||||
|
||||
global.window.algoliasearch = algoliasearch;
|
||||
|
||||
localStorage.clear();
|
||||
/* eslint-disable-next-line no-underscore-dangle */
|
||||
localStorage.__STORE__ = {};
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ export const Article = ({
|
|||
const timeAgoIndicator = timeAgo({
|
||||
oldTimeInSeconds: article.published_at_int,
|
||||
formatter: x => x,
|
||||
});
|
||||
})
|
||||
|
||||
return (
|
||||
<div
|
||||
|
|
@ -74,7 +74,7 @@ export const Article = ({
|
|||
<ContentTitle article={article} currentTag={currentTag} />
|
||||
{article.class_name === 'Article' && (
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
<SearchSnippet highlightText={article.highlight} />
|
||||
<SearchSnippet snippetResult={article._snippetResult} />
|
||||
)}
|
||||
</div>
|
||||
</a>
|
||||
|
|
@ -94,7 +94,7 @@ export const Article = ({
|
|||
)}
|
||||
{article.published_at_int ? (
|
||||
<span className="time-ago-indicator">
|
||||
{timeAgoIndicator.length > 0 ? `(${timeAgoIndicator})` : ''}
|
||||
{timeAgoIndicator.length > 0 ? `(${timeAgoIndicator})`: '' }
|
||||
</span>
|
||||
) : null}
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -1101,7 +1101,7 @@ exports[`<Article /> component should render with a snippet result 1`] = `
|
|||
class="search-snippet"
|
||||
>
|
||||
<span>
|
||||
…copying Rest withdrawal Handcrafted multi-state Pre-emptive e-markets feed...overriding RSS Fantastic Plastic Gloves invoice productize systemic Monaco…
|
||||
…copying Rest withdrawal Handcrafted multi-state Pre-emptive e-markets feed overriding RSS Fantastic Plastic Gloves invoice productize systemic Monaco…
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ export const article = {
|
|||
},
|
||||
published_at_int: 1582037964819,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 14:59:24 GMT',
|
||||
published_at: '2020-02-18T14:59:24Z',
|
||||
readable_publish_date: 'February 18',
|
||||
};
|
||||
|
||||
|
|
@ -85,13 +84,18 @@ export const articleWithSnippetResult = {
|
|||
},
|
||||
published_at_int: 1582037964819,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 14:59:24 GMT',
|
||||
published_at: '2020-02-18T14:59:24Z',
|
||||
readable_publish_date: 'February 18',
|
||||
highlight: {
|
||||
body_text: [
|
||||
'copying Rest withdrawal Handcrafted multi-state Pre-emptive e-markets feed',
|
||||
'overriding RSS Fantastic Plastic Gloves invoice productize systemic Monaco',
|
||||
],
|
||||
_snippetResult: {
|
||||
body_text: {
|
||||
matchLevel: 'full',
|
||||
value:
|
||||
'copying Rest withdrawal Handcrafted multi-state Pre-emptive e-markets feed overriding RSS Fantastic Plastic Gloves invoice productize systemic Monaco',
|
||||
},
|
||||
comments_blob: {
|
||||
matchLevel: 'none',
|
||||
value:
|
||||
'virtual index Global mindshare Malagasy Ariary back up Handmade green Interactions violet bandwidth Chief e-commerce front-end neural',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -119,7 +123,6 @@ export const articleWithReactions = {
|
|||
},
|
||||
published_at_int: 1582037964819,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 14:59:24 GMT',
|
||||
published_at: '2020-03-19T10:04:15-05:00',
|
||||
readable_publish_date: 'February 18',
|
||||
positive_reactions_count: 232,
|
||||
};
|
||||
|
|
@ -148,7 +151,6 @@ export const articleWithComments = {
|
|||
},
|
||||
published_at_int: 1582037964819,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 14:59:24 GMT',
|
||||
published_at: '2020-03-19T10:04:15-05:00',
|
||||
readable_publish_date: 'February 18',
|
||||
positive_reactions_count: 428,
|
||||
comments_count: 213,
|
||||
|
|
@ -178,7 +180,6 @@ export const articleWithReadingTimeGreaterThan1 = {
|
|||
},
|
||||
published_at_int: 1582037964819,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 14:59:24 GMT',
|
||||
published_at: '2020-02-18T14:59:24Z',
|
||||
readable_publish_date: 'February 18',
|
||||
reading_time: 8,
|
||||
};
|
||||
|
|
@ -207,7 +208,6 @@ export const videoArticle = {
|
|||
},
|
||||
published_at_int: 1582038662478,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 15:11:02 GMT',
|
||||
published_at: '2020-02-18T14:59:24Z',
|
||||
readable_publish_date: 'February 18',
|
||||
cloudinary_video_url: '/images/onboarding-background.png',
|
||||
video_duration_in_minutes: 10,
|
||||
|
|
@ -237,7 +237,6 @@ export const podcastArticle = {
|
|||
},
|
||||
published_at_int: 1582038662478,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 15:11:02 GMT',
|
||||
published_at: '2020-02-18T15:11:02Z',
|
||||
readable_publish_date: 'February 18',
|
||||
podcast: {
|
||||
slug: 'monitor-recontextualize',
|
||||
|
|
@ -271,7 +270,6 @@ export const podcastEpisodeArticle = {
|
|||
},
|
||||
published_at_int: 1582038662478,
|
||||
published_timestamp: 'Tue, 18 Feb 2020 15:11:02 GMT',
|
||||
published_at: '2020-02-18T15:11:02Z',
|
||||
readable_publish_date: 'February 18',
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,45 @@
|
|||
import { h } from 'preact';
|
||||
import { articleSnippetResultPropTypes } from '../../src/components/common-prop-types';
|
||||
|
||||
export const SearchSnippet = ({ highlightText }) => {
|
||||
if (highlightText && highlightText.body_text.length > 0) {
|
||||
const hitHighlights = highlightText.body_text;
|
||||
export const SearchSnippet = ({ snippetResult }) => {
|
||||
if (snippetResult && snippetResult.body_text) {
|
||||
let bodyTextSnippet = '';
|
||||
|
||||
if (hitHighlights[0]) {
|
||||
const firstSnippetChar = hitHighlights[0];
|
||||
if (snippetResult.body_text.matchLevel !== 'none') {
|
||||
const firstSnippetChar = snippetResult.body_text.value[0];
|
||||
|
||||
let startingEllipsis = '';
|
||||
if (firstSnippetChar.toLowerCase() !== firstSnippetChar.toUpperCase()) {
|
||||
startingEllipsis = '…';
|
||||
}
|
||||
bodyTextSnippet = `${startingEllipsis + hitHighlights.join('...')}…`;
|
||||
|
||||
bodyTextSnippet = `${startingEllipsis + snippetResult.body_text.value}…`;
|
||||
}
|
||||
|
||||
if (bodyTextSnippet.length > 0) {
|
||||
let commentsBlobSnippet = '';
|
||||
|
||||
if (
|
||||
snippetResult.comments_blob.matchLevel !== 'none' &&
|
||||
bodyTextSnippet === ''
|
||||
) {
|
||||
const firstSnippetChar = snippetResult.comments_blob.value[0];
|
||||
let startingEllipsis = '';
|
||||
|
||||
if (firstSnippetChar.toLowerCase() !== firstSnippetChar.toUpperCase()) {
|
||||
startingEllipsis = '…';
|
||||
}
|
||||
|
||||
commentsBlobSnippet = `${startingEllipsis +
|
||||
snippetResult.comments_blob.value}… <i>(comments)</i>`;
|
||||
}
|
||||
|
||||
if (bodyTextSnippet.length > 0 || commentsBlobSnippet.length > 0) {
|
||||
return (
|
||||
<div className="search-snippet">
|
||||
<span>{bodyTextSnippet}</span>
|
||||
<span>
|
||||
{bodyTextSnippet}
|
||||
{commentsBlobSnippet}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -30,7 +50,7 @@ export const SearchSnippet = ({ highlightText }) => {
|
|||
|
||||
SearchSnippet.propTypes = {
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
highlightText: articleSnippetResultPropTypes.isRequired,
|
||||
snippetResult: articleSnippetResultPropTypes.isRequired,
|
||||
};
|
||||
|
||||
SearchSnippet.displayName = 'SearchSnippet';
|
||||
|
|
|
|||
|
|
@ -3,7 +3,14 @@ import { tagPropTypes } from './tag-prop-types';
|
|||
import { organizationPropType } from './organization-prop-type';
|
||||
|
||||
export const articleSnippetResultPropTypes = PropTypes.shape({
|
||||
body_text: PropTypes.arrayOf(PropTypes.string),
|
||||
body_text: PropTypes.shape({
|
||||
matchLevel: PropTypes.oneOf(['full', 'none']),
|
||||
value: PropTypes.string.isRequired,
|
||||
}),
|
||||
comments_blob: PropTypes.shape({
|
||||
matchLevel: PropTypes.oneOf(['full', 'none']),
|
||||
value: PropTypes.string.isRequired,
|
||||
}),
|
||||
});
|
||||
|
||||
export const articlePropTypes = PropTypes.shape({
|
||||
|
|
@ -28,7 +35,7 @@ export const articlePropTypes = PropTypes.shape({
|
|||
name: PropTypes.string.isRequired,
|
||||
}),
|
||||
organization: organizationPropType,
|
||||
highlight: articleSnippetResultPropTypes,
|
||||
_snippetResult: articleSnippetResultPropTypes,
|
||||
positive_reactions_count: PropTypes.number,
|
||||
reactions_count: PropTypes.number,
|
||||
comments_count: PropTypes.number,
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ describe('Search utilities', () => {
|
|||
describe('preloadSearchResults', () => {
|
||||
beforeEach(() => {
|
||||
global.InstantClick = {
|
||||
preload: (url) => url,
|
||||
preload: url => url,
|
||||
};
|
||||
jest.spyOn(InstantClick, 'preload');
|
||||
});
|
||||
|
|
@ -145,7 +145,7 @@ describe('Search utilities', () => {
|
|||
describe('displaySearchResults', () => {
|
||||
beforeEach(() => {
|
||||
global.InstantClick = {
|
||||
display: (url) => url,
|
||||
display: url => url,
|
||||
};
|
||||
jest.spyOn(InstantClick, 'display');
|
||||
});
|
||||
|
|
@ -242,7 +242,7 @@ describe('Search utilities', () => {
|
|||
});
|
||||
|
||||
test('should return response formatted as JSON', () => {
|
||||
responsePromise.then((response) => {
|
||||
responsePromise.then(response => {
|
||||
expect(response).toBeInstanceOf(Object);
|
||||
expect(response).toMatchObject({ results: expect.any(Array) });
|
||||
});
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ function fixedEncodeURIComponent(str) {
|
|||
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
|
||||
return encodeURIComponent(str).replace(
|
||||
/[!'()*]/g,
|
||||
(c) => `%${c.charCodeAt(0).toString(16)}`,
|
||||
c => `%${c.charCodeAt(0).toString(16)}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -98,5 +98,5 @@ export function fetchSearch(endpoint, dataHash) {
|
|||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
}).then((response) => response.json());
|
||||
}).then(response => response.json());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,13 +16,16 @@
|
|||
|
||||
var params = getQueryParams(document.location.search);
|
||||
|
||||
// var query = escape(params.q);
|
||||
function searchMain() {
|
||||
var query = filterXSS(params.q);
|
||||
var filters = filterXSS(params.filters || "");
|
||||
document.getElementById("substories").innerHTML = '<div class="query-results-nothing"><div class="query-results-loader"></div><br/></div>'
|
||||
var client = algoliasearch('<%= ApplicationConfig["ALGOLIASEARCH_APPLICATION_ID"] %>', '<%= ALGOLIASEARCH_PUBLIC_SEARCH_ONLY_KEY %>');
|
||||
var index = client.initIndex('searchables_<%= Rails.env %>');
|
||||
if (document.getElementById("query-wrapper")) {
|
||||
search(query, filters);
|
||||
initializeFilters(query, filters);
|
||||
search(query, index, filters);
|
||||
initializeFilters(query, index, filters);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -30,7 +33,7 @@
|
|||
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
}
|
||||
|
||||
function initializeFilters(query, filters) {
|
||||
function initializeFilters(query, index, filters) {
|
||||
var filterButts = document.getElementsByClassName("query-filter-button");
|
||||
for (var i = 0; i < filterButts.length; i++) {
|
||||
if (filters == filterButts[i].dataset.filter) {
|
||||
|
|
@ -50,58 +53,60 @@
|
|||
if (className.indexOf("selected") == -1) {
|
||||
e.target.classList.add("selected");
|
||||
window.history.replaceState(null, null, "/search?q=" + query + "&filters=" + filters);
|
||||
search(query, filters);
|
||||
search(query, index, filters);
|
||||
} else {
|
||||
window.history.replaceState(null, null, "/search?q=" + query);
|
||||
search(query, "");
|
||||
search(query, index, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function search(query, filters) {
|
||||
var hashtags = query.match(/#\w+/g);
|
||||
var searchTerm = query.replace(/#/g, '').trim();
|
||||
var searchHash = { per_page: 60, page: 0 };
|
||||
function search(query, index, filters) {
|
||||
var hashtags = query.match(/#\w+/g)
|
||||
var searchObj = {
|
||||
hitsPerPage: 60,
|
||||
page: "0",
|
||||
queryType: "prefixNone",
|
||||
attributesToRetrieve: [
|
||||
'id',
|
||||
'title',
|
||||
'path',
|
||||
'class_name',
|
||||
'comments_count',
|
||||
'tag_list',
|
||||
'readable_publish_date',
|
||||
'positive_reactions_count',
|
||||
'flare_tag',
|
||||
'user',
|
||||
'reading_time'
|
||||
],
|
||||
attributesToHighlight: [],
|
||||
exactOnSingleWordQuery: "none",
|
||||
attributesToSnippet: ['body_text:19', 'comments_blob:8'],
|
||||
}
|
||||
|
||||
if (hashtags && hashtags.length > 0) {
|
||||
if (filters === "MY_POSTS" && userData()) {
|
||||
searchObj["tagFilters"] = [['user_' + userData()['id']]]
|
||||
} else if (hashtags != null && hashtags.length > 0) {
|
||||
for(var i = 0; i < hashtags.length; i++){
|
||||
hashtags[i] = hashtags[i].replace(/#/,'');
|
||||
hashtags[i] = hashtags[i].replace(/#/,'')
|
||||
}
|
||||
searchHash.tag_names = hashtags;
|
||||
searchObj["tagFilters"] = hashtags
|
||||
} else {
|
||||
searchObj["filters"] = filters
|
||||
}
|
||||
|
||||
if (filters) {
|
||||
filters.split('&').forEach((filter) => {
|
||||
const [key, value] = filter.split(':');
|
||||
searchHash[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
if (searchTerm) { searchHash.search_fields = searchTerm; }
|
||||
|
||||
const searchParams = new URLSearchParams(searchHash).toString();
|
||||
|
||||
fetch(`/search/feed_content?${searchParams}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'X-CSRF-Token': window.csrfToken,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then((content) => {
|
||||
index.search(query, searchObj)
|
||||
.then(function searchDone(content) {
|
||||
var resultDivs = []
|
||||
content.result.forEach(function (story, i) {
|
||||
content.hits.forEach(function (story, i) {
|
||||
resultDivs.push(buildArticleHTML(story));
|
||||
})
|
||||
document.getElementById("substories").innerHTML = resultDivs.join("");
|
||||
initializeReadingListIcons();
|
||||
initializeAllFollowButts();
|
||||
document.getElementById("substories").classList.add("search-results-loaded");
|
||||
if (content.result.length == 0) {
|
||||
if (content.hits.length == 0) {
|
||||
document.getElementById("substories").innerHTML = '<div class="query-results-nothing">No results match that query</div>'
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
data-params="<%= params.to_json(only: %i[tag username q]) %>" data-which="<%= @list_of %>"
|
||||
data-tag=""
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime&.iso8601 %>">
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>">
|
||||
|
||||
<%= render "articles/sidebar" %>
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
data-params="<%= params.to_json(only: %i[tag username q]) %>" data-which="<%= @list_of %>"
|
||||
data-tag=""
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime&.iso8601 %>">
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>">
|
||||
<%= render "articles/search/sidebar" %>
|
||||
<div class="articles-list" id="articles-list">
|
||||
<div class="substories" id="substories">
|
||||
|
|
|
|||
|
|
@ -9,9 +9,8 @@
|
|||
<a class="query-filter-button" data-filter="class_name:Article">POSTS</a>
|
||||
<a class="query-filter-button" data-filter="class_name:PodcastEpisode">PODCASTS</a>
|
||||
<a class="query-filter-button" data-filter="class_name:User">PEOPLE</a>
|
||||
<a class="query-filter-button" data-filter="class_name:Comment">COMMENTS</a>
|
||||
<hr>
|
||||
<a class="query-filter-button my-posts-query-button" data-filter="class_name:Article&user_id:<%= current_user&.id %>">ONLY MY POSTS</a>
|
||||
<a class="query-filter-button my-posts-query-button" data-filter="MY_POSTS">ONLY MY POSTS</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@
|
|||
data-tag="<%= @tag %>"
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-requires-approval="<%= @tag_model.requires_approval %>"
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime&.iso8601 %>">
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>">
|
||||
<%= render "articles/tags/sidebar" %>
|
||||
<div class="articles-list" id="articles-list">
|
||||
<% if @tag_model && @tag_model.supported %>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
data-params="<%= params.to_json(only: %i[tag username q]) %>" data-which="<%= @list_of %>"
|
||||
data-tag="<%= "organization_#{@organization.id}" %>"
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime&.iso8601 %>">
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>">
|
||||
<%= render "organizations/sidebar" %>
|
||||
<div class="articles-list" id="articles-list">
|
||||
<div class="substories" id="substories">
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@
|
|||
data-params="<%= params.to_json(only: %i[tag username q]) %>" data-which="<%= @list_of %>"
|
||||
data-tag=""
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime&.iso8601 %>">
|
||||
data-articles-since="<%= Timeframer.new(params[:timeframe]).datetime.to_i %>">
|
||||
<%= render "podcast_episodes/sidebar" %>
|
||||
<div class="articles-list" id="articles-list">
|
||||
<div class="substories" id="substories">
|
||||
|
|
|
|||
|
|
@ -171,8 +171,8 @@
|
|||
<% end %>
|
||||
|
||||
<div class="home sub-home" id="index-container"
|
||||
data-params="<%= params.merge(user_id: @user.id, class_name: "Article").to_json(only: %i[tag user_id search_fields class_name]) %>" data-which="<%= @list_of %>"
|
||||
data-tag=""
|
||||
data-params="<%= params.to_json(only: %i[tag username q]) %>" data-which="<%= @list_of %>"
|
||||
data-tag="<%= "user_#{@user.id}" %>"
|
||||
data-feed="<%= params[:timeframe] || "base-feed" %>"
|
||||
data-articles-since="0">
|
||||
<%= render "users/sidebar" %>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue