HistoryLayout.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. var __assign = (this && this.__assign) || Object.assign || function(t) {
  12. for (var s, i = 1, n = arguments.length; i < n; i++) {
  13. s = arguments[i];
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  15. t[p] = s[p];
  16. }
  17. return t;
  18. };
  19. import * as React from 'react';
  20. import { Dragact } from '../../src/lib/dragact';
  21. var HistoryDragact = /** @class */ (function (_super) {
  22. __extends(HistoryDragact, _super);
  23. function HistoryDragact(props) {
  24. var _this = _super.call(this, props) || this;
  25. _this._actionsHistory = [];
  26. _this._cacheCurrentLayoutStart = function (layoutItem) {
  27. _this._activeItem = layoutItem;
  28. if (!_this._dragact) {
  29. return;
  30. }
  31. _this._cachingLayouts(_this._dragact);
  32. };
  33. _this._cacheCurrentLayoutEnd = function (layoutItem) {
  34. var _a = _this._activeItem, GridY = _a.GridY, GridX = _a.GridX, h = _a.h, w = _a.w;
  35. if (GridX === layoutItem.GridX && GridY === layoutItem.GridY && h === layoutItem.h && w === layoutItem.w) {
  36. return;
  37. }
  38. _this._storeLayoutToHistory(_this._cacheLayouts);
  39. };
  40. _this._cachingLayouts = function (d) {
  41. var initiateSnapShot = JSON.stringify({
  42. layout: d.getLayout(),
  43. });
  44. return _this._cacheLayouts = initiateSnapShot;
  45. };
  46. _this.goBack = function () {
  47. var mapLayoutHistory = _this._actionsHistory;
  48. if (mapLayoutHistory.length > 1) {
  49. var last = mapLayoutHistory.pop();
  50. if (!last) {
  51. return;
  52. }
  53. _this._changeDragactLayouts(last);
  54. }
  55. };
  56. _this.reset = function () {
  57. if (!_this._dragact) {
  58. return;
  59. }
  60. _this._cachingLayouts(_this._dragact);
  61. _this._storeLayoutToHistory(_this._cacheLayouts);
  62. var initiateSnapShot = _this._actionsHistory[0];
  63. _this._changeDragactLayouts(initiateSnapShot);
  64. };
  65. _this.clear = function () {
  66. _this._actionsHistory = _this._actionsHistory.slice(0, 1);
  67. _this._changeDragactLayouts(_this._actionsHistory[0]);
  68. };
  69. _this.onDragStart = function (event) {
  70. _this._cacheCurrentLayoutStart(event);
  71. _this.props.onDragStart && _this.props.onDragStart(event);
  72. };
  73. _this.onDragEnd = function (event) {
  74. _this._cacheCurrentLayoutEnd(event);
  75. _this.props.onDragEnd && _this.props.onDragEnd(event);
  76. };
  77. _this._changeDragactLayouts = function (snapshot) {
  78. if (!_this._dragact) {
  79. return;
  80. }
  81. try {
  82. var layout = JSON.parse(snapshot).layout;
  83. _this.setState({
  84. layout: layout
  85. });
  86. }
  87. catch (e) {
  88. }
  89. };
  90. _this._storeLayoutToHistory = function (layouts) {
  91. _this._actionsHistory.push(layouts);
  92. };
  93. _this._dragactRefCallback = function (d) {
  94. _this._dragact = d;
  95. };
  96. _this.state = { layout: props.layout };
  97. return _this;
  98. }
  99. HistoryDragact.prototype.componentDidMount = function () {
  100. if (this._dragact) {
  101. var initiateSnapShot = this._cachingLayouts(this._dragact);
  102. this._storeLayoutToHistory(initiateSnapShot);
  103. }
  104. };
  105. HistoryDragact.prototype.componentWillReceiveProps = function (nextProps) {
  106. this.setState({
  107. layout: nextProps.layout
  108. });
  109. };
  110. Object.defineProperty(HistoryDragact.prototype, "getDragact", {
  111. get: function () {
  112. return this._dragact;
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. HistoryDragact.prototype.render = function () {
  118. var layout = this.state.layout;
  119. return React.createElement(Dragact, __assign({ ref: this._dragactRefCallback }, this.props, { layout: layout, onDragStart: this.onDragStart, onDragEnd: this.onDragEnd }));
  120. };
  121. return HistoryDragact;
  122. }(React.Component));
  123. export { HistoryDragact };