Code coverage report for src/components/task/breadcrumbs.cjsx

Statements: 88.14% (52 / 59)      Branches: 68.75% (11 / 16)      Functions: 83.33% (15 / 18)      Lines: 88.14% (52 / 59)      Ignored: none     

All files » src/components/task/ » breadcrumbs.cjsx
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                                 24×               24× 24× 24×     24× 24× 24×     24×     24× 24× 24× 24× 111× 111× 111× 111× 111× 111×   24× 24×               24× 24× 24×     124× 23×       76×         122×         124×     23× 23× 23×         124×                                   148× 148× 148× 148× 148× 665× 665×     665×                         148× 148×     148×            
var BreadcrumbTaskDynamic, ChapterSectionMixin, CrumbMixin, React, ResizeListenerMixin, TaskStepActions, TaskStepStore, TaskStore, _, ref1, ref2;
 
React = require('react');
 
ref1 = require('../../flux/task-step'), TaskStepActions = ref1.TaskStepActions, TaskStepStore = ref1.TaskStepStore;
 
TaskStore = require('../../flux/task').TaskStore;
 
_ = require('underscore');
 
CrumbMixin = require('./crumb-mixin');
 
ref2 = require('openstax-react-components'), ChapterSectionMixin = ref2.ChapterSectionMixin, ResizeListenerMixin = ref2.ResizeListenerMixin;
 
BreadcrumbTaskDynamic = require('../breadcrumb').BreadcrumbTaskDynamic;
 
module.exports = React.createClass({
  displayName: 'Breadcrumbs',
  mixins: [ChapterSectionMixin, CrumbMixin, ResizeListenerMixin],
  propTypes: {
    id: React.PropTypes.string.isRequired,
    currentStep: React.PropTypes.number.isRequired,
    goToStep: React.PropTypes.func.isRequired
  },
  getInitialState: function() {
    return {
      updateOnNext: true,
      hoverCrumb: this.props.currentStep,
      shouldShrink: null,
      crumbsWidth: null
    };
  },
  componentWillMount: function() {
    var crumbs, listeners;
    listeners = this.getMaxListeners();
    Iif ((listeners != null) && (listeners + 1) > 10) {
      TaskStepStore.setMaxListeners(listeners + 1);
    }
    TaskStore.on('task.beforeRecovery', this.stopUpdate);
    TaskStore.on('task.afterRecovery', this.update);
    return crumbs = this.getCrumableCrumbs();
  },
  componentDidMount: function() {
    return this.calculateCrumbsWidth();
  },
  calculateCrumbsWidth: function(crumbDOM) {
    var crumbsWidth, currentCrumbWidth;
    Eif (this.isMounted()) {
      currentCrumbWidth = 0;
      crumbsWidth = _.reduce(this.refs, function(memo, ref) {
        var computedStyle, refDOM, refDOMBox;
        refDOM = ref.getDOMNode();
        computedStyle = window.getComputedStyle(refDOM);
        refDOMBox = refDOM.getBoundingClientRect();
        currentCrumbWidth = refDOMBox.width + parseInt(computedStyle.marginRight) + parseInt(computedStyle.marginLeft);
        return currentCrumbWidth + memo;
      }, 0);
      crumbsWidth += currentCrumbWidth;
      Iif (crumbsWidth > this.state.crumbsWidth) {
        return this.setState({
          crumbsWidth: crumbsWidth
        });
      }
    }
  },
  componentWillUnmount: function() {
    TaskStepStore.setMaxListeners(10);
    TaskStore.off('task.beforeRecovery', this.stopUpdate);
    return TaskStore.off('task.afterRecovery', this.update);
  },
  componentDidUpdate: function(prevProps, prevState) {
    if (this.didWidthChange(prevState, this.state)) {
      return this.setShouldShrink(this.state);
    }
  },
  componentWillReceiveProps: function(nextProps) {
    return this.setState({
      hoverCrumb: nextProps.currentStep
    });
  },
  crumbMounted: function() {
    Iif (this.state.crumbsWidth != null) {
      return this.calculateCrumbsWidth();
    }
  },
  didWidthChange: function(prevState, currentState) {
    return currentState.crumbsWidth !== prevState.crumbsWidth || currentState.componentEl.width !== prevState.componentEl.width;
  },
  setShouldShrink: function(sizes) {
    var shouldShrink;
    shouldShrink = sizes.componentEl.width < this.state.crumbsWidth;
    return this.setState({
      shouldShrink: shouldShrink
    });
  },
  shouldComponentUpdate: function(nextProps, nextState) {
    return nextState.updateOnNext;
  },
  update: function() {
    return this.setState({
      updateOnNext: true
    });
  },
  stopUpdate: function() {
    return this.setState({
      updateOnNext: false
    });
  },
  updateHoverCrumb: function(hover) {
    return this.setState({
      hoverCrumb: hover
    });
  },
  render: function() {
    var classes, crumbs, currentStep, goToStep, ref3, stepButtons;
    crumbs = this.getCrumableCrumbs();
    ref3 = this.props, currentStep = ref3.currentStep, goToStep = ref3.goToStep;
    stepButtons = _.map(crumbs, (function(_this) {
      return function(crumb, crumbIndex) {
        var crumbStyle;
        crumbStyle = {
          zIndex: crumbs.length - Math.abs(_this.state.hoverCrumb - crumbIndex)
        };
        return React.createElement(BreadcrumbTaskDynamic, {
          "onMouseEnter": _this.updateHoverCrumb.bind(_this, crumbIndex),
          "onMouseLeave": _this.updateHoverCrumb.bind(_this, _this.props.currentStep),
          "onMount": _this.crumbMounted,
          "style": crumbStyle,
          "crumb": crumb,
          "currentStep": currentStep,
          "goToStep": goToStep,
          "key": "breadcrumb-" + crumb.type + "-" + crumb.key,
          "ref": "breadcrumb-" + crumb.type + "-" + crumb.key
        });
      };
    })(this));
    classes = 'task-breadcrumbs';
    Iif (this.state.shouldShrink) {
      classes += ' shrink';
    }
    return React.createElement("div", {
      "className": classes
    }, stepButtons);
  }
});