UNPKG

6.42 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 _style = require('./style.css');
10
11var _style2 = _interopRequireDefault(_style);
12
13var _each = require('lodash/each');
14
15var _each2 = _interopRequireDefault(_each);
16
17var _flag = require('../flag');
18
19var Flags = _interopRequireWildcard(_flag);
20
21var _tableItem = require('../table-item');
22
23var _tableItem2 = _interopRequireDefault(_tableItem);
24
25var _react = require('react');
26
27var _react2 = _interopRequireDefault(_react);
28
29var _reactAddonsShallowCompare = require('react-addons-shallow-compare');
30
31var _reactAddonsShallowCompare2 = _interopRequireDefault(_reactAddonsShallowCompare);
32
33function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
34
35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
37function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
38
39function _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; }
40
41function _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; }
42
43var Body = function (_Component) {
44 _inherits(Body, _Component);
45
46 function Body() {
47 var _ref;
48
49 var _temp, _this, _ret;
50
51 _classCallCheck(this, Body);
52
53 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
54 args[_key] = arguments[_key];
55 }
56
57 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Body.__proto__ || Object.getPrototypeOf(Body)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
58 headerPosition: 0
59 }, _this.step = function () {
60 var top = _this.refs.component.getBoundingClientRect().top;
61 var offset = top > 0 ? 0 : Math.abs(top);
62 (0, _each2.default)(_this.headers, function (header) {
63 header.style.transform = 'translateY(' + offset + 'px)'; // eslint-disable-line no-param-reassign
64 });
65 _this.requestId = requestAnimationFrame(_this.step);
66 }, _temp), _possibleConstructorReturn(_this, _ret);
67 }
68
69 /**
70 * Define the prop types of the component.
71 *
72 * @type {Object}
73 */
74
75
76 /**
77 * The state of the component.
78 *
79 * @type {Object}
80 */
81
82
83 _createClass(Body, [{
84 key: 'componentDidMount',
85
86
87 /**
88 * Invoked when the component is mounted.
89 *
90 * @return {void}
91 */
92 value: function componentDidMount() {
93 this.headers = this.refs.component.querySelectorAll('tr > th');
94 this.requestId = requestAnimationFrame(this.step);
95 }
96
97 /**
98 * Determines if the component should be updated.
99 *
100 * @param {Object} nextProps
101 * @param {Object} nextState
102 * @return {Boolean}
103 */
104
105 }, {
106 key: 'shouldComponentUpdate',
107 value: function shouldComponentUpdate(nextProps, nextState) {
108 return (0, _reactAddonsShallowCompare2.default)(this, nextProps, nextState);
109 }
110
111 /**
112 * Invoked when the component will unmount.
113 *
114 * @return {void}
115 */
116
117 }, {
118 key: 'componentWillUnmount',
119 value: function componentWillUnmount() {
120 cancelAnimationFrame(this.requestId);
121 }
122
123 /**
124 * Get the width of the component.
125 *
126 * @return {Object}
127 */
128
129 }, {
130 key: 'getWidth',
131 value: function getWidth() {
132 return {
133 scrollWidth: this.refs.component.scrollWidth,
134 width: this.refs.component.getBoundingClientRect().width
135 };
136 }
137
138 /**
139 * Move the header according to the scroll position.
140 *
141 * @return {void}
142 */
143
144 }, {
145 key: 'render',
146
147
148 /**
149 * Render the component.
150 *
151 * @return {ReactElement}
152 */
153 value: function render() {
154 var _this2 = this;
155
156 return _react2.default.createElement(
157 'tbody',
158 { ref: 'component', className: _style2.default.body, onScroll: this.props.onScroll },
159 this.props.countries.map(function (country) {
160 return _react2.default.createElement(
161 'tr',
162 {
163 key: country,
164 style: { position: 'relative' },
165 className: [_style2.default.column, _style2.default.body_column].join(' ')
166 },
167 _react2.default.createElement(
168 'th',
169 { className: _style2.default.body_header },
170 _react2.default.createElement(Flags[country], { height: 20 })
171 ),
172 _this2.props.entries.map(function (entry) {
173 return _react2.default.createElement(
174 _tableItem2.default,
175 {
176 align: 'center',
177 key: entry.id,
178 className: _style2.default.body_item
179 },
180 entry.votes[country] || '.'
181 );
182 })
183 );
184 })
185 );
186 }
187 }]);
188
189 return Body;
190}(_react.Component);
191
192Body.propTypes = {
193 onScroll: _react.PropTypes.func.isRequired,
194 entries: _react.PropTypes.array.isRequired,
195 countries: _react.PropTypes.array.isRequired
196};
197exports.default = Body;
\No newline at end of file