Completed
Push — master ( e01337...7c5d19 )
by
unknown
02:22
created

template-engine.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A template-engine.js ➔ ... ➔ ??? 0 7 1
1
/*global document, window, $ */
2
3
import {Devtool} from './devtool/Devtool'
4
5
import EditorInputs from './modules/EditorInputs'
6
import EditorBlock from './modules/EditorBlock'
7
import EditorFiles from './modules/EditorFiles'
8
import EditorSave from './modules/EditorSave'
9
import EditorJson from './modules/EditorJson'
10
import EditorManager from './modules/EditorManager'
11
import EditorAutocomplete from './modules/EditorAutocomplete'
12
import EditorReload from './modules/EditorReload'
13
14
var htmlTag = document.querySelector('html')
15
window.CONFIG = JSON.parse(htmlTag.getAttribute('data-config'))
16
// window.json = JSON.parse(unescape(htmlTag.getAttribute('data-json').replace(/"/g, '\"')))
17
var j = htmlTag.getAttribute('data-json')
18
j = j.replace(/"/g, '"')
19
j = unescape(j)
20
j = j.replace(/\%27/g, '\'')
21
window.json = JSON.parse(j)
22
window.Locales = JSON.parse(htmlTag.getAttribute('data-locales'))
23
24
class Engine {
25
26
  constructor() {
27
    this._blocks = new EditorBlock()
28
    this._inputs = new EditorInputs()
29
    this._files = new EditorFiles()
30
    this._save = new EditorSave()
31
    this._manager = new EditorManager()
32
    this._autocomplete = new EditorAutocomplete()
33
    this._dev = new Devtool()
34
35
    this.json = EditorJson.instance
36
37
    this._bindEvents()
38
39
    this.table = null
40
    $(document).ready(() => {
41
      this.table = $('#navigation-list').DataTable({
42
        //"order": [[ 3, 'desc' ]],
43
        'pageLength': 50,
44
        'autoWidth': false
45
      })
46
    })
47
48
    var abeReady = new Event('abeReady');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
    document.dispatchEvent(abeReady);
50
  }
51
52
  inject() {
53
    var findComments = function(el) { 
54
      var arr = [] 
55
      for(var i = 0; i < el.childNodes.length; i++) { 
56
        var node = el.childNodes[i] 
57
        if(node.nodeType === 8) { 
58
          arr.push(node) 
59
        } else { 
60
          arr.push.apply(arr, findComments(node)) 
61
        } 
62
      } 
63
      return arr 
64
    } 
65
 
66
    var commentNodes = findComments(document) 
67
  
68
    Array.prototype.forEach.call(commentNodes, (comment) => { 
69
      if (comment.nodeValue.indexOf('[pageHTML]') > -1) { 
70
        var base = comment.data 
71
        if(typeof base !== 'undefined' && base !== null) { 
72
          base = base.replace(/\[pageHTML\]/g, '') 
73
          base = base.replace(/<ABE!--/g, '<!--').replace(/--ABE>/g, '-->') 
74
          EditorReload.instance.inject(base) 
75
        } 
76
      } 
77
    })
78
  }
79
80
  _bindEvents() {
81
82
    this._blocks.onNewBlock(() => {
83
      this._files.rebind()
84
      this._inputs.rebind()
85
    })
86
87
    this._manager.remove((el) => {
88
      this.table.row($(el)).remove().draw()
89
    })
90
91
    this._inputs.onReload(() => {
92
      this._save.serializeForm()
93
      EditorReload.instance.reload()
94
    })
95
96
    this._autocomplete.onReload(() => {
97
      EditorReload.instance.reload()
98
    })
99
100
    this._inputs.onBlur(() => {
101
      this._save.serializeForm()
102
    })
103
104
    this._blocks.onRemoveBlock(() => {
105
      this._inputs.rebind()
106
      this._save.serializeForm() ///**************************************** HOOLA
107
    })
108
  }
109
}
110
111
var engine = new Engine()
112
window.abe = {
113
  save: engine._save,
114
  json: engine.json,
115
  inputs: engine._inputs,
116
  files: engine._files,
117
  blocks: engine._blocks,
118
  autocomplete: engine._autocomplete,
119
  editorReload: EditorReload
120
}
121
122
document.addEventListener('DOMContentLoaded', function() {
123
  if(document.querySelector('#page-template')) engine.inject()
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
124
})