diff --git a/src/js/classes/config/gogo-config.class.ts b/src/js/classes/config/gogo-config.class.ts index d3ad740..3006293 100644 --- a/src/js/classes/config/gogo-config.class.ts +++ b/src/js/classes/config/gogo-config.class.ts @@ -4,7 +4,6 @@ import { GoGoFeature } from './gogo-feature.class'; import { ElementStatus } from '../classes'; import { DEFAULT_FEATURES } from './gogo-default-feature' ; declare var L : any; -declare var commonmark; export class GoGoConfig { @@ -201,11 +200,6 @@ export class GoGoConfig switch(prop) { case 'defaultBounds' : new_prop = L.latLngBounds(object[prop]);break; case 'defaultCenter' : new_prop = L.latLng(object[prop]);break; - case 'bodyTemplate': - let parser = new commonmark.Parser() - let htmlRenderer = new commonmark.HtmlRenderer(); - new_prop = htmlRenderer.render(parser.parse(object[prop])); - break; default: new_prop = object[prop];break; } that[prop] = new_prop; diff --git a/src/js/components/element/element.component.ts b/src/js/components/element/element.component.ts index 424a638..6fe5a2d 100644 --- a/src/js/components/element/element.component.ts +++ b/src/js/components/element/element.component.ts @@ -61,7 +61,9 @@ export class ElementComponent // If there is a body template configured, then we use it. We use the default body otherwise. if (App.config.infobar.bodyTemplate) { - options.body = nunjucks.renderString(App.config.infobar.bodyTemplate, this.element); + // Compile the body template once for all + if(!App.config.infobar.bodyTemplate.compiled) App.config.infobar.bodyTemplate = nunjucks.compile(App.config.infobar.bodyTemplate.content); + options.body = App.config.infobar.bodyTemplate.render(this.element); options.body = options.body.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, '"'); } else options.body = nunjucks.render('components/element/body.html.njk', options); diff --git a/src/js/gogocarto.ts b/src/js/gogocarto.ts index dcb0835..227a830 100644 --- a/src/js/gogocarto.ts +++ b/src/js/gogocarto.ts @@ -120,8 +120,11 @@ export class GoGoCartoModule App.routerModule.loadInitialState(); - // wait for initial state to be loaded - setTimeout( () => App.elementsJsonModule.loadLocalElements(), 100); + App.templateModule.initialize(App.config, function() + { + // wait for initial state to be loaded + setTimeout( () => App.elementsJsonModule.loadLocalElements(), 100); + }); this.bindEvents(); }, 0); diff --git a/src/js/modules/core/template.module.ts b/src/js/modules/core/template.module.ts index f0740d2..4a802c0 100644 --- a/src/js/modules/core/template.module.ts +++ b/src/js/modules/core/template.module.ts @@ -1,7 +1,11 @@ import { AppModule } from "../../app.module"; import { App } from "../../gogocarto"; +import { GoGoConfig } from "../../classes/config/gogo-config.class"; + +declare var $; declare var nunjucks; +declare var commonmark; export class TemplateModule { @@ -14,6 +18,50 @@ export class TemplateModule nunjucks.configure('../src/views', { autoescape: true }); } + initialize(config: GoGoConfig, callback: () => void) + { + if(config.infobar.bodyTemplate) + { + switch(config.infobar.bodyTemplate.type) + { + case "string": + let content = config.infobar.bodyTemplate.content; + if(Array.isArray(content)) + { + content = content.join('\n'); + } + config.infobar.bodyTemplate.content = this.parseMarkdownSyntax(content); + config.infobar.bodyTemplate.compiled = false; + callback(); + break; + case "url": + let that = this; + $.ajax({ + dataType: 'text', + url: config.infobar.bodyTemplate.content, + success: (data) => { + config.infobar.bodyTemplate.content = that.parseMarkdownSyntax(data); + config.infobar.bodyTemplate.compiled = false; + callback(); + }, + error: () => { console.error("Error while getting the body template at url ", config.infobar.bodyTemplate.content)} + }); + break; + } + } + else + { + callback(); + } + } + + private parseMarkdownSyntax(markdownString: string): string + { + let parser = new commonmark.Parser() + let htmlRenderer = new commonmark.HtmlRenderer(); + return htmlRenderer.render(parser.parse(markdownString)); + } + render(templateName : string, options : any = {}) : string { let fileUrl = '';