Skip to content

Commit

Permalink
Issue #11
Browse files Browse the repository at this point in the history
Body template can be an URL of a local or distant file with Markdown or Nunjucks syntax
Body template is compiled once and for all

____________________________________________________________

Template body peut être l'URL d'un fichier local ou distant de syntaxe Markdown ou Nunjucks
Template body est compilé une fois pour toutes
  • Loading branch information
regazzoj committed Aug 3, 2018
1 parent dca5661 commit 9b46cd3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
6 changes: 0 additions & 6 deletions src/js/classes/config/gogo-config.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/js/components/element/element.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&quot;/g, '"');
}
else options.body = nunjucks.render('components/element/body.html.njk', options);
Expand Down
7 changes: 5 additions & 2 deletions src/js/gogocarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 48 additions & 0 deletions src/js/modules/core/template.module.ts
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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 = '';
Expand Down

0 comments on commit 9b46cd3

Please sign in to comment.