This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
rowEditorMixin.js
90 lines (88 loc) · 3.34 KB
/
rowEditorMixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*jslint browser: true, plusplus: true, unparam: true, vars: true */
"use strict";
/**
* Row Editor
*/
var rowEditorMixin = {
editChanged: false,
editRow: null,
updateEditChanged: function () {
if (this.undoStack.length > 1) {
this.editChanged = true;
} else if (this.undoStack.length === 1) {
this.editChanged = this.rowHasBeenEdited(this.undoStack[0]);
} else {
this.editChanged = false;
}
return this.editChanged;
},
rowHasBeenEdited: function (undoElement) {
return Object.getOwnPropertyNames(undoElement.original).some(function (val) {
return undoElement.ref[val] !== undoElement.original[val] && undoElement.original[val] !== null;
});
},
rowEdit: function (e) {
if (this.rowEditorTemplate && (this.multiRowEdit || this.undoStack.length === 0)) {
var model = e.target.templateInstance.model;
var row = model.row;
if (model.record) { row = model.record.row; }
if (this.undoStack.length > 0 && !this.rowHasBeenEdited(this.undoStack[this.undoStack.length - 1])) { this.undoStack.pop(); }
if (this.undoStack.length === 0) {
this.editChanged = false;
this.undoStack.push({
ref: row,
original: JSON.parse(JSON.stringify(row))
});
} else {
this.editChanged = true;
if (this.undoStack[this.undoStack.length - 1].ref !== row) {
this.undoStack.push({
ref: row,
original: JSON.parse(JSON.stringify(row))
});
}
}
this.editRow = row;
}
},
closeEdit: function () {
this.editChanged = false;
this.undoStack = [];
this.editRow = null;
},
undoAllEdit: function (e) {
while (this.undoStack.length > 0) { this.undoEdit(this, e); }
},
undoEdit: function () {
if (this.undoStack.length > 0) {
var first = this.undoStack.pop();
Object.getOwnPropertyNames(first.original).forEach(function (val) {
if (first.ref[val] !== first.original[val] && first.original[val] !== null) {
first.ref[val] = first.original[val];
}
});
if (this.undoStack.length > 0) {
this.editRow = this.undoStack[this.undoStack.length - 1].ref;
} else {
this.editRow = null;
}
this.refreshFooter();
}
},
cancelEdit: function (e) {
this.undoAllEdit(this, e);
this.closeEdit(this, e);
},
/**
* Row Editor template helper functions
*/
moveToPage: function (e) { this.page = Number(e.currentTarget.textContent); },
moveToPreviousPage: function () { if (this.page > 1) { this.page--; } },
moveToNextPage: function () { if (this.dataSize > this.page * this.pageSize) { this.page++; } },
moveToFirstPage: function () { this.page = 1; },
moveToLastPage: function () { this.page = this.lastPage; },
showMore: function () {
if (!this.originalPageSize) { this.originalPageSize = this.pageSize; }
this.pageSize = Math.min(this.dataSize, this.pageSize + this.originalPageSize);
}
};