From f434e154d0bac93d5f057a3a2425287ec02254b9 Mon Sep 17 00:00:00 2001 From: Gwendolyn Date: Wed, 25 Dec 2024 11:24:54 +0100 Subject: [PATCH] js cleanup --- src/c3nav/site/static/site/js/c3nav.js | 602 ++++++++++++------------- 1 file changed, 297 insertions(+), 305 deletions(-) diff --git a/src/c3nav/site/static/site/js/c3nav.js b/src/c3nav/site/static/site/js/c3nav.js index e7a761ec..1e602b2f 100644 --- a/src/c3nav/site/static/site/js/c3nav.js +++ b/src/c3nav/site/static/site/js/c3nav.js @@ -4,12 +4,12 @@ * and resulting anti-aliasing. * https://github.com/Leaflet/Leaflet/issues/3575 */ - var originalInitTile = L.GridLayer.prototype._initTile; + const originalInitTile = L.GridLayer.prototype._initTile; L.GridLayer.include({ _initTile: function (tile) { originalInitTile.call(this, tile); - var tileSize = this.getTileSize(); + const tileSize = this.getTileSize(); tile.style.width = tileSize.x + 1 + 'px'; tile.style.height = tileSize.y + 1 + 'px'; @@ -19,7 +19,7 @@ /* * Fix scroll wheel zoom on precise scrolling devices */ - var originalPerformZoom = L.Map.ScrollWheelZoom.prototype._performZoom; + const originalPerformZoom = L.Map.ScrollWheelZoom.prototype._performZoom; L.Map.ScrollWheelZoom.include({ _performZoom: function () { if (this._delta) this._delta = (this._delta > 0) ? Math.max(this._delta, 60) : Math.min(this._delta, -60); @@ -34,11 +34,11 @@ return Math.log(x) * Math.LOG2E; }; - var originalGetIconBox = L.LayerGroup.Collision.prototype._getIconBox; + const originalGetIconBox = L.LayerGroup.Collision.prototype._getIconBox; L.LayerGroup.Collision.prototype._getIconBox = function (el) { - var result = originalGetIconBox(el); - var offsetX = (result[2] - result[0] / 2), - offsetY = (result[3] - result[1] / 2); + const result = originalGetIconBox(el); + const offsetX = (result[2] - result[0] / 2); + const offsetY = (result[3] - result[1] / 2); result[0] -= offsetX; result[1] -= offsetY; result[2] -= offsetX; @@ -79,7 +79,7 @@ localStorageWrapper = { try { localStorage.setItem(keyName, keyValue) } catch (e) { - console.log("can't set localstorage preference for " + keyName); + console.warn("can't set localstorage preference for " + keyName); } }, removeItem: function (keyName) { @@ -124,7 +124,7 @@ c3nav = { $('.app-ads').remove(); } - var $body = $('body'); + const $body = $('body'); if (window.mobileclient) { $body.addClass('mobileclient'); c3nav._set_user_location(null); @@ -152,8 +152,10 @@ c3nav = { }); }, _sort_labels: function (a, b) { - var result = (a[0].effective_label_settings.min_zoom || -10) - (b[0].effective_label_settings.min_zoom || -10); - if (result === 0) result = b[0].effective_label_settings.font_size - a[0].effective_label_settings.font_size; + let result = (a[0].effective_label_settings.min_zoom || -10) - (b[0].effective_label_settings.min_zoom || -10); + if (result === 0) { + result = b[0].effective_label_settings.font_size - a[0].effective_label_settings.font_size; + } return result; }, _last_time_searchable_locations_loaded: null, @@ -161,11 +163,11 @@ c3nav = { _searchable_locations_loaded: function (data) { c3nav._last_time_searchable_locations_loaded = Date.now(); if (data !== undefined) { - var locations = [], - locations_by_id = {}, - labels = {}; - for (var i = 0; i < data.length; i++) { - var location = data[i]; + const locations = []; + const locations_by_id = {}; + const labels = {}; + for (let i = 0; i < data.length; i++) { + const location = data[i]; location.elem = c3nav._build_location_html(location); location.title_words = location.title.toLowerCase().split(/\s+/); location.subtitle_words = location.subtitle.toLowerCase().split(/\s+/); @@ -177,7 +179,7 @@ c3nav = { labels[location.point[0]].push([location, c3nav._build_location_label(location)]); } } - for (level_id in labels) { + for (const level_id in labels) { labels[level_id].sort(c3nav._sort_labels); } c3nav.locations = locations; @@ -198,8 +200,9 @@ c3nav = { $('.locationinput').data('location', null); - var $main = $('main'), - state = JSON.parse($main.attr('data-state')); + const $main = $('main'); + const state = JSON.parse($main.attr('data-state')); + c3nav.embed = $main.is('[data-embed]'); c3nav.last_site_update = JSON.parse($main.attr('data-last-site-update')); @@ -231,14 +234,14 @@ c3nav = { $('#location-buttons').find('.route').on('click', c3nav._location_buttons_route_click); - var $result_buttons = $('#location-buttons, #route-result-buttons'); + const $result_buttons = $('#location-buttons, #route-result-buttons'); $result_buttons.find('.share').on('click', c3nav._buttons_share_click); $result_buttons.find('.details').on('click', c3nav._buttons_details_click); $('#route-search-buttons, #route-result-buttons').find('.swap').on('click', c3nav._route_buttons_swap_click); $('#route-search-buttons').find('.close').on('click', c3nav._route_buttons_close_click); $('#route-summary').find('.options').on('click', c3nav._buttons_options_click); - var $route_options = $('#route-options'); + const $route_options = $('#route-options'); $route_options.find('.close').on('click', c3nav._route_options_close_click); $route_options.find('button').on('click', c3nav._route_options_submit); $('#map').on('click', '.location-popup .button-clear', c3nav._popup_button_click); @@ -295,17 +298,17 @@ c3nav = { options = false; } - var destination = $('#destination-input').data('location'), - origin = $('#origin-input').data('location'), - new_state = { - routing: routing, - origin: origin, - destination: destination, - sidebar: true, - details: !!details, - options: !!options, - nearby: !!nearby, - }; + const destination = $('#destination-input').data('location'); + const origin = $('#origin-input').data('location'); + const new_state = { + routing: routing, + origin: origin, + destination: destination, + sidebar: true, + details: !!details, + options: !!options, + nearby: !!nearby, + }; c3nav._push_state(new_state, replace); @@ -315,7 +318,7 @@ c3nav = { return c3nav._levelControl.currentLevel || c3nav.resume_level; }, update_map_state: function (replace, level, center, zoom) { - var new_state = { + const new_state = { level: center ? level : c3nav.current_level(), center: L.GeoJSON.latLngToCoords(center ? center : c3nav.map.getCenter(), 2), zoom: Math.round((center ? zoom : c3nav.map.getZoom()) * 100) / 100 @@ -326,7 +329,7 @@ c3nav = { }, _first_sidebar_state: true, _sidebar_state_updated: function (state, nofly) { - var view; + let view; if (state.routing) { view = (!state.origin || !state.destination) ? 'route-search' : 'route-result'; } else { @@ -366,10 +369,10 @@ c3nav = { }, 300); } - var $search = $('#search'); + const $search = $('#search'); $search.removeClass('loading'); - var $selected_locationinputs = $('.locationinput.selected'); + const $selected_locationinputs = $('.locationinput.selected'); $selected_locationinputs.filter(':focus').blur(); if (!c3nav._first_sidebar_state || !window.mobileclient) { $('#destination-input, [data-view^=route] #origin-input').filter(':not(.selected)').find('input').first().focus(); @@ -384,12 +387,12 @@ c3nav = { _clear_route_layers: function () { c3nav._firstRouteLevel = null; c3nav._routeLayerBounds = {}; - for (var id in c3nav._routeLayers) { + for (const id in c3nav._routeLayers) { c3nav._routeLayers[id].clearLayers(); } }, _clear_detail_layers: function () { - for (var id in c3nav._detailLayers) { + for (const id in c3nav._detailLayers) { c3nav._detailLayers[id].clearLayers(); } }, @@ -397,15 +400,15 @@ c3nav = { update_location_labels: function () { if (!c3nav._labelControl.labelsActive) return; c3nav._labelLayer.clearLayers(); - var labels = c3nav.labels[c3nav.current_level()], - bounds = c3nav.map.getBounds().pad(0.15), - zoom = c3nav.map.getZoom(); + const labels = c3nav.labels[c3nav.current_level()]; + const bounds = c3nav.map.getBounds().pad(0.15); + const zoom = c3nav.map.getZoom(); if (!labels) return; - var valid_upper = [], location, label; - for (var item of labels) { - location = item[0]; - label = item[1]; + const valid_upper = []; + for (const item of labels) { + const location = item[0]; + const label = item[1]; if (zoom < (location.effective_label_settings.min_zoom || -10)) { // since the labels are sorted by min_zoom, we can just leave here break; @@ -418,21 +421,21 @@ c3nav = { } } } - for (label of valid_upper) { + + for (const label of valid_upper) { c3nav._labelLayer._maybeAddLayerToRBush(label); } - }, load_location_details: function (location) { - var $location_details = $('#location-details'); + const $location_details = $('#location-details'); if ($location_details.attr('data-id') !== String(location.id)) { $location_details.addClass('loading').attr('data-id', location.id); c3nav._clear_route_layers(); c3nav_api.get(`map/locations/${location.id}/display`).then(c3nav._location_details_loaded) .catch(data => { console.error(data); - var $location_details = $('#location-details'); + const $location_details = $('#location-details'); $location_details.find('.details-body').text('Error ' + String(data.status)); $location_details.find('.details-body').html(''); $location_details.find('.editor').hide(); @@ -443,28 +446,29 @@ c3nav = { } }, _location_details_loaded: function (data) { - var $location_details = $('#location-details'); + const $location_details = $('#location-details'); if ($location_details.attr('data-id') !== String(data.id)) { // loaded too late, information no longer needed return; } - var line, sublocations, loc, loclist, elem = $('
'); - for (var i = 0; i < data.display.length; i++) { - line = data.display[i]; + + const elem = $('
'); + for (let i = 0; i < data.display.length; i++) { + const line = data.display[i]; elem.append($('
').text(line[0])); if (typeof line[1] === 'string') { elem.append($('
').text(line[1])); } else if (line[1] === null || line.length === 0) { elem.append($('
').text('-')); } else if (line.length === 2 && line[1].url !== undefined) { - loclist = $('
'); + const loclist = $('
'); loclist.append($('').attr('href', line[1].url).attr('target', '_blank').text(line[1].title)); elem.append(loclist); } else { - sublocations = (line[1].length === undefined) ? [line[1]] : line[1]; - loclist = $('
'); - for (var j = 0; j < sublocations.length; j++) { - loc = sublocations[j]; + const sublocations = (line[1].length === undefined) ? [line[1]] : line[1]; + const loclist = $('
'); + for (let j = 0; j < sublocations.length; j++) { + const loc = sublocations[j]; if (loc.can_search) { loclist.append($('').attr('href', '/l/' + loc.effective_slug + '/details/').attr('data-id', loc.id).click(function (e) { e.preventDefault(); @@ -480,7 +484,7 @@ c3nav = { } $location_details.find('.details-body').html('').append(elem); - var $editor = $location_details.find('.editor'); + const $editor = $location_details.find('.editor'); if (data.editor_url) { $editor.attr('href', data.editor_url).show(); } else { @@ -488,8 +492,7 @@ c3nav = { } if (data.geometry) { - var custom_location = typeof data.id !== 'number', - report_url = '/report/l/' + String(data.id) + '/'; + const report_url = '/report/l/' + String(data.id) + '/'; $location_details.find('.report').attr('href', report_url); } else { $location_details.find('.report').hide(); @@ -515,9 +518,9 @@ c3nav = { next_route_options: null, session_route_options: null, load_route: function (origin, destination, nofly) { - var $route = $('#route-summary'), - $details_wrapper = $('#route-details'), - $options_wrapper = $('#route-options'); + const $route = $('#route-summary'); + const $details_wrapper = $('#route-details'); + const $options_wrapper = $('#route-options'); if (c3nav.next_route_options || $route.attr('data-origin') !== String(origin.id) || $route.attr('data-destination') !== String(destination.id)) { c3nav._clear_route_layers(); $route.addClass('loading').attr('data-origin', origin.id).attr('data-destination', destination.id); @@ -537,7 +540,7 @@ c3nav = { c3nav.next_route_options = null; }, _route_loaded: function (data, nofly) { - var $route = $('#route-summary'); + const $route = $('#route-summary'); if (data.error && $route.is('.loading')) { console.error(data.error); $route.find('span').text(data.error); @@ -554,15 +557,9 @@ c3nav = { c3nav._display_route_options(data.options_form); }, _display_route_result: function (result, nofly) { - var $route = $('#route-summary'), - $details_wrapper = $('#route-details'), - $details = $details_wrapper.find('.details-body'), - first_primary_level = null, - last_primary_level = null, - level_collect = [], - next_level_collect = [], - in_intermediate_level = true, - item, coords, description; + const $route = $('#route-summary'); + const $details_wrapper = $('#route-details'); + const $details = $details_wrapper.find('.details-body'); c3nav._clear_route_layers(); $details_wrapper.find('.report') @@ -570,15 +567,20 @@ c3nav = { $details.html(''); $details.append(c3nav._build_location_html(result.origin)); - for (var i = 0; i < result.items.length; i++) { - item = result.items[i]; + let first_primary_level = null; + let last_primary_level = null; + let level_collect = []; + let next_level_collect = []; + let in_intermediate_level = true; + for (let i = 0; i < result.items.length; i++) { + const item = result.items[i]; - for (var j = 0; j < item.descriptions.length; j++) { - description = item.descriptions[j]; + for (let j = 0; j < item.descriptions.length; j++) { + const description = item.descriptions[j]; $details.append(c3nav._build_route_item(description[0], description[1])); } - coords = [item.coordinates[0], item.coordinates[1]]; + const coords = [item.coordinates[0], item.coordinates[1]]; level_collect.push(coords); if (item.level) { if (in_intermediate_level) { @@ -655,7 +657,7 @@ c3nav = { return false; }, _build_route_item: function (icon, text) { - var elem = $('
'); + const elem = $('
'); if (icon.indexOf('.') === -1) { elem.append($('' + icon + '')); } else { @@ -665,24 +667,24 @@ c3nav = { return elem; }, _add_intermediate_point: function (origin, destination, next) { - var angle = Math.atan2(destination[1] - next[1], destination[0] - next[0]), - distance = Math.sqrt(Math.pow(origin[0] - destination[0], 2) + Math.pow(origin[1] - destination[1], 2)), - offset = Math.min(1.5, distance / 4), - point = [destination[0] + Math.cos(angle) * offset, destination[1] + Math.sin(angle) * offset]; + const angle = Math.atan2(destination[1] - next[1], destination[0] - next[0]); + const distance = Math.sqrt(Math.pow(origin[0] - destination[0], 2) + Math.pow(origin[1] - destination[1], 2)); + const offset = Math.min(1.5, distance / 4); + const point = [destination[0] + Math.cos(angle) * offset, destination[1] + Math.sin(angle) * offset]; return [origin, point, destination]; }, _add_line_to_route: function (level, coords, dots, link_to_level) { if (coords.length < 2) return; - var latlngs = L.GeoJSON.coordsToLatLngs(c3nav._smooth_line(coords)), - routeLayer = c3nav._routeLayers[level]; - line = L.polyline(latlngs, { + const latlngs = L.GeoJSON.coordsToLatLngs(c3nav._smooth_line(coords)); + const routeLayer = c3nav._routeLayers[level]; + const line = L.polyline(latlngs, { className: dots ? 'c3nav-route-dashes' : 'c3nav-route-line', color: dots ? 'var(--color-route-dashes)' : 'var(--color-map-overlay)', dashArray: (dots || link_to_level) ? '7' : null, interactive: false, smoothFactor: 0.5 }).addTo(routeLayer); - bounds = {}; + const bounds = {}; bounds[level] = line.getBounds(); c3nav._merge_bounds(c3nav._routeLayerBounds, bounds); @@ -699,7 +701,7 @@ c3nav = { }, _smooth_line: function (coords) { if (coords.length > 2) { - for (var i = 0; i < 4; i++) { + for (let i = 0; i < 4; i++) { coords = c3nav._smooth_line_iteration(coords); } } @@ -707,8 +709,8 @@ c3nav = { }, _smooth_line_iteration: function (coords) { // Chaikin'S Corner Cutting Algorithm - var new_coords = [coords[0]]; - for (var i = 1; i < coords.length - 1; i++) { + const new_coords = [coords[0]]; + for (let i = 1; i < coords.length - 1; i++) { new_coords.push([(coords[i][0] * 5 + coords[i - 1][0]) / 6, (coords[i][1] * 5 + coords[i - 1][1]) / 6]); new_coords.push([(coords[i][0] * 5 + coords[i + 1][0]) / 6, (coords[i][1] * 5 + coords[i + 1][1]) / 6]); } @@ -716,18 +718,19 @@ c3nav = { return new_coords }, _display_route_options: function (options) { - var $options_wrapper = $('#route-options'), - $options = $options_wrapper.find('.route-options-fields'), - option, field, field_id, choice; + const $options_wrapper = $('#route-options'); + const $options = $options_wrapper.find('.route-options-fields'); $options.html(''); - for (var i = 0; i < options.length; i++) { - option = options[i]; - field_id = 'option_id_' + option.name; + for (let i = 0; i < options.length; i++) { + // TODO: I think this logic is broken, field is only assigned for type=select but always used + const option = options[i]; + const field_id = 'option_id_' + option.name; $options.append($('