-
Notifications
You must be signed in to change notification settings - Fork 0
/
enableExcelFnToUiTable.js
301 lines (259 loc) · 12.4 KB
/
enableExcelFnToUiTable.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
function enableExcelFnToUiTable() {
pageMaster.addEventDelegate({
onAfterRendering: function () {
const tableColumns = tableExcel.getColumns();
let objTableData = {};
let arrayTableData = [];
// reset table data
modeltableExcel.setData([]);
// arrange some example data
let peoplesArr = ["Fabrizio Pace", "User Example", "Another People", "Other One", "Hello World"];
for (var a = 0; a < 5; a++) {
objTableData.id = a;
objTableData.firstName = peoplesArr[a].split(" ")[0];
objTableData.lastName = peoplesArr[a].split(" ")[1];
objTableData.country = "Italy";
objTableData.interests = "Neptune Software, SAPUI5..";
arrayTableData.push({ ...objTableData });
}
// bind rows and set model data
tableExcel.bindRows("/");
modeltableExcel.setData(arrayTableData);
const tableExcelObj = document.getElementById(tableExcel.sId);
var startInputSel = null;
var isMouseUp = true;
var lastInputSel = null;
if (tableExcelObj) {
// onkeydown event for arrow keys / enter navigation
tableExcelObj.onkeydown = function (e) {
// close popover
popoverBulkEdit.close();
// on key down check if the focused element is a input
const currentTarget = e.target;
// get table rows
const tableRows = tableExcel.getRows();
if (currentTarget) {
if (currentTarget.classList) {
if (currentTarget.classList.contains("sapMInputBaseInner")) {
if (e.key === "ArrowLeft" || e.key === "ArrowUp" || e.key === "ArrowRight" || e.key === "ArrowDown" || e.key === "Enter") {
// The closest() method of the Element interface traverses the element and its parents (heading toward the document root)
// until it finds a node that matches the specified CSS selector.
// Ref. https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
// navigation arrows between inputs
const targetClosestTableCell = currentTarget.closest(".sapUiTableCell");
let currentRowIndex = 0;
let currentColIndex = 0;
if (targetClosestTableCell.id) {
// based on the cell id get current row and column indexes
// example table-rows-row1-col1-fixed
$.each(targetClosestTableCell.id.split("-"), (i, idEl) => {
if (idEl.toString().substr(0, 4) === "rows") {
return true;
}
if (idEl.toString().substr(0, 3) === "row") {
currentRowIndex = idEl.replace("row", "");
}
if (idEl.toString().substr(0, 3) === "col") {
currentColIndex = idEl.replace("col", "");
}
});
currentRowIndex = parseInt(currentRowIndex) ? parseInt(currentRowIndex) : 0;
currentColIndex = parseInt(currentColIndex) ? parseInt(currentColIndex) : 0;
}
let newRowIndex = 0;
let newColIndex = 0;
// calculate the new index based on the current row / cell index selected
// key arrow left
if (e.key === "ArrowLeft") {
newRowIndex = currentRowIndex;
newColIndex = currentColIndex - 1;
}
// key arrow up
if (e.key === "ArrowUp") {
newRowIndex = currentRowIndex > 0 ? currentRowIndex - 1 : currentRowIndex;
newColIndex = currentColIndex;
}
// key arrow right
if (e.key === "ArrowRight") {
newRowIndex = currentRowIndex;
newColIndex = currentColIndex < tableColumns.length ? currentColIndex + 1 : currentColIndex;
}
// key arrow down / key enter
if (e.key === "ArrowDown" || e.key === "Enter") {
newRowIndex = currentRowIndex + 1;
newColIndex = currentColIndex;
}
if (newRowIndex !== -1 && newColIndex !== -1 && tableRows[newRowIndex]) {
const newTableRowToFocus = tableRows[newRowIndex];
if (newTableRowToFocus) {
const newTableCellToFocus = newTableRowToFocus.getCells()[newColIndex];
if (newTableCellToFocus) {
let newInputToFocusHtml = $(`#${newTableCellToFocus.sId}`);
let newInputToFocus = sap.ui.getCore().byId(newTableCellToFocus.sId);
if (newInputToFocus && newInputToFocus.getEnabled()) {
// reset old selections
$(".customFocusExcelStyle").removeClass("customFocusExcelStyle");
// focus new input
newInputToFocusHtml.addClass("customFocusExcelStyle");
newInputToFocus.focus();
}
}
}
}
} else {
return;
}
}
}
}
};
// onmousedown event for excel-like cells selection
tableExcelObj.onmousedown = function (e) {
if (e.button !== 0) {
// if the mouse button pressed is not the left exit
return;
}
// called when the drag start
const currentTarget = e.target;
startInputSel = null;
isMouseUp = false;
// reset old selections if the ctrl button is not pressed
if (!e.ctrlKey) {
$(".customFocusExcelStyle").removeClass("customFocusExcelStyle");
}
// check if the element is a input
if (currentTarget) {
if (currentTarget.classList) {
if (currentTarget.classList.contains("sapMInputBaseInner")) {
startInputSel = currentTarget;
// select first cell
let newTableCellToFocus = startInputSel.closest(".sapUiTableCell");
if (newTableCellToFocus) {
let newInputToFocusHtml = $(`#${newTableCellToFocus.id}`);
if (newInputToFocusHtml) {
newInputToFocusHtml.addClass("customFocusExcelStyle");
// if ctrl key is pressed
// and we have more than 1 cell selected
// show the popover for bulk edit
let numInputFocused = document.getElementsByClassName("customFocusExcelStyle");
let htmlInputFocused = startInputSel.closest(".sapMInputBase");
if (htmlInputFocused) {
htmlInputFocused.classList.add("customFocusExcelStyle");
}
if (e.ctrlKey && numInputFocused.length > 1) {
setTimeout(function () {
let lastInputFocused = sap.ui.getCore().byId(htmlInputFocused.id);
popoverBulkEdit.openBy(lastInputFocused);
}, 100);
}
}
}
}
}
}
};
// onmousemove event for excel-like cells selection
tableExcelObj.onmousemove = function (e) {
// when the mouse is dragged over the cells
// this code will select all cells (for bulk edit etc)
const currentTarget = e.target;
if (isMouseUp) {
return;
}
// get table rows
const tableRows = tableExcel.getRows();
if (currentTarget) {
if (currentTarget.classList) {
if (currentTarget.classList.contains("sapMInputBaseInner") && startInputSel) {
const startTargetClosestTableCell = startInputSel.closest(".sapUiTableCell");
const endTargetClosestTableCell = currentTarget.closest(".sapUiTableCell");
let fromRowIndex = 0;
let toRowIndex = 0;
let fromColIndex = 0;
let toColIndex = 0;
let newTableRowToFocus = null;
if (startTargetClosestTableCell.id) {
// based on the cell id get current row and column indexes
// example table-rows-row1-col1-fixed
$.each(startTargetClosestTableCell.id.split("-"), (i, idEl) => {
if (idEl.toString().substr(0, 4) === "rows") {
return true;
}
if (idEl.toString().substr(0, 3) === "row") {
fromRowIndex = idEl.replace("row", "");
}
if (idEl.toString().substr(0, 3) === "col") {
fromColIndex = idEl.replace("col", "");
}
});
fromRowIndex = parseInt(fromRowIndex) ? parseInt(fromRowIndex) : 0;
fromColIndex = parseInt(fromColIndex) ? parseInt(fromColIndex) : 0;
}
if (endTargetClosestTableCell.id) {
// based on the cell id get current row and column indexes
// example table-rows-row1-col1-fixed
$.each(endTargetClosestTableCell.id.split("-"), (i, idEl) => {
if (idEl.toString().substr(0, 4) === "rows") {
return true;
}
if (idEl.toString().substr(0, 3) === "row") {
toRowIndex = idEl.replace("row", "");
}
if (idEl.toString().substr(0, 3) === "col") {
toColIndex = idEl.replace("col", "");
}
});
toRowIndex = parseInt(toRowIndex) ? parseInt(toRowIndex) : 0;
toColIndex = parseInt(toColIndex) ? parseInt(toColIndex) : 0;
}
if (fromRowIndex !== -1 && toRowIndex !== -1 && fromColIndex !== -1 && toColIndex !== -1) {
let startRowIndex = fromRowIndex > toRowIndex ? toRowIndex : fromRowIndex;
let endRowIndex = toRowIndex > fromRowIndex ? toRowIndex : fromRowIndex;
let startColIndex = fromColIndex > toColIndex ? toColIndex : fromColIndex;
let endColIndex = toColIndex > fromColIndex ? toColIndex : fromColIndex;
let newInputToFocus = null;
let counterCells = 0;
// loop each cell in each row for the massive selection
for (var a = startRowIndex; a <= endRowIndex; a++) {
// rows
newTableRowToFocus = tableRows[a];
if (newTableRowToFocus) {
for (var b = startColIndex; b <= endColIndex; b++) {
counterCells++;
// columns
let newTableCellToFocus = newTableRowToFocus.getCells()[b];
if (newTableCellToFocus) {
let newInputToFocusHtml = $(`#${newTableCellToFocus.sId}`);
newInputToFocus = sap.ui.getCore().byId(newTableCellToFocus.sId);
if (newInputToFocusHtml) {
newInputToFocusHtml.addClass("customFocusExcelStyle");
}
}
if (b === endColIndex && a === endRowIndex && newInputToFocus && counterCells > 1) {
// store the last cell of last row selected to a variable
lastInputSel = newInputToFocus;
}
}
}
}
}
}
}
}
};
// onmouseup event for excel-like cells selection
document.onmouseup = function (e) {
// called when the drag ends
// reset variable
isMouseUp = true;
if (lastInputSel) {
// on last cell of last row selected open the popover
popoverBulkEdit.openBy(lastInputSel);
// reset variable
lastInputSel = null;
}
};
}
},
});
}