Use promises for downloads.

master
Pacman Ghost 3 years ago
parent 35f31b12ba
commit 411a06a0b6
  1. 30
      asl_rulebook2/webapp/static/ASOP.js
  2. 121
      asl_rulebook2/webapp/static/MainApp.js
  3. 10
      asl_rulebook2/webapp/static/NavPane.js
  4. 11
      asl_rulebook2/webapp/static/SearchPane.js
  5. 41
      asl_rulebook2/webapp/static/utils.js

@ -1,5 +1,5 @@
import { gMainApp, gASOPChapterIndex, gASOPSectionIndex, gEventBus } from "./MainApp.js" ;
import { getASOPChapterIdFromSectionId, wrapMatches, isChildOf } from "./utils.js" ;
import { getURL, getASOPChapterIdFromSectionId, wrapMatches, isChildOf } from "./utils.js" ;
let gSectionContentOverrides = {} ;
@ -65,9 +65,9 @@ gMainApp.component( "asop", {
this.sections = [] ;
this.isSingleSection = true ;
this.chapterId = "intro" ;
$.get( gGetASOPIntroUrl, (resp) => { //eslint-disable-line no-undef
getURL( gGetASOPIntroUrl ).then( (resp) => { //eslint-disable-line no-undef
this.sections = [ this.fixupContent( resp ) ] ;
} ).fail( (xhr, status, errorMsg) => {
} ).catch( (errorMsg) => {
// NOTE: We show the error in the content, not as a notification balloon.
this.sections = [ "Couldn't get the ASOP intro." + " <div class='pre'>" + errorMsg + "</div>" ] ;
} ) ;
@ -99,18 +99,14 @@ gMainApp.component( "asop", {
addSectionContent( sectionNo, contentOverride ) ;
} else {
// nope - download the section from the backend
new Promise( (resolve, reject) => {
let url = gGetASOPSectionUrl.replace( "SECTION_ID", sectionId ) ; //eslint-disable-line no-undef
$.get( url, (resp) => {
addSectionContent( sectionNo, resp ) ;
resolve() ;
} ).fail( (xhr, status, errorMsg) => {
// NOTE: We show the error in the content, not as a notification balloon.
this.sections[ sectionNo ] =
"Couldn't get ASOP section <tt>" + sectionId + "</tt>."
+ " <div class='pre'>" + errorMsg + "</div>" ;
reject() ;
} ) ;
let url = gGetASOPSectionUrl.replace( "SECTION_ID", sectionId ) ; //eslint-disable-line no-undef
getURL( url ).then( (resp) => {
addSectionContent( sectionNo, resp ) ;
} ).catch( (errorMsg) => {
// NOTE: We show the error in the content, not as a notification balloon.
this.sections[ sectionNo ] =
"Couldn't get ASOP section <tt>" + sectionId + "</tt>."
+ " <div class='pre'>" + errorMsg + "</div>" ;
} ) ;
}
} ) ;
@ -121,9 +117,9 @@ gMainApp.component( "asop", {
// show the specified ASOP section
let sectionId = section.section_id ;
let url = gGetASOPSectionUrl.replace( "SECTION_ID", sectionId ) ; //eslint-disable-line no-undef
$.get( url, (resp) => {
getURL( url ).then( (resp) => {
this.doShowASOPSection( chapter, section, resp ) ;
} ).fail( (xhr, status, errorMsg) => {
} ).catch( (errorMsg) => {
// NOTE: We show the error in the content, not as a notification balloon.
this.sections = [
"Couldn't get ASOP section <tt>"+sectionId+"</tt>." + " <div class='pre'>" + errorMsg + "</div>"

@ -1,4 +1,4 @@
import { showErrorMsg, showNotificationMsg } from "./utils.js" ;
import { getJSON, showErrorMsg, showNotificationMsg } from "./utils.js" ;
// parse any URL parameters
export let gUrlParams = new URLSearchParams( window.location.search.substring(1) ) ;
@ -74,80 +74,62 @@ gMainApp.component( "main-app", {
methods: {
getAppConfig() {
return new Promise( (resolve, reject) => {
// get the app config
$.getJSON( gGetAppConfigUrl, (resp) => { //eslint-disable-line no-undef
gAppConfig = resp ;
resolve() ;
} ).fail( (xhr, status, errorMsg) => {
let msg = "Couldn't get the app config." ;
showErrorMsg( msg + " <div class='pre'>" + errorMsg + "</div>" ) ;
reject( msg )
} ) ;
// get the app config
return getJSON( gGetAppConfigUrl ).then( (resp) => { //eslint-disable-line no-undef
gAppConfig = resp ;
} ).catch( (errorMsg) => {
this.showErrorMsg( "Couldn't get the app config.", errorMsg ) ;
} ) ;
},
getContentDocs( self ) {
return new Promise( (resolve, reject) => {
// get the content docs
$.getJSON( gGetContentDocsUrl, (resp) => { //eslint-disable-line no-undef
if ( gUrlParams.get( "add-empty-doc" ) )
resp["empty"] = { "cdoc_id": "empty", "title": "Empty document" } ; // nb: for testing porpoises
self.contentDocs = resp ;
self.installContentDocs( resp ) ;
let cdocIds = Object.keys( resp ) ;
if ( cdocIds.length > 0 ) {
Vue.nextTick( () => {
gEventBus.emit( "show-page", cdocIds[0], 1 ) ; // FIXME! which cdoc do we choose?
} ) ;
}
resolve() ;
} ).fail( (xhr, status, errorMsg) => {
let msg = "Couldn't get the content docs." ;
showErrorMsg( msg + " <div class='pre'>" + errorMsg + "</div>" ) ;
reject( msg )
} ) ;
// get the content docs
return getJSON( gGetContentDocsUrl ).then( (resp) => { //eslint-disable-line no-undef
// install the content docs
if ( gUrlParams.get( "add-empty-doc" ) )
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 ) {
Vue.nextTick( () => {
gEventBus.emit( "show-page", cdocIds[0], 1 ) ; // FIXME! which cdoc do we choose?
} ) ;
}
} ).catch( (errorMsg) => {
this.showErrorMsg( "Couldn't get the content docs.", errorMsg ) ;
} ) ;
},
getFootnoteIndex() {
return new Promise( (resolve, reject) => {
// get the footnotes
$.getJSON( gGetFootnotesUrl, (resp) => { //eslint-disable-line no-undef
gFootnoteIndex = resp ;
resolve() ;
} ).fail( (xhr, status, errorMsg) => {
let msg = "Couldn't get the footnote index." ;
showErrorMsg( msg + " <div class='pre'>" + errorMsg + "</div>" ) ;
reject( msg )
} ) ;
// get the footnotes
return getJSON( gGetFootnotesUrl ).then( (resp) => { //eslint-disable-line no-undef
gFootnoteIndex = resp ;
} ).catch( (errorMsg) => {
this.showErrorMsg( "Couldn't get the footnote index.", errorMsg ) ;
} ) ;
},
getASOP() {
return new Promise( (resolve, reject) => {
// get the ASOP
$.getJSON( gGetASOPUrl, (resp) => { //eslint-disable-line no-undef
this.asop = resp ;
// build an index of the ASOP chapters and sections
gASOPChapterIndex = {} ;
gASOPSectionIndex = {} ;
if ( resp.chapters ) {
resp.chapters.forEach( (chapter) => {
gASOPChapterIndex[ chapter.chapter_id ] = chapter ;
if ( chapter.sections ) {
chapter.sections.forEach( (section) => {
gASOPSectionIndex[ section.section_id ] = section ;
} ) ;
}
} ) ;
}
resolve() ;
} ).fail( (xhr, status, errorMsg) => {
let msg = "Couldn't get the ASOP." ;
showErrorMsg( msg + " <div class='pre'>" + errorMsg + "</div>" ) ;
reject( msg )
} ) ;
// get the ASOP
return getJSON( gGetASOPUrl ).then( (resp) => { //eslint-disable-line no-undef
this.asop = resp ;
// build an index of the ASOP chapters and sections
gASOPChapterIndex = {} ;
gASOPSectionIndex = {} ;
if ( resp.chapters ) {
resp.chapters.forEach( (chapter) => {
gASOPChapterIndex[ chapter.chapter_id ] = chapter ;
if ( chapter.sections ) {
chapter.sections.forEach( (section) => {
gASOPSectionIndex[ section.section_id ] = section ;
} ) ;
}
} ) ;
}
} ).catch( (errorMsg) => {
this.showErrorMsg( "Couldn't get the ASOP.", errorMsg ) ;
} ) ;
},
@ -185,8 +167,8 @@ gMainApp.component( "main-app", {
},
showStartupMsgs() {
$.getJSON( gGetStartupMsgsUrl, (resp) => { //eslint-disable-line no-undef
// show any startup messages
// show any startup messages
return getJSON( gGetStartupMsgsUrl ).then( (resp) => { //eslint-disable-line no-undef
[ "info", "warning", "error" ].forEach( (msgType) => {
if ( ! resp[msgType] )
return ;
@ -196,11 +178,16 @@ gMainApp.component( "main-app", {
showNotificationMsg( msgType, msg ) ;
} ) ;
} ) ;
} ).fail( (xhr, status, errorMsg) => { //eslint-disable-line no-unused-vars
showErrorMsg( "Couldn't get the startup messages." ) ;
} ).catch( (errorMsg) => {
this.showErrorMsg( "Couldn't get the startup messages.", errorMsg ) ;
} ) ;
},
showErrorMsg( msg, errorMsg ) {
// show an error notification balloon
showErrorMsg( msg + " <div class='pre'>" + errorMsg + "</div>" ) ;
},
onEscapePressed() {
// check if an image is currently zoomed
if ( $(".jquery-image-zoom").length > 0 ) {

@ -1,5 +1,5 @@
import { gMainApp, gAppConfig, gContentDocs, gEventBus } from "./MainApp.js" ;
import { getASOPChapterIdFromSectionId, showWarningMsg } from "./utils.js" ;
import { getJSON, getURL, getASOPChapterIdFromSectionId, showWarningMsg } from "./utils.js" ;
// --------------------------------------------------------------------
@ -38,12 +38,12 @@ gMainApp.component( "nav-pane", {
// the right thing to do - what if there is a ruleid that exists in multiple content set,
// but is referenced in the Q+A? Hopefully, this will never happen... :-/
let url = gGetRuleInfoUrl.replace( "RULEID", ruleid ) ; //eslint-disable-line no-undef
$.getJSON( url, (resp) => {
getJSON( url ).then( (resp) => {
if ( resp.length > 0 ) {
// install the rule info entries
this.ruleInfo = resp ;
}
} ).fail( (xhr, status, errorMsg) => {
} ).catch( (errorMsg) => {
showWarningMsg( "Couldn't get the Q+A for " + ruleid + ". <div class='pre'>" + errorMsg + "</div>" ) ;
} ) ;
} ) ;
@ -178,9 +178,9 @@ gMainApp.component( "nav-pane-asop", {
created() {
// get the ASOP footer
$.get( gGetASOPFooterUrl, (resp) => { //eslint-disable-line no-undef
getURL( gGetASOPFooterUrl ).then( (resp) => { //eslint-disable-line no-undef
this.footer = resp ;
} ).fail( (xhr, status, errorMsg) => {
} ).catch( (errorMsg) => {
console.log( "Couldn't get the ASOP footer: " + errorMsg ) ;
} ) ;

@ -1,5 +1,5 @@
import { gMainApp, gAppConfig, gEventBus } from "./MainApp.js" ;
import { findTargets, getPrimaryTarget, fixupSearchHilites, hideFootnotes } from "./utils.js" ;
import { postURL, findTargets, getPrimaryTarget, fixupSearchHilites, hideFootnotes } from "./utils.js" ;
// --------------------------------------------------------------------
@ -103,10 +103,9 @@ gMainApp.component( "search-results", {
this.errorMsg = errorMsg ;
onSearchDone() ;
} ;
$.ajax( { url: gSearchUrl, type: "POST", //eslint-disable-line no-undef
data: { queryString: queryString },
dataType: "json",
} ).done( (resp) => {
postURL( gSearchUrl, //eslint-disable-line no-undef
{ queryString: queryString }
).then( (resp) => {
// check if there was an error
if ( resp.error !== undefined ) {
onError( resp.error || "Unknown error." ) ;
@ -125,7 +124,7 @@ gMainApp.component( "search-results", {
}
// flag that the search was completed
onSearchDone() ;
} ).fail( (xhr, status, errorMsg) => {
} ).catch( (errorMsg) => {
onError( errorMsg ) ;
} ) ;
},

@ -145,6 +145,47 @@ export function makeImagesZoomable( $elem )
// --------------------------------------------------------------------
export function getJSON( url )
{
// get the specified URL
return new Promise( (resolve, reject) => {
$.getJSON( url, (resp) => {
resolve( resp ) ;
} ).fail( (xhr, status, errorMsg) => {
reject( errorMsg ) ;
} ) ;
} ) ;
}
export function getURL( url )
{
// get the specified URL
return new Promise( (resolve, reject) => {
$.get( url, (resp) => {
resolve( resp ) ;
} ).fail( (xhr, status, errorMsg) => {
reject( errorMsg ) ;
} ) ;
} ) ;
}
export function postURL( url, data )
{
// post the data to the specified URL
return new Promise( (resolve, reject) => {
$.ajax( {
url: url, type: "POST",
data: data, dataType: "json"
} ).done( (resp) => {
resolve( resp ) ;
} ).fail( (xhr, status, errorMsg) => {
reject( errorMsg ) ;
} ) ;
} ) ;
}
// --------------------------------------------------------------------
export function wrapMatches( val, searchFor, delim1, delim2 )
{
// search for a regex and wrap all matches with the specified delimiters

Loading…
Cancel
Save