Debian Codegraph @

src/main/org/h2/server/web/res/table.js

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * * Initial Developer: H2 Group
5 */
6
7 addEvent(window, "load", initSort);
8
9 function addEvent(elm, evType, fn, useCapture) {
10 // addEvent and removeEvent
11 // cross-browser event handling for IE5+, NS6 and Mozilla
12 // By Scott Andrew
13 if (elm.addEventListener){
14 elm.addEventListener(evType, fn, useCapture);
15 return true;
16 } else if (elm.attachEvent){
17 var r = elm.attachEvent("on"+evType, fn);
18 return r;
19 } else {
20 alert("Handler could not be added");
21 }
22 }
23
24 function initSort() {
25 if (document.getElementById('editing') != undefined) {
26 // don't allow sorting while editing
27 return;
28 }
29 var tables = document.getElementsByTagName("table");
30 for (var i=0; i<tables.length; i++) {
31 table = tables[i];
32 if (table.rows && table.rows.length > 0) {
33 var header = table.rows[0];
34 for(var j=0;j<header.cells.length;j++) {
35 var cell = header.cells[j];
36 var text = cell.innerHTML;
37 cell.innerHTML = '<a href="#" style="text-decoration: none;" class="sortHeader" onclick="resortTable(this);">'+text+'<span class="sortArrow">&nbsp;&nbsp;</span></a>';
38 }
39 }
40 }
41 }
42
43 function editRow(row, session, write, undo) {
44 var table = document.getElementById('editTable');
45 var y = row < 0 ? table.rows.length - 1 : row;
46 var i;
47 for(i=1; i<table.rows.length; i++) {
48 var cell = table.rows[i].cells[0];
49 if (i == y) {
50 var edit = '<img width=16 height=16 src="ico_ok.gif" onclick="editOk('+row+')" onmouseover = "this.className =\'icon_hover\'" onmouseout = "this.className=\'icon\'" class="icon" alt="'+write+'" title="'+write+'" border="1"/>';
51 var undo = '<img width=16 height=16 src="ico_undo.gif" onclick="editCancel('+row+')" onmouseover = "this.className =\'icon_hover\'" onmouseout = "this.className=\'icon\'" class="icon" alt="'+undo+'" title="'+undo+'" border="1"/>';
52 cell.innerHTML = edit + undo;
53 } else {
54 cell.innerHTML = '';
55 }
56 }
57 var cells = table.rows[y].cells;
58 for (i=1; i<cells.length; i++) {
59 var cell = cells[i];
60 var text = getInnerText(cell);
61 // escape so we can edit data that contains HTML special characters
62 // '&' needs to be replaced first
63 text = text.replace(/&/g, '&amp;').
64 replace(/'/g, '&apos;').
65 replace(/"/g, '&quot;').
66 replace(/</g, '&lt;').
67 replace(/>/g, '&gt;');
68 var size;
69 var newHTML;
70 if (text.indexOf('\n') >= 0) {
71 size = 40;
72 newHTML = '<textarea name="$rowName" cols="$size" onkeydown="return editKeyDown($row, this, event)">$t</textarea/>';
73 } else {
74 size = text.length+5;
75 newHTML = '<input type="text" name="$rowName" value="$t" size="$size" onkeydown="return editKeyDown($row, this, event)" />';
76 }
77 newHTML = newHTML.replace('$rowName', 'r' + row + 'c' + i);
78 newHTML = newHTML.replace('$row', row);
79 newHTML = newHTML.replace('$t', text);
80 newHTML = newHTML.replace('$size', size);
81 cell.innerHTML = newHTML;
82 }
83 }
84
85 function deleteRow(row, session, write, undo) {
86 var table = document.getElementById('editTable');
87 var y = row < 0 ? table.rows.length - 1 : row;
88 var i;
89 for(i=1; i<table.rows.length; i++) {
90 var cell = table.rows[i].cells[0];
91 if (i == y) {
92 var edit = '<img width=16 height=16 src="ico_remove_ok.gif" onclick="deleteOk('+row+')" onmouseover = "this.className =\'icon_hover\'" onmouseout = "this.className=\'icon\'" class="icon" alt="'+write+'" title="'+write+'" border="1"/>';
93 var undo = '<img width=16 height=16 src="ico_undo.gif" onclick="editCancel('+row+')" onmouseover = "this.className =\'icon_hover\'" onmouseout = "this.className=\'icon\'" class="icon" alt="'+undo+'" title="'+undo+'" border="1"/>';
94 cell.innerHTML = edit + undo;
95 } else {
96 cell.innerHTML = '';
97 }
98 }
99 var cells = table.rows[y].cells;
100 for (i=1; i<cells.length; i++) {
101 var s = cells[i].style;
102 s.color = 'red';
103 s.textDecoration = 'line-through';
104 }
105 }
106
107 function editFinish(row, res) {
108 var editing = document.getElementById('editing');
109 editing.row.value = row;
110 editing.op.value = res;
111 editing.submit();
112 }
113
114 function editCancel(row) {
115 editFinish(row, '3');
116 }
117
118 function editOk(row) {
119 editFinish(row, '1');
120 }
121
122 function deleteOk(row) {
123 editFinish(row, '2');
124 }
125
126 function editKeyDown(row, object, event) {
127 var key=event.keyCode? event.keyCode : event.charCode;
128 if (key == 46 && event.ctrlKey) {
129 // ctrl + delete
130 object.value = 'null';
131 return false;
132 } else if (key == 13) {
133 if (!event.ctrlKey && !event.shiftKey && !event.altKey) {
134 editOk(row);
135 return false;
136 }
137 } else if (key == 27) {
138 editCancel(row);
139 return false;
140 }
141 }
142
143 function getInnerText(el) {
144 if (typeof el == "string") return el;
145 if (typeof el == "undefined") return el;
146 if (el.innerText) {
147 // not needed but it is faster
148 return el.innerText;
149 }
150 var str = "";
151 var cs = el.childNodes;
152 var l = cs.length;
153 for (var i = 0; i < l; i++) {
154 switch (cs[i].nodeType) {
155 case 1: //ELEMENT_NODE
156 str += getInnerText(cs[i]);
157 break;
158 case 3: //TEXT_NODE
159 str += cs[i].nodeValue;
160 break;
161 }
162 }
163 return str;
164 }
165
166 function isNullCell(td) {
167 return td.childNodes.length == 1 && (td.childNodes[0].nodeName == "I");
168 }
169
170 function resortTable(link) {
171 // get the span
172 var span;
173 for (var ci=0;ci<link.childNodes.length;ci++) {
174 if (link.childNodes[ci].tagName && link.childNodes[ci].tagName.toLowerCase() == 'span') {
175 span = link.childNodes[ci];
176 }
177 }
178 var td = link.parentNode;
179 var column = td.cellIndex;
180 var table = getParent(td,'TABLE');
181 var rows = table.rows;
182 if (rows.length <= 1) return;
183
184 // detect sort type
185 var sortNumeric = true;
186 for (i = 1; i < rows.length; i++) {
187 var td = rows[i].cells[column];
188 if (!isNullCell(td)) {
189 var x = getInnerText(td);
190 // H2 does not return numeric values with leading +, but may return
191 // values in scientific notation
192 if (!x.match(/^\-?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)) {
193 sortNumeric = false;
194 break;
195 }
196 }
197 }
198 var newRows = new Array();
199 for (i=1; i<rows.length; i++) {
200 var o = new Object();
201 o.data = rows[i];
202 o.id = i;
203 var td = o.data.cells[column];
204 var n = isNullCell(td);
205 o.isNull = n;
206 if (!n) {
207 var txt = getInnerText(td);
208 if (sortNumeric) {
209 o.sort = parseFloat(txt);
210 if (isNaN(o.sort)) o.sort = 0;
211 } else {
212 o.sort = txt;
213 }
214 }
215 newRows[i-1] = o;
216 }
217 newRows.sort(sortCallback);
218 var arrow;
219 if (span.getAttribute("sortDir") == 'down') {
220 arrow = '&nbsp;<span style="color:gray">&#x25b2;</span>';
221 newRows.reverse();
222 span.setAttribute('sortDir','up');
223 } else {
224 arrow = '&nbsp;<span style="color:gray">&#x25bc;</span>';
225 span.setAttribute('sortDir','down');
226 }
227
228 // we appendChild rows that already exist to the tbody,
229 // so it moves them rather than creating new ones
230 var body = table.tBodies[0];
231 for (i=0; i<newRows.length; i++) {
232 body.appendChild(newRows[i].data);
233 }
234
235 // delete any other arrows there may be showing
236 var allSpans = document.getElementsByTagName("span");
237 for (var i=0;i<allSpans.length;i++) {
238 if (allSpans[i].className == 'sortArrow') {
239 // in the same table as us?
240 if (getParent(allSpans[i],"table") == getParent(link,"table")) {
241 allSpans[i].innerHTML = '&nbsp;&nbsp;';
242 }
243 }
244 }
245 span.innerHTML = arrow;
246 }
247
248 function getParent(el, pTagName) {
249 if (el == null) return null;
250 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) {
251 // Gecko bug, supposed to be uppercase
252 return el;
253 } else {
254 return getParent(el.parentNode, pTagName);
255 }
256 }
257
258 function sortCallback(ra, rb) {
259 if (ra.isNull) {
260 return rb.isNull ? (ra.id - rb.id) : -1;
261 } else if (rb.IsNull) {
262 return 1;
263 }
264 return (ra.sort==rb.sort) ? (ra.id-rb.id) : (ra.sort<rb.sort ? -1 : 1);
265 }