From 45c6c5e48f84741fef7b9120fb92074242b2ebec Mon Sep 17 00:00:00 2001 From: Taka Date: Sun, 18 Apr 2021 20:13:31 +1000 Subject: [PATCH] Load content docs on demand. --- asl_rulebook2/webapp/static/ContentPane.js | 42 +++++++++++++++++++--- asl_rulebook2/webapp/static/MainApp.js | 16 ++++++--- asl_rulebook2/webapp/tests/test_startup.py | 10 ------ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/asl_rulebook2/webapp/static/ContentPane.js b/asl_rulebook2/webapp/static/ContentPane.js index cbaaef0..4921d0b 100644 --- a/asl_rulebook2/webapp/static/ContentPane.js +++ b/asl_rulebook2/webapp/static/ContentPane.js @@ -6,11 +6,14 @@ import { findTargets, showErrorMsg, showNotificationMsg, hideFootnotes } from ". gMainApp.component( "content-pane", { props: [ "contentDocs" ], + data() { return { + loadedContentDocs: {}, + } ; }, template: `
- + @@ -34,16 +37,47 @@ gMainApp.component( "content-pane", { mounted() { const showContentDoc = (cdocId) => { - this.$refs.tabbedPages.activateTab( cdocId ) ; // nb: tabId == cdocId + // check if the content doc has already been loaded + let cdoc = this.loadedContentDocs[ cdocId ] ; + if ( cdoc == undefined ) { + // nope - load it + this.loadedContentDocs[ cdocId ] = this.contentDocs[ cdocId ] ; + } + this.$nextTick( () => { + this.$refs.tabbedPages.activateTab( cdocId ) ; // nb: tabId == cdocId + } ) ; + return (cdoc == undefined) ; } gEventBus.on( "show-target", (cdocId, ruleid) => { //eslint-disable-line no-unused-vars - showContentDoc( cdocId ) ; + let wasLoaded = showContentDoc( cdocId ) ; + if ( wasLoaded ) { + // FUDGE! If we just loaded a new content doc, it won't have been around to receive + // the "show-target" event, so we re-issue it here. This might cause some minor + // problems (e.g. footnotes showing twice), but we seem to be OK. + this.$nextTick( () => { + gEventBus.emit( "show-target", cdocId, ruleid ) ; + } ) ; + } } ) ; gEventBus.on( "show-page", (cdocId, pageNo) => { //eslint-disable-line no-unused-vars - showContentDoc( cdocId ) ; + let wasLoaded = showContentDoc( cdocId ) ; + if ( wasLoaded ) { + // FUDGE! If we just loaded a new content doc, it won't have been around to receive + // the "show-page" event, so we re-issue it here. + this.$nextTick( () => { + gEventBus.emit( "show-page", cdocId, pageNo ) ; + } ) ; + } } ) ; }, + beforeUpdate() { + // make sure the test empty document is loaded + let cdoc = this.contentDocs[ "empty" ] ; + if ( cdoc != undefined ) + this.loadedContentDocs[ "empty" ] = cdoc ; + }, + methods: { showFootnotes( footnotes ) { diff --git a/asl_rulebook2/webapp/static/MainApp.js b/asl_rulebook2/webapp/static/MainApp.js index 8d3344d..0ecbab9 100644 --- a/asl_rulebook2/webapp/static/MainApp.js +++ b/asl_rulebook2/webapp/static/MainApp.js @@ -115,11 +115,19 @@ gMainApp.component( "main-app", { resp["empty"] = { "cdoc_id": "empty", "title": "Empty document" } ; // nb: for testing porpoises self.contentDocs = resp ; self.installContentDocs( resp ) ; - // start off showing the first content doc - let cdocIds = Object.keys( resp ) ; - if ( cdocIds.length > 0 ) { + // start off showing the main ASL rulebook + // NOTE: To avoid forcing the user to configure which document this is, we assume that + // it's the one with the most targets. + let targetCdocId = null ; + for ( let cdocId in self.contentDocs ) { + if ( self.contentDocs[cdocId].targets == undefined ) + continue + if ( targetCdocId == null || Object.keys(self.contentDocs[cdocId].targets).length > Object.keys(self.contentDocs[targetCdocId].targets).length ) + targetCdocId = cdocId ; + } + if ( targetCdocId != null ) { Vue.nextTick( () => { - gEventBus.emit( "show-page", cdocIds[0], 1 ) ; // FIXME! which cdoc do we choose? + gEventBus.emit( "show-page", targetCdocId, 1 ) ; } ) ; } } ).catch( (errorMsg) => { diff --git a/asl_rulebook2/webapp/tests/test_startup.py b/asl_rulebook2/webapp/tests/test_startup.py index 10b7a7a..92a1bbc 100644 --- a/asl_rulebook2/webapp/tests/test_startup.py +++ b/asl_rulebook2/webapp/tests/test_startup.py @@ -19,16 +19,6 @@ def test_load_content_docs( webapp, webdriver ): # for it), and we degrade gracefully. assert len( find_children( "#content .tabbed-page" ) ) == 0 - # test handling of an invalid targets file - webapp.control_tests.set_data_dir( "invalid-targets" ) - init_webapp( webapp, webdriver, warnings=["Couldn't load \"test.targets\"."] ) - assert len( find_children( "#content .tabbed-page" ) ) == 1 - - # test handling of an invalid footnotes file - webapp.control_tests.set_data_dir( "invalid-footnotes" ) - init_webapp( webapp, webdriver, warnings=["Couldn't load \"test.footnotes\"."] ) - assert len( find_children( "#content .tabbed-page" ) ) == 1 - # --------------------------------------------------------------------- def test_init_search( webapp, webdriver ):