-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
107 lines (90 loc) · 3.19 KB
/
index.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
const DB_CONFIG = 'DB_CONFIG'
const generate = require('./generate')
const {dialog} = require('electron').remote;
const $progressBar = document.getElementById('progressBar');
const $exportBtn = document.getElementById('exportBtn');
const $directoryInput = document.getElementById('directory');
const $portInput = document.getElementById('port');
const DIALECT_PORTS = [
{ dialect:'mssql', port:1433 },
{ dialect:'mysql', port:3306 },
{ dialect:'mariadb', port:3306 },
{ dialect:'postgres', port:5432 },
{ dialect:'sqlite', port:null }
]
init();
/**
* form input initial value process
* @return
*/
function init() {
let cacheConfig = localStorage.getItem(DB_CONFIG);
if (cacheConfig) {
try{
cacheConfig = JSON.parse(cacheConfig);
setFormData(cacheConfig);
} catch(e) {
console.log(e)
}
}
}
function handleSubmit(form) {
const config = getFormData(form);
config.tables = config.tables ? config.tables.split(',') : '';
config.directory = config.directory || __dirname + '/models'
const genConfig = {
...config,
camelCase: config.camelCase === 'on',
schema: config.schema ? config.schema : undefined,
}
if (config.dialect === 'postgres') {
genConfig.additional = {
schema: config.schema ? config.schema : 'public'
};
}
$progressBar.style.display="block";
$exportBtn.disabled = true;
generate(genConfig, function() {
//set the configration to the localStorage when success
localStorage.setItem(DB_CONFIG, JSON.stringify(config));
$progressBar.style.display="none";
$exportBtn.disabled = false;
})
}
function openFileDialog() {
dialog.showOpenDialog({properties: ['openDirectory', 'createDirectory']}, function(filePaths) {
if (filePaths) {
$directoryInput.value = filePaths[0];
}
})
}
function getFormData(form) {
const data = {};
const inputs = form.getElementsByTagName('input')
for (let i = 0; i < inputs.length; i++) {
if(inputs[i].type == 'text' || inputs[i].type == 'password') {
data[inputs[i].name] = String(inputs[i].value)
} else if (inputs[i].type == 'radio' || inputs[i].type == 'checkbox') {
if (inputs[i].checked) {
data[inputs[i].name] = inputs[i].value;
}
}
}
return data;
}
function setFormData(data) {
const form = document.getElementsByTagName('form')[0];
const inputs = form.getElementsByTagName('input');
for (let i = 0; i < inputs.length; i++) {
if(inputs[i].type == 'text' || inputs[i].type == 'password') {
inputs[i].value = data[inputs[i].name] ? data[inputs[i].name] : ''
} else if (inputs[i].type == 'radio' || inputs[i].type == 'checkbox') {
if (data[inputs[i].name] == inputs[i].value) {
inputs[i].checked = true;
}
}
}
}
function onDialectChange(dialect) {
$portInput.value = DIALECT_PORTS.find(x => x.dialect === dialect).port
}