Issues (791)

src/cli/cms/Page.js (3 issues)

1
import Handlebars from 'handlebars'
2
import path from 'path'
3
import fse from 'fs-extra'
4
5
import {
6
  abeEngine,
7
  cmsData,
8
  cmsTemplates,
9
  config,
10
  abeExtend,
11
  Manager
12
} from '../'
13
14
/**
15
 * Page class
16
 * manage HTML generation for page template
17
 */
18
export default class Page {
19
20
  /**
21
   * Create new page object
22
   * @param  {Object} params req.params from express route
0 ignored issues
show
The parameter params does not exist. Did you maybe forget to remove this comment?
Loading history...
23
   * @param  {Object} i18n translation
0 ignored issues
show
The parameter i18n does not exist. Did you maybe forget to remove this comment?
Loading history...
24
   * @param  {Function} callback 
0 ignored issues
show
The parameter callback does not exist. Did you maybe forget to remove this comment?
Loading history...
25
   * @param  {Boolean} onlyHTML default = false, if true HTML content will contains abe attributes
26
   * @return {String} HTML page as string
27
   */
28
  constructor(templateId, template, json, onlyHTML = false) {
29
    // HOOKS beforePageJson
30
    json = abeExtend.hooks.instance.trigger('beforePageJson', json)
31
32
    abeEngine.instance.content = json
33
      
34
    if(typeof Handlebars.templates[templateId] !== 'undefined' && 
35
        Handlebars.templates[templateId] !== null && 
36
        config.files.templates.precompile
37
    ){
38
      template = Handlebars.templates[templateId]
39
      this.html = template(json, {data: {intl: config.intlData}})
40
    } else {
41
42
      this._onlyHTML = onlyHTML
43
      this.template = template
44
      this.HbsTemplatePath = path.join(config.root, config.templates.url, 'hbs/'+templateId+'.hbs')
45
46
      // Remove text with attribute "visible=false"
47
      this.template = cmsTemplates.prepare.removeHiddenAbeTag(this.template)
48
49
      if(!this._onlyHTML){
50
        // Surrounds each Abe tag (which are text/rich/textarea and not in html attribute) with <abe> tag
51
        // ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title>
52
        this.template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template)
53
54
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.template)
55
56
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.template)
57
58
        this.template = cmsTemplates.prepare.addAbeSourceComment(this.template, json)
59
      } else {
60
        this.template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template)
61
      }
62
63
      // je rajoute les index pour chaque bloc lié à un each
64
      this.template = cmsTemplates.prepare.indexEachBlocks(this.template, json, this._onlyHTML)
65
66
      // We remove the {{abe type=data ...}} from the text 
67
      this.template = cmsData.source.removeDataList(this.template)
68
69
      // It's time to replace the [index] by {{@index}} (concerning each blocks)
70
      this.template = cmsTemplates.prepare.replaceAbeEachIndex(this.template)
71
72
      if(config.files.templates.precompile){
73
        // Let's persist the precompiled template for future use (kind of cache)
74
        fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
75
        Manager.instance.addHbsTemplate(templateId)
76
      }
77
78
      // I compile the text
79
      var compiledTemplate = Handlebars.compile(cmsTemplates.insertDebugtoolUtilities(this.template, this._onlyHTML))
80
81
      // I create the html page ! yeah !!!
82
      this.html = compiledTemplate(json, {data: {intl: config.intlData}})
83
    }
84
85
    if(this._onlyHTML) {
86
      this.html = abeExtend.hooks.instance.trigger('afterPageSaveCompile', this.html, json)
87
    }else {
88
      this.html = abeExtend.hooks.instance.trigger('afterPageEditorCompile', this.html, json)
89
    }
90
  }
91
}