-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchbar_view.js
176 lines (170 loc) · 4.87 KB
/
searchbar_view.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
const {
input,
div,
a,
text,
script,
domReady,
style,
} = require("@saltcorn/markup/tags");
const { renderForm } = require("@saltcorn/markup");
const { search_bar } = require("@saltcorn/markup/helpers");
const View = require("@saltcorn/data/models/view");
const Workflow = require("@saltcorn/data/models/workflow");
const Table = require("@saltcorn/data/models/table");
const db = require("@saltcorn/data/db");
const Form = require("@saltcorn/data/models/form");
const Field = require("@saltcorn/data/models/field");
const { stateFieldsToWhere } = require("@saltcorn/data/plugin-helper");
const Nominatim = require("nominatim-geocoder");
const geocoder = new Nominatim();
const configuration_workflow = () =>
new Workflow({
steps: [
{
name: "views",
form: async (context) => {
const table = await Table.findOne({ id: context.table_id });
const fields = await table.getFields();
return new Form({
fields: [
{
name: "latitude_field",
label: "Latitude field",
type: "String",
sublabel:
"The table need a fields of type 'Float' for latitude.",
required: true,
attributes: {
options: fields
.filter((f) => f.type.name === "Float")
.map((f) => f.name)
.join(),
},
},
{
name: "longtitude_field",
label: "Longtitude field",
type: "String",
sublabel:
"The table need a fields of type 'Float' for longtitude.",
required: true,
attributes: {
options: fields
.filter((f) => f.type.name === "Float")
.map((f) => f.name)
.join(),
},
},
{
name: "mylocation",
label: "Allow current location",
type: "Bool",
},
],
});
},
},
],
});
const get_state_fields = async (table_id) => [];
const usemylocscript = ({ latitude_field, longtitude_field }) => `
function use_my_location() {
navigator.geolocation.getCurrentPosition(function(position) {
set_state_fields({
_near_lat_${latitude_field}:position.coords.latitude,
_near_long_${longtitude_field}:position.coords.longitude,
_locq: {unset: true}
})
})
}
function use_this_location(lat,long, nm) {
set_state_fields({
_near_lat_${latitude_field}:lat,
_near_long_${longtitude_field}:long,
_locq: nm
})
}
function search_location(q) {
set_state_fields({
_near_lat_${latitude_field}:{unset: true},
_near_long_${longtitude_field}:{unset: true},
_locq: q
})
}
function call_search_location() {
const inputObj = $("input.search-bar");
const q = inputObj.val()
? inputObj.val()
: inputObj[0].placeholder?.startsWith("[")
? inputObj[0].placeholder.replaceAll("\u00B0", "") // remove degree symbol
: "";
search_location(q);
}
`;
const run = async (
table_id,
viewname,
{ mylocation, latitude_field, longtitude_field },
state,
extraArgs,
queryObj
) => {
const round = (x) => Math.round(x * 100) / 100;
const latStateField = `_near_lat_${latitude_field}`;
const longStateField = `_near_long_${longtitude_field}`;
const placeHolder =
state[latStateField] && state[longStateField]
? `[${round(state[latStateField])}°, ${round(
state[longStateField]
)}°]`
: "Enter Location...";
let choices = "";
if (state._locq && !(state[latStateField] && state[longStateField])) {
const response = queryObj?.geocoder_query
? await queryObj.geocoder_query(state._locq)
: await geocoder_query_impl(state._locq);
choices = div(
"Possible locations:",
response.map((r) =>
div(
a(
{
href: `javascript:use_this_location(${r.lat},${r.lon}, '${r.display_name}')`,
},
"→",
r.display_name
)
)
)
);
}
return (
search_bar("_locq", state._locq, {
placeHolder,
onClick: "call_search_location()",
}) +
choices +
(mylocation
? a({ href: `javascript:use_my_location()` }, "Use my current location") +
script(usemylocscript({ latitude_field, longtitude_field }))
: "")
);
};
const geocoder_query_impl = async (locq) => {
const response = await geocoder.search({ q: locq });
db.sql_log({ response });
return response;
};
module.exports = {
name: "Geosearch input",
display_state_form: false,
get_state_fields,
configuration_workflow,
run,
queries: () => ({
async geocoder_query(locq) {
return await geocoder_query_impl(locq);
},
}),
};