-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_handler.js
233 lines (227 loc) · 6.9 KB
/
point_handler.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var m_SQL = null;
function empty_gps_point() {
return {
'id': 0, // integer
'compass': "",// direction deg. N is 0. CCW is pos.
'x': "",// lon
'y': "",// lat
'z': "",// alt
'accuracy': "",// meter for accuracy circle
'altitudeAccuracy': "",
'timestamp': "",
};
}
class IPointHanlder {
constructor() {
if (this.constructor === IPointHanlder) {
throw new Error('interface can not be called as class');
}
}
get_point_id_list() { throw new Error('not implemented'); }
get_point(gp_id){ throw new Error('not implemented'); }
set_point(gp) { throw new Error('not implemented'); }
delete_point(gp_id) { throw new Error('not implemented'); }
}
class PointHanlder extends IPointHanlder {
constructor() {
super();
this.LSKEY_SQL_FILEPATH = "pgis_point_handler.sql";
this.LSKEY_ID_LIST = "pgis_point_id_list";
this.create_table_callbacks = [];
this.insert_callbacks = [];
this.update_callbacks = [];
this.delete_callbacks = [];
this.file_api = {
read_all_lines: (filepath, callback) => {
if(!callback){
return;
}
var sql = localStorage.getItem(filepath);
if(!sql){
callback([]);
}else{
callback(sql.split(/\r\n|\n/));
}
},
append_all_lines: (filepath, lines) => {
if(!lines || lines.length == 0){
return;
}
var sql = localStorage.getItem(filepath);
if(!sql){
sql = "";
}else{
sql += '\n';
}
sql += lines.join('\n');
localStorage.setItem(filepath, sql);
},
};
if(!m_SQL){
var base_path = (function() {
try{
var path = document.currentScript.src.split('?')[0];
var mydir = path.split('/').slice(0, -1).join('/') + '/';
if(mydir.startsWith('file://')){
mydir = mydir.substr('file://'.length);
}
return mydir;
}catch(e){
return '';
}
})();
const config = {
locateFile: filename => base_path + `./lib/sql-js/${filename}`
}
initSqlJs(config).then((SQL) => {
m_SQL = SQL;
});
}
}
init(callback){
if(!m_SQL){
setTimeout(() => {
this.init(callback);
}, 100);
return;
}
//sqlite columns type:[NULL,INTEGER,REAL,TEXT,BLOB]
var columns = {
id: "INTEGER PRIMARY KEY AUTOINCREMENT",
compass: "REAL",
x: "REAL",
y: "REAL",
z: "REAL",
accuracy: "REAL",
altitudeAccuracy: "REAL",
timestamp: "TEXT",
};
for(var create_table_callback of this.create_table_callbacks){
create_table_callback(columns);
}
var names = "";
for(var name in columns){
var type = null;
if(!columns[name]){
//pass through
}else if(typeof columns[name] === 'object'){
type = columns[name].type;
}else{
type = columns[name];
}
if(type){
names += `,${name} ${type}`;
}else{
names += `,${name}`;
}
}
names = names.slice(1);
var sql = `create table points(${names});`;
this.db = new m_SQL.Database();
this.db.run(sql);
this.file_api.read_all_lines(this.LSKEY_SQL_FILEPATH, (lines) => {
for(var line of lines){
if(line[0] == '#'){
continue;
}
this.db.run(line);
}
if(callback){
callback();
}
});
}
add_create_table_callback(callback){
this.create_table_callbacks.push(callback);
}
add_insert_callback(callback){
this.insert_callbacks.push(callback);
}
add_update_callback(callback){
this.update_callbacks.push(callback);
}
add_delete_callback(callback){
this.delete_callbacks.push(callback);
}
set_file_api(file_api){
this.file_api = file_api;
}
_to_obj_ary(res) {
if(!res || !res.values){
return [];
}
var ary = [];
for(var i=0;i<res.values.length;i++){
ary[i] = {};
for(var j=0;j<res.columns.length;j++){
ary[i][res.columns[j]] = res.values[i][j];
}
}
return ary;
}
get_points() {
var sql = `select * from points;`;
var res = this.db.exec(sql);
var ary = this._to_obj_ary(res[0]);
return ary;
}
get_point(gp_id){
var sql = `select * from points where id="${gp_id}";`;
var res = this.db.exec(sql);
var ary = this._to_obj_ary(res[0]);
if(ary.length == 0){
return null;
}else{
return ary[0];
}
}
set_point(gp) {
//sql
var columns = {
compass: gp.compass,
x: gp.x,
y: gp.y,
z: gp.z,
accuracy: gp.accuracy,
altitudeAccuracy: gp.altitudeAccuracy,
timestamp: gp.timestamp,
};
for(var insert_callback of this.insert_callbacks){
insert_callback(columns, gp);
}
var names = "";
var values = "";
for(var name in columns){
var value = null;
if(!columns[name]){
//pass through
}else if(typeof columns[name] === 'object'){
value = columns[name].value;
}else{
value = columns[name];
}
if(value){
names += `,${name}`;
if(typeof (value) === "string" || value instanceof String){
values += `,"${value}"`;
}else{
values += `,${value}`;
}
}
}
names = names.slice(1);
values = values.slice(1);
var sql = `insert into points(${names}) values(${values});`;
this.db.run(sql);
this.file_api.append_all_lines(this.LSKEY_SQL_FILEPATH, [sql]);
}
delete_point(gp_id){
//sql
for(var delete_callback of this.delete_callbacks){
delete_callback(gp_id);
}
var sql = `delete from points where id="${gp_id}";`;
this.db.run(sql);
this.file_api.append_all_lines(this.LSKEY_SQL_FILEPATH, [sql]);
}
}