2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* MapList is a list of maps with one active index.
|
|
|
|
* It contains some utility functions to change the map.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class MapList
|
|
|
|
*/
|
2019-11-05 11:07:36 +01:00
|
|
|
export class MapList {
|
2019-11-20 15:59:10 +01:00
|
|
|
constructor(active = null, maps = {}) {
|
2019-12-11 15:29:59 +01:00
|
|
|
this._maps = maps
|
|
|
|
this._active = active
|
2019-11-05 11:07:36 +01:00
|
|
|
|
|
|
|
if (Object.keys(maps).length > 0) this.select(active)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects a map from the map list.
|
|
|
|
*
|
2019-12-09 18:15:28 +01:00
|
|
|
* @public
|
2019-11-05 11:07:36 +01:00
|
|
|
* @param {string} active - Name of the map to select.
|
2019-12-09 18:15:28 +01:00
|
|
|
* @returns {Map} - Returns the active map. Returns null if no map was added to the MapList.
|
2019-11-05 11:07:36 +01:00
|
|
|
* @memberof MapList
|
|
|
|
*/
|
|
|
|
select(active) {
|
|
|
|
let map = null
|
|
|
|
|
|
|
|
if (active !== this.active) {
|
|
|
|
let keys = Object.keys(this.maps)
|
|
|
|
if (keys.length > 0) {
|
|
|
|
if (this.maps[active] == null) {
|
|
|
|
let altActive = keys[0]
|
|
|
|
console.warn(
|
|
|
|
`The MapList does not contain the provided active key '${active}'. Used '${altActive}' as fallback.`
|
|
|
|
)
|
|
|
|
|
|
|
|
active = altActive
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.active !== active) {
|
2019-12-11 15:29:59 +01:00
|
|
|
this._active = active
|
2019-11-05 11:07:36 +01:00
|
|
|
map = this.maps[active]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error(`Could not provide a fallback map! The map object is empty.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* Clones the entire maplist.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @returns {MapList} - Returns a cloned instance of this map list.
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
2019-11-20 15:59:10 +01:00
|
|
|
clone() {
|
|
|
|
let maps = {}
|
|
|
|
|
|
|
|
for (let name of Object.keys(this.maps)) {
|
|
|
|
maps[name] = this.maps[name].clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MapList(this.active, maps)
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* Adds a new map to the map list.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {string} key - Key to identify the map.
|
|
|
|
* @param {GeoMap} map - The GeoMap to add.
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
2019-11-05 11:07:36 +01:00
|
|
|
add(key, map) {
|
|
|
|
if (this.maps[key] != null) consol.warn('Key already in mapList. The existing key was overwritten.')
|
2019-12-11 15:29:59 +01:00
|
|
|
if (this.active == null) this._active = key
|
2019-11-05 11:07:36 +01:00
|
|
|
map.name = key
|
|
|
|
this.maps[key] = map
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* Returns the the active map.
|
|
|
|
* If none is set, it returns null.
|
|
|
|
*
|
|
|
|
*@public
|
|
|
|
* @readonly
|
2019-12-11 15:29:59 +01:00
|
|
|
* @member {GeoMap}
|
2019-12-09 18:15:28 +01:00
|
|
|
* @memberof MapList
|
|
|
|
*/
|
2019-11-05 11:07:36 +01:00
|
|
|
get map() {
|
2019-11-20 15:59:10 +01:00
|
|
|
return this.maps && this.maps[this.active] ? this.maps[this.active] : null
|
2019-11-05 11:07:36 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 15:29:59 +01:00
|
|
|
/**
|
|
|
|
* Returns the list of addedd cards.
|
|
|
|
*
|
|
|
|
* @member {object}
|
|
|
|
* @readonly
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
|
|
|
get maps() {
|
|
|
|
return this._maps
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the active key.
|
|
|
|
*
|
|
|
|
* @member {string}
|
|
|
|
* @readonly
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
|
|
|
get active() {
|
|
|
|
return this._active
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* Selects the next map in the map array.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @returns {GeoMap} - Returns the next map in the list.
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
2019-11-05 11:07:36 +01:00
|
|
|
next() {
|
|
|
|
let keys = Object.keys(this.maps)
|
|
|
|
let idx = keys.indexOf(this.active)
|
|
|
|
|
2019-11-20 15:59:10 +01:00
|
|
|
let next = idx + 1 < keys.length ? keys[idx + 1] : keys[0]
|
2019-11-05 11:07:36 +01:00
|
|
|
return next
|
|
|
|
}
|
2019-11-25 18:04:11 +01:00
|
|
|
|
2019-12-09 18:15:28 +01:00
|
|
|
/**
|
|
|
|
* Removes all maps from the maplist.
|
|
|
|
* And cleans up all maps.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @memberof MapList
|
|
|
|
*/
|
2019-11-25 18:04:11 +01:00
|
|
|
cleanup() {
|
|
|
|
for (let key in this.maps) {
|
|
|
|
let map = this.maps[key]
|
|
|
|
map.remove()
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 11:07:36 +01:00
|
|
|
}
|