1 var ReactiveData =
function(table){
6 this.currentVersion = 0;
9 ReactiveData.prototype.watchData =
function(data){
13 this.currentVersion ++;
15 version = this.currentVersion;
22 self.origFuncs.push = data.push;
24 Object.defineProperty(
self.data,
"push", {
28 var args = Array.from(arguments);
30 if(!
self.blocked && version ===
self.currentVersion){
31 args.forEach(
function (arg){
32 self.table.rowManager.addRowActual(arg,
false);
36 return self.origFuncs.push.apply(data, arguments);
41 self.origFuncs.unshift = data.unshift;
43 Object.defineProperty(
self.data,
"unshift", {
47 var args = Array.from(arguments);
49 if(!
self.blocked && version ===
self.currentVersion){
50 args.forEach(
function (arg){
51 self.table.rowManager.addRowActual(arg,
true);
55 return self.origFuncs.unshift.apply(data, arguments);
61 self.origFuncs.shift = data.shift;
63 Object.defineProperty(
self.data,
"shift", {
69 if(!
self.blocked && version ===
self.currentVersion){
71 row =
self.table.rowManager.getRowFromDataObject(
self.data[0]);
79 return self.origFuncs.shift.call(data);
84 self.origFuncs.pop = data.pop;
86 Object.defineProperty(
self.data,
"pop", {
91 if(!
self.blocked && version ===
self.currentVersion){
93 row =
self.table.rowManager.getRowFromDataObject(
self.data[
self.data.length - 1]);
100 return self.origFuncs.pop.call(data);
106 self.origFuncs.splice = data.splice;
108 Object.defineProperty(
self.data,
"splice", {
112 var args = Array.from(arguments),
113 start = args[0] < 0 ? data.length + args[0] : args[0],
115 newRows = args[2] ? args.slice(2) :
false,
118 if(!
self.blocked && version ===
self.currentVersion){
122 startRow = data[start] ?
self.table.rowManager.getRowFromDataObject(data[start]) :
false;
125 newRows.forEach(
function(rowData){
126 self.table.rowManager.addRowActual(rowData,
true, startRow,
true);
129 newRows = newRows.slice().reverse();
131 newRows.forEach(
function(rowData){
132 self.table.rowManager.addRowActual(rowData,
true,
false,
true);
139 var oldRows = data.slice(start, typeof args[1] ===
"undefined" ? args[1] : start + end);
141 oldRows.forEach(
function(rowData, i){
142 var row =
self.table.rowManager.getRowFromDataObject(rowData);
145 row.deleteActual(i !== oldRows.length - 1);
150 if(newRows || end !== 0){
151 self.table.rowManager.reRenderInPosition();
155 return self.origFuncs.splice.apply(data, arguments);
160 ReactiveData.prototype.unwatchData =
function(){
161 if(this.data !==
false){
162 for(var key in this.origFuncs){
163 Object.defineProperty(this.data, key, {
167 value: this.origFuncs.key,
173 ReactiveData.prototype.watchRow =
function(row){
175 data = row.getData();
179 for(var key in data){
180 this.watchKey(row, data, key);
183 this.blocked =
false;
186 ReactiveData.prototype.watchKey =
function(row, data, key){
188 props = Object.getOwnPropertyDescriptor(data, key),
190 version = this.currentVersion;
192 Object.defineProperty(data, key, {
193 set:
function(newValue){
195 if(!
self.blocked && version ===
self.currentVersion){
197 update[key] = newValue;
198 row.updateData(update);
216 ReactiveData.prototype.unwatchRow =
function(row){
217 var data = row.getData();
219 for(var key in data){
220 Object.defineProperty(data, key, {
226 ReactiveData.prototype.block =
function(){
230 ReactiveData.prototype.unblock =
function(){
231 this.blocked =
false;
234 Tabulator.prototype.registerModule(
"reactiveData", ReactiveData);