Skip to content

Commit

Permalink
Issue #11
Browse files Browse the repository at this point in the history
Simplification of some methods and their usage

____________________

Simplification de certaines méthodes et de leur usage
  • Loading branch information
regazzoj committed Oct 24, 2018
1 parent 5f293b0 commit 1e74dde
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 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 @@ -46,10 +46,10 @@ export class ElementComponent
currOptionsValues: this.element.getCurrDeepestOptionsValues().filter( (oV) => oV.option.displayInInfoBar).sort( (a,b) => a.isFilledByFilters ? -1 : 1),

// body
body: App.templateModule.elementTemplate.renderHtmlElement('body', elementTodisplay),
body: App.templateModule.elementTemplate.renderBody(elementTodisplay),

//header
header: App.templateModule.elementTemplate.renderHtmlElement('header', elementTodisplay),
header: App.templateModule.elementTemplate.renderHeader(elementTodisplay),

listingMode: App.mode == AppModes.List,
isIframe: App.isIframe,
Expand Down
56 changes: 23 additions & 33 deletions src/js/modules/element/template-element.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ declare var $;
declare var nunjucks;
declare var commonmark;

export enum HtmlElement {
Body = 'body',
Header = 'header'
enum HtmlElement {
Body,
Header
}

export class TemplateElementModule
Expand All @@ -25,15 +25,17 @@ export class TemplateElementModule
initialize()
{
this.bodyConfig = App.config.infobar.bodyTemplate;
this.bodyConfig.type = HtmlElement.Body;

this.headerConfig = App.config.infobar.headerTemplate;
this.headerConfig.type = HtmlElement.Header;

this.getHtmlElementData(this.bodyConfig, HtmlElement.Body);
this.getHtmlElementData(this.bodyConfig);

this.getHtmlElementData(this.headerConfig, HtmlElement.Header);
this.getHtmlElementData(this.headerConfig);
}

private getHtmlElementData(htmlElementConfig: any, htmlElementConcerned: HtmlElement)
private getHtmlElementData(htmlElementConfig: any)
{
if (!htmlElementConfig.content) { this.onReady.emit(); return; } // nothing to do

Expand All @@ -42,15 +44,15 @@ export class TemplateElementModule
case "string":
let content = htmlElementConfig.content;
if (Array.isArray(content)) content = content.join('\n');
this.compile(htmlElementConfig, htmlElementConcerned, content);
this.compile(htmlElementConfig, content);
this.onReady.emit();
break;
case "url":
$.ajax({
dataType: 'text',
url: htmlElementConfig.content,
success: (data) => { this.compile(htmlElementConfig, htmlElementConcerned, data);this.onReady.emit(); },
error: () => { this.showError(htmlElementConcerned, htmlElementConfig.content); }
success: (data) => { this.compile(htmlElementConfig, data);this.onReady.emit(); },
error: () => { this.showError(htmlElementConfig.type, htmlElementConfig.content); }
});
break;
}
Expand All @@ -71,38 +73,26 @@ export class TemplateElementModule
console.error(errorMessage, urlConcerned);
}

renderHtmlElement(htmlElementConcerned: string, element): any
{
let renderedTemplate = "";
switch(htmlElementConcerned)
{
case HtmlElement.Body:
renderedTemplate = this.renderBody(element);
break;
case HtmlElement.Header:
renderedTemplate = this.renderHeader(element);
break;
}

return this.fixTemplate(renderedTemplate);
}

// If there is a body template configured, then we use it. We use the default body otherwise.
private renderBody(element): any
renderBody(element): any
{
let renderedTemplate;
if (this.bodyTemplate)
return this.bodyTemplate.render(element);
renderedTemplate = this.bodyTemplate.render(element);
else
return App.templateModule.render('element-body-default', element);
renderedTemplate = App.templateModule.render('element-body-default', element);
return this.fixTemplate(renderedTemplate);
}

// If there is a header template configured, then we use it. We use the default header otherwise.
private renderHeader(element): any
renderHeader(element): any
{
let renderedTemplate;
if (this.headerTemplate)
return this.headerTemplate.render(element);
renderedTemplate = this.headerTemplate.render(element);
else
return App.templateModule.render('element-header-default', element);
renderedTemplate = App.templateModule.render('element-header-default', element);
return this.fixTemplate(renderedTemplate);
}

private fixTemplate(template) {
Expand All @@ -114,11 +104,11 @@ export class TemplateElementModule
}

// Compile given content as a template once for all
private compile(htmlElementConfig : any, htmlElementConcerned: HtmlElement, content: any)
private compile(htmlElementConfig : any, content: any)
{
if (htmlElementConfig.isMarkdown) content = this.parseMarkdownSyntax(content);
let template = App.templateModule.compile(content);
switch(htmlElementConcerned)
switch(htmlElementConfig.type)
{
case HtmlElement.Body:
this.bodyTemplate = template;
Expand Down

0 comments on commit 1e74dde

Please sign in to comment.