All files / src/components MSAViewer.js

94.59% Statements 35/37
87.5% Branches 7/8
90% Functions 9/10
94.59% Lines 35/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                                                                          4x       4x                           6x 6x   6x 6x               6x                                                                                       20x 20x 120x 1x 1x 119x   4x     20x       6x 6x     6x   2x             2x         7x           7x 7x 2x                 5x 5x 5x 5x     5x     5x     5x                                                     4x       4x   4x 4x                      
/**
* Copyright 2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
 
import React, { Component } from 'react';
import createRef from 'create-react-ref/lib/createRef';
 
import {
  forOwn,
} from 'lodash-es'
 
import {
  msaDefaultProps,
  MSAPropTypes,
  PropTypes,
} from '../PropTypes';
 
import {
  Labels,
  OverviewBar,
  PositionBar,
  SequenceViewer,
} from './index';
 
import MSAProvider from '../store/provider';
import propsToRedux from '../store/propsToRedux';
 
import {
  actions,
  positionReducer,
  createPositionStore,
} from '../store/positionReducers';
 
const same = "FORWARD_SAME_PROP_NAME";
 
// list of events with a default implementation
// mapping: eventName -> DOM event name
const defaultEvents = {
  "onResidueClick": "residueClick",
};
 
/**
 * A general-purpose layout for the MSA components
 *
 * When children are passed it acts as a Context Provider for the msaStore,
 * otherwise it provides a default layout and forwards it props the respective
 * components.
 */
class MSAViewerComponent extends Component {
 
    constructor(props) {
      super(props);
      this.el = createRef();
      // add default event callback
      forOwn(defaultEvents, (domEventName, eventName) => {
        this["_" + eventName] = (e) => {
          const event = new CustomEvent(domEventName, {
            detail: e,
            bubbles: true,
          });
          this.el.current.dispatchEvent(event);
        };
      });
      this._setupStores();
    }
 
  // List of props forwarded to the SequenceViewer component
  static sequenceViewerProps = {
    "showModBar": same,
    "onResidueMouseEnter": same,
    "onResidueMouseLeave": same,
    "onResidueClick": same,
    "onResidueDoubleClick": same,
    "sequenceBorder": "border",
    "sequenceBorderColor": "borderColor",
    "sequenceBorderWidth": "borderWidth",
    "sequenceTextColor": "textColor",
    "sequenceTextFont": "textFont",
    "sequenceOverflow": "overflow",
    "sequenceOverflowX": "overflowX",
    "sequenceOverflowy": "overflowY",
    "sequenceScrollBarPositionX": "scrollBarPositionX",
    "sequenceScrollBarPositionY": "scrollBarPositionY",
  };
 
  // List of props forwarded to the Labels component
  static labelsProps = {
    "labelComponent": same,
    "labelStyle": same,
    "labelAttributes": same,
  };
 
  // List of props forwarded to the PositionBar component
  static positionBarProps = {
    "markerComponent": same,
    "markerStyle": same,
    "markerAttributes": same,
  };
 
  // List of props forwarded to the OverviewBar component
  static overviewBarProps = {
    "barComponent": same,
    "barStyle": same,
    "barAttributes": same,
  };
 
  forwardProps(propsToBeForwarded) {
    const options = {};
    forOwn(propsToBeForwarded, (forwardedName, currentName) => {
      if (this.props[currentName] !== undefined) {
        const name = forwardedName === same ? currentName : forwardedName;
        options[name] = this.props[currentName];
      } else if (currentName in defaultEvents) {
        // inject default event handler
        options[currentName] = this["_" + currentName];
      }
    });
    return options;
  }
 
  _setupStores() {
    this.positionStore = createPositionStore(positionReducer);
    this.positionStore.dispatch(
      actions.updateMainStore(this.props.msaStore.getState())
    );
    this.msaStoreUnsubscribe = this.props.msaStore.subscribe(() => {
      // forward the msaStore to the positionStore for convenience
      this.positionStore.dispatch(
        actions.updateMainStore(this.props.msaStore.getState())
      );
    });
  }
 
  componentWillUnmount() {
    this.msaStoreUnsubscribe();
  }
 
  // TODO: we could inject this in the main redux store for better compatibility
  getChildContext() {
    return {
      positionMSAStore: this.positionStore,
    };
  }
 
  render() {
    const {children, msaStore, ...otherProps} = this.props;
    if (children) {
      return (
        <MSAProvider store={msaStore}>
          <div {...otherProps} ref={this.el}>
            {children}
          </div>
        </MSAProvider>
      );
    } else {
      // TODO: add more advanced layouts
      const currentState = msaStore.getState();
      const labelsPadding = currentState.props.tileHeight;
      const overviewBarHeight = 50;
      const labelsAndSequenceDiv = {
        display: "flex",
      };
      const labelsStyle = {
        paddingTop: labelsPadding + overviewBarHeight,
      }
      const separatorPadding = {
        height: 10,
      };
      return (
        <MSAProvider store={msaStore}>
          <div style={labelsAndSequenceDiv} ref={this.el}>
            <Labels
              style={labelsStyle}
              {...this.forwardProps(MSAViewerComponent.labelsProps)}
            />
            <div>
              <OverviewBar height={overviewBarHeight}
                {...this.forwardProps(MSAViewerComponent.overviewBarProps)}
              />
              <PositionBar
                {...this.forwardProps(MSAViewerComponent.positionBarProps)}
              />
              <SequenceViewer
                {...this.forwardProps(MSAViewerComponent.sequenceViewerProps)}
              />
              <div style={separatorPadding} />
            </div>
          </div>
        </MSAProvider>
      );
      //<SequenceOverview />
    }
  }
}
 
MSAViewerComponent.childContextTypes = {
  positionMSAStore: PropTypes.object,
};
 
const MSAViewer = propsToRedux(MSAViewerComponent);
 
MSAViewer.defaultProps = msaDefaultProps;
MSAViewer.propTypes = {
  /**
   * A custom msaStore (created with `createMSAStore`).
   * Useful for custom interaction with other components
   */
  msaStore: PropTypes.object,
 
  ...MSAPropTypes
};
 
export default MSAViewer;