forked from netnr/zoning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.html
More file actions
173 lines (149 loc) · 6.02 KB
/
build.html
File metadata and controls
173 lines (149 loc) · 6.02 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>生成</title>
</head>
<body>
<h2>
根据JSON文件生成,请打开控制台
<small>
<sub>
(需要服务器环境访问,以http形式)
</sub>
</small>
</h2>
<hr />
<p>请选择 <b>zoning-*.json</b> 文件,生成 <b>SQLite</b></p>
<input type="file" name="name" id="txtFileForSqlite" />
<hr />
<p>请选择 <b>zoning-*.json</b> 文件,生成 <b>CSV</b></p>
<input type="file" name="name" id="txtFileForCsv" />
<p>注意:导出编码为utf-8,Excel打开中文会乱码,需要转换编码为ANSI 或 指定BOM,用记事本或notepad++ 等</p>
<script src="https://lib.baomitu.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<script src="https://lib.baomitu.com/jszip/3.1.4/jszip.min.js"></script>
<script>
var zoningdb = {
worker: new Worker("worker.sql.js"),
execute: function (sql) {
zoningdb.worker.postMessage({ action: 'exec', sql: sql.join(';') });
},
level: "",
change: function (id) {
document.getElementById(id).onchange = function () {
var file = this.files[0];
if (!file) {
return false;
}
file.name.replace(/\d/, function (x) { zoningdb.level = "-" + x });
var reader = new FileReader();
reader.onload = function () {
console.log('json parse ...');
var json = JSON.parse(reader.result);
zoningdb.createtb();
console.log('write db ...');
zoningdb.writedb(json, null, 1);
console.log('down db ...');
zoningdb.downdb();
};
reader.readAsText(file);
}
},
createtb: function () {
var sql = [];
sql.push("DROP TABLE IF EXISTS zoning");
sql.push("CREATE TABLE zoning(id text, text text, pid text)");
zoningdb.execute(sql);
},
writedb: function (json, id) {
id = id || "0";
if (id in json) {
var arr = json[id], sql = [], child = [];
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
sql.push("INSERT INTO zoning VALUES ('" + item.id + "','" + item.text + "','" + id + "')")
if (item.id in json) {
child.push(item.id);
}
}
zoningdb.execute(sql);
for (var j = 0; j < child.length; j++) {
zoningdb.writedb(json, child[j]);
}
}
},
downdb: function () {
zoningdb.defer = setInterval(function () {
console.log('wait ...');
}, 1000 * 3);
zoningdb.worker.onmessage = function (event) {
var blob = new Blob([event.data.buffer]);
if (blob.size > 1000) {
clearInterval(zoningdb.defer);
saveAs(blob, "zoning" + zoningdb.level + ".db");
}
};
zoningdb.worker.postMessage({ action: 'export' });
},
init: function () {
zoningdb.worker.onerror = function (e) {
console.log(e);
};
zoningdb.worker.postMessage({ action: 'open' });
}
}
var zoningcsv = {
change: function (id) {
document.getElementById(id).onchange = function () {
var file = this.files[0];
if (!file) {
return false;
}
file.name.replace(/\d/, function (x) { zoningcsv.level = "-" + x });
var reader = new FileReader();
reader.onload = function () {
console.log('json parse ...');
var json = JSON.parse(reader.result);
zoningcsv.data.push("id,text,pid");
zoningcsv.writecsv(json, null);
zoningcsv.downcsv(zoningcsv.data);
};
reader.readAsText(file);
}
},
level: "",
data: [],
downcsv: function (data) {
var zip = new JSZip();
zip.file("zoning" + zoningcsv.level + ".csv", data.join('\r\n'));
zip.generateAsync({ type: "blob" }).then(function (content) {
saveAs(content, "zoning" + zoningcsv.level + ".zip");
});
},
writecsv: function (json, id) {
id = id || "0";
if (id in json) {
var arr = json[id], sql = [], child = [];
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var arri = [];
arri.push(item.id);
arri.push(item.text);
arri.push(id);
zoningcsv.data.push(arri.join(','));
if (item.id in json) {
child.push(item.id);
}
}
for (var j = 0; j < child.length; j++) {
zoningcsv.writecsv(json, child[j]);
}
}
}
}
zoningdb.change('txtFileForSqlite');
zoningdb.init();
zoningcsv.change('txtFileForCsv');
</script>
</body>
</html>