MsaRowTitleCheckbox.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 {RcsbFvStateManager} from "../../../../../RcsbFvState/RcsbFvStateManager";
  7. import {TagDelimiter} from "@rcsb/rcsb-saguaro-app";
  8. import {Subscription} from "rxjs";
  9. interface MsaRowTitleCheckboxInterface {
  10. disabled:boolean;
  11. entryId: string;
  12. entityId: string;
  13. tag:"aligned"|"polymer"|"non-polymer";
  14. stateManager:RcsbFvStateManager
  15. }
  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 MsaRowTitleCheckbox extends React.Component <MsaRowTitleCheckboxInterface,MsaRowTitleCheckboxState> {
  23. readonly state: MsaRowTitleCheckboxState = {
  24. checked: globalState[ this.entityId()+this.props.tag ] == "active" || this.props.tag == "aligned" ? true : false,
  25. disabled: globalState[ this.entityId()+this.props.tag ] == "disabled" ? true : false
  26. };
  27. private subscription: Subscription;
  28. constructor(props: MsaRowTitleCheckboxInterface) {
  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.entityId()+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.entityId()+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;};}
  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.entityId() == `${pdb.entryId}${TagDelimiter.entity}${pdb.entityId}` && this.props.tag == pdb.tag){
  79. globalState[this.entityId()+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.entityId() == `${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.entityId()+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;};}>({
  104. view:"1d-view",
  105. type: "representation-change",
  106. data:{
  107. pdb:{
  108. entryId: this.props.entryId,
  109. entityId: this.props.entityId
  110. },
  111. isHidden:!this.state.checked,
  112. tag:this.props.tag
  113. }
  114. });
  115. });
  116. }
  117. private style():React.CSSProperties {
  118. const color = {
  119. "checked":"rgb(51, 122, 183)",
  120. "unchecked":"rgba(51,122,183,0.44)",
  121. "disabled":"#ddd"
  122. }
  123. return {
  124. width:7,
  125. height:7,
  126. backgroundColor: this.props.disabled || this.state.disabled ? color.disabled : color[ this.state.checked ? "checked" : "unchecked"],
  127. border: 1,
  128. borderStyle: "solid",
  129. borderColor: this.props.disabled || this.state.disabled ? color.disabled : color.checked,
  130. display:"inline-block",
  131. marginLeft:4,
  132. cursor: this.props.disabled || this.state.disabled ? undefined : "pointer"
  133. };
  134. }
  135. private entityId(): string {
  136. return `${this.props.entryId}${TagDelimiter.entity}${this.props.entityId}`;
  137. };
  138. private title(): string | undefined{
  139. if(this.props.disabled || this.state.disabled )
  140. return undefined;
  141. switch (this.props.tag){
  142. case "aligned":
  143. return `${this.state.checked ? "Hide" : "Show"} Aligned Polymer Chain`;
  144. case "polymer":
  145. return `${this.state.checked ? "Hide" : "Show"} Other Polymer Chains`;
  146. case "non-polymer":
  147. return `${this.state.checked ? "Hide" : "Show"} Non-polymer Chains`;
  148. }
  149. }
  150. }