import * as api from "./api";
import journey_wrapper from "./types/wrapper";
import { migrator } from "./types/migration";
import { journey_add_place, journey_from_url } from "./helper/journey";
import { focus_day, focus_leg, nav_mouseleave, nav_mousemove } from "./helper/nav";
import { set_search_set_results } from "./helper/api";
import toast from "./types/toast";
import search_drawer from "./types/search_drawer";
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-polyline", window.Vue2Leaflet.LPolyline);
Vue.component("l-control-scale", window.Vue2Leaflet.LControlScale);
const journey_base = journey_from_url()
const app = new Vue({
el: "#app",
data: {
edit_active: journey_base.edit,
journey: new journey_wrapper(journey_base.id),
search_drawer: new search_drawer(),
useDay: false,
toast: new toast(),
lang: {
format: "ddd D MMM",
formatLocale: {
firstDayOfWeek: 1,
},
monthBeforeYear: true,
},
},
methods: {
start_journey: function () { window.location.href = "/" + this.journey.id },
generate_rotation: function (index, list) {
if (index < 0 || index >= list.length) return 0;
const c0 = list[(index == 0) ? index : (index - 1)]
const c1 = list[(index == list.length - 1) ? index : (index + 1)]
const brng = Math.atan2(c1[1] - c0[1], c1[0] - c0[0]);
return `rotate:${brng - 0 * Math.PI / 2}rad`;
},
generate_marker: function (item, fcolor) {
return `
`
},
generate_icon: function (item, fcolor = "", styling = "", classes = "") {
return `
`;
},
drawer_hover_item: function (item) {
if (item) {
this.search_drawer.map_override.active = true
if (item.type == 'flight') {
this.search_drawer.map_override.elements = [[item.from_geo.lat, item.from_geo.lon], [item.to_geo.lat, item.to_geo.lon]]
} else {
this.search_drawer.map_override.elements = [[item.lat, item.lon]]
}
} else {
this.search_drawer.map_override.active = false
}
},
drawer_click_item: function (item) {
const tpe = this.search_drawer.query.type
this.search_drawer.reset();
this.drawer_hover_item();
if (item) {
item.step = -1;
journey_add_place(this.journey, tpe, item)
}
},
refreshTextAreaHeight: function (e) {
e.target.style['height'] = 'auto';
e.target.style['height'] = e.target.scrollHeight + 'px';
e.target.style['max-height'] = "100%";
},
onMapClick(e) {
if (this.search_drawer.query.addmarker) {
const newMarker = {
latlon: [e.latlng.lat, e.latlng.lng],
sname: this.search_drawer.query.query,
type: this.search_drawer.query.type,
};
this.drawer_click_item(newMarker)
}
},
},
created: function () {
set_search_set_results((r) => { this.search_drawer.results(r); });
this.nav_mouseleave = nav_mouseleave;
this.nav_mousemove = nav_mousemove;
this.focus_day = focus_day;
this.focus_leg = focus_leg;
this.save_data = api.throttle(() => {
api.save(this.journey.id, this.journey.data);
}, 1000);
this.pressed_prev_next = api.throttle((dir_prev) => {
if (this.useDay && dir_prev) return this.journey.day_prev();
if (!this.useDay && dir_prev) return this.journey.leg_prev();
if (this.useDay && !dir_prev) return this.journey.day_next();
if (!this.useDay && !dir_prev) return this.journey.leg_next();
}, 250)
window.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowLeft": return this.pressed_prev_next(true)
case "ArrowRight": return this.pressed_prev_next(false)
default: return;
}
});
api.load(this.journey.id).then((r) => {
app.journey.data = migrator(r)
app.journey.sel_leg = Math.min(app.journey.leg_count() - 1, journey_base.leg);
app.journey.sel_day = Math.min(app.journey.day_count() - 1, journey_base.day);
setTimeout(() => focus_leg(this.journey) && focus_day(this.journey), 25);
this.$refs.map.mapObject.keyboard.disable();
this.search_drawer.refresh_map = () => { this.$refs.map.mapObject.invalidateSize() }
this.search_drawer.get_leg = () => { return this.journey.leg_get() }
this.search_drawer.get_bb = () => {
if (!this.$refs.map) return []
const bounds = this.$refs.map.mapObject.getBounds();
return [[bounds.getSouthWest().lng, bounds.getSouthWest().lat],
[bounds.getNorthEast().lng, bounds.getNorthEast().lat]]
}
});
},
watch: {
journey: {
handler: function (ndata, odata) {
if (this.edit_active) this.save_data();
window.location.hash = `#${this.journey.sel_leg}_${this.journey.sel_day}`
},
deep: true,
},
},
});