HistoryLayout.js 4.8 KB

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