-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
133 lines (116 loc) · 3.62 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const {
dialog
} = require('electron').remote
const mustache = require('mustache')
const fs = require('fs')
const path = require('path')
const input = require('./src/input')
const mapping = require('./src/mapping')
const output = require('./src/output')
const views = [input, mapping, output];
selectExcelFile = done => {
dialog.showOpenDialog({
filters: [{
name: 'Excel Files',
extensions: ['xls', 'xlsx']
}],
properties: ['openFile']
})
.then(result => {
var relativePath
if (!result.canceled && result.filePaths.length >= 1) {
relativePath = path.relative(process.cwd(), result.filePaths[0]);
}
done(relativePath)
})
}
saveExcelFile = done => {
dialog.showSaveDialog({
filters: [{
name: 'Excel Files',
extensions: ['xls', 'xlsx']
}],
defaultPath: 'output.xlsx'
})
.then(result => {
var relativePath
if (!result.canceled && result.filePath) {
relativePath = path.relative(process.cwd(), result.filePath);
}
done(relativePath)
})
}
const deactivateDocLinks = () => {
const active = document.querySelectorAll('.nav-group-item.active')
Array.prototype.forEach.call(active, item => {
item.classList.remove('active')
})
}
const activateDocLink = view => {
const docLink = document.querySelector(`#${view}-view.nav-group-item`)
docLink.classList.add('active')
}
const renderDataPane = viewName => {
const view = views.find(v => v.name == viewName)
if (view) {
var template = fs.readFileSync(path.join(__dirname, 'templates', viewName + '.mustache'), 'utf-8')
const dataPane = document.querySelector('#data-pane')
dataPane.innerHTML = mustache.render(template, view);
}
}
// Events
const docButtons = document.querySelectorAll('.nav-group-item')
Array.prototype.forEach.call(docButtons, button => {
const viewName = button.id.substring(0, button.id.indexOf("-view"))
if (viewName != undefined) {
button.addEventListener('click', () => {
deactivateDocLinks()
renderDataPane(viewName)
activateDocLink(viewName)
})
}
})
document.querySelector(`#select-input-file`).addEventListener('click', () => {
document.querySelector(`#select-input-file`).classList.add('active')
selectExcelFile(fileName => {
document.querySelector('#select-input-file').classList.remove('active')
if (fileName) {
input.parseFile(fileName, () => {
deactivateDocLinks()
activateDocLink('input')
renderDataPane('input')
})
}
})
})
document.querySelector(`#select-mapping-file`).addEventListener('click', () => {
document.querySelector(`#select-mapping-file`).classList.add('active')
selectExcelFile(fileName => {
document.querySelector('#select-mapping-file').classList.remove('active')
if (fileName) {
mapping.parseFile(fileName, () => {
deactivateDocLinks()
activateDocLink('mapping')
renderDataPane('mapping')
})
}
})
})
document.querySelector(`#select-output-file`).addEventListener('click', () => {
document.querySelector(`#select-output-file`).classList.add('active')
saveExcelFile(fileName => {
document.querySelector('#select-output-file').classList.remove('active')
if (fileName) {
output.saveFile(fileName, input.data, mapping.data, () => {
deactivateDocLinks()
activateDocLink('output')
renderDataPane('output')
})
}
})
})
// Global
renderDataPane('input')