NGL@1.0.0-beta.7 Home Manual Reference Source Gallery

src/component/representation-component.js

  1. /**
  2. * @file Representation Component
  3. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  4. * @private
  5. */
  6.  
  7. import Signal from '../../lib/signals.es6.js'
  8.  
  9. import { defaults } from '../utils.js'
  10. import Component from './component.js'
  11.  
  12. // add here to avoid cyclic import dependency
  13. Component.prototype.__getRepresentationComponent = function (repr, p) {
  14. return new RepresentationComponent(
  15. this.stage, repr, p, this
  16. )
  17. }
  18.  
  19. /**
  20. * Extends {@link ComponentSignals}
  21. *
  22. * @typedef {Object} RepresentationComponentSignals
  23. * @property {Signal<String>} parametersChanged - on parameters change
  24. */
  25.  
  26. /**
  27. * Component wrapping a {@link Representation} object
  28. */
  29. class RepresentationComponent extends Component {
  30. /**
  31. * Create representation component
  32. * @param {Stage} stage - stage object the component belongs to
  33. * @param {Representation} repr - representation object to wrap
  34. * @param {RepresentationParameters} [params] - component parameters
  35. * @param {Component} [parent] - parent component
  36. */
  37. constructor (stage, repr, params, parent) {
  38. var p = params || {}
  39. p.name = defaults(p.name, repr.type)
  40.  
  41. super(stage, p)
  42.  
  43. /**
  44. * Events emitted by the component
  45. * @type {RepresentationComponentSignals}
  46. */
  47. this.signals = Object.assign(this.signals, {
  48. parametersChanged: new Signal()
  49. })
  50.  
  51. this.parent = parent
  52.  
  53. this.setRepresentation(repr)
  54. }
  55.  
  56. /**
  57. * Component type
  58. * @type {String}
  59. */
  60. get type () { return 'representation' }
  61.  
  62. getType () {
  63. return this.repr.type
  64. }
  65.  
  66. setRepresentation (repr) {
  67. this.disposeRepresentation()
  68. this.repr = repr
  69. // this.name = repr.type;
  70. this.stage.tasks.listen(this.repr.tasks)
  71. this.updateVisibility()
  72. }
  73.  
  74. /**
  75. * @ignore
  76. * @alias RepresentationComponent#addRepresentation
  77. * @return {undefined}
  78. */
  79. addRepresentation () {}
  80.  
  81. /**
  82. * @ignore
  83. * @alias RepresentationComponent#removeRepresentation
  84. * @return {undefined}
  85. */
  86. removeRepresentation () {}
  87.  
  88. /**
  89. * @ignore
  90. * @alias RepresentationComponent#hasRepresentation
  91. * @return {undefined}
  92. */
  93. hasRepresentation () {}
  94.  
  95. disposeRepresentation () {
  96. if (this.repr) {
  97. this.stage.tasks.unlisten(this.repr.tasks)
  98. this.repr.dispose()
  99. }
  100. }
  101.  
  102. dispose () {
  103. if (this.parent && this.parent.hasRepresentation(this)) {
  104. this.parent.removeRepresentation(this)
  105. } else {
  106. this.disposeRepresentation()
  107. this.signals.disposed.dispatch()
  108. }
  109. }
  110.  
  111. /**
  112. * Set the visibility of the component, takes parent visibility into account
  113. * @param {Boolean} value - visibility flag
  114. * @return {RepresentationComponent} this object
  115. */
  116. setVisibility (value) {
  117. this.visible = value
  118. this.updateVisibility()
  119. this.signals.visibilityChanged.dispatch(this.visible)
  120.  
  121. return this
  122. }
  123.  
  124. getVisibility () {
  125. if (this.parent) {
  126. return this.parent.visible && this.visible
  127. } else {
  128. return this.visible
  129. }
  130. }
  131.  
  132. /**
  133. * Toggle visibility of the component, takes parent visibility into account
  134. * @return {RepresentationComponent} this object
  135. */
  136. toggleVisibility () {
  137. return this.setVisibility(!this.visible)
  138. }
  139.  
  140. updateVisibility () {
  141. this.repr.setVisibility(this.getVisibility())
  142. }
  143.  
  144. /**
  145. * Set selection
  146. * @param {Object} what - flags indicating what attributes to update
  147. * @param {Boolean} what.position - update position attribute
  148. * @param {Boolean} what.color - update color attribute
  149. * @param {Boolean} what.radius - update radius attribute
  150. * @return {RepresentationComponent} this object
  151. */
  152. update (what) {
  153. this.repr.update(what)
  154.  
  155. return this
  156. }
  157.  
  158. build (params) {
  159. this.repr.build(params)
  160.  
  161. return this
  162. }
  163.  
  164. /**
  165. * Set selection
  166. * @param {String} string - selection string
  167. * @return {RepresentationComponent} this object
  168. */
  169. setSelection (string) {
  170. this.repr.setSelection(string)
  171.  
  172. return this
  173. }
  174.  
  175. /**
  176. * Set representation parameters
  177. * @param {RepresentationParameters} params - parameter object
  178. * @return {RepresentationComponent} this object
  179. */
  180. setParameters (params) {
  181. this.repr.setParameters(params)
  182. this.signals.parametersChanged.dispatch(
  183. this.repr.getParameters()
  184. )
  185.  
  186. return this
  187. }
  188.  
  189. /**
  190. * Get representation parameters
  191. * @return {RepresentationParameters} parameter object
  192. */
  193. getParameters () {
  194. return this.repr.getParameters()
  195. }
  196.  
  197. /**
  198. * Set color
  199. * @param {String|Color|Hex} value - color value
  200. * @return {RepresentationComponent} this object
  201. */
  202. setColor (value) {
  203. this.repr.setColor(value)
  204.  
  205. return this
  206. }
  207.  
  208. /**
  209. * @ignore
  210. * @return {undefined}
  211. */
  212. getCenter () {}
  213.  
  214. /**
  215. * @ignore
  216. * @return {undefined}
  217. */
  218. getZoom () {}
  219.  
  220. /**
  221. * @ignore
  222. * @return {undefined}
  223. */
  224. getBox () {}
  225. }
  226.  
  227. export default RepresentationComponent