Skip to content

Commit

Permalink
Issue #11
Browse files Browse the repository at this point in the history
Add 'isMarkdown' property to 'bodyTemplate' object, on true, the content will be parsed thanks to the Markdown syntax
Add possibility to create custom filters and add one example 'gogotags' in 'src/js/modules/core/template.module.ts' file
Correction typo in the scss file
_____________________________________________________________________

Ajout d'une propriété 'isMakrdown' dans l'objet 'bodyTemplate', si true, le contenu sera parsé selon la syntaxe Markdown
Ajout de la possibilité de créer des filtres customs et ajout d'un exemple 'gogotags' dans le fichier' src/js/modules/core/template.module.ts'
Correction d'une typo dans le fichier scss
  • Loading branch information
regazzoj committed Aug 13, 2018
1 parent 9b46cd3 commit 5cd7fa5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/js/components/element/element.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export class ElementComponent
if (App.config.infobar.bodyTemplate)
{
// Compile the body template once for all
if(!App.config.infobar.bodyTemplate.compiled) App.config.infobar.bodyTemplate = nunjucks.compile(App.config.infobar.bodyTemplate.content);
if(!App.config.infobar.bodyTemplate.compiled) App.config.infobar.bodyTemplate = App.templateModule.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);
else options.body = App.templateModule.defaultBodyRender(options);

let html = App.templateModule.render('element', options);

Expand Down
46 changes: 42 additions & 4 deletions src/js/modules/core/template.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ declare var commonmark;

export class TemplateModule
{
nunjucksEnvironment: any;

constructor()
{
// we can configure this path and the templates names from GoGoCarto
// to override default templates
// As default templates are precompiled into javascript templates.js file
// if there is some templates we did not override, nunjucks will get the precompiled one
nunjucks.configure('../src/views', { autoescape: true });
this.nunjucksEnvironment = nunjucks.configure('../src/views', { autoescape: true });

// Add custom filters here
this.nunjucksEnvironment.addFilter('gogotags', function(tags) {
let value = '<div class="tags-container">';
for(let currentIndex=0;currentIndex<tags.length;++currentIndex)
{
value += '<span class="gogo-tag">' + tags[currentIndex] + '</span>';
}
value += '</div>'

return value;
});
}

initialize(config: GoGoConfig, callback: () => void)
Expand All @@ -30,7 +44,14 @@ export class TemplateModule
{
content = content.join('\n');
}
config.infobar.bodyTemplate.content = this.parseMarkdownSyntax(content);
if(config.infobar.bodyTemplate.isMarkdown)
{
config.infobar.bodyTemplate.content = this.parseMarkdownSyntax(content);
}
else
{
config.infobar.bodyTemplate.content = content;
}
config.infobar.bodyTemplate.compiled = false;
callback();
break;
Expand All @@ -40,7 +61,14 @@ export class TemplateModule
dataType: 'text',
url: config.infobar.bodyTemplate.content,
success: (data) => {
config.infobar.bodyTemplate.content = that.parseMarkdownSyntax(data);
if(config.infobar.bodyTemplate.isMarkdown)
{
config.infobar.bodyTemplate.content = this.parseMarkdownSyntax(data);
}
else
{
config.infobar.bodyTemplate.content = data;
}
config.infobar.bodyTemplate.compiled = false;
callback();
},
Expand Down Expand Up @@ -76,6 +104,16 @@ export class TemplateModule
default: console.warn('[GoGoCarto] No template associated to templateName', templateName);
}

return nunjucks.render(fileUrl, options);
return this.nunjucksEnvironment.render(fileUrl, options);
}

compile(template:string): any
{
return nunjucks.compile(template, this.nunjucksEnvironment);
}

defaultBodyRender(options: any = {})
{
return this.nunjucksEnvironment.render('components/element/body.html.njk', options);
}
}
2 changes: 1 addition & 1 deletion src/scss/components/element/_element-body.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.collapsible-body
{
background-color: transpraent;
background-color: transparent;
padding: 0;
position: relative;

Expand Down

0 comments on commit 5cd7fa5

Please sign in to comment.