MsaRowTitleCheckboxComponent.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2021 RCSB PDB and contributors, licensed under MIT, See LICENSE file for more info.
  3. * @author Joan Segura Mora <joan.segura@rcsb.org>
  4. */
  5. import * as React from "react";
  6. import {TagDelimiter} from "@rcsb/rcsb-saguaro-app";
  7. import {Subscription} from "rxjs";
  8. import {RcsbFvStateInterface} from "../../../../../RcsbFvState/RcsbFvStateInterface";
  9. interface MsaRowTitleCheckboxInterface {
  10. disabled:boolean;
  11. entryId: string;
  12. tag:"aligned"|"polymer"|"non-polymer";
  13. stateManager:RcsbFvStateInterface
  14. }
  15. type MsaRowTitleCheckboxType = MsaRowTitleCheckboxInterface & {entityId:string} | MsaRowTitleCheckboxInterface & {instanceId:string;}
  16. interface MsaRowTitleCheckboxState {
  17. checked:boolean;
  18. disabled:boolean;
  19. }
  20. //TODO keeps a global state of the (checkboxes <=> mol-star components) This needs further review!!!
  21. const globalState: {[key:string]: "active"|"inactive"|"disabled"|undefined;} = {};
  22. export class MsaRowTitleCheckboxComponent extends React.Component <MsaRowTitleCheckboxType,MsaRowTitleCheckboxState> {
  23. readonly state: MsaRowTitleCheckboxState = {
  24. checked: globalState[this.compId() + this.props.tag] == "active" || this.props.tag == "aligned",
  25. disabled: globalState[this.compId() + this.props.tag] == "disabled"
  26. };
  27. private subscription: Subscription;
  28. constructor(props: MsaRowTitleCheckboxType) {
  29. super(props);
  30. }
  31. public render():JSX.Element {
  32. return (
  33. <div
  34. style={this.style()}
  35. onClick={()=>{this.click()}}
  36. title={this.title()}
  37. />);
  38. }
  39. public componentDidMount() {
  40. this.subscribe();
  41. }
  42. public componentDidUpdate(prevProps: Readonly<MsaRowTitleCheckboxInterface>, prevState: Readonly<MsaRowTitleCheckboxState>, snapshot?: any) {
  43. if(prevProps.disabled != this.props.disabled && !this.props.disabled ) {
  44. switch (globalState[ this.compId()+this.props.tag ]){
  45. case "active":
  46. this.setState({checked: true, disabled:false});
  47. break;
  48. case "inactive":
  49. this.setState({checked: false, disabled:false});
  50. break;
  51. case "disabled":
  52. this.setState({disabled:true})
  53. break;
  54. case undefined:
  55. break;
  56. }
  57. }else if(prevProps.disabled != this.props.disabled) {
  58. this.setState({checked: this.props.tag == "aligned"},()=>{
  59. globalState[ this.compId()+this.props.tag ] = this.state.checked ? "active" : "inactive";
  60. });
  61. }
  62. }
  63. public componentWillUnmount() {
  64. this.subscription.unsubscribe();
  65. }
  66. private subscribe(): void{
  67. this.subscription = this.props.stateManager.subscribe<
  68. "representation-change"|"missing-component",
  69. {label:string;isHidden:boolean;} & {tag:MsaRowTitleCheckboxInterface["tag"];isHidden:boolean;pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;};}
  70. >((o)=>{
  71. if(o.type == "representation-change" && o.view == "3d-view" && o.data)
  72. this.structureViewerRepresentationChange(o.data);
  73. if(o.type == "missing-component" && o.view == "3d-view" && o.data)
  74. this.missingComponent(o.data as any);
  75. })
  76. }
  77. private missingComponent(pdb:{entryId:string;entityId:string;tag:string;}): void{
  78. if(this.compId() == this.getRcsbId(pdb) && this.props.tag == pdb.tag){
  79. globalState[this.compId()+this.props.tag] = "disabled"
  80. this.setState({disabled:true});
  81. }
  82. }
  83. private structureViewerRepresentationChange(d:{label:string;isHidden:boolean;}): void {
  84. const row: string[] = d.label.split(TagDelimiter.entity);
  85. const suffix: string = row.pop()!;
  86. const entryId: string = row.join(TagDelimiter.entity);
  87. const entityId: string = suffix.substring(0,1);
  88. if( this.compId() == `${entryId}${TagDelimiter.entity}${entityId}` ){
  89. //TODO this is a one to many relationship
  90. /*if( d.label.includes("polymer") && this.props.tag == "polymer" && d.isHidden == this.state.checked){
  91. this.setState({checked:!this.state.checked});
  92. }
  93. if( !d.label.includes("polymer") && this.props.tag == "non-polymer" && d.isHidden == this.state.checked){
  94. this.setState({checked:!this.state.checked});
  95. }*/
  96. }
  97. }
  98. private click(): void {
  99. if(this.props.disabled || this.state.disabled)
  100. return;
  101. this.setState({checked:!this.state.checked},()=>{
  102. globalState[this.compId()+this.props.tag] = this.state.checked ? "active" : "inactive";
  103. this.props.stateManager.next<"representation-change",{tag:MsaRowTitleCheckboxInterface["tag"];isHidden:boolean;pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;};}>({
  104. view:"1d-view",
  105. type: "representation-change",
  106. data:{
  107. pdb: "entityId" in this.props ? {
  108. entryId: this.props.entryId,
  109. entityId: this.props.entityId,
  110. } : {
  111. entryId: this.props.entryId,
  112. instanceId: this.props.instanceId
  113. },
  114. isHidden:!this.state.checked,
  115. tag:this.props.tag
  116. }
  117. });
  118. });
  119. }
  120. private style():React.CSSProperties {
  121. const color = {
  122. "checked":"rgb(51, 122, 183)",
  123. "unchecked":"rgba(51,122,183,0.44)",
  124. "disabled":"#ddd"
  125. }
  126. return {
  127. width:7,
  128. height:7,
  129. opacity: (this.props.disabled || this.state.disabled) && this.props.tag != "aligned" ? 0 : 1,
  130. backgroundColor: this.props.disabled || this.state.disabled ? color.disabled : color[ this.state.checked ? "checked" : "unchecked"],
  131. border: 1,
  132. borderStyle: "solid",
  133. borderColor: this.props.disabled || this.state.disabled ? color.disabled : color.checked,
  134. display:"inline-block",
  135. marginLeft:4,
  136. cursor: this.props.disabled || this.state.disabled ? undefined : "pointer"
  137. };
  138. }
  139. private compId(): string {
  140. if("entityId" in this.props)
  141. return `${this.props.entryId}${TagDelimiter.entity}${this.props.entityId}`;
  142. else
  143. return `${this.props.entryId}${TagDelimiter.instance}${this.props.instanceId}`;
  144. };
  145. private title(): string | undefined{
  146. if(this.props.disabled || this.state.disabled )
  147. return undefined;
  148. switch (this.props.tag){
  149. case "aligned":
  150. return `${this.state.checked ? "Hide" : "Show"} Aligned Polymer Chain`;
  151. case "polymer":
  152. return `${this.state.checked ? "Hide" : "Show"} Other Polymer Chains`;
  153. case "non-polymer":
  154. return `${this.state.checked ? "Hide" : "Show"} Non-polymer Chains`;
  155. }
  156. }
  157. private getRcsbId(pdb:{entryId:string;entityId:string;}|{entryId:string;instanceId:string;}): string {
  158. if("instanceId" in pdb)
  159. return `${pdb.entryId}${TagDelimiter.instance}${pdb.instanceId}`;
  160. else
  161. return `${pdb.entryId}${TagDelimiter.entity}${pdb.entityId}`;
  162. }
  163. }