complex-representation.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Structure } from 'mol-model/structure';
  8. import { Task } from 'mol-task'
  9. import { Loci, EmptyLoci } from 'mol-model/loci';
  10. import { StructureRepresentation, StructureParams } from './representation';
  11. import { ComplexVisual } from './complex-visual';
  12. import { PickingId } from 'mol-geo/geometry/picking';
  13. import { MarkerAction } from 'mol-geo/geometry/marker-data';
  14. import { RepresentationContext, RepresentationParamsGetter } from 'mol-repr/representation';
  15. import { Theme, createTheme } from 'mol-theme/theme';
  16. import { ParamDefinition as PD } from 'mol-util/param-definition';
  17. import { BehaviorSubject } from 'rxjs';
  18. export function ComplexRepresentation<P extends StructureParams>(label: string, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: () => ComplexVisual<P>): StructureRepresentation<P> {
  19. const updated = new BehaviorSubject(0)
  20. let visual: ComplexVisual<P> | undefined
  21. let _structure: Structure
  22. let _params: P
  23. let _props: PD.Values<P>
  24. let _theme: Theme
  25. function createOrUpdate(ctx: RepresentationContext, props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  26. if (structure && structure !== _structure) {
  27. _params = getParams(ctx, structure)
  28. _structure = structure
  29. if (!_props) _props = PD.getDefaultValues(_params)
  30. }
  31. _props = Object.assign({}, _props, props)
  32. _theme = createTheme(ctx, { structure: _structure }, props, _theme)
  33. return Task.create('Creating or updating ComplexRepresentation', async runtime => {
  34. if (!visual) visual = visualCtor()
  35. await visual.createOrUpdate({ ...ctx, runtime }, _theme, _props, structure)
  36. updated.next(updated.getValue() + 1)
  37. });
  38. }
  39. function getLoci(pickingId: PickingId) {
  40. return visual ? visual.getLoci(pickingId) : EmptyLoci
  41. }
  42. function mark(loci: Loci, action: MarkerAction) {
  43. return visual ? visual.mark(loci, action) : false
  44. }
  45. function setVisibility(value: boolean) {
  46. if (visual) visual.setVisibility(value)
  47. }
  48. function destroy() {
  49. if (visual) visual.destroy()
  50. }
  51. return {
  52. label,
  53. get renderObjects() {
  54. return visual && visual.renderObject ? [ visual.renderObject ] : []
  55. },
  56. get props() { return _props },
  57. get params() { return _params },
  58. get updated() { return updated },
  59. createOrUpdate,
  60. getLoci,
  61. mark,
  62. setVisibility,
  63. destroy
  64. }
  65. }