UNPKG

3.19 kBJavaScriptView Raw
1/**
2 * Baobab-React Mixins
3 * ====================
4 *
5 * Old style react mixins.
6 */
7'use strict';
8
9var PropTypes = require('./utils/prop-types.js'),
10 helpers = require('./utils/helpers.js'),
11 makeError = require('baobab').helpers.makeError;
12
13/**
14 * Helpers
15 */
16function displayName(instance) {
17 return (instance.constructor || {}).displayName || 'Component';
18}
19
20function bindActions(actions) {
21 var tree = this.context.tree,
22 suppl = {};
23
24 this.actions = {};
25
26 Object.keys(actions).forEach(function (k) {
27 this.actions[k] = actions[k].bind(tree, tree);
28 }, this);
29}
30
31/**
32 * Root mixin
33 */
34var RootMixin = {
35
36 // Component prop Type
37 propTypes: {
38 tree: PropTypes.baobab
39 },
40
41 // Context prop types
42 childContextTypes: {
43 tree: PropTypes.baobab
44 },
45
46 // Handling child context
47 getChildContext: function getChildContext() {
48 return {
49 tree: this.props.tree
50 };
51 }
52};
53
54/**
55 * Branch mixin
56 */
57var BranchMixin = {
58
59 // Context prop types
60 contextTypes: {
61 tree: PropTypes.baobab
62 },
63
64 // Building initial state
65 getInitialState: function getInitialState() {
66 var name = displayName(this);
67
68 if (this.actions) {
69 this.__actionsMapping = this.actions;
70 bindActions.call(this, this.__actionsMapping);
71 }
72
73 if (this.cursors) {
74 this.__cursorsMapping = this.cursors;
75
76 var solvedMapping = helpers.solveMapping(this.__cursorsMapping, this.props, this.context);
77
78 // The given cursors property should be valid
79 if (!solvedMapping) throw makeError('baobab-react:mixins.branch: given mapping is invalid (check the "' + name + '" component).', { mapping: solvedMapping });
80
81 // Creating the watcher
82 this.__watcher = this.context.tree.watch(solvedMapping);
83
84 // Binding cursors
85 this.cursors = this.__watcher.getCursors();
86
87 // Setting initial state
88 return this.__watcher.get();
89 }
90
91 return null;
92 },
93
94 // On component mount
95 componentWillMount: function componentWillMount() {
96 if (!this.__watcher) return;
97
98 var handler = (function () {
99 if (this.__watcher) this.setState(this.__watcher.get());
100 }).bind(this);
101
102 this.__watcher.on('update', handler);
103 },
104
105 // On component unmount
106 componentWillUnmount: function componentWillUnmount() {
107 if (!this.__watcher) return;
108
109 // Releasing facet
110 this.__watcher.release();
111 this.__watcher = null;
112 },
113
114 // On new props
115 componentWillReceiveProps: function componentWillReceiveProps(props) {
116 if (!this.__watcher) return;
117
118 // Refreshing the watcher
119 var solvedMapping = helpers.solveMapping(this.__cursorsMapping, props, this.context);
120
121 if (!solvedMapping) throw makeError('baobab-react:mixins.branch: given mapping is invalid (check the "' + displayName(this) + '" component).', { mapping: solvedMapping });
122
123 this.__watcher.refresh(solvedMapping);
124 this.cursors = this.__watcher.getCursors();
125 this.setState(this.__watcher.get());
126 },
127
128 // On update
129 componentWillUpdate: function componentWillUpdate() {
130 if (this.__actionsMapping) bindActions.call(this, this.__actionsMapping);
131 }
132};
133
134// Exporting
135exports.root = RootMixin;
136exports.branch = BranchMixin;
\No newline at end of file