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