92a010b756f4787de6c99cd90824c99a8b1808f891394b369f70b658be394ca43567a9e4c4c56a76a882df4e74faf74c10976728a074acf84641b0d1c3f0d5 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Store from './index';
  2. import debounce from 'throttle-debounce/debounce';
  3. export function createStore(table, initialState = {}) {
  4. if (!table) {
  5. throw new Error('Table is required.');
  6. }
  7. const store = new Store();
  8. store.table = table;
  9. // fix https://github.com/ElemeFE/element/issues/14075
  10. // related pr https://github.com/ElemeFE/element/pull/14146
  11. store.toggleAllSelection = debounce(10, store._toggleAllSelection);
  12. Object.keys(initialState).forEach(key => {
  13. store.states[key] = initialState[key];
  14. });
  15. return store;
  16. }
  17. export function mapStates(mapper) {
  18. const res = {};
  19. Object.keys(mapper).forEach(key => {
  20. const value = mapper[key];
  21. let fn;
  22. if (typeof value === 'string') {
  23. fn = function() {
  24. return this.store.states[value];
  25. };
  26. } else if (typeof value === 'function') {
  27. fn = function() {
  28. return value.call(this, this.store.states);
  29. };
  30. } else {
  31. console.error('invalid value type');
  32. }
  33. if (fn) {
  34. res[key] = fn;
  35. }
  36. });
  37. return res;
  38. };