-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.js
414 lines (377 loc) · 9.25 KB
/
Sort.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
function initTable(obj)
{
// Check whether it's viewed by IE 5.0 or greater
if (! checkBrowser()) return;
// Local variables
var countCol;
var nChildNodes;
var innerMostNode;
var nColSpan, nRowSpannedTitleCol, colPos;
var cell, cellText;
var titleFound = false;
var rNRowSpan, rNColSpan;
// Initializing global table object variable
if (obj.tagName == "TABLE")
{
// Assumes that the obj is THE OBJECT
table = obj;
}
else
{
// Assumes that the obj is the id of the object
table = document.getElementById(obj);
}
// Check whether it's an object
if (table == null) return;
// Check whether it's a table
if (table.tagName != "TABLE") return;
// Initializing the max col number with the size of last data row
maxNCol = table.rows[table.rows.length-1].cells.length;
// Initializing arrays
rowArray = new Array();
colSpanArray = new Array();
colTitleFilled = new Array();
titleRowArray = new Array();
titleRowCellArray = new Array();
for (var i=0; i<maxNCol; i++)
colTitleFilled[i] = false;
// Setting the number of rows
nRow = table.rows.length;
// Should have at least 1 row
if (nRow < 1) return;
// Initialization of local variables
actualNRow = 0; // Number of actual data rows
rNRowSpan = 0; // Remaining rows in the row span
rNColSpan = 0; // Remaining cols in the col span
nRowSpannedTitleCol = 0; // Number of title cols from row span
// Loop through rows
for (var i=0; i<nRow; i++)
{
nColSpan = 1, colPos = 0;
// Loop through columns
// Initializing
for (var j=0; j<table.rows[i].cells.length; j++)
{
// Do this iff title has not been found
if (titleFound == false)
{
if (table.rows[i].cells[j].rowSpan > 1)
{
if (table.rows[i].cells[j].colSpan < 2)
{
titleSpanCellArray[colPos] =
table.rows[i].cells[j];
colTitleFilled[colPos] = true;
nRowSpannedTitleCol++;
}
if (table.rows[i].cells[j].rowSpan - 1
> rNRowSpan)
{
rNRowSpan =
table.
rows[i].cells[j].
rowSpan - 1;
if (table.rows[i].
cells[j].colSpan > 1)
rNColSpan =
rNRowSpan + 1;
}
}
}
if (table.rows[i].cells[j].colSpan > 1 &&
rNColSpan == 0)
{
nColSpan = table.rows[i].cells[j].colSpan;
colPos += nColSpan;
}
else
{
colPos++;
}
}
// Setting up the title cells
if (titleFound == false && nColSpan == 1 &&
rNRowSpan == 0 && rNColSpan == 0 && titleFound == false)
{
colSpanArray[i] = true;
titleFound = true;
// Using indivisual cell as an array element
countCol = 0;
for (var j=0;
j<table.rows[i].cells.length
+ nRowSpannedTitleCol; j++)
{
if (colTitleFilled[j] != true)
{
titleRowCellArray[j] =
table.rows[i].cells[countCol];
countCol++;
}
else
{
titleRowCellArray[j] =
titleSpanCellArray[j];
}
}
}
// Setting up the data rows
else if (titleFound == true && nColSpan == 1 && rNRowSpan == 0)
{
for (var j=0; j<table.rows[i].cells.length; j++)
{
// Can't have row span in record rows ...
if (table.rows[i].cells[j].rowSpan > 1) return;
nChildNodes =
table.rows[i].
cells[j].firstChild.childNodes.
length;
innerMostNode =
table.rows[i].
cells[j].firstChild;
while ( nChildNodes != 0)
{
innerMostNode =
innerMostNode.
firstChild;
nChildNodes =
innerMostNode.
childNodes.
length;
}
if (j == 0)
{
rowArray[actualNRow] =
innerMostNode.data;
}
else
{
rowArray[actualNRow] += recDelimiter +
innerMostNode.data;
}
}
// Inconsistent col lengh for data rows
if (table.rows[i].cells.length > maxNCol)
return;
actualNRow++;
colSpanArray[i] = false;
}
else if (nColSpan == 1 && rNRowSpan == 0 &&
rNColSpan == 0 && titleFound == false)
{
colSpanArray[i] = false;
}
else
{
colSpanArray[i] = true;
}
// Counters for row/column spans
if (rNRowSpan > 0) rNRowSpan--;
if (rNColSpan > 0) rNColSpan--;
}
// If the row number is < 1, no need to do anything ...
if (actualNRow < 1) return;
// Re-drawing the title row
for (var j=0; j<maxNCol; j++)
{
// If for some reason, the rows do NOT have any child, then
// simply return ...
if (titleRowCellArray[j].childNodes.length == 0) return;
if (titleRowCellArray[j].firstChild != null)
{
nChildNodes =
titleRowCellArray[j].
firstChild.childNodes.length;
innerMostNode =
titleRowCellArray[j].firstChild;
while ( nChildNodes != 0)
{
innerMostNode =
innerMostNode.firstChild;
nChildNodes =
innerMostNode.
childNodes.
length;
}
cellText = innerMostNode.data;
}
else
{
cellText = "column(" + j + ")";
}
titleRowArray[j] = cellText;
titleRowCellArray[j].innerHTML =
'<a ' +
linkEventString +
j + ',' + '"' + table.id + '"' + ');\'>' +
'<' + titleFace + '>' + cellText +
'</' + titleFace +'></a>';
}
}
//*****************************************************************************
// Function called when user clicks on a title to sort
//*****************************************************************************
function sortTable(index,obj)
{
// Re-inializing the table object
initTable(obj);
// Local variables
var nChildNodes;
var innerMostNode;
var rowContent;
var rowCount;
var cell, cellText;
var newTitle;
// Can't sort past the max allowed column size
if (index < 0 || index >= maxNCol) return;
// Assignment of sort index
sortIndex = index;
// Doing the sort using JavaScript generic function for an Array
rowArray.sort(compare);
// Re-drawing the title row
for (var j=0; j<maxNCol; j++)
{
cellText = titleRowArray[j];
cellText = '<' + titleFace +'>' +
cellText + '</' + titleFace + '></a>';
newTitle = '<a ' +
linkEventString +
j + ',' + '"' + table.id + '"' + ');\'>' +
cellText +
'</a>';
if (j == sortIndex)
{
newTitle += ' <font color=' + updownColor + '>';
if (descending)
newTitle += desChr;
else
newTitle += ascChr;
newTitle += '</font>';
}
titleRowCellArray[j].innerHTML = newTitle;
}
// Re-drawing the table
rowCount = 0;
for (var i=0; i<nRow; i++)
{
if (! colSpanArray[i])
{
for (var j=0; j<maxNCol; j++)
{
rowContent = rowArray[rowCount].
split(recDelimiter);
nChildNodes =
table.rows[i].cells[j].firstChild.
childNodes.length;
innerMostNode =
table.rows[i].cells[j].firstChild;
while ( nChildNodes != 0)
{
innerMostNode =
innerMostNode.firstChild;
nChildNodes =
innerMostNode.
childNodes.
length;
}
innerMostNode.data = rowContent[j];
}
rowCount++;
}
}
// Switching btw descending/ascending sort
if (descending)
descending = false;
else
descending = true;
}
//*****************************************************************************
// Function to be used for Array sorting
//*****************************************************************************
function compare(a, b)
{
// Getting the element array for inputs (a,b)
var aRowContent = a.split(recDelimiter);
var bRowContent = b.split(recDelimiter);
// Needed in case the data conversion is necessary
var aToBeCompared, bToBeCompared;
if (! isNaN(aRowContent[sortIndex]))
aToBeCompared = parseInt(aRowContent[sortIndex], 10);
else
aToBeCompared = aRowContent[sortIndex];
if (! isNaN(bRowContent[sortIndex]))
bToBeCompared = parseInt(bRowContent[sortIndex], 10);
else
bToBeCompared = bRowContent[sortIndex];
if (aToBeCompared < bToBeCompared)
if (!descending)
{
return -1;
}
else
{
return 1;
}
if (aToBeCompared > bToBeCompared)
if (!descending)
{
return 1;
}
else
{
return -1;
}
return 0;
}
//*****************************************************************************
// Function to set the cursor
//*****************************************************************************
function setCursor(obj)
{
// Show hint text at the browser status bar
window.status = "Sort by " + obj.firstChild.innerHTML;
// Change the mouse cursor to hand or pointer
if (isIE)
obj.firstChild.style.cursor = "hand";
else
obj.firstChild.style.cursor = "pointer";
}
//*****************************************************************************
// Function to set the title color
//*****************************************************************************
function setColor(obj,mode)
{
if (mode == "selected")
{
// Remember the original color
if (obj.style.color != selectedColor)
defaultColor = obj.style.color;
obj.style.color = selectedColor;
}
else
{
// Restoring original color and re-setting the status bar
obj.style.color = defaultColor;
window.status = '';
}
}
//*****************************************************************************
// Function to check browser type/version
//*****************************************************************************
function checkBrowser()
{
if (navigator.appName == "Microsoft Internet Explorer"
&& navigator.appVersion.indexOf("5.") >= 0)
{
isIE = true;
return true;
}
// For some reason, appVersion returns 5 for Netscape 6.2 ...
else if (navigator.appName == "Netscape"
&& navigator.appVersion.indexOf("5.") >= 0)
{
isIE = false;
return true;
}
else
return false;
}