UNPKG

5.89 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = undefined;
7
8var _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; }; }();
9
10exports.resetStores = resetStores;
11
12var _immutable = require('immutable');
13
14var _immutable2 = _interopRequireDefault(_immutable);
15
16var _transmitter = require('transmitter');
17
18var _transmitter2 = _interopRequireDefault(_transmitter);
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
23
24function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
26var reset = (0, _transmitter2.default)();
27function resetStores() {
28 reset.publish();
29}
30
31var ImmutableStore = function () {
32 function ImmutableStore(initialData) {
33 var _this = this;
34
35 _classCallCheck(this, ImmutableStore);
36
37 this.reset = function () {
38 _this.change(_this.initialData);
39 };
40
41 this.getItemById = function (id, listName) {
42 var list = _this.state.get(listName || _this.listName);
43 if (list) {
44 return list.find(function (item) {
45 return item.get('id') === id;
46 });
47 } else {
48 console.warn(_this, 'Failed in getItemById(): list not found');
49 }
50 return null;
51 };
52
53 this.getIndexById = function (id, listName) {
54 listName = listName || _this.listName;
55 var list = _this.state.get(listName);
56 if (list) {
57 return list.indexOf(_this.getItemById(id, listName));
58 }
59 console.warn(_this, 'Failed in getIndexById(): list not found');
60 return -1;
61 };
62
63 this.find = function (listName, filter) {
64 var list = _this.state.get(listName);
65 var result = list.find(function (item) {
66 var match = true;
67 Object.keys(filter).map(function (prop) {
68 var itemValue = item.get(prop);
69 var filterValue = filter[prop];
70 if (itemValue !== filterValue) {
71 match = false;
72 }
73 });
74 return match;
75 });
76 return result;
77 };
78
79 this.listName = 'items';
80 this.exportPublicMethods({
81 getItemById: this.getItemById,
82 getIndexById: this.getIndexById,
83 find: this.find,
84 reset: this.reset
85 });
86 if (initialData) {
87 this.init(initialData);
88 }
89 reset.subscribe(function () {
90 var data = _this.initialData || {};
91 var nextState = data.toJS ? data : _immutable2.default.fromJS(data);
92 _this.setState(nextState);
93 });
94
95 if (process.env.NODE_ENV === 'development') {
96 window.postMessage({
97 payload: { action: 'REFRESH' },
98 source: 'alt-hook'
99 }, '*');
100 }
101 }
102
103 _createClass(ImmutableStore, [{
104 key: 'init',
105 value: function init(data) {
106 this.initialData = data;
107 this.state = data.toJS ? data : _immutable2.default.fromJS(data);
108 return this.state;
109 }
110 }, {
111 key: 'change',
112 value: function change(prop, value) {
113 if (arguments.length === 2 && typeof prop !== 'string') {
114 this.changeItem(prop, value);
115 } else if (arguments.length === 2) {
116 this.setState(this.state.set(prop, value.toJS ? value : _immutable2.default.fromJS(value)));
117 } else {
118 this.setState(this.state.merge(prop.toJS ? prop : _immutable2.default.fromJS(prop)));
119 }
120 }
121 }, {
122 key: 'setItemProp',
123 value: function setItemProp(id, key, value, listName) {
124 return this.state.merge(_defineProperty({}, listName || this.listName, this.state.get(listName || this.listName).map(function (item) {
125 if (item.get('id') === id) {
126 return item.set(key, value);
127 }
128 return item;
129 })));
130 }
131 }, {
132 key: 'changeItem',
133 value: function changeItem(filter, data) {
134 var listName = filter.list || this.listName;
135 var list = this.state.get(listName);
136
137 var matchingItem = this.find(listName, filter.item);
138 if (matchingItem) {
139 var matchingIndex = list.indexOf(matchingItem);
140 var newItem = matchingItem.merge(data);
141 var newList = list.set(matchingIndex, newItem);
142
143 this.change(_defineProperty({}, listName, newList));
144 } else {
145 throw new Error('changeItem: no item found for filter: ' + JSON.stringify(filter) + ', list:', JSON.stringify(list.toJS()));
146 }
147 }
148 }, {
149 key: 'useApi',
150 value: function useApi() {
151 return this.getInstance();
152 }
153 }]);
154
155 return ImmutableStore;
156}();
157
158exports.default = ImmutableStore;
\No newline at end of file