Built files from Bizgaze WebServer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

history.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Tabulator v4.8.2 (c) Oliver Folkerd */
  2. var History = function History(table) {
  3. this.table = table; //hold Tabulator object
  4. this.history = [];
  5. this.index = -1;
  6. };
  7. History.prototype.clear = function () {
  8. this.history = [];
  9. this.index = -1;
  10. };
  11. History.prototype.action = function (type, component, data) {
  12. this.history = this.history.slice(0, this.index + 1);
  13. this.history.push({
  14. type: type,
  15. component: component,
  16. data: data
  17. });
  18. this.index++;
  19. };
  20. History.prototype.getHistoryUndoSize = function () {
  21. return this.index + 1;
  22. };
  23. History.prototype.getHistoryRedoSize = function () {
  24. return this.history.length - (this.index + 1);
  25. };
  26. History.prototype.clearComponentHistory = function (component) {
  27. var index = this.history.findIndex(function (item) {
  28. return item.component === component;
  29. });
  30. if (index > -1) {
  31. this.history.splice(index, 1);
  32. if (index <= this.index) {
  33. this.index--;
  34. }
  35. this.clearComponentHistory(component);
  36. }
  37. };
  38. History.prototype.undo = function () {
  39. if (this.index > -1) {
  40. var action = this.history[this.index];
  41. this.undoers[action.type].call(this, action);
  42. this.index--;
  43. this.table.options.historyUndo.call(this.table, action.type, action.component.getComponent(), action.data);
  44. return true;
  45. } else {
  46. console.warn("History Undo Error - No more history to undo");
  47. return false;
  48. }
  49. };
  50. History.prototype.redo = function () {
  51. if (this.history.length - 1 > this.index) {
  52. this.index++;
  53. var action = this.history[this.index];
  54. this.redoers[action.type].call(this, action);
  55. this.table.options.historyRedo.call(this.table, action.type, action.component.getComponent(), action.data);
  56. return true;
  57. } else {
  58. console.warn("History Redo Error - No more history to redo");
  59. return false;
  60. }
  61. };
  62. History.prototype.undoers = {
  63. cellEdit: function cellEdit(action) {
  64. action.component.setValueProcessData(action.data.oldValue);
  65. },
  66. rowAdd: function rowAdd(action) {
  67. action.component.deleteActual();
  68. },
  69. rowDelete: function rowDelete(action) {
  70. var newRow = this.table.rowManager.addRowActual(action.data.data, action.data.pos, action.data.index);
  71. if (this.table.options.groupBy && this.table.modExists("groupRows")) {
  72. this.table.modules.groupRows.updateGroupRows(true);
  73. }
  74. this._rebindRow(action.component, newRow);
  75. },
  76. rowMove: function rowMove(action) {
  77. this.table.rowManager.moveRowActual(action.component, this.table.rowManager.rows[action.data.posFrom], !action.data.after);
  78. this.table.rowManager.redraw();
  79. }
  80. };
  81. History.prototype.redoers = {
  82. cellEdit: function cellEdit(action) {
  83. action.component.setValueProcessData(action.data.newValue);
  84. },
  85. rowAdd: function rowAdd(action) {
  86. var newRow = this.table.rowManager.addRowActual(action.data.data, action.data.pos, action.data.index);
  87. if (this.table.options.groupBy && this.table.modExists("groupRows")) {
  88. this.table.modules.groupRows.updateGroupRows(true);
  89. }
  90. this._rebindRow(action.component, newRow);
  91. },
  92. rowDelete: function rowDelete(action) {
  93. action.component.deleteActual();
  94. },
  95. rowMove: function rowMove(action) {
  96. this.table.rowManager.moveRowActual(action.component, this.table.rowManager.rows[action.data.posTo], action.data.after);
  97. this.table.rowManager.redraw();
  98. }
  99. };
  100. //rebind rows to new element after deletion
  101. History.prototype._rebindRow = function (oldRow, newRow) {
  102. this.history.forEach(function (action) {
  103. if (action.component instanceof Row) {
  104. if (action.component === oldRow) {
  105. action.component = newRow;
  106. }
  107. } else if (action.component instanceof Cell) {
  108. if (action.component.row === oldRow) {
  109. var field = action.component.column.getField();
  110. if (field) {
  111. action.component = newRow.getCell(field);
  112. }
  113. }
  114. }
  115. });
  116. };
  117. Tabulator.prototype.registerModule("history", History);