@xailabs/altx
Version:
Flux flavor based on alt.js
192 lines (165 loc) • 7.63 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
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; }; }();
exports.resetStores = resetStores;
var _immutable = require('immutable');
var _immutable2 = _interopRequireDefault(_immutable);
var _transmitter = require('transmitter');
var _transmitter2 = _interopRequireDefault(_transmitter);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _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; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var reset = (0, _transmitter2.default)();
function resetStores() {
reset.publish();
}
var ImmutableStore = function () {
function ImmutableStore(initialData) {
var _this = this;
_classCallCheck(this, ImmutableStore);
this.reset = function () {
_this.change(_this.initialData);
};
this.getItemById = function (id, listName) {
var list = _this.state.get(listName || _this.listName);
if (list) {
return list.find(function (item) {
return item.get('id') === id;
});
} else {
console.warn(_this, 'Failed in getItemById(): list not found');
//debugger; //eslint-disable-line
}
return null;
};
this.getIndexById = function (id, listName) {
listName = listName || _this.listName;
var list = _this.state.get(listName);
if (list) {
return list.indexOf(_this.getItemById(id, listName));
}
console.warn(_this, 'Failed in getIndexById(): list not found');
return -1;
};
this.find = function (listName, filter) {
var list = _this.state.get(listName);
var result = list.find(function (item) {
var match = true;
Object.keys(filter).map(function (prop) {
var itemValue = item.get(prop);
var filterValue = filter[prop];
if (itemValue !== filterValue) {
match = false;
}
});
return match;
});
return result;
};
this.listName = 'items';
this.exportPublicMethods({
getItemById: this.getItemById,
getIndexById: this.getIndexById,
find: this.find,
reset: this.reset
});
if (initialData) {
this.init(initialData);
}
reset.subscribe(function () {
var data = _this.initialData || {};
var nextState = data.toJS ? data : _immutable2.default.fromJS(data);
_this.setState(nextState);
});
}
_createClass(ImmutableStore, [{
key: 'init',
value: function init(data) {
this.initialData = data;
this.state = data.toJS ? data : _immutable2.default.fromJS(data);
return this.state;
}
}, {
key: 'change',
/**
* Changes a property to a new value.
* There are three modes of calling this function:
*
* __change('key', value)__
* changes one property of the state object.
* @see https://facebook.github.io/immutable-js/docs/#/Map/set
*
* __change({key: value, ...})__
* changes any number of state properties at once
* @see https://facebook.github.io/immutable-js/docs/#/Map/mergeDeep
*
* __change({id:5}, {key: value})__
* changes any number of properties on any matching item within a list
* @see {@link #changeItem()}
* @see https://facebook.github.io/immutable-js/docs/#/Map/mergeDeep
*/
value: function change(prop, value) {
if (arguments.length === 2 && typeof prop !== 'string') {
this.changeItem(prop, value);
} else if (arguments.length === 2) {
this.setState(this.state.set(prop, value.toJS ? value : _immutable2.default.fromJS(value)));
} else {
this.setState(this.state.merge(prop.toJS ? prop : _immutable2.default.fromJS(prop)));
}
}
}, {
key: 'setItemProp',
value: function setItemProp(id, key, value, listName) {
return this.state.merge(_defineProperty({}, listName || this.listName, this.state.get(listName || this.listName).map(function (item) {
if (item.get('id') === id) {
return item.set(key, value);
}
return item;
})));
}
/**
* Changes a specific item in a list within the state object.
* @throws Error if when no item could be found
* @param {object} filter
* @param {object} filter.item - An object that specifies key/value pairs that must be matched by list items, e.g. {id: 13}
* @param {string} [filter.list] - The name of the list to search in. Defaults to 'items'
* @param {object} data - An object containing key/value pairs for data to be set at matched items
*/
}, {
key: 'changeItem',
value: function changeItem(filter, data) {
//console.log('changeItem', filter, data);
var listName = filter.list || this.listName;
var list = this.state.get(listName);
//console.log('list found', list.toJS());
var matchingItem = this.find(listName, filter.item);
if (matchingItem) {
//console.log('item found', matchingItem);
var matchingIndex = list.indexOf(matchingItem);
var newItem = matchingItem.merge(data);
var newList = list.set(matchingIndex, newItem);
//console.log('new list', newList.toJS());
this.change(_defineProperty({}, listName, newList));
} else {
throw new Error('changeItem: no item found for filter: ' + JSON.stringify(filter) + ', list:', JSON.stringify(list.toJS()));
}
}
/**
* Returns a list of items that match a filter.
*
* @param {string} listName - The name of the list within the state object.
* @param {object} filter - An object with key/value pairs to test items against
* @returns {List} A new immutable list that only contains items that matched all key/value pairs of `filter`
*/
}, {
key: 'useApi',
value: function useApi() {
return this.getInstance();
}
}]);
return ImmutableStore;
}();
exports.default = ImmutableStore;