OTM/public/js/main.js

362 lines
14 KiB
JavaScript
Raw Normal View History

2021-07-16 09:12:30 +02:00
const gen_id = (length)=>{
var result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const len = characters.length;
for (let i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * len));
}
return result;
}
2021-08-04 14:43:00 +02:00
Date.prototype.toJSONLocal = (function() {
function addZ(n) {
return (n<10? '0' : '') + n;
}
return function() {
return this.getFullYear() + '-' +
addZ(this.getMonth() + 1) + '-' +
addZ(this.getDate());
};
}())
2023-06-14 15:46:43 +02:00
function toEncoded(string) {
const codeUnits = Uint16Array.from(
{ length: string.length },
(element, index) => string.charCodeAt(index)
);
const charCodes = new Uint8Array(codeUnits.buffer);
let result = "";
charCodes.forEach((char) => {
result += String.fromCharCode(char);
});
return window.btoa(result);
}
function toDecoded(string) {
let binary = window.atob(string)
const bytes = Uint8Array.from({ length: binary.length }, (element, index) =>
binary.charCodeAt(index)
);
const charCodes = new Uint16Array(bytes.buffer);
let result = "";
charCodes.forEach((char) => {
result += String.fromCharCode(char);
});
return result;
}
2023-06-14 15:34:42 +02:00
const query_nominatim = (q,f) => axios.get('/api/place/'+q).then(res=>res.data).then(res=>res.filter(f))
const query_flight = (q) => axios.get('/api/flight/'+q).then(res=>res.data)
2021-07-16 09:12:30 +02:00
2023-06-14 15:34:42 +02:00
const is_restauration_type = e => ["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"].indexOf(e.type)!=-1;
2021-07-16 09:12:30 +02:00
2023-06-18 17:23:11 +02:00
const is_attraction_type = e => (["tourism", "leisure", "place", "amenity", "highway", "historic", "natural", "waterway"].indexOf(e.category)!=-1 ||
["place_of_worship", "national_park", "nature_reserve", "protected_area"].indexOf(e.type!=-1));
2021-07-16 09:12:30 +02:00
const icon_type = (item)=>{
let t = item.type
2023-06-25 12:26:17 +02:00
let c = item.category
2021-07-16 09:12:30 +02:00
const arr = ["restaurant", "cafe", "pub", "bar", "fast_food", "food_court"];
if(arr.indexOf(t)!=-1){
return 'utensils';
2021-08-04 07:40:26 +02:00
}else if(t=='hotel' || t=='hostel'){
2021-07-16 09:12:30 +02:00
return 'bed';
2023-06-25 12:26:17 +02:00
}else if(t=='museum' || c=='historic' || t=='place_of_worship'){
2021-07-16 09:12:30 +02:00
return 'landmark';
2021-07-16 19:03:55 +02:00
}else if(t=='peak' || t=='viewpoint'){
2021-07-16 09:12:30 +02:00
return 'mountain';
}else if(t=='parking'){
return 'parking';
2021-08-04 15:19:26 +02:00
}else if(t=='water' || t=='river' || t=='lake' || t=='torrent' || t=='aquarium'){
2021-07-16 09:12:30 +02:00
return 'water';
2021-08-06 12:33:58 +02:00
}else if(t=='community_centre' || t=='locality'){
2021-07-16 09:12:30 +02:00
return 'building';
}else if(t=='attraction'){
return 'landmark';
2021-08-04 15:19:26 +02:00
}else if(t=='information' || t=='university'){
2021-07-16 09:12:30 +02:00
return 'landmark';
}else if(t=='bridge'){
return 'archway';
2023-06-25 12:26:17 +02:00
}else if(t=='woodland'|| t=='shieling' || t=='national_park' || t=='zoo' || t=='park' || t=='garden' || 0){
2021-07-16 19:03:55 +02:00
return 'tree';
2021-08-06 12:33:58 +02:00
}else if(t=='water_park', t=='theme_park'){
2021-07-16 23:28:45 +02:00
return 'dice-five';
2023-06-25 12:26:17 +02:00
}else if(t=='?' || t=='neighbourhood' || t=='quarter' || c=='highway'){
2021-07-16 09:12:30 +02:00
return '';
}else{
2021-08-04 15:17:30 +02:00
console.log(item.display_name, item.category, item.type);
2021-07-16 09:12:30 +02:00
return 'question';
}
}
2022-03-23 10:47:49 +01:00
Vue.component('l-map', window.Vue2Leaflet.LMap)
Vue.component('l-tile-layer', window.Vue2Leaflet.LTileLayer)
Vue.component('l-marker', window.Vue2Leaflet.LMarker)
Vue.component('l-icon', window.Vue2Leaflet.LIcon)
Vue.component('l-popup', window.Vue2Leaflet.LPopup)
Vue.component('l-tooltip', window.Vue2Leaflet.LTooltip)
Vue.component('l-control-scale', window.Vue2Leaflet.LControlScale)
Vue.component('multiselect', window.VueMultiselect.default)
Vue.use(window.VueTextareaAutosize);
2021-07-16 09:12:30 +02:00
2022-03-23 10:35:46 +01:00
const app = new Vue({
2021-07-16 09:12:30 +02:00
el: '#app',
data: {
2023-06-25 23:10:03 +02:00
journey_edit: (['view','short'].indexOf(window.location.pathname.split('/')[1])==-1),
2021-07-16 22:53:57 +02:00
journey_id : window.location.pathname.split('/').pop() || gen_id(16),
2023-06-25 23:10:03 +02:00
journey_step_data: {day:1, section:0},
2021-07-16 09:12:30 +02:00
journey_data : {
name: "New Journey",
main:[],
},
2023-06-25 23:10:03 +02:00
2021-07-16 09:12:30 +02:00
query:{hotel:[],flight:[],nominatim:[]},
querying:{hotel:false,flight:false,place:false,food:false},
impexp:"",
2021-08-04 14:43:00 +02:00
lang: {
2023-06-25 23:10:03 +02:00
format: 'ddd D MMM',
2021-08-04 14:43:00 +02:00
formatLocale: {
firstDayOfWeek: 1,
},
monthBeforeYear: true,
},
2021-07-16 09:12:30 +02:00
},
methods: {
start_journey: function(event){
window.location.href = '/'+this.journey_id;
},
add_section: function(event){
2023-06-14 15:34:42 +02:00
if(this.journey_data.main==undefined) this.journey_data.main=[];
2023-06-25 23:10:03 +02:00
this.journey_data.main.push({title:"?",step_title:[],map:{zoom:2}, hotel:{latlon:[0,0]},places:{restaurants:[],places:[]}});
2021-07-16 09:12:30 +02:00
},
2023-07-15 13:06:51 +02:00
step_len: function(idx){
return this.journey_data.main[idx].dateRange?
((this.journey_data.main[idx].dateRange[1]-this.journey_data.main[idx].dateRange[0])/(1000*60*60*24))+1
: 1;
},
2021-07-16 09:12:30 +02:00
next_step: function(){
2023-06-25 23:10:03 +02:00
this.journey_step_data.day += 1;
let s = this.journey_step_data.section;
2023-07-15 13:45:50 +02:00
let cd = this.step_len(s);
2023-06-25 23:10:03 +02:00
if(this.journey_step_data.day>cd){
this.journey_step_data.section +=1;
if(this.journey_step_data.section>=this.journey_data.main.length){
2023-06-25 23:26:35 +02:00
this.journey_step_data.section = this.journey_data.main.length-1;
2023-07-15 13:45:50 +02:00
this.journey_step_data.day = cd
}else{
this.journey_step_data.day = 1;
2023-06-25 23:10:03 +02:00
}
}
2021-07-16 16:17:55 +02:00
},
prev_step: function(){
2023-06-25 23:10:03 +02:00
this.journey_step_data.day -=1;
if(this.journey_step_data.day<=0){
this.journey_step_data.section -=1;
if(this.journey_step_data.section <0){
this.first_step();
}else{
let s = this.journey_step_data.section;
2023-06-25 23:26:35 +02:00
2023-07-15 13:45:50 +02:00
let cd = this.step_len(s);
2023-06-25 23:10:03 +02:00
this.journey_step_data.day = cd ;
2021-08-03 13:18:25 +02:00
}
}
2023-06-25 23:10:03 +02:00
},
nextnext_step: function(){
this.journey_step_data.section += 1;
this.journey_step_data.day = 1;
if(this.journey_step_data.section>=this.journey_data.main.length)
this.first_step();
2021-08-03 13:18:25 +02:00
},
prevprev_step: function(){
2023-06-25 23:10:03 +02:00
this.journey_step_data.section -= 1;
this.journey_step_data.day = 1;
if(this.journey_step_data.section<0)
this.first_step();
2021-08-03 13:18:25 +02:00
},
2021-07-16 09:12:30 +02:00
first_step: function(){
2023-06-25 23:10:03 +02:00
this.journey_step_data.section =0;
this.journey_step_data.day = 1;
2021-07-16 09:12:30 +02:00
},
2023-06-25 23:10:03 +02:00
2021-08-03 13:18:25 +02:00
active_date: function(){
if(this.journey_step_data.day < 0) return "?";
if(!this.journey_data.main[this.journey_step_data.section].dateRange) return "?";
var date = new Date(this.journey_data.main[this.journey_step_data.section].dateRange[0]);
date.setDate(date.getDate() + this.journey_step_data.day-1);
return this.format_date(date)
},
format_date: function(d){
2023-06-25 23:10:03 +02:00
return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][d.getDay()] + ' '
+ d.getDate() + ' '
+ ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth()];
2021-08-03 13:18:25 +02:00
},
2023-07-15 13:06:51 +02:00
update_date: function(idx){
let dateRange = this.journey_data.main[idx].dateRange;
let start_end = [0,0];
let step_len = 0;
let last_start = dateRange[0];
for(let i = idx-1; i >=0; --i){
step_len = this.step_len(i)-1;
if (this.journey_data.main[i].dateRange){
start_end = [last_start.getDate() - step_len, last_start.getDate()];
}else{
this.journey_data.main[i].dateRange= [new Date(), new Date()]
start_end = [last_start.getDate() - step_len, last_start.getDate()];
}
this.journey_data.main[i].dateRange[0].setTime(last_start.getTime())
this.journey_data.main[i].dateRange[0].setDate(start_end[0]);
this.journey_data.main[i].dateRange[1].setTime(last_start.getTime())
this.journey_data.main[i].dateRange[1].setDate(start_end[1]);
last_start = this.journey_data.main[i].dateRange[0];
}
let last_end = dateRange[1];
for(let i = idx+1; i<this.journey_data.main.length; ++i){
step_len = this.step_len(i)-1;
if (this.journey_data.main[i].dateRange){
start_end = [last_end.getDate(), last_end.getDate() + step_len];
}else{
this.journey_data.main[i].dateRange= [new Date(), new Date()]
start_end = [last_end.getDate(), last_end.getDate() + step_len];
}
this.journey_data.main[i].dateRange[0].setTime(last_end.getTime())
this.journey_data.main[i].dateRange[0].setDate(start_end[0]);
this.journey_data.main[i].dateRange[1].setTime(last_end.getTime())
this.journey_data.main[i].dateRange[1].setDate(start_end[1]);
last_end = this.journey_data.main[i].dateRange[1];
}
},
2021-08-03 13:18:25 +02:00
2021-07-16 09:12:30 +02:00
rm_section: function(idx){
this.journey_data.main.splice(idx,1);
2023-06-25 23:10:03 +02:00
if(this.journey_step_data.section==idx){
this.prevprev_step();
}
2021-07-16 09:12:30 +02:00
},
2023-06-25 23:10:03 +02:00
sel_section: function(idx){
this.journey_step_data.section = idx;
2023-06-25 23:19:24 +02:00
this.journey_step_data.day = 1;
2023-06-19 12:47:50 +02:00
},
2021-07-16 09:12:30 +02:00
search_nominatim: function(txt,f){
if(txt==""){
this.query.nominatim=[];
return Promise.resolve([]);
}
return query_nominatim(txt,f)
.then((results) =>{
2021-07-16 16:17:55 +02:00
results.forEach(r=>{
r.latlon=[parseFloat(r.lat),parseFloat(r.lon)];
r.sname=r.display_name.split(',')[0];
})
2021-07-16 09:12:30 +02:00
this.query.nominatim=results;
});
},
search_flight: function(txt){
if(txt=="") return;
this.querying.flight=true;
query_flight(txt.replace(" ", ""))
.then((results) =>{
if(results.results==""){
this.query.flight=[];this.querying.flight=false;return;
}
this.query.flight=results.results;
this.querying.flight=false;
});
},
2021-07-16 16:17:55 +02:00
generate_icon: function(item,fcolor){
return L.AwesomeMarkers.icon({
icon: icon_type(item) || 'star', prefix: 'fa',
markerColor: fcolor || item.color || 'blue'}
2021-07-16 09:12:30 +02:00
).createIcon().outerHTML
},
save_data: function(){
2023-06-14 15:51:23 +02:00
this.impexp = toEncoded(JSON.stringify(this.journey_data));
2021-07-16 09:12:30 +02:00
axios.post('/api/'+this.journey_id, this.journey_data).then(response => {
console.log("Saved...")
}).catch(error => {
console.warn('Error! Could not reach the API.')
})
},
import_data:function(){
2023-06-14 15:46:43 +02:00
this.journey_data = Object.assign({}, JSON.parse(toDecoded(this.impexp)));
2023-07-15 13:08:56 +02:00
this.journey_data.main.forEach(e=>{
2023-07-15 13:45:50 +02:00
if(e.dateRange){
e.dateRange[0] = new Date(e.dateRange[0]);
e.dateRange[1] = new Date(e.dateRange[1]);
}
2023-07-15 13:08:56 +02:00
})
2021-07-16 09:12:30 +02:00
},
export_data:function(){
2023-06-14 15:46:43 +02:00
this.impexp = toEncoded(JSON.stringify(this.journey_data));
2021-07-16 09:12:30 +02:00
},
filter_selected:function(list,step){
2023-06-25 23:10:03 +02:00
return list.filter(e=>(step?(e.step==this.journey_step_data.day):(e.step>=0)))
2021-07-16 09:12:30 +02:00
},
filter_unselected:function(list){
return list.filter(e=>(e.step==undefined || e.step<0))
},
remove_item:function(list,idx){
list[idx].step=-1;
list.splice(idx, 1);
},
log:function(e){console.log(e)},
2023-07-15 13:45:50 +02:00
keyboardEvent (e) {
if (e.which === 13) {
}
},
2021-07-16 09:12:30 +02:00
},
created: function () {
2023-07-15 13:45:50 +02:00
window.addEventListener('keydown', (e) => {
switch(e.key){
case 'ArrowLeft': this.prev_step(); break;
case 'ArrowRight': this.next_step(); break;
default: console.log(e.key);
}
});
2021-07-16 09:12:30 +02:00
axios.get('/api/'+this.journey_id).then(response =>{
2023-06-14 15:34:42 +02:00
if(response.data=='') throw "Invalid Journey Data Received";
2021-07-16 09:12:30 +02:00
app.journey_data = response.data;
2023-06-14 15:34:42 +02:00
2021-07-16 09:12:30 +02:00
for(let e of app.journey_data.main){
2023-07-15 13:45:50 +02:00
if(e.dateRange){
2021-07-16 09:12:30 +02:00
e.dateRange[0]= new Date(e.dateRange[0]);
e.dateRange[1]= new Date(e.dateRange[1]);
}
2023-06-25 23:14:34 +02:00
e.step_title = e.step_title || [];
2021-07-16 09:12:30 +02:00
}
});
2023-07-13 18:57:49 +02:00
2021-07-16 09:12:30 +02:00
this.debounceSave = _.debounce(this.save_data, 500)
2021-08-04 07:40:26 +02:00
this.debounceSearch = {"hotel":_.debounce((q)=>{this.querying.hotel=true;this.search_nominatim(q,(r)=>(r.type=="hotel" || r.type=="hostel")).then((r)=>{this.querying.hotel=false});}, 500),
2021-08-04 14:51:33 +02:00
"restaurants":_.debounce((q)=>{this.querying.food=true;this.search_nominatim(q,(r)=>(is_restauration_type(r))).then((r)=>{this.querying.food=false});}, 500),
"places":_.debounce((q)=>{this.querying.place=true;this.search_nominatim(q,(r)=>(is_attraction_type(r))).then((r)=>{this.querying.place=false});}, 500),
2021-07-16 09:12:30 +02:00
"other":_.debounce((q)=>{this.querying.any=true;this.search_nominatim(q,(r)=>(true)).then((r)=>{this.querying.any=false});}, 500),
"flight":_.debounce((q)=>this.search_flight(q), 500)
}
},
watch: {
journey_data: {
handler:function (ndata, odata){
2023-06-25 23:10:03 +02:00
this.debounceSave();
2021-07-16 09:12:30 +02:00
},
deep: true,
},
},
2022-03-23 10:06:37 +01:00
})
2022-03-23 10:35:46 +01:00