Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/js/location/osm/leaflet/src/ |
Upload File : |
import * as Leaflet from './vendor/leaflet-src.esm'; import TokenContainer from '../../src/tokencontainer'; export default class TileLayerAuth extends Leaflet.TileLayer { #tokenContainer; #hostName; #waitingRequests = []; #processingUnauthorized = false; setTokenContainer(tokenContainer: TokenContainer) { this.#tokenContainer = tokenContainer; } setHostName(hostName: string) { this.#hostName = hostName; } requestTile(url: string, img: Element, done: Function, isUnAuth: boolean) { fetch(url, { method: 'GET', cache: 'force-cache', headers: new Headers({ 'Authorization': `Bearer ${this.#tokenContainer.token}`, 'Bx-Location-Osm-Host': this.#hostName, }), }) .then((response) => { if(response.status === 200) { return response.blob(); } if(response.status === 401 && !isUnAuth) { this.#processUnauthorizedResponse(url, img, done); return null; } console.error(`Response status: ${response.status}`); }) .then((blobResponse) => { if(blobResponse) { const reader = new FileReader(); reader.onload = () => { img.src = reader.result; }; reader.readAsDataURL(blobResponse); done(null, img); } }) .catch((response) => { console.error(response); }); } createTile(coords, done) { const url = this.getTileUrl(coords); const img = document.createElement('img'); if(this.#processingUnauthorized) { this.#waitingRequests.push([url, img, done]); } else { this.requestTile(url, img, done, false); } return img; } #processUnauthorizedResponse(url, img, done) { this.#processingUnauthorized = true; this.#waitingRequests.push([url, img, done]); this.#tokenContainer.refreshToken() .then((sourceToken) => { while(this.#waitingRequests.length > 0) { const item = this.#waitingRequests.pop(); setTimeout(() => { this.requestTile(item[0], item[1], item[2], true); }, 1); } this.#processingUnauthorized = false; }); } }