nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

commit c0cb9000c06ee7564930d9a889e58cd09c4c0dfc
parent a7fc2d2babf4745dbda5ff2bfe5ecee31f126652
Author: Sergey Orlov <wladimirych@gmail.com>
Date:   Thu, 17 Sep 2020 20:51:56 +0200

fix linter errors in recent js code

Diffstat:
Msrc/lib/anyElementResizeEvent/index.js | 2+-
Msrc/lib/leaflet.control.external-maps/index.js | 6+++---
Msrc/lib/leaflet.control.search/index.js | 46+++++++++++++++++++++++++---------------------
Msrc/lib/leaflet.control.search/providers/coordinates.js | 29++++++++++++++---------------
Msrc/lib/leaflet.control.search/providers/index.js | 2+-
Msrc/lib/leaflet.control.search/providers/links.js | 66++++++++++++++++++++++++++++++++++++------------------------------
Msrc/lib/leaflet.control.search/providers/mapycz/index.js | 4++--
Msrc/lib/leaflet.control.search/providers/photon.js | 8++++----
Msrc/lib/leaflet.control.search/providers/remoteBase.js | 11++++++-----
Msrc/lib/leaflet.map.sidebars/index.js | 18+++++++++---------
10 files changed, 101 insertions(+), 91 deletions(-)

diff --git a/src/lib/anyElementResizeEvent/index.js b/src/lib/anyElementResizeEvent/index.js @@ -5,7 +5,7 @@ function onElementResize(element, cb) { } else { let width = element.offsetWidth; let height = element.offsetHeight; - setInterval(function() { + setInterval(function () { const newWidth = element.offsetWidth; const newHeight = element.offsetHeight; if (newWidth !== width || newHeight !== height) { diff --git a/src/lib/leaflet.control.external-maps/index.js b/src/lib/leaflet.control.external-maps/index.js @@ -71,11 +71,11 @@ const ExternalMaps = L.Control.extend({ { title: 'Wikimapia', externalMap: new ExternalMap('https://wikimapia.org/#lat={lat}&lon={lng}&z={zoom}', 3, 22), - } + }, ], }, - onAdd: function(map) { + onAdd: function (map) { this._map = map; const {container, link} = makeButton(null, 'View this place on another map', 'icon-external-links'); this._container = container; @@ -92,7 +92,7 @@ const ExternalMaps = L.Control.extend({ return container; }, - onClick: function(e) { + onClick: function (e) { this.menu.show(e); }, diff --git a/src/lib/leaflet.control.search/index.js b/src/lib/leaflet.control.search/index.js @@ -10,8 +10,8 @@ import './style.css'; import controlTemplate from './control.html'; ko.bindingHandlers.hasFocusNested = { - init: function(element, valueAccessor) { - function hasFocusNested(element) { + init: function (element, valueAccessor) { + function hasFocusNested() { let active = document.activeElement; while (active) { if (element === active) { @@ -25,7 +25,7 @@ ko.bindingHandlers.hasFocusNested = { function handleFocusChange() { // wait for all related focus/blur events to fire setTimeout(() => { - valueAccessor()(hasFocusNested(element)); + valueAccessor()(hasFocusNested()); }, 0); } element.addEventListener('focus', handleFocusChange, true); @@ -43,29 +43,31 @@ class SearchViewModel { controlOrChildHasFocus = ko.observable(false); highlightedIndex = ko.observable(null); attribution = ko.observable(null); + allowMinimize = ko.observable(true); - controlHasFocus = ko.pureComputed(function() { + /* eslint-disable no-invalid-this */ + controlHasFocus = ko.pureComputed(function () { return this.inputHasFocus() || this.controlOrChildHasFocus(); }, this); - showResults = ko.pureComputed(function() { + showResults = ko.pureComputed(function () { return this.items().length > 0 && this.controlHasFocus(); }, this); - showError = ko.pureComputed(function() { + showError = ko.pureComputed(function () { return this.error() !== null && this.controlHasFocus(); }, this); - isQueryLengthOk = ko.computed(function() { + isQueryLengthOk = ko.computed(function () { return this.query().trim().length >= this.minSearchQueryLength; }, this); - showWarningTooShort = ko.pureComputed(function() { + showWarningTooShort = ko.pureComputed(function () { return this.controlHasFocus() && this.query() && !this.isQueryLengthOk(); }, this); - minimizeToButton = ko.pureComputed(function() { + minimizeToButton = ko.pureComputed(function () { return this.allowMinimize() && !this.controlHasFocus(); }, this); @@ -158,6 +160,7 @@ class SearchViewModel { } return false; }; + /* eslint-enable no-invalid-this */ maybeRequestSearch() { if (this.isQueryLengthOk() && this.controlHasFocus()) { @@ -224,7 +227,7 @@ const SearchControl = L.Control.extend({ help: 'Coordinates in any format. Links to maps: Yandex, Google, OSM, Mapy.cz, Nakarte', }, - initialize: function(options) { + initialize: function (options) { L.Control.prototype.initialize.call(this, options); this.provider = new providers[this.options.provider](this.options.providerOptions); this.magicProviders = magicProviders.map((Cls) => new Cls()); @@ -239,7 +242,7 @@ const SearchControl = L.Control.extend({ this.viewModel.escapePressed.subscribe(this.setFocusToMap.bind(this)); }, - onAdd: function(map) { + onAdd: function (map) { this._map = map; const container = L.DomUtil.create('div', 'leaflet-search-container'); container.innerHTML = controlTemplate; @@ -260,11 +263,11 @@ const SearchControl = L.Control.extend({ return container; }, - setFocusToMap: function() { + setFocusToMap: function () { this._map.getContainer().focus(); }, - onSearchRequested: async function() { + onSearchRequested: async function () { const query = this.viewModel.query(); const searchOptions = { bbox: this._map.getBounds(), @@ -272,12 +275,13 @@ const SearchControl = L.Control.extend({ zoom: this._map.getZoom(), }; let provider = this.provider; - for (let magicProvider of this.magicProviders) { + for (const magicProvider of this.magicProviders) { if (magicProvider.isOurQuery(query)) { provider = magicProvider; } } - const searchPromise = (this.searchPromise = provider.search(query, searchOptions)); + this.searchPromise = provider.search(query, searchOptions); + const searchPromise = this.searchPromise; const result = await searchPromise; this.fire('resultreceived', {provider: provider.name, query, result}); if (this.searchPromise === searchPromise) { @@ -291,11 +295,11 @@ const SearchControl = L.Control.extend({ } }, - onSearchAborted: function() { + onSearchAborted: function () { this.searchPromise = null; }, - onResultItemClicked: function(item) { + onResultItemClicked: function (item) { if (item.bbox) { this._map.fitBounds(item.bbox); } else { @@ -305,13 +309,13 @@ const SearchControl = L.Control.extend({ this.setFocusToMap(); }, - onDocumentKeyUp: function(e) { + onDocumentKeyUp: function (e) { if (e.keyCode === this.options.hotkey.codePointAt(0) && e.altKey) { this.viewModel.setFocus(); } }, - updateMinimizeAllowed: function() { + updateMinimizeAllowed: function () { const mapSize = this._map.getSize(); this.viewModel.setMinimizeAllowed( mapSize.y < this.options.maxMapHeightToMinimize || mapSize.x < this.options.maxMapWidthToMinimize @@ -323,7 +327,7 @@ SearchControl.include(L.Mixin.HashState); SearchControl.include({ stateChangeEvents: ['querychange'], - serializeState: function() { + serializeState: function () { const query = this.viewModel.query(); if (query) { return [encodeURIComponent(query)]; @@ -331,7 +335,7 @@ SearchControl.include({ return null; }, - unserializeState: function(state) { + unserializeState: function (state) { if (state?.length === 1) { this.viewModel.query(decodeURIComponent(state[0])); return true; diff --git a/src/lib/leaflet.control.search/providers/coordinates.js b/src/lib/leaflet.control.search/providers/coordinates.js @@ -1,10 +1,9 @@ import L from 'leaflet'; -const - reInteger = '\\d+', - reFractional = '\\d+(?:\\.\\d+)?', - reSignedFractional = '-?\\d+(?:\\.\\d+)?', - reHemisphere = '[NWSE]'; +const reInteger = '\\d+'; +const reFractional = '\\d+(?:\\.\\d+)?'; +const reSignedFractional = '-?\\d+(?:\\.\\d+)?'; +const reHemisphere = '[NWSE]'; class Coordinates { getLatitudeLetter() { @@ -116,12 +115,12 @@ class CoordinatesDMS extends Coordinates { if (!m) { return {error: true}; } - let [h1, d1, m1, s1, h2, d2, m2, s2, h3] = m.slice(1); + const [h1, d1Str, m1Str, s1Str, h2, d2Str, m2Str, s2Str, h3] = m.slice(1); const hemispheres = CoordinatesDMS.parseHemispheres(h1, h2, h3, true); if (hemispheres.error) { return {error: true}; } - [d1, m1, s1, d2, m2, s2] = [d1, m1, s1, d2, m2, s2].map(parseFloat); + let [d1, m1, s1, d2, m2, s2] = [d1Str, m1Str, s1Str, d2Str, m2Str, s2Str].map(parseFloat); const coords = []; if (hemispheres.empty) { const coord1 = new CoordinatesDMS(d1, m1, s1, false, d2, m2, s2, false); @@ -209,12 +208,12 @@ class CoordinatesDM extends Coordinates { if (!m) { return {error: true}; } - let [h1, d1, m1, h2, d2, m2, h3] = m.slice(1); + const [h1, d1Str, m1Str, h2, d2Str, m2Str, h3] = m.slice(1); const hemispheres = CoordinatesDM.parseHemispheres(h1, h2, h3, true); if (hemispheres.error) { return {error: true}; } - [d1, m1, d2, m2] = [d1, m1, d2, m2].map(parseFloat); + let [d1, m1, d2, m2] = [d1Str, m1Str, d2Str, m2Str].map(parseFloat); const coords = []; if (hemispheres.empty) { const coord1 = new CoordinatesDM(d1, m1, false, d2, m2, false); @@ -289,15 +288,15 @@ class CoordinatesD extends Coordinates { if (!m) { return {error: true}; } - let [h1, d1, h2, d2, h3] = m.slice(1); + const [h1, d1Str, h2, d2Str, h3] = m.slice(1); const hemispheres = CoordinatesD.parseHemispheres(h1, h2, h3); if (hemispheres.error) { return {error: true}; } + let [d1, d2] = [d1Str, d2Str].map(parseFloat); if (hemispheres.swapLatLon) { [d1, d2] = [d2, d1]; } - [d1, d2] = [d1, d2].map(parseFloat); const coord = new CoordinatesD(d1, hemispheres.latIsSouth, d2, hemispheres.lonIsWest); if (coord.isValid()) { return { @@ -343,7 +342,7 @@ class CoordinatesDSigned extends Coordinates { return {error: true}; } const coords = []; - let [d1, d2] = m.slice(1).map(parseFloat); + const [d1, d2] = m.slice(1).map(parseFloat); const coord1 = new CoordinatesDSigned(d1, d2); if (coord1.isValid()) { coords.push(coord1); @@ -382,8 +381,8 @@ class CoordinatesProvider { static parsers = [CoordinatesDMS, CoordinatesDM, CoordinatesD, CoordinatesDSigned]; - normalizeInput(s) { - s = s.normalize('NFKC'); // convert subscripts and superscripts to normal chars + normalizeInput(inp) { + let s = inp.normalize('NFKC'); // convert subscripts and superscripts to normal chars s = ' ' + s + ' '; // replace everything that is not letter, number, minus, dot or comma to space s = s.replace(CoordinatesProvider.regexps.symbols, ' '); @@ -423,7 +422,7 @@ class CoordinatesProvider { async search(query) { const s = this.normalizeInput(query); - for (let parser of CoordinatesProvider.parsers) { + for (const parser of CoordinatesProvider.parsers) { const result = parser.fromString(s); if (!result.error) { const resultItems = result.coordinates.map((it) => { diff --git a/src/lib/leaflet.control.search/providers/index.js b/src/lib/leaflet.control.search/providers/index.js @@ -1,7 +1,7 @@ import {MapyCzProvider} from './mapycz'; import {PhotonProvider} from './photon'; import {LinksProvider} from './links'; -import {CoordinatesProvider} from "./coordinates"; +import {CoordinatesProvider} from './coordinates'; const providers = { mapycz: MapyCzProvider, diff --git a/src/lib/leaflet.control.search/providers/links.js b/src/lib/leaflet.control.search/providers/links.js @@ -21,13 +21,10 @@ function makeSearchResult(lat, lon, zoom, title) { ) { throw new Error('Invalid view state value'); } - if (zoom > MAX_ZOOM) { - zoom = MAX_ZOOM; - } return { latlng: L.latLng(lat, lon), - zoom, + zoom: zoom > MAX_ZOOM ? MAX_ZOOM : zoom, title, category: null, address: null, @@ -40,24 +37,27 @@ function makeSearchResults(lat, lon, zoom, title) { } const YandexMapsUrl = { - isOurUrl: function(url) { + isOurUrl: function (url) { return ( (url.hostname.match(/\byandex\./u) && url.pathname.match(/^\/maps\//u)) || url.hostname.match(/static-maps\.yandex\./u) ); }, - getResults: async function(url) { + getResults: async function (url) { let isShort = false; + let actualUrl; try { if (url.pathname.match(/^\/maps\/-\//u)) { isShort = true; const xhr = await fetch(urlViaCorsProxy(url.toString())); const dom = new DOMParser().parseFromString(xhr.response, 'text/html'); - url = new URL(dom.querySelector('meta[property="og:image:secure_url"]').content); + actualUrl = new URL(dom.querySelector('meta[property="og:image:secure_url"]').content); + } else { + actualUrl = url; } - const paramLl = url.searchParams.get('ll'); - const paramZ = url.searchParams.get('z'); + const paramLl = actualUrl.searchParams.get('ll'); + const paramZ = actualUrl.searchParams.get('z'); const [lon, lat] = paramLl.split(',').map(parseFloat); const zoom = Math.round(parseFloat(paramZ)); return makeSearchResults(lat, lon, zoom, 'Yandex map view'); @@ -76,11 +76,11 @@ const GoogleMapsSimpleMapUrl = { placeRe: /\/place\/([^/]+)/u, placeZoom: 14, - isOurUrl: function url(url) { + isOurUrl: function (url) { return Boolean(url.pathname.match(this.viewRe)) || Boolean(url.pathname.match(this.placeRe)); }, - getResults: function(url) { + getResults: function (url) { const results = []; const path = url.pathname; @@ -120,11 +120,11 @@ const GoogleMapsQueryUrl = { zoom: 17, title: 'Google map view', - isOurUrl: function(url) { + isOurUrl: function (url) { return url.searchParams.has('q'); }, - getResults: function(url) { + getResults: function (url) { const data = url.searchParams.get('q'); const m = data.match(/^(?:loc:)?([-\d.]+),([-\d.]+)$/u); const lat = parseFloat(m[1]); @@ -136,25 +136,28 @@ const GoogleMapsQueryUrl = { const GoogleMapsUrl = { subprocessors: [GoogleMapsSimpleMapUrl, GoogleMapsQueryUrl], - isOurUrl: function(url) { + isOurUrl: function (url) { return (url.hostname.match(/\bgoogle\./u) || url.hostname === 'goo.gl') && url.pathname.match(/^\/maps(\/|$)/u); }, - getResults: async function(url) { + getResults: async function (url) { let isShort = false; + let actualUrl; try { if (url.hostname === 'goo.gl') { isShort = true; const xhr = await fetch(urlViaCorsProxy(url.toString()), {method: 'HEAD'}); - url = new URL(xhr.responseURL); + actualUrl = new URL(xhr.responseURL); + } else { + actualUrl = url; } } catch (e) { // pass } - for (let subprocessor of this.subprocessors) { + for (const subprocessor of this.subprocessors) { try { - if (subprocessor.isOurUrl(url)) { - return subprocessor.getResults(url); + if (subprocessor.isOurUrl(actualUrl)) { + return subprocessor.getResults(actualUrl); } } catch (e) { // pass @@ -167,21 +170,24 @@ const GoogleMapsUrl = { }; const MapyCzUrl = { - isOurUrl: function(url) { + isOurUrl: function (url) { return Boolean(url.hostname.match(/\bmapy\.cz$/u)); }, - getResults: async function(url) { + getResults: async function (url) { let isShort = false; + let actualUrl; try { if (url.pathname.match(/^\/s\//u)) { isShort = true; const xhr = await fetch(urlViaCorsProxy(url.toString()), {method: 'HEAD'}); - url = new URL(xhr.responseURL); + actualUrl = new URL(xhr.responseURL); + } else { + actualUrl = url; } - const lon = parseFloat(url.searchParams.get('x')); - const lat = parseFloat(url.searchParams.get('y')); - const zoom = Math.round(parseFloat(url.searchParams.get('z'))); + const lon = parseFloat(actualUrl.searchParams.get('x')); + const lat = parseFloat(actualUrl.searchParams.get('y')); + const zoom = Math.round(parseFloat(actualUrl.searchParams.get('z'))); return makeSearchResults(lat, lon, zoom, 'Mapy.cz view'); } catch (_) { return { @@ -194,11 +200,11 @@ const MapyCzUrl = { }; const OpenStreetMapUrl = { - isOurUrl: function(url) { + isOurUrl: function (url) { return Boolean(url.hostname.match(/\bopenstreetmap\./u)); }, - getResults: function(url) { + getResults: function (url) { const m = url.hash.match(/map=([\d.]+)\/([\d.-]+)\/([\d.-]+)/u); try { const zoom = Math.round(parseFloat(m[1])); @@ -212,11 +218,11 @@ const OpenStreetMapUrl = { }; const NakarteUrl = { - isOurUrl: function(url) { + isOurUrl: function (url) { return url.hostname.match(/\bnakarte\b/u) || !this.getResults(url).error; }, - getResults: function(url) { + getResults: function (url) { const m = url.hash.match(/\bm=([\d]+)\/([\d.-]+)\/([\d.-]+)/u); try { const zoom = Math.round(parseFloat(m[1])); @@ -245,7 +251,7 @@ class LinksProvider { } catch (e) { return {error: 'Invalid link'}; } - for (let processor of urlProcessors) { + for (const processor of urlProcessors) { if (processor.isOurUrl(url)) { return processor.getResults(url); } diff --git a/src/lib/leaflet.control.search/providers/mapycz/index.js b/src/lib/leaflet.control.search/providers/mapycz/index.js @@ -24,7 +24,7 @@ const MapyCzProvider = BaseProvider.extend({ defaultLanguage: 'en', }, - initialize: function(options) { + initialize: function (options) { BaseProvider.prototype.initialize.call(this, options); this.langStr = this.getRequestLanguages(this.options.languages).join(','); this.categoriesLanguage = this.getRequestLanguages( @@ -33,7 +33,7 @@ const MapyCzProvider = BaseProvider.extend({ )[0]; }, - search: async function(query, {latlng, zoom}) { + search: async function (query, {latlng, zoom}) { if (!(await this.waitNoNewRequestsSent())) { return {error: 'Request cancelled'}; } diff --git a/src/lib/leaflet.control.search/providers/photon.js b/src/lib/leaflet.control.search/providers/photon.js @@ -18,13 +18,13 @@ const PhotonProvider = BaseProvider.extend({ defaultLanguage: 'en', }, - initialize: function(options) { + initialize: function (options) { BaseProvider.prototype.initialize.call(this, options); this.lang = this.getRequestLanguages(this.options.languages, this.options.defaultLanguage)[0]; }, - search: async function(query, {latlng}) { - if (!await this.waitNoNewRequestsSent()) { + search: async function (query, {latlng}) { + if (!(await this.waitNoNewRequestsSent())) { return {error: 'Request cancelled'}; } const url = new URL(this.options.apiUrl); @@ -87,7 +87,7 @@ const PhotonProvider = BaseProvider.extend({ }; }); return {results: places}; - } + }, }); export {PhotonProvider}; diff --git a/src/lib/leaflet.control.search/providers/remoteBase.js b/src/lib/leaflet.control.search/providers/remoteBase.js @@ -11,12 +11,12 @@ const BaseProvider = L.Class.extend({ delay: 500, }, - initialize: function(options) { + initialize: function (options) { L.setOptions(this, options); this.attribution = this.options.attribution; }, - getRequestLanguages: function(supportedLanguages, defaultLanguage) { + getRequestLanguages: function (supportedLanguages, defaultLanguage) { let languages = (navigator.languages ?? []) .map((s) => s.split('-')[0]) .filter((value, index, arr) => arr.indexOf(value) === index) @@ -27,14 +27,15 @@ const BaseProvider = L.Class.extend({ return languages; }, - waitNoNewRequestsSent: async function() { + waitNoNewRequestsSent: async function () { if (this.options.delay) { - const sleepPromise = this._sleep = sleep(this.options.delay); + this._sleep = sleep(this.options.delay); + const sleepPromise = this._sleep; await sleepPromise; return this._sleep === sleepPromise; } return true; - } + }, }); export {BaseProvider}; diff --git a/src/lib/leaflet.map.sidebars/index.js b/src/lib/leaflet.map.sidebars/index.js @@ -3,15 +3,16 @@ import './style.css'; import {onElementResize} from '~/lib/anyElementResizeEvent'; const MapWithSidebars = L.Map.extend({ - initialize: function(id, options) { + initialize: function (id, options) { this._sidebarsContainer = L.DomUtil.get(id); this.setupSidebarsLayout(this._sidebarsContainer); L.Map.prototype.initialize.call(this, this._mapContainer, options); onElementResize(this._mapContainer, L.Util.requestAnimFrame.bind(null, this.invalidateSize.bind(this))); }, - setupSidebarsLayout: function(container) { - const sidebars = this._sidebarContainers = {}; + setupSidebarsLayout: function (container) { + this._sidebarContainers = {}; + const sidebars = this._sidebarContainers; L.DomUtil.addClass(container, 'leaflet-map-sidebars-container'); sidebars['left'] = L.DomUtil.create('div', 'leaflet-map-sidebar-left', this._sidebarsContainer); const midColumn = L.DomUtil.create('div', 'leaflet-map-sidebar-mid-column', this._sidebarsContainer); @@ -21,14 +22,13 @@ const MapWithSidebars = L.Map.extend({ sidebars['bottom'] = L.DomUtil.create('div', 'leaflet-map-sidebar-bottom', midColumn); }, - addElementToSidebar: function(name, element) { - this._sidebarContainers[name].appendChild(element); + addElementToSidebar: function (barName, element) { + this._sidebarContainers[barName].appendChild(element); }, - removeElementFromSidebar: function(name, element) { - this._sidebarContainers[name].removeChild(element); - } - + removeElementFromSidebar: function (barName, element) { + this._sidebarContainers[barName].removeChild(element); + }, }); export {MapWithSidebars};