1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | 'use strict';
|
8 |
|
9 | Object.defineProperty(exports, '__esModule', {
|
10 | value: true
|
11 | });
|
12 |
|
13 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
14 |
|
15 | var _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; }; })();
|
16 |
|
17 | var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
|
18 |
|
19 | exports.root = root;
|
20 | exports.branch = branch;
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
23 |
|
24 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
25 |
|
26 | function _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; }
|
27 |
|
28 | var _react = require('react');
|
29 |
|
30 | var _react2 = _interopRequireDefault(_react);
|
31 |
|
32 | var _baobab = require('baobab');
|
33 |
|
34 | var _baobab2 = _interopRequireDefault(_baobab);
|
35 |
|
36 | var _utilsHelpersJs = require('./utils/helpers.js');
|
37 |
|
38 | var _utilsPropTypesJs = require('./utils/prop-types.js');
|
39 |
|
40 | var _utilsPropTypesJs2 = _interopRequireDefault(_utilsPropTypesJs);
|
41 |
|
42 | var makeError = _baobab2['default'].helpers.makeError;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function root(Component, tree) {
|
49 | if (!(tree instanceof _baobab2['default'])) throw makeError('baobab-react:higher-order.root: given tree is not a Baobab.', { target: tree });
|
50 |
|
51 | var componentDisplayName = Component.name || Component.displayName || 'Component';
|
52 |
|
53 | var ComposedComponent = (function (_React$Component) {
|
54 | _inherits(ComposedComponent, _React$Component);
|
55 |
|
56 | function ComposedComponent() {
|
57 | _classCallCheck(this, ComposedComponent);
|
58 |
|
59 | _get(Object.getPrototypeOf(ComposedComponent.prototype), 'constructor', this).apply(this, arguments);
|
60 | }
|
61 |
|
62 | _createClass(ComposedComponent, [{
|
63 | key: 'getChildContext',
|
64 |
|
65 |
|
66 | value: function getChildContext() {
|
67 | return {
|
68 | tree: tree
|
69 | };
|
70 | }
|
71 |
|
72 |
|
73 | }, {
|
74 | key: 'render',
|
75 | value: function render() {
|
76 | return _react2['default'].createElement(Component, this.props);
|
77 | }
|
78 | }], [{
|
79 | key: 'displayName',
|
80 | value: 'Rooted' + componentDisplayName,
|
81 | enumerable: true
|
82 | }, {
|
83 | key: 'childContextTypes',
|
84 | value: {
|
85 | tree: _utilsPropTypesJs2['default'].baobab
|
86 | },
|
87 | enumerable: true
|
88 | }]);
|
89 |
|
90 | return ComposedComponent;
|
91 | })(_react2['default'].Component);
|
92 |
|
93 | return ComposedComponent;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function branch(Component) {
|
101 | var mapping = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
|
102 |
|
103 | var componentDisplayName = Component.name || Component.displayName || 'Component';
|
104 |
|
105 | var ComposedComponent = (function (_React$Component2) {
|
106 | _inherits(ComposedComponent, _React$Component2);
|
107 |
|
108 | _createClass(ComposedComponent, [{
|
109 | key: 'getChildContext',
|
110 |
|
111 |
|
112 | value: function getChildContext() {
|
113 | return this.cursors ? {
|
114 | cursors: this.cursors
|
115 | } : {};
|
116 | }
|
117 |
|
118 |
|
119 | }], [{
|
120 | key: 'displayName',
|
121 | value: 'Branched' + componentDisplayName,
|
122 | enumerable: true
|
123 | }, {
|
124 | key: 'contextTypes',
|
125 | value: {
|
126 | tree: _utilsPropTypesJs2['default'].baobab
|
127 | },
|
128 | enumerable: true
|
129 | }, {
|
130 | key: 'childContextTypes',
|
131 | value: {
|
132 | cursors: _utilsPropTypesJs2['default'].cursors
|
133 | },
|
134 | enumerable: true
|
135 | }]);
|
136 |
|
137 | function ComposedComponent(props, context) {
|
138 | _classCallCheck(this, ComposedComponent);
|
139 |
|
140 | _get(Object.getPrototypeOf(ComposedComponent.prototype), 'constructor', this).call(this, props, context);
|
141 |
|
142 | if (mapping.cursors) {
|
143 | var solvedMapping = (0, _utilsHelpersJs.solveMapping)(mapping.cursors, props, context);
|
144 |
|
145 | if (!solvedMapping) throw makeError('baobab-react:higher-order.branch: given cursors mapping is invalid (check the "' + displayName + '" component).', { mapping: solvedMapping });
|
146 |
|
147 |
|
148 | this.watcher = this.context.tree.watch(solvedMapping);
|
149 | this.cursors = this.watcher.getCursors();
|
150 | this.state = this.watcher.get();
|
151 | }
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 | _createClass(ComposedComponent, [{
|
157 | key: 'componentWillMount',
|
158 | value: function componentWillMount() {
|
159 | if (!this.watcher) return;
|
160 |
|
161 | var handler = (function () {
|
162 | if (this.watcher) this.setState(this.watcher.get());
|
163 | }).bind(this);
|
164 |
|
165 | this.watcher.on('update', handler);
|
166 | }
|
167 |
|
168 |
|
169 | }, {
|
170 | key: 'render',
|
171 | value: function render() {
|
172 | var tree = this.context.tree,
|
173 | suppl = {};
|
174 |
|
175 |
|
176 | if (mapping.actions) {
|
177 | suppl.actions = {};
|
178 |
|
179 | Object.keys(mapping.actions).forEach(function (k) {
|
180 | suppl.actions[k] = mapping.actions[k].bind(tree, tree);
|
181 | });
|
182 | }
|
183 |
|
184 | return _react2['default'].createElement(Component, _extends({}, this.props, suppl, this.state));
|
185 | }
|
186 |
|
187 |
|
188 | }, {
|
189 | key: 'componentWillUnmount',
|
190 | value: function componentWillUnmount() {
|
191 | if (!this.watcher) return;
|
192 |
|
193 |
|
194 | this.watcher.release();
|
195 | this.watcher = null;
|
196 | }
|
197 |
|
198 |
|
199 | }, {
|
200 | key: 'componentWillReceiveProps',
|
201 | value: function componentWillReceiveProps(props) {
|
202 | if (!this.watcher || !mapping.cursors) return;
|
203 |
|
204 | var solvedMapping = (0, _utilsHelpersJs.solveMapping)(mapping.cursors, props, this.context);
|
205 |
|
206 | if (!solvedMapping) throw makeError('baobab-react:higher-order.branch: given mapping is invalid (check the "' + displayName + '" component).', { mapping: solvedMapping });
|
207 |
|
208 |
|
209 | this.watcher.refresh(solvedMapping);
|
210 | this.cursors = this.watcher.getCursors();
|
211 | this.setState(this.watcher.get());
|
212 | }
|
213 | }]);
|
214 |
|
215 | return ComposedComponent;
|
216 | })(_react2['default'].Component);
|
217 |
|
218 | return ComposedComponent;
|
219 | } |
\ | No newline at end of file |