Commit 3719df29 authored by Jörg Richter's avatar Jörg Richter

Model: add `hasObject(id)` to Topicmap

See dmx-platform/dmx-platform#305
parent f46cb8ea
Pipeline #8374 passed with stage
in 2 seconds
This diff is collapsed.
......@@ -6,7 +6,7 @@ import typeCache from './type-cache'
import permCache from './permission-cache'
import utils from './utils'
console.log('[DMX] Client API 2019/10/31')
console.log('[DMX] Client API 2019/11/01')
export default {
......
......@@ -645,6 +645,17 @@ class Topicmap extends Topic {
return assoc
}
/**
* @param id a topic ID or an assoc ID
*/
getObject (id) {
const o = this.getTopicIfExists(id) || this.getAssocIfExists(id)
if (!o) {
throw Error(`topic/assoc ${id} not found in topicmap ${this.id}`)
}
return o
}
getTopicIfExists (id) {
return this._topics[id]
}
......@@ -661,6 +672,10 @@ class Topicmap extends Topic {
return this.getAssocIfExists(id)
}
hasObject (id) {
return this.hasTopic(id) || this.hasAssoc(id)
}
/**
* @return all topics of this topicmap, including hidden ones (array of dm5.ViewTopic)
*/
......@@ -675,6 +690,28 @@ class Topicmap extends Topic {
return Object.values(this._assocs)
}
/**
* Returns the position of the given topic/assoc.
*
* Note: ViewTopic has getPosition() too but ViewAssoc has not
* as a ViewAssoc doesn't know the Topicmap it belongs to.
*
* @param id a topic ID or an assoc ID
*/
getPosition (id) {
const o = this.getObject(id)
if (o.isTopic()) {
return o.getPosition()
} else {
const pos1 = this.getPosition(o.player1.id)
const pos2 = this.getPosition(o.player2.id)
return {
x: (pos1.x + pos2.x) / 2,
y: (pos1.y + pos2.y) / 2
}
}
}
/**
* @param topic a dm5.ViewTopic
*/
......@@ -828,41 +865,6 @@ class Topicmap extends Topic {
return this.getObject(_id)
}
// Generic
/**
* @param id a topic ID or an assoc ID
*/
getObject (id) {
const o = this.getTopicIfExists(id) || this.getAssocIfExists(id)
if (!o) {
throw Error(`topic/assoc ${id} not found in topicmap ${this.id}`)
}
return o
}
/**
* Returns the position of the given topic/assoc.
*
* Note: ViewTopic has getPosition() too but ViewAssoc has not
* as a ViewAssoc doesn't know the Topicmap it belongs to.
*
* @param id a topic ID or an assoc ID
*/
getPosition (id) {
const o = this.getObject(id)
if (o.isTopic()) {
return o.getPosition()
} else {
const pos1 = this.getPosition(o.player1.id)
const pos2 = this.getPosition(o.player2.id)
return {
x: (pos1.x + pos2.x) / 2,
y: (pos1.y + pos2.y) / 2
}
}
}
// Topicmap
setViewport (pan, zoom) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment