webpack-bootstrap-ui-kit/src/js_old/_components/mapStorage.js

320 lines
8.5 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
import mapbox from 'mapbox-gl'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
window.offlineMaps = {}
2019-06-08 17:20:51 +02:00
window.offlineMaps.eventManager = {
_events: {},
on: function (event, action) {
2022-05-03 20:50:57 +02:00
console.log(`event.on: ${event}`)
2019-06-08 17:20:51 +02:00
if (!(event in this._events)) {
2022-05-03 20:50:57 +02:00
this._events[event] = []
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
this._events[event].push(action)
return this
2019-06-08 17:20:51 +02:00
},
off: function (event) {
2022-05-03 20:50:57 +02:00
console.log(`event.off: ${event}`)
delete this._events[event]
return this
2019-06-08 17:20:51 +02:00
},
fire: function (event) {
2022-05-03 20:50:57 +02:00
console.log(`event.fire: ${event}`)
const events = this._events
2019-06-08 17:20:51 +02:00
if (event in events) {
2022-05-03 20:50:57 +02:00
const actions = events[event]
const args = Array.prototype.slice.call(arguments, 1)
for (let i = 0, l = actions.length; i < l; i++) {
const action = actions[i]
2019-06-08 17:20:51 +02:00
if (action instanceof Function) {
2022-05-03 20:50:57 +02:00
action.apply(null, args)
2019-06-08 17:20:51 +02:00
} else {
2022-05-03 20:50:57 +02:00
this.fire.apply(this, [action].concat(args))
2019-06-08 17:20:51 +02:00
}
}
}
2022-05-03 20:50:57 +02:00
return this
2019-06-08 17:20:51 +02:00
},
};
(function (window, emr, undefined) {
2022-05-03 20:50:57 +02:00
const getIndexedDBStorage = function () {
const indexedDB =
2021-08-18 20:51:15 +02:00
window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
2022-05-03 20:50:57 +02:00
window.msIndexedDB
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const IndexedDBImpl = function () {
const self = this
let db = null
const request = indexedDB.open('TileStorage')
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
request.onsuccess = function () {
2022-05-03 20:50:57 +02:00
db = this.result
emr.fire('storageLoaded', self)
}
2019-06-08 17:20:51 +02:00
request.onerror = function (error) {
2022-05-03 20:50:57 +02:00
console.log(error)
}
2019-06-08 17:20:51 +02:00
request.onupgradeneeded = function () {
2022-05-03 20:50:57 +02:00
const store = this.result.createObjectStore('tile', { keyPath: 'key' })
store.createIndex('key', 'key', { unique: true })
}
2019-06-08 17:20:51 +02:00
this.add = function (key, value) {
2022-05-03 20:50:57 +02:00
const transaction = db.transaction(['tile'], 'readwrite')
const objectStore = transaction.objectStore('tile')
objectStore.put({ key, value })
}
2019-06-08 17:20:51 +02:00
this.delete = function (key) {
2022-05-03 20:50:57 +02:00
const transaction = db.transaction(['tile'], 'readwrite')
const objectStore = transaction.objectStore('tile')
objectStore.delete(key)
}
2019-06-08 17:20:51 +02:00
this.get = function (key, successCallback, errorCallback) {
2022-05-03 20:50:57 +02:00
const transaction = db.transaction(['tile'], 'readonly')
const objectStore = transaction.objectStore('tile')
const result = objectStore.get(key)
2019-06-08 17:20:51 +02:00
result.onsuccess = function () {
2022-05-03 20:50:57 +02:00
successCallback(this.result ? this.result.value : undefined)
}
result.onerror = errorCallback
}
}
return indexedDB ? new IndexedDBImpl() : null
}
const getWebSqlStorage = function () {
const openDatabase = window.openDatabase
const WebSqlImpl = function () {
const self = this
const db = openDatabase(
'TileStorage',
'1.0',
'Tile Storage',
2021-08-18 20:51:15 +02:00
5 * 1024 * 1024
2022-05-03 20:50:57 +02:00
)
2019-06-08 17:20:51 +02:00
db.transaction((tx) => {
2021-08-18 20:51:15 +02:00
tx.executeSql(
2022-05-03 20:50:57 +02:00
'CREATE TABLE IF NOT EXISTS tile (key TEXT PRIMARY KEY, value TEXT)',
2021-08-18 20:51:15 +02:00
[],
() => {
2022-05-03 20:50:57 +02:00
emr.fire('storageLoaded', self)
2021-08-18 20:51:15 +02:00
}
2022-05-03 20:50:57 +02:00
)
})
2019-06-08 17:20:51 +02:00
this.add = function (key, value) {
db.transaction((tx) => {
2022-05-03 20:50:57 +02:00
tx.executeSql('INSERT INTO tile (key, value) VALUES (?, ?)', [
2021-08-18 20:51:15 +02:00
key,
value,
2022-05-03 20:50:57 +02:00
])
})
}
2019-06-08 17:20:51 +02:00
this.delete = function (key) {
db.transaction((tx) => {
2022-05-03 20:50:57 +02:00
tx.executeSql('DELETE FROM tile WHERE key = ?', [key])
})
}
2019-06-08 17:20:51 +02:00
this.get = function (key, successCallback, errorCallback) {
db.transaction((tx) => {
2021-08-18 20:51:15 +02:00
tx.executeSql(
2022-05-03 20:50:57 +02:00
'SELECT value FROM tile WHERE key = ?',
2021-08-18 20:51:15 +02:00
[key],
(tx, result) => {
successCallback(
result.rows.length ? result.rows.item(0).value : undefined
2022-05-03 20:50:57 +02:00
)
2021-08-18 20:51:15 +02:00
},
errorCallback
2022-05-03 20:50:57 +02:00
)
})
}
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return openDatabase ? new WebSqlImpl() : null
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
emr.on('storageLoad', () => {
const storage = getIndexedDBStorage() || getWebSqlStorage() || null
2019-06-08 17:20:51 +02:00
if (!storage) {
2022-05-03 20:50:57 +02:00
emr.fire('storageLoaded', null)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-06-08 17:20:51 +02:00
})(window, window.offlineMaps.eventManager);
(function (window, emr, mapbox, MM, undefined) {
2022-05-03 20:50:57 +02:00
const StorageRequestManager = function (storage) {
MM.RequestManager.apply(this, [])
this._storage = storage
}
2019-06-08 17:20:51 +02:00
StorageRequestManager.prototype._imageToDataUri = function (image) {
2022-05-03 20:50:57 +02:00
const canvas = window.document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const context = canvas.getContext('2d')
context.drawImage(image, 0, 0)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return canvas.toDataURL('image/png')
}
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
StorageRequestManager.prototype._createTileImage = function (
id,
coord,
value,
cache
) {
2022-05-03 20:50:57 +02:00
const img = window.document.createElement('img')
img.id = id
img.style.position = 'absolute'
img.coord = coord
this.loadingBay.appendChild(img)
2019-06-08 17:20:51 +02:00
if (cache) {
2022-05-03 20:50:57 +02:00
img.onload = this.getLoadCompleteWithCache()
img.crossOrigin = 'Anonymous'
2019-06-08 17:20:51 +02:00
} else {
2022-05-03 20:50:57 +02:00
img.onload = this.getLoadComplete()
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
img.onerror = this.getLoadComplete()
img.src = value
}
2019-06-08 17:20:51 +02:00
StorageRequestManager.prototype._loadTile = function (id, coord, url) {
2022-05-03 20:50:57 +02:00
const self = this
2019-06-08 17:20:51 +02:00
if (this._storage) {
2021-08-18 20:51:15 +02:00
this._storage.get(
id,
(value) => {
if (value) {
2022-05-03 20:50:57 +02:00
self._createTileImage(id, coord, value, false)
2021-08-18 20:51:15 +02:00
} else {
2022-05-03 20:50:57 +02:00
self._createTileImage(id, coord, url, true)
2021-08-18 20:51:15 +02:00
}
},
() => {
2022-05-03 20:50:57 +02:00
self._createTileImage(id, coord, url, true)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
)
2019-06-08 17:20:51 +02:00
} else {
2022-05-03 20:50:57 +02:00
self._createTileImage(id, coord, url, false)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
}
2019-06-08 17:20:51 +02:00
StorageRequestManager.prototype.processQueue = function (sortFunc) {
if (sortFunc && this.requestQueue.length > 8) {
2022-05-03 20:50:57 +02:00
this.requestQueue.sort(sortFunc)
2019-06-08 17:20:51 +02:00
}
2021-08-18 20:51:15 +02:00
while (
this.openRequestCount < this.maxOpenRequests &&
this.requestQueue.length > 0
) {
2022-05-03 20:50:57 +02:00
let request = this.requestQueue.pop()
2019-06-08 17:20:51 +02:00
if (request) {
2022-05-03 20:50:57 +02:00
this.openRequestCount++
this._loadTile(request.id, request.coord, request.url)
request = request.id = request.coord = request.url = null
2019-06-08 17:20:51 +02:00
}
}
2022-05-03 20:50:57 +02:00
}
2019-06-08 17:20:51 +02:00
StorageRequestManager.prototype.getLoadCompleteWithCache = function () {
if (!this._loadComplete) {
2022-05-03 20:50:57 +02:00
const theManager = this
2021-08-18 20:51:15 +02:00
this._loadComplete = function (e) {
2022-05-03 20:50:57 +02:00
// e = e || window.event;
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const img = e.srcElement || e.target
img.onload = img.onerror = null
2019-06-08 17:20:51 +02:00
if (theManager._storage) {
2022-05-03 20:50:57 +02:00
theManager._storage.add(this.id, theManager._imageToDataUri(this))
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
theManager.loadingBay.removeChild(img)
theManager.openRequestCount--
delete theManager.requestsById[img.id]
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
if (
2022-05-03 20:50:57 +02:00
e.type === 'load' &&
(img.complete || (img.readyState && img.readyState === 'complete'))
2021-08-18 20:51:15 +02:00
) {
2022-05-03 20:50:57 +02:00
theManager.dispatchCallback('requestcomplete', img)
2019-06-08 17:20:51 +02:00
} else {
2022-05-03 20:50:57 +02:00
theManager.dispatchCallback('requesterror', {
2019-06-08 17:20:51 +02:00
element: img,
2021-08-18 20:51:15 +02:00
url: `${img.src}`,
2022-05-03 20:50:57 +02:00
})
img.src = null
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
setTimeout(theManager.getProcessQueue(), 0)
}
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
return this._loadComplete
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
MM.extend(StorageRequestManager, MM.RequestManager)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const StorageLayer = function (provider, parent, name, storage) {
this.parent = parent || document.createElement('div')
2021-08-18 20:51:15 +02:00
this.parent.style.cssText =
2022-05-03 20:50:57 +02:00
'position: absolute; top: 0px; left: 0px;' +
'width: 100%; height: 100%; margin: 0; padding: 0; z-index: 0'
this.name = name
this.levels = {}
this.requestManager = new StorageRequestManager(storage)
this.requestManager.addCallback('requestcomplete', this.getTileComplete())
this.requestManager.addCallback('requesterror', this.getTileError())
2019-06-08 17:20:51 +02:00
if (provider) {
2022-05-03 20:50:57 +02:00
this.setProvider(provider)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
MM.extend(StorageLayer, MM.Layer)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const StorageTemplatedLayer = function (template, subdomains, name, storage) {
2021-08-18 20:51:15 +02:00
return new StorageLayer(
new MM.Template(template, subdomains),
null,
name,
storage
2022-05-03 20:50:57 +02:00
)
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
emr.on('mapLoad', (storage) => {
const map = mapbox.map('map')
2021-08-18 20:51:15 +02:00
map.addLayer(
new StorageTemplatedLayer(
2022-05-03 20:50:57 +02:00
'http://{S}.tile.osm.org/{Z}/{X}/{Y}.png',
['a', 'b', 'c'],
2021-08-18 20:51:15 +02:00
undefined,
storage
)
2022-05-03 20:50:57 +02:00
)
map.ui.zoomer.add()
map.ui.zoombox.add()
map.centerzoom({ lat: 53.902254, lon: 27.56185 }, 13)
})
2019-06-08 17:20:51 +02:00
})(window, window.offlineMaps.eventManager, mapbox, MM);
(function (emr) {
2022-05-03 20:50:57 +02:00
emr.on('storageLoaded', 'mapLoad')
emr.fire('storageLoad')
})(window.offlineMaps.eventManager)