"use strict";

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react2 = require("@storybook/react");

var _addonKnobs = require("@storybook/addon-knobs");

var _index = _interopRequireDefault(require("./index.js"));

var _Button = _interopRequireDefault(require("../Button"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// Helper wrapper class to store the state so the stories are usable/interactive
var SortedTable =
/*#__PURE__*/
function (_React$Component) {
  _inherits(SortedTable, _React$Component);

  function SortedTable() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, SortedTable);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SortedTable)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      tableContents: _this.props.tableContents,
      currentOrder: _this.props.currentOrder,
      currentSortedColumn: _this.props.currentSortedColumn
    });

    _defineProperty(_assertThisInitialized(_this), "handleSorting", function (columnName) {
      var _sortToggleMap;

      // If the user isn't switching sort columns, toggle the sort direction
      var sortToggleMap = (_sortToggleMap = {}, _defineProperty(_sortToggleMap, "asc", "desc"), _defineProperty(_sortToggleMap, "desc", "asc"), _sortToggleMap);
      var newOrder = "asc";

      if (_this.state.currentSortedColumn === columnName) {
        newOrder = sortToggleMap[_this.state.currentOrder];
      }

      _this.setState({
        currentOrder: newOrder,
        currentSortedColumn: columnName
      });

      if (newOrder === "asc") {
        _this.state.tableContents.sort(function (a, b) {
          return a[columnName] > b[columnName] ? 1 : -1;
        });
      } else {
        _this.state.tableContents.sort(function (b, a) {
          return a[columnName] > b[columnName] ? 1 : -1;
        });
      }
    });

    return _this;
  }

  _createClass(SortedTable, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          currentOrder = _this$state.currentOrder,
          tableContents = _this$state.tableContents;
      return _react["default"].createElement(_index["default"], null, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, {
        sorting: {
          canSort: true,
          handleSort: function handleSort() {
            return _this2.handleSorting("Experiment");
          },
          order: currentOrder
        }
      }, "Experiment"), _react["default"].createElement(_index["default"].TH, {
        sorting: {
          canSort: true,
          handleSort: function handleSort() {
            return _this2.handleSorting("Conversion Rate");
          },
          order: currentOrder
        }
      }, "Conversion Rate"), _react["default"].createElement(_index["default"].TH, {
        sorting: {
          canSort: true,
          handleSort: function handleSort() {
            return _this2.handleSorting("Status");
          },
          order: currentOrder
        }
      }, "Status"))), _react["default"].createElement(_index["default"].TBody, null, tableContents.map(function (row, idx) {
        return _react["default"].createElement(_index["default"].TR, {
          key: idx
        }, _react["default"].createElement(_index["default"].TD, null, row.Experiment), _react["default"].createElement(_index["default"].TD, {
          width: "20%"
        }, row["Conversion Rate"], " "), _react["default"].createElement(_index["default"].TD, null, " ", row.Status, " "));
      })));
    }
  }]);

  return SortedTable;
}(_react["default"].Component);

SortedTable.propTypes = {
  currentOrder: _propTypes["default"].string,
  currentSortedColumn: _propTypes["default"].string,
  tableContents: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    Experiment: _propTypes["default"].string,
    "Conversion Rate": _propTypes["default"].string,
    Status: _propTypes["default"].string
  }))
};
var stories = (0, _react2.storiesOf)("Data Organization|Table", module);
stories.addDecorator(_addonKnobs.withKnobs).addDecorator(function (story) {
  return _react["default"].createElement("div", {
    id: "root-preview"
  }, story());
});
stories.add("Default", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], {
    density: (0, _addonKnobs.select)("density", {
      loose: "loose",
      tight: "tight"
    }, "tight"),
    tableLayoutAlgorithm: "fixed"
  }, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, " Experiment "), _react["default"].createElement(_index["default"].TH, null, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, null, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    width: "20%"
  }, " 12% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, null, " 4% "), _react["default"].createElement(_index["default"].TD, null, " Draft ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Larger search bar "), _react["default"].createElement(_index["default"].TD, null, " 6.7% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, null, " 9.3% "), _react["default"].createElement(_index["default"].TD, null, " Running ")))));
}).add("Custom tr borders", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], null, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, " Experiment "), _react["default"].createElement(_index["default"].TH, null, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, null, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, {
    borderStyle: "ends",
    backgroundColor: "faint"
  }, _react["default"].createElement(_index["default"].TD, null, " Header CTA "), _react["default"].createElement(_index["default"].TD, null, " 12% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, null, " 4% "), _react["default"].createElement(_index["default"].TD, null, " Draft ")))));
}).add("Loose & rule style", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], {
    density: "loose",
    style: "rule",
    tableLayoutAlgorithm: "fixed"
  }, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, " Experiment "), _react["default"].createElement(_index["default"].TH, {
    isNumerical: true
  }, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, null, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 12% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 4% "), _react["default"].createElement(_index["default"].TD, null, " Draft ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Larger search bar "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 6.7% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 9.3% "), _react["default"].createElement(_index["default"].TD, null, " Running ")))));
}).add("Tight & no bottom border", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], {
    density: "tight",
    style: "rule-no-bottom-border",
    tableLayoutAlgorithm: "fixed"
  }, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, "Experiment "), _react["default"].createElement(_index["default"].TH, {
    isNumerical: true
  }, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, null, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 12% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 4% "), _react["default"].createElement(_index["default"].TD, null, " Draft ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Larger search bar "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 6.7% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, null, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true
  }, " 9.3% "), _react["default"].createElement(_index["default"].TD, null, " Running ")))));
}).add("Dome", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], {
    density: "tight",
    style: "wall",
    tableLayoutAlgorithm: "auto"
  }, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, " Experiment "), _react["default"].createElement(_index["default"].TH, {
    isNumerical: true,
    width: "20%"
  }, "Conversion Rate", " "), _react["default"].createElement(_index["default"].TH, {
    isCollapsed: true
  }, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true,
    verticalAlign: "middle"
  }, " ", "12%", " "), _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, _react["default"].createElement(_Button["default"], {
    size: "small",
    width: "default"
  }, " ", "Start", " "))), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true,
    verticalAlign: "middle"
  }, " ", "4%", " "), _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, _react["default"].createElement(_Button["default"], {
    size: "small",
    width: "default"
  }, " ", "Start", " "))), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, " Larger search bar "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true,
    verticalAlign: "middle"
  }, " ", "6.7%", " "), _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, _react["default"].createElement(_Button["default"], {
    size: "small",
    width: "default"
  }, " ", "Start", " "))), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, {
    isNumerical: true,
    verticalAlign: "middle"
  }, " ", "9.3%", " "), _react["default"].createElement(_index["default"].TD, {
    verticalAlign: "middle"
  }, _react["default"].createElement(_Button["default"], {
    size: "small",
    width: "default"
  }, " ", "Start", " "))))));
}).add("Highlighted row w/ no border (loose style)", function () {
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], {
    density: "loose",
    style: "rule",
    tableLayoutAlgorithm: "fixed"
  }, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, null, " Experiment "), _react["default"].createElement(_index["default"].TH, null, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, null, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, {
    noBorder: true
  }, _react["default"].createElement(_index["default"].TD, null, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    width: "20%"
  }, " 12% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, {
    noBorder: true,
    isHighlighted: true
  }, _react["default"].createElement(_index["default"].TD, null, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, null, " 4% "), _react["default"].createElement(_index["default"].TD, null, " Draft ")), _react["default"].createElement(_index["default"].TR, {
    noBorder: true
  }, _react["default"].createElement(_index["default"].TD, null, " Larger search bar "), _react["default"].createElement(_index["default"].TD, null, " 6.7% "), _react["default"].createElement(_index["default"].TD, null, " Paused ")), _react["default"].createElement(_index["default"].TR, {
    noBorder: true
  }, _react["default"].createElement(_index["default"].TD, null, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, null, " 9.3% "), _react["default"].createElement(_index["default"].TD, null, " Running ")))));
}).add("Sortable table", function () {
  var currentOrder = "asc";
  var currentSortedColumn = "Experiment";
  var tableContents = [{
    Experiment: "Header CTA",
    "Conversion Rate": "12%",
    Status: "Paused"
  }, {
    Experiment: "Shorter Contact Form",
    "Conversion Rate": "4%",
    Status: "Draft"
  }, {
    Experiment: "Larger Search Bar",
    "Conversion Rate": "6.7%",
    Status: "Paused"
  }, {
    Experiment: "Center aligned headline",
    "Conversion Rate": "9.3%",
    Status: "Running"
  }]; // See top of file for implementation of a sortable table

  return _react["default"].createElement("div", null, _react["default"].createElement(SortedTable, {
    tableContents: tableContents,
    currentOrder: currentOrder,
    currentSortedColumn: currentSortedColumn
  }));
}).add("Text Alignment", function () {
  var textAlignment = (0, _addonKnobs.select)("textAlign", {
    center: "center",
    right: "right",
    left: "left"
  }, "left");
  return _react["default"].createElement("div", null, _react["default"].createElement(_index["default"], null, _react["default"].createElement(_index["default"].THead, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TH, {
    textAlign: textAlignment
  }, " Experiment "), _react["default"].createElement(_index["default"].TH, {
    textAlign: textAlignment
  }, " Conversion Rate "), _react["default"].createElement(_index["default"].TH, {
    textAlign: textAlignment
  }, " Status "))), _react["default"].createElement(_index["default"].TBody, null, _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Header CTA "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " 12% "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Shorter Contact Form "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " 4% "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Draft ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Larger search bar "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " 6.7% "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Paused ")), _react["default"].createElement(_index["default"].TR, null, _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Center aligned headline "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " 9.3% "), _react["default"].createElement(_index["default"].TD, {
    textAlign: textAlignment
  }, " Running ")))));
});