UNPKG

6.96 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _body = require('./body');
10
11var _body2 = _interopRequireDefault(_body);
12
13var _head = require('./head');
14
15var _head2 = _interopRequireDefault(_head);
16
17var _foot = require('./foot');
18
19var _foot2 = _interopRequireDefault(_foot);
20
21var _style = require('./style.css');
22
23var _style2 = _interopRequireDefault(_style);
24
25var _sortBy = require('lodash/sortBy');
26
27var _sortBy2 = _interopRequireDefault(_sortBy);
28
29var _debounce = require('lodash/debounce');
30
31var _debounce2 = _interopRequireDefault(_debounce);
32
33var _reactAddonsShallowCompare = require('react-addons-shallow-compare');
34
35var _reactAddonsShallowCompare2 = _interopRequireDefault(_reactAddonsShallowCompare);
36
37var _react = require('react');
38
39var _react2 = _interopRequireDefault(_react);
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
43function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
44
45function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
46
47function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
48
49var ResultsTable = function (_Component) {
50 _inherits(ResultsTable, _Component);
51
52 function ResultsTable() {
53 var _ref;
54
55 var _temp, _this, _ret;
56
57 _classCallCheck(this, ResultsTable);
58
59 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
60 args[_key] = arguments[_key];
61 }
62
63 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ResultsTable.__proto__ || Object.getPrototypeOf(ResultsTable)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
64 sortBy: 'label',
65 orderByDesc: true,
66 showPrevious: false,
67 showNext: true
68 }, _this.onScroll = function (event) {
69 event.persist();
70 _this.updateNavigationIndicators(event);
71 }, _this.onHeaderClick = function (header) {
72 if (_this.state.sortBy === header.props.id) {
73 return _this.setState({ orderByDesc: !_this.state.orderByDesc });
74 }
75
76 return _this.setState({
77 sortBy: header.props.id,
78 orderByDesc: true
79 });
80 }, _this.updateNavigationIndicators = (0, _debounce2.default)(function (event) {
81 var scrollLeft = event.target.scrollLeft;
82
83 var _this$refs$body$getWi = _this.refs.body.getWidth(),
84 width = _this$refs$body$getWi.width,
85 scrollWidth = _this$refs$body$getWi.scrollWidth;
86
87 _this.setState({
88 showPrevious: scrollLeft > 60,
89 showNext: scrollLeft + width < scrollWidth - 60
90 });
91 }, 200), _temp), _possibleConstructorReturn(_this, _ret);
92 }
93
94 /**
95 * Define the types of props the component accepts.
96 *
97 * @type {Object}
98 */
99
100
101 /**
102 * Describe the state of the component.
103 *
104 * @type {Object}
105 */
106
107
108 _createClass(ResultsTable, [{
109 key: 'shouldComponentUpdate',
110
111
112 /**
113 * Determines if the component should be updated.
114 *
115 * @param {Object} nextProps
116 * @param {Object} nextState
117 * @return {Boolean}
118 */
119 value: function shouldComponentUpdate(nextProps, nextState) {
120 return (0, _reactAddonsShallowCompare2.default)(this, nextProps, nextState);
121 }
122
123 /**
124 * Invoked when the body of the table is scrolled.
125 *
126 * @param {Event}
127 * @return {void}
128 */
129
130
131 /**
132 * Invoked when a header is clicked.
133 * When the same header is clicked as it's currently ordered by we reverse
134 * the other, otherwise we order by the header & set the default of DESC as
135 * order.
136 *
137 * @param {String} header
138 * @return {void}
139 */
140
141 }, {
142 key: 'getEntries',
143
144
145 /**
146 * Get the entries for the table, ordered & sorted by as defined by the state.
147 *
148 * @return {Array}
149 */
150 value: function getEntries() {
151 var _this2 = this;
152
153 var entries = (0, _sortBy2.default)(this.props.entries, function (entry) {
154 var sorter = entry[_this2.state.sortBy];
155 return (0, _react.isValidElement)(sorter) ? sorter.props[sorter.props.sortBy] : sorter;
156 });
157 return this.state.orderByDesc ? entries : entries.reverse();
158 }
159
160 /**
161 * Update the navigation indicators, showing the previous/next arrows in
162 * the head/foot.
163 * This method is debounced for performance reasons since it's attached
164 * to the scroll event.
165 *
166 * @param {Event}
167 * @return {void}
168 */
169
170 }, {
171 key: 'render',
172
173
174 /**
175 * Render the component
176 *
177 * @return {ReactElement}
178 */
179 value: function render() {
180 return _react2.default.createElement(
181 'table',
182 { className: _style2.default.component, ref: 'component' },
183 _react2.default.createElement(_head2.default, {
184 sortBy: this.state.sortBy,
185 orderByDesc: this.state.orderByDesc,
186 entries: this.getEntries(),
187 onHeaderClick: this.onHeaderClick,
188 showNavigation: this.state.showPrevious
189 }),
190 _react2.default.createElement(_body2.default, {
191 ref: 'body',
192 entries: this.getEntries(),
193 countries: this.props.countries,
194 onScroll: this.onScroll
195 }),
196 _react2.default.createElement(_foot2.default, {
197 sortBy: this.state.sortBy,
198 orderByDesc: this.state.orderByDesc,
199 entries: this.getEntries(),
200 onHeaderClick: this.onHeaderClick,
201 showNavigation: this.state.showNext
202 })
203 );
204 }
205 }]);
206
207 return ResultsTable;
208}(_react.Component);
209
210ResultsTable.propTypes = {
211 entries: _react.PropTypes.array.isRequired,
212 countries: _react.PropTypes.array.isRequired
213};
214exports.default = ResultsTable;
\No newline at end of file