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