9fd040c982670c592b60aa8ed47dd6ee99429981d65914ac968928eb04485b02f07e803f4e722b97f5d6d5ff888919dc07238ecf4de26745f883236417be1a 1.4 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 ColumnsMapper
  7. * @plugin ManualColumnMove
  8. */
  9. class ColumnsMapper {
  10. constructor(manualColumnMove) {
  11. /**
  12. * Instance of ManualColumnMove plugin.
  13. *
  14. * @type {ManualColumnMove}
  15. */
  16. this.manualColumnMove = manualColumnMove;
  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 columnsMapper.
  38. *
  39. * @param {Number} from Column index to move.
  40. * @param {Number} to Target index.
  41. */
  42. moveColumn(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(ColumnsMapper, arrayMapper);
  55. export default ColumnsMapper;