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