Skip to content

Commit e0a4aff

Browse files
committed
Added lib for installing from git
1 parent 7843d75 commit e0a4aff

File tree

6 files changed

+238
-2
lines changed

6 files changed

+238
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@ npm-debug.log
2020
Session.vim
2121
.netrwhist
2222
*~
23-
24-
lib

lib/connect.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; };
8+
9+
exports.default = connect;
10+
11+
var _normalizeProps = require('./normalizeProps');
12+
13+
var _normalizeProps2 = _interopRequireDefault(_normalizeProps);
14+
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16+
17+
function noop() {}
18+
19+
function getStore(component) {
20+
return component.$store;
21+
}
22+
23+
function getAttrs(component) {
24+
return component._self.$options._parentVnode.data.attrs;
25+
}
26+
27+
function getStates(component, mapStateToProps) {
28+
var store = getStore(component);
29+
var attrs = getAttrs(component);
30+
31+
return mapStateToProps(store.getState(), attrs) || {};
32+
}
33+
34+
function getActions(component, mapActionsToProps) {
35+
var store = getStore(component);
36+
37+
return mapActionsToProps(store.dispatch, getAttrs.bind(null, component)) || {};
38+
}
39+
40+
function getProps(component) {
41+
var props = {};
42+
var attrs = getAttrs(component);
43+
var stateNames = component.vuaReduxStateNames;
44+
var actionNames = component.vuaReduxActionNames;
45+
46+
for (var ii = 0; ii < stateNames.length; ii++) {
47+
props[stateNames[ii]] = component[stateNames[ii]];
48+
}
49+
50+
for (var _ii = 0; _ii < actionNames.length; _ii++) {
51+
props[actionNames[_ii]] = component[actionNames[_ii]];
52+
}
53+
54+
return _extends({}, props, attrs);
55+
}
56+
57+
function defaultMergeProps(stateProps, actionsProps) {
58+
return _extends({}, stateProps, actionsProps);
59+
}
60+
61+
/**
62+
* 1. utilities are moved above because vue stores methods, states and props
63+
* in the same namespace
64+
* 2. actions are set while created
65+
*/
66+
67+
/**
68+
* @param mapStateToProps
69+
* @param mapActionsToProps
70+
* @param mergeProps
71+
* @returns Object
72+
*/
73+
function connect(mapStateToProps, mapActionsToProps, mergeProps) {
74+
mapStateToProps = mapStateToProps || noop;
75+
mapActionsToProps = mapActionsToProps || noop;
76+
mergeProps = mergeProps || defaultMergeProps;
77+
78+
return function (children) {
79+
80+
/** @namespace children.collect */
81+
if (children.collect) {
82+
children.props = _extends({}, (0, _normalizeProps2.default)(children.props || {}), (0, _normalizeProps2.default)(children.collect || {}));
83+
84+
var msg = 'vua-redux: collect is deprecated, use props ' + ('in ' + (children.name || 'anonymous') + ' component');
85+
86+
console.warn(msg);
87+
}
88+
89+
return {
90+
name: 'ConnectVuaRedux-' + (children.name || 'children'),
91+
92+
render: function render(h) {
93+
var props = getProps(this);
94+
95+
return h(children, { props: props });
96+
},
97+
data: function data() {
98+
var state = getStates(this, mapStateToProps);
99+
var actions = getActions(this, mapActionsToProps);
100+
var stateNames = Object.keys(state);
101+
var actionNames = Object.keys(actions);
102+
103+
return _extends({}, mergeProps(state, actions), {
104+
vuaReduxStateNames: stateNames,
105+
vuaReduxActionNames: actionNames
106+
});
107+
},
108+
created: function created() {
109+
var _this = this;
110+
111+
var store = getStore(this);
112+
113+
this.vuaReduxUnsubscribe = store.subscribe(function () {
114+
var state = getStates(_this, mapStateToProps);
115+
var stateNames = Object.keys(state);
116+
_this.vuaReduxStateNames = stateNames;
117+
118+
for (var ii = 0; ii < stateNames.length; ii++) {
119+
_this[stateNames[ii]] = state[stateNames[ii]];
120+
}
121+
});
122+
},
123+
beforeDestroy: function beforeDestroy() {
124+
this.vuaReduxUnsubscribe();
125+
}
126+
};
127+
};
128+
}

lib/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.reduxStorePlugin = exports.connect = undefined;
7+
8+
var _connect2 = require('./connect');
9+
10+
var _connect3 = _interopRequireDefault(_connect2);
11+
12+
var _reduxStorePlugin2 = require('./reduxStorePlugin');
13+
14+
var _reduxStorePlugin3 = _interopRequireDefault(_reduxStorePlugin2);
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
exports.connect = _connect3.default;
19+
exports.reduxStorePlugin = _reduxStorePlugin3.default;

lib/normalizeProps.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = normalizeProps;
7+
8+
var _isArray = require('lodash/isArray');
9+
10+
var _isArray2 = _interopRequireDefault(_isArray);
11+
12+
var _isPlainObject = require('lodash/isPlainObject');
13+
14+
var _isPlainObject2 = _interopRequireDefault(_isPlainObject);
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
// https://github.com/vuejs/vue/blob/dev/src/util/options.js
19+
function normalizeProps(props) {
20+
var i,
21+
val,
22+
normalizedProps = {};
23+
24+
if ((0, _isArray2.default)(props)) {
25+
i = props.length;
26+
while (i--) {
27+
val = props[i];
28+
if (typeof val === 'string') {
29+
normalizedProps[val] = null;
30+
} else if (val.name) {
31+
normalizedProps[val.name] = val;
32+
}
33+
}
34+
} else if ((0, _isPlainObject2.default)(props)) {
35+
var keys = Object.keys(props);
36+
i = keys.length;
37+
while (i--) {
38+
var key = keys[i];
39+
val = props[key];
40+
normalizedProps[key] = props[key];
41+
if (typeof val === 'function') {
42+
normalizedProps[key] = { type: val };
43+
}
44+
}
45+
}
46+
47+
return normalizedProps;
48+
}

lib/normalizeProps.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var _expect = require('expect');
4+
5+
var _expect2 = _interopRequireDefault(_expect);
6+
7+
var _normalizeProps = require('./normalizeProps');
8+
9+
var _normalizeProps2 = _interopRequireDefault(_normalizeProps);
10+
11+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12+
13+
describe('normalize props', function () {
14+
it('should normalize array props', function () {
15+
(0, _expect2.default)((0, _normalizeProps2.default)(['a', 'b'])).toEqual({ a: null, b: null });
16+
});
17+
18+
it('should normalize object props', function () {
19+
var props = { 'a': { type: String }, 'b': null };
20+
(0, _expect2.default)((0, _normalizeProps2.default)(props)).toEqual(props);
21+
});
22+
});

lib/reduxStorePlugin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.default = reduxStorePlugin;
7+
function reduxStorePlugin(Vue) {
8+
var storeId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'store';
9+
10+
Vue.mixin({
11+
beforeCreate: function beforeCreate() {
12+
var options = this.$options;
13+
// store injection
14+
if (options[storeId]) {
15+
this.$store = options.store;
16+
} else if (options.parent && options.parent.$store) {
17+
this.$store = options.parent.$store;
18+
}
19+
}
20+
});
21+
}

0 commit comments

Comments
 (0)