a5f684470d28149bc1ba10830e672dd253e79d15f582522e576e2ba032151c1f712bf3728ec9b7e226abea44da83e76d98f753a32ca64ba36b34c61de89944 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import arrayMapper from './../../mixins/arrayMapper';
  2. import {arrayFilter} from './../../helpers/array';
  3. import {mixin} from './../../helpers/object';
  4. import {rangeEach} from './../../helpers/number';
  5. /**
  6. * @class RowsMapper
  7. * @plugin ManualRowMove
  8. */
  9. class RowsMapper {
  10. constructor(manualRowMove) {
  11. /**
  12. * Instance of ManualRowMove plugin.
  13. *
  14. * @type {ManualRowMove}
  15. */
  16. this.manualRowMove = manualRowMove;
  17. }
  18. /**
  19. * Reset current map array and create new one.
  20. *
  21. * @param {Number} [length] Custom generated map length.
  22. */
  23. createMap(length) {
  24. let originLength = length === void 0 ? this._arrayMap.length : length;
  25. this._arrayMap.length = 0;
  26. rangeEach(originLength - 1, (itemIndex) => {
  27. this._arrayMap[itemIndex] = itemIndex;
  28. });
  29. }
  30. /**
  31. * Destroy class.
  32. */
  33. destroy() {
  34. this._arrayMap = null;
  35. }
  36. /**
  37. * Moving elements in rowsMapper.
  38. *
  39. * @param {Number} from Row index to move.
  40. * @param {Number} to Target index.
  41. */
  42. moveRow(from, to) {
  43. let indexToMove = this._arrayMap[from];
  44. this._arrayMap[from] = null;
  45. this._arrayMap.splice(to, 0, indexToMove);
  46. }
  47. /**
  48. * Clearing arrayMap from `null` entries.
  49. */
  50. clearNull() {
  51. this._arrayMap = arrayFilter(this._arrayMap, (i) => i !== null);
  52. }
  53. }
  54. mixin(RowsMapper, arrayMapper);
  55. export default RowsMapper;