3 var ReactiveData =
function ReactiveData(table) {
8 this.currentVersion = 0;
11 ReactiveData.prototype.watchData =
function (data) {
16 this.currentVersion++;
18 version = this.currentVersion;
25 self.origFuncs.push = data.push;
27 Object.defineProperty(
self.data,
"push", {
30 value:
function value() {
31 var args = Array.from(arguments);
33 if (!
self.blocked && version ===
self.currentVersion) {
34 args.forEach(
function (arg) {
35 self.table.rowManager.addRowActual(arg,
false);
39 return self.origFuncs.push.apply(data, arguments);
44 self.origFuncs.unshift = data.unshift;
46 Object.defineProperty(
self.data,
"unshift", {
49 value:
function value() {
50 var args = Array.from(arguments);
52 if (!
self.blocked && version ===
self.currentVersion) {
53 args.forEach(
function (arg) {
54 self.table.rowManager.addRowActual(arg,
true);
58 return self.origFuncs.unshift.apply(data, arguments);
63 self.origFuncs.shift = data.shift;
65 Object.defineProperty(
self.data,
"shift", {
68 value:
function value() {
71 if (!
self.blocked && version ===
self.currentVersion) {
72 if (
self.data.length) {
73 row =
self.table.rowManager.getRowFromDataObject(
self.data[0]);
81 return self.origFuncs.shift.call(data);
86 self.origFuncs.pop = data.pop;
88 Object.defineProperty(
self.data,
"pop", {
91 value:
function value() {
93 if (!
self.blocked && version ===
self.currentVersion) {
94 if (
self.data.length) {
95 row =
self.table.rowManager.getRowFromDataObject(
self.data[
self.data.length - 1]);
102 return self.origFuncs.pop.call(data);
107 self.origFuncs.splice = data.splice;
109 Object.defineProperty(
self.data,
"splice", {
112 value:
function value() {
113 var args = Array.from(arguments),
114 start = args[0] < 0 ? data.length + args[0] : args[0],
116 newRows = args[2] ? args.slice(2) :
false,
119 if (!
self.blocked && version ===
self.currentVersion) {
123 startRow = data[start] ?
self.table.rowManager.getRowFromDataObject(data[start]) :
false;
126 newRows.forEach(
function (rowData) {
127 self.table.rowManager.addRowActual(rowData,
true, startRow,
true);
130 newRows = newRows.slice().reverse();
132 newRows.forEach(
function (rowData) {
133 self.table.rowManager.addRowActual(rowData,
true,
false,
true);
140 var oldRows = data.slice(start, typeof args[1] ===
"undefined" ? args[1] : start + end);
142 oldRows.forEach(
function (rowData, i) {
143 var row =
self.table.rowManager.getRowFromDataObject(rowData);
146 row.deleteActual(i !== oldRows.length - 1);
151 if (newRows || end !== 0) {
152 self.table.rowManager.reRenderInPosition();
156 return self.origFuncs.splice.apply(data, arguments);
161 ReactiveData.prototype.unwatchData =
function () {
162 if (this.data !==
false) {
163 for (var key in this.origFuncs) {
164 Object.defineProperty(this.data, key, {
168 value: this.origFuncs.key
174 ReactiveData.prototype.watchRow =
function (row) {
176 data = row.getData();
180 for (var key in data) {
181 this.watchKey(row, data, key);
184 this.blocked =
false;
187 ReactiveData.prototype.watchKey =
function (row, data, key) {
189 props = Object.getOwnPropertyDescriptor(data, key),
191 version = this.currentVersion;
193 Object.defineProperty(data, key, {
194 set:
function set(newValue) {
196 if (!
self.blocked && version ===
self.currentVersion) {
198 update[key] = newValue;
199 row.updateData(update);
206 get:
function get() {
217 ReactiveData.prototype.unwatchRow =
function (row) {
218 var data = row.getData();
220 for (var key in data) {
221 Object.defineProperty(data, key, {
227 ReactiveData.prototype.block =
function () {
231 ReactiveData.prototype.unblock =
function () {
232 this.blocked =
false;
235 Tabulator.prototype.registerModule(
"reactiveData", ReactiveData);