Skip to content

Commit

Permalink
Subsite support (#1472)
Browse files Browse the repository at this point in the history
* subsite support pt1

* subsite support

* pr comment
  • Loading branch information
jpope19 authored Aug 19, 2019
1 parent f180811 commit 0588075
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
55 changes: 43 additions & 12 deletions lib/nav/page-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@
*/
function getInitialSites() {
const configSites = _.get(this.$store, 'state.url.sites'),
selectedSites = configSites ? configSites.split(',') : [];
selectedSites = configSites ? configSites.split(',') : [],
currentSlug = _.get(this.$store, 'state.site.subsiteSlug') || _.get(this.$store, 'state.site.slug');
// make an array of all sites, sorted by slug
return _.sortBy(_.map(_.get(this.$store, 'state.allSites'), (site) => {
const siteSlug = site.subsiteSlug || site.slug;
return {
slug: site.slug,
subsiteSlug: site.subsiteSlug,
name: site.name,
selected: selectedSites.length ? _.includes(selectedSites, site.slug) : site.slug === _.get(this.$store, 'state.site.slug')
selected: selectedSites.length ? _.includes(selectedSites, siteSlug) : siteSlug === currentSlug
};
}), 'name');
}
Expand All @@ -77,7 +81,7 @@
* @param {string} username
* @return {object}
*/
function buildQuery({ siteFilter, queryText, queryUser, offset, statusFilter, isMyPages, username }) { // eslint-disable-line
function buildQuery({ siteFilter, queryText, queryUser, offset, statusFilter, isMyPages, username, subsiteFilter }) { // eslint-disable-line
let query = {
index: 'pages',
body: {
Expand Down Expand Up @@ -130,21 +134,44 @@
});
}
// filter by selected sites
// filter by selected sites with subsite support
const siteFilterShould = {
bool: {
should: []
}
};
if (siteFilter.length) {
query.body.query.bool.must.push({
siteFilterShould.bool.should.push({
terms: {
siteSlug: siteFilter
}
});
} else {
query.body.query.bool.must.push({
siteFilterShould.bool.should.push({
terms: {
siteSlug: ['no-site-selected']
}
});
}
if (subsiteFilter.length) {
siteFilterShould.bool.should.push({
terms: {
subsiteSlug: subsiteFilter
}
});
} else {
// slug is shared between parent and subsites. if parent is selected, we don't want to bring subsite pages back
query.body.query.bool.must_not = {
exists: {
field: 'subsiteSlug'
}
};
}
query.body.query.bool.must.push(siteFilterShould);
// filter by search string
if (queryText) {
query.body.query.bool.must.push({
Expand Down Expand Up @@ -258,11 +285,14 @@
onPopoverClose() {
this.isPopoverOpen = false;
},
compareSite(site, slug) {
return site.subsiteSlug === slug || site.slug === slug;
},
selectSite(slug) {
const site = _.find(this.sites, s => s.slug === slug);
const site = _.find(this.sites, s => this.compareSite(s, slug));
site.selected = !site.selected;
this.$store.commit('FILTER_PAGELIST_SITE', _.map(this.selectedSites, site => site.slug).join(', '));
this.$store.commit('FILTER_PAGELIST_SITE', _.map(this.selectedSites, site => site.subsiteSlug || site.slug).join(', '));
this.offset = 0;
this.fetchPages();
},
Expand All @@ -276,14 +306,14 @@
setSingleSite(slug) {
// loop through all sites, making sure that only one is selected
_.each(this.sites, (site) => {
if (site.slug === slug) {
if (this.compareSite(site, slug)) {
site.selected = true;
} else {
site.selected = false;
}
});
this.$store.commit('FILTER_PAGELIST_SITE', _.map(this.selectedSites, site => site.slug).join(', '));
this.$store.commit('FILTER_PAGELIST_SITE', _.map(this.selectedSites, site => site.subsiteSlug || site.slug).join(', '));
this.offset = 0;
this.fetchPages();
},
Expand All @@ -305,7 +335,8 @@
this.fetchPages();
},
fetchPages() {
const siteFilter = _.map(this.selectedSites, site => site.slug),
const siteFilter = _.map(_.filter(this.selectedSites, site => !site.subsiteSlug), site => site.slug),
subsiteFilter = _.map(_.filter(this.selectedSites, site => site.subsiteSlug), site => site.subsiteSlug),
queryText = this.queryText,
queryUser = this.queryUser,
offset = this.offset,
Expand All @@ -314,7 +345,7 @@
username = _.get(this.$store, 'state.user.username'),
statusFilter = this.selectedStatus,
query = buildQuery({
siteFilter, queryText, queryUser, offset, statusFilter, isMyPages, username
siteFilter, queryText, queryUser, offset, statusFilter, isMyPages, username, subsiteFilter
});
return postJSON(prefix + searchRoute, query).then((res) => {
Expand Down
5 changes: 4 additions & 1 deletion lib/nav/site-selector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<template>
<div class="site-selector">
<div class="site-selector-body">
<ui-checkbox v-for="site in sites" color="accent" :key="site.slug" :label="site.name" :value="site.selected" @change="select(site.slug)"></ui-checkbox>
<ui-checkbox v-for="site in sites" color="accent" :key="siteSlug(site)" :label="site.name" :value="site.selected" @change="select(siteSlug(site))"></ui-checkbox>
</div>
<div class="site-selector-footer">
<ui-button type="secondary" :color="multiSelectColor" @click="multiSelect">{{ multiSelectText }}</ui-button>
Expand Down Expand Up @@ -59,6 +59,9 @@
},
select(slug) {
this.$emit('select', slug);
},
siteSlug(site) {
return site.subsiteSlug || site.slug;
}
},
components: {
Expand Down
5 changes: 3 additions & 2 deletions lib/preloader/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export default function getSites(currentSite) {
if (prefix) {
return getObject(`${prefix}/_sites`).then((res) => {
return _.reduce(res, (sites, rawSite) => {
const slug = _.get(rawSite, '_source.slug');
const slug = _.get(rawSite, '_source.slug'),
subsiteSlug = _.get(rawSite, '_source.subsiteSlug');

sites[slug] = rawSite._source;
sites[subsiteSlug || slug] = rawSite._source;

return sites;
}, {});
Expand Down

0 comments on commit 0588075

Please sign in to comment.