Skip to content

Commit

Permalink
Load site data asynchronously (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Chin authored and yamgent committed Feb 4, 2019
1 parent e66010c commit 75dbcf5
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 18 deletions.
26 changes: 17 additions & 9 deletions asset/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function setupAnchors() {
function removeLoadingOverlay() {
jQuery('#loading-overlay').remove();
}

function updateSearchData(vm) {
jQuery.getJSON(`${baseUrl}/siteData.json`)
.then((siteData) => {
// eslint-disable-next-line no-param-reassign
vm.searchData = siteData.pages;
});
}

function executeAfterMountedRoutines() {
flattenModals();
scrollToUrlAnchorHeading();
Expand Down Expand Up @@ -84,11 +93,7 @@ function setup() {
setupPageNav();
}

function setupWithSearch(siteData) {
if (!siteData.enableSearch) {
setup();
return;
}
function setupWithSearch() {
const { searchbar } = VueStrap.components;
// eslint-disable-next-line no-unused-vars
const vm = new Vue({
Expand All @@ -98,7 +103,7 @@ function setupWithSearch(siteData) {
},
data() {
return {
searchData: siteData.pages,
searchData: [],
};
},
methods: {
Expand All @@ -110,12 +115,15 @@ function setupWithSearch(siteData) {
},
mounted() {
executeAfterMountedRoutines();
updateSearchData(this);
},
});
setupSiteNav();
setupPageNav();
}

jQuery.getJSON(`${baseUrl}/siteData.json`)
.then(siteData => setupWithSearch(siteData))
.catch(() => setup());
if (enableSearch) {
setupWithSearch();
} else {
setup();
}
2 changes: 2 additions & 0 deletions src/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function Page(pageConfig) {
this.layout = pageConfig.layout;
this.layoutsAssetPath = pageConfig.layoutsAssetPath;
this.rootPath = pageConfig.rootPath;
this.enableSearch = pageConfig.enableSearch;
this.searchable = pageConfig.searchable;
this.src = pageConfig.src;
this.tags = pageConfig.tags;
Expand Down Expand Up @@ -234,6 +235,7 @@ Page.prototype.prepareTemplateData = function () {
pageNav: this.isPageNavigationSpecifierValid(),
siteNav: this.frontMatter.siteNav,
title: prefixedTitle,
enableSearch: this.enableSearch,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Site.prototype.createPage = function (config) {
tags: this.siteConfig.tags,
pageTemplate: this.pageTemplate,
rootPath: this.rootPath,
enableSearch: this.siteConfig.enableSearch,
searchable: this.siteConfig.enableSearch && config.searchable,
src: config.pageSrc,
layoutsAssetPath: path.relative(path.dirname(resultPath),
Expand Down
1 change: 1 addition & 0 deletions src/template/page.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<script src="<%- asset.bootstrapVueJs %>"></script>
<script>
const baseUrl = '<%- baseUrl %>'
const enableSearch = <%- enableSearch %>
</script>
<script src="<%- asset.setup %>"></script>
<% if (asset.externalScripts) { -%>
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_site/expected/bugs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ <h2 id="remove-extra-space-in-links">Remove extra space in links<a class="fa fa-
<script src="../markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="../markbind/js/setup.js"></script>
<script src="../markbind/layouts/default/scripts.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_site/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ <h1 id="heading-in-footer-should-not-be-indexed">Heading in footer should not be
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/default/scripts.js"></script>
Expand Down
26 changes: 17 additions & 9 deletions test/functional/test_site/expected/markbind/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function setupAnchors() {
function removeLoadingOverlay() {
jQuery('#loading-overlay').remove();
}

function updateSearchData(vm) {
jQuery.getJSON(`${baseUrl}/siteData.json`)
.then((siteData) => {
// eslint-disable-next-line no-param-reassign
vm.searchData = siteData.pages;
});
}

function executeAfterMountedRoutines() {
flattenModals();
scrollToUrlAnchorHeading();
Expand Down Expand Up @@ -84,11 +93,7 @@ function setup() {
setupPageNav();
}

function setupWithSearch(siteData) {
if (!siteData.enableSearch) {
setup();
return;
}
function setupWithSearch() {
const { searchbar } = VueStrap.components;
// eslint-disable-next-line no-unused-vars
const vm = new Vue({
Expand All @@ -98,7 +103,7 @@ function setupWithSearch(siteData) {
},
data() {
return {
searchData: siteData.pages,
searchData: [],
};
},
methods: {
Expand All @@ -110,12 +115,15 @@ function setupWithSearch(siteData) {
},
mounted() {
executeAfterMountedRoutines();
updateSearchData(this);
},
});
setupSiteNav();
setupPageNav();
}

jQuery.getJSON(`${baseUrl}/siteData.json`)
.then(siteData => setupWithSearch(siteData))
.catch(() => setup());
if (enableSearch) {
setupWithSearch();
} else {
setup();
}
1 change: 1 addition & 0 deletions test/functional/test_site/expected/sub_site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h2 id="feature-list">Feature list<a class="fa fa-anchor" href="#feature-list"><
<script src="../markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="../markbind/js/setup.js"></script>
<script src="../markbind/layouts/default/scripts.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/default/scripts.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_site/expected/testLayouts.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ <h1 id="uses-a-front-matter-layout">Uses a front matter layout<a class="fa fa-an
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/testLayout/scripts.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ <h1 id="uses-a-site-json-layout-overriding-front-matter">Uses a site.json layout
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/testLayout/scripts.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_site/expected/testTags.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h1 id="div-with-hidden-tag">Div with hidden tag<a class="fa fa-anchor" href="#d
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/default/scripts.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_site/expected/test_md_fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h1 id="some-heading">Some heading<a class="fa fa-anchor" href="#some-heading"><
<script src="markbind/js/bootstrap-vue.min.js"></script>
<script>
const baseUrl = '/test_site'
const enableSearch = true
</script>
<script src="markbind/js/setup.js"></script>
<script src="markbind/layouts/default/scripts.js"></script>
Expand Down

0 comments on commit 75dbcf5

Please sign in to comment.