java/HrisniciSpameri/web/tridenitabulky.js
author František Kučera <franta-hg@frantovo.cz>
Thu, 12 Feb 2009 01:02:12 +0100
changeset 10 025cde619dd9
parent 7 29bb67aa99fd
permissions -rw-r--r--
Doladění Antu a další drobnosti.
     1 addEvent(window, "load", sortables_init);
     2 
     3 var SORT_COLUMN_INDEX;
     4 
     5 function trim(myText)
     6 {
     7   if (myText != null)
     8   {
     9     while (myText.indexOf(". ") != -1 || myText.indexOf(": ") != -1 || myText.indexOf("\n") != -1)
    10     {
    11       myText = myText.replace(/\. /,".");
    12       myText = myText.replace(/\: /,".");
    13       myText = myText.replace(/\s /,"");
    14     }
    15   }
    16   return myText;
    17 }
    18 
    19 function sortables_init()
    20 {
    21   predemNactiNahoru = new Image(14,11);
    22   predemNactiNahoru.src ="tabulka-nahoru.gif";
    23   predemNactiDolu = new Image(14,11);
    24   predemNactiDolu.src ="tabulka-dolu.gif";
    25 
    26   if (!document.getElementsByTagName)
    27    return;
    28   tbls = document.getElementsByTagName("table");
    29   for (ti=0;ti<tbls.length;ti++)
    30   {
    31     thisTbl = tbls[ti];
    32     if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id))
    33     {
    34       ts_makeSortable(thisTbl);
    35     }
    36   }
    37 }
    38 
    39 function ts_makeSortable(table)
    40 {
    41   if (table.rows && table.rows.length > 0)
    42   {
    43     var firstRow = table.rows[0];
    44   }
    45   if (!firstRow)
    46     return;
    47   var cell;
    48   var txt;
    49   for (var i=0;i<firstRow.cells.length;i++)
    50   {
    51     cell = firstRow.cells[i];
    52     if (cell.className != 'dontsort')
    53     {
    54       txt = ts_getInnerText(cell);
    55       cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this); return false;">'+txt+'<span class="sortarrow"></span></a>';
    56     }
    57   }
    58 }
    59 
    60 function ts_getInnerText(el)
    61 {
    62   if (typeof el == "string")
    63     return el;
    64   if (typeof el == "undefined")
    65     return el;
    66   if (el.innerText)
    67     return el.innerText;  //Not needed but it is faster
    68   var str = "";
    69 
    70   var cs = el.childNodes;
    71   var l = cs.length;
    72   for (var i = 0; i < l; i++)
    73   {
    74     switch (cs[i].nodeType)
    75     {
    76       case 1: //ELEMENT_NODE
    77         str += ts_getInnerText(cs[i]);
    78       break;
    79       case 3:  //TEXT_NODE
    80         str += cs[i].nodeValue;
    81       break;
    82     }
    83   }
    84   return str;
    85 }
    86 
    87 function findValidRowInColumn(table,column)
    88 {
    89   var ir = 1;
    90   for (ir=1 ;ir < table.rows.length; ir++)
    91   {
    92     if (table.rows[ir].cells[column] != undefined && table.rows[ir].cells[column].className != 'dontsort' && ts_getInnerText(table.rows[ir].cells[column]).length > 0  && ts_getInnerText(table.rows[ir].cells[column]) != " ")
    93       return ir;
    94   }
    95   return 0;
    96 }
    97 
    98 function ts_resortTable(lnk)
    99 {
   100   var span;
   101   for (var ci=0;ci<lnk.childNodes.length;ci++)
   102   {
   103     if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
   104   }
   105   var spantext = ts_getInnerText(span);
   106   var td = lnk.parentNode;
   107   var column = td.cellIndex;
   108   var table = getParent(td,'TABLE');
   109 
   110   if (table.rows.length <= 1)
   111     return;
   112   var vr = findValidRowInColumn(table,column);
   113   var itm = ts_getInnerText(table.rows[vr].cells[column]);
   114 
   115   if (itm.match(/^\d{1,2}[\/.\/-][ ]?\d{1,2}[\/.\/-][ ]?(\d{2}|\d{4})([ ]{0,3}\d{1,2}[:]{1}\d{1,2}([ ]{0}|[:]{1}\d{1,2}))?$/))
   116     sortfn = ts_sort_date;
   117   else if (itm.match(/^(([�L\$])|k� |K� |K� ){1}.*|.*(([�L\$])|k� |K� |K� ){1}$/))
   118     sortfn = ts_sort_currency;
   119   else if (itm.match(/^[\d\,]+$/))
   120     sortfn = ts_sort_numeric;
   121   else
   122     sortfn = ts_sort_caseinsensitive;
   123 
   124   SORT_COLUMN_INDEX = column;
   125   var firstRow = new Array();
   126   var newRows = new Array();
   127   var noSortRows = new Array();
   128   firstRow[0] = table.rows[0];
   129   var tableRowIndex = 1;
   130   var newRowIndex = 0;
   131   var noSortIndex = 0;
   132   while (tableRowIndex < table.rows.length)
   133   {
   134     if (!table.rows[tableRowIndex].className || (table.rows[tableRowIndex].className && (table.rows[tableRowIndex].className != 'dontsort')))
   135     {
   136       newRows[newRowIndex] = table.rows[tableRowIndex];
   137       newRowIndex++;
   138     }
   139     else
   140     {
   141       noSortRows[noSortIndex] = table.rows[tableRowIndex];
   142       noSortIndex++;
   143     }
   144     tableRowIndex++;
   145   }
   146 
   147   newRows.sort(sortfn);
   148 
   149   if (span.getAttribute("sortdir") == 'down')
   150   {
   151     ARROW = '&nbsp;&nbsp;<img src=\"tabulka-dolu.gif\">';
   152     //ARROW = '&nbsp;&nbsp;&uarr;';
   153     newRows.reverse();
   154     span.setAttribute('sortdir','up');
   155   }
   156   else
   157   {
   158     //ARROW = '&nbsp;&nbsp;&darr;';
   159     ARROW = '&nbsp;&nbsp;<img src=\"tabulka-nahoru.gif\">';
   160     span.setAttribute('sortdir','down');
   161   }
   162 
   163   for (i=0;i<noSortRows.length;i++)
   164   {
   165     newRows[newRows.length] = noSortRows[i];
   166   }
   167 
   168   for (i=0;i<newRows.length;i++)
   169   {
   170     table.tBodies[0].appendChild(newRows[i]);
   171   }
   172 
   173   var allspans = document.getElementsByTagName("span");
   174   for (var ci=0;ci<allspans.length;ci++)
   175     if (allspans[ci].className == 'sortarrow')
   176       if (getParent(allspans[ci],"table") == getParent(lnk,"table"))
   177         allspans[ci].innerHTML = '';
   178   span.innerHTML = ARROW;
   179 }
   180 
   181 function getParent(el, pTagName)
   182 {
   183   if (el == null)
   184     return null;
   185   else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
   186     return el;
   187   else
   188     return getParent(el.parentNode, pTagName);
   189 }
   190 
   191 function ts_sort_date(a,b)
   192 {
   193   var aaDateTime;
   194   if (a.cells[SORT_COLUMN_INDEX] != undefined && a.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).length > 0  && ts_getInnerText(a.cells[SORT_COLUMN_INDEX]) != " ")
   195   {
   196     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
   197     var aaList = trim(aa).split(" ");
   198     var aaDateList;
   199     var aaTimeList;
   200     if (aaList[0].indexOf(".") != -1)
   201     {// první je datum
   202 
   203       aaDateList = aaList[0].split(".");
   204 
   205       if (aaList[1] != null && aaList[1].indexOf(":") != -1)
   206       { // rozbrakovat čas
   207         aaTimeList = aaList[1].split(":");
   208         if (aaTimeList.length > 2)
   209           aaDateTime = new Date(aaDateList[2],(aaDateList[1]-1),aaDateList[0],aaTimeList[0],aaTimeList[1],aaTimeList[2]);
   210         else
   211           aaDateTime = new Date(aaDateList[2],(aaDateList[1]-1),aaDateList[0],aaTimeList[0],aaTimeList[1],0);
   212       }
   213       else
   214       { // jenom datum
   215         aaDateTime = new Date(aaDateList[2],(aaDateList[1]-1),aaDateList[0]);
   216       }
   217     }
   218     else
   219     { // první je čas
   220       aaTimeList = aaList[1].split(":");
   221       aaDateList = aaList[0].split(".");
   222       if (aaTimeList.length > 2)
   223         aaDateTime = new Date(aaDateList[2],(aaDateList[1]-1),aaDateList[0],aaTimeList[0],aaTimeList[1],aaTimeList[2]);
   224       else
   225         aaDateTime = new Date(aaDateList[2],(aaDateList[1]-1),aaDateList[0],aaTimeList[0],aaTimeList[1],0);
   226     }
   227   }
   228   else
   229     aaDateTime = new Date(1900,0,1);
   230 
   231   var bbDateTime;
   232   if (b.cells[SORT_COLUMN_INDEX] != undefined && b.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).length > 0  && ts_getInnerText(b.cells[SORT_COLUMN_INDEX]) != " ")
   233   {
   234     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
   235     var bbList = trim(bb).split(" ");
   236     var bbDateList;
   237     var bbTimeList;
   238 
   239     if (bbList[0].indexOf(".") != -1)
   240     {// první je datum
   241       bbDateList = bbList[0].split(".");
   242       if (bbList[1] != null && bbList[1].indexOf(":") != -1)
   243       { // rozbrakovat čas
   244         bbTimeList = bbList[1].split(":");
   245         if (bbTimeList.length > 2)
   246           bbDateTime = new Date(bbDateList[2],(bbDateList[1]-1),bbDateList[0],bbTimeList[0],bbTimeList[1],bbTimeList[2]);
   247         else
   248           bbDateTime = new Date(bbDateList[2],(bbDateList[1]-1),bbDateList[0],bbTimeList[0],bbTimeList[1],0);
   249       }
   250       else
   251       { // jenom datum
   252         bbDateTime = new Date(bbDateList[2],(bbDateList[1]-1),bbDateList[0]);
   253       }
   254     }
   255     else
   256     { // první je čas
   257       bbTimeList = bbList[1].split(":");
   258       bbDateList = bbList[0].split(".");
   259       if (bbTimeList.length > 2)
   260         bbDateTime = new Date(bbDateList[2],(bbDateList[1]-1),bbDateList[0],bbTimeList[0],bbTimeList[1],bbTimeList[2]);
   261       else
   262         bbDateTime = new Date(bbDateList[2],(bbDateList[1]-1),bbDateList[0],bbTimeList[0],bbTimeList[1],0);
   263     }
   264   }
   265   else
   266     bbDateTime = new Date(1900,0,1);
   267 
   268   if (aaDateTime == bbDateTime)
   269     return 0;
   270   else if (aaDateTime < bbDateTime)
   271     return -1;
   272   else
   273     return 1;
   274 }
   275 
   276 function ts_sort_currency(a,b)
   277 {
   278   aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
   279   bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
   280   return parseFloat(aa) - parseFloat(bb);
   281 }
   282 
   283 function ts_sort_numeric(a,b)
   284 {
   285   if (a.cells[SORT_COLUMN_INDEX] != undefined && a.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).length > 0  && ts_getInnerText(a.cells[SORT_COLUMN_INDEX]) != " ")
   286     aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
   287   else
   288     aa = 0;
   289   if (b.cells[SORT_COLUMN_INDEX] != undefined && b.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).length > 0  && ts_getInnerText(b.cells[SORT_COLUMN_INDEX]) != " ")
   290     bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
   291   else
   292     bb = 0;
   293   return aa-bb;
   294 }
   295 
   296 function char2Diacritic(transDiacritic)
   297 {
   298   var charDiacritic = "�����ĚĽ�����ŠŤ�Ŏ�Ž";
   299   var numDiacritic = "ACDEILLNOORRSTUUYZ";
   300   var tmpDiacritic = "";
   301   var newDiacritic = "";
   302   transDiacritic = transDiacritic.toUpperCase();
   303   for(i=0;i<transDiacritic.length;i++)
   304   {
   305     if (charDiacritic.indexOf(transDiacritic.charAt(i))!=-1)
   306       tmpDiacritic += numDiacritic.charAt(charDiacritic.indexOf(transDiacritic.charAt(i)))+'|';
   307     else
   308      tmpDiacritic += transDiacritic.charAt(i);
   309   }
   310   return tmpDiacritic;
   311 }
   312 
   313 function ts_sort_caseinsensitive(a,b)
   314 {
   315   var aa = "_";
   316   var bb = "_";
   317   if (a.cells[SORT_COLUMN_INDEX] != undefined && a.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).length > 0)
   318     aa = char2Diacritic(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
   319   if (b.cells[SORT_COLUMN_INDEX] != undefined && b.cells[SORT_COLUMN_INDEX].className != 'dontsort' && ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).length > 0)
   320     bb = char2Diacritic(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
   321   if (aa==bb)
   322     return 0;
   323   else if (aa < bb)
   324     return -1;
   325   else
   326     return 1;
   327 }
   328 
   329 function ts_sort_default(a,b)
   330 {
   331   aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
   332   bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
   333   if (aa==bb)
   334     return 0;
   335   else if (aa<bb)
   336     return -1;
   337   else
   338     return 1;
   339 }
   340 
   341 function addEvent(elm, evType, fn, useCapture)
   342 {
   343   if (elm.addEventListener)
   344   {
   345     elm.addEventListener(evType, fn, useCapture);
   346     return true;
   347   }
   348   else if (elm.attachEvent)
   349   {
   350     var r = elm.attachEvent("on"+evType, fn);
   351     return r;
   352   }
   353   else
   354   {
   355     alert("Handler could not be removed");
   356   }
   357 }