• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*******************************************************************************
2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors
3 * This program and the accompanying materials are made available under
4 * the terms of the Eclipse Public License 2.0 which is available at
5 * http://www.eclipse.org/legal/epl-2.0
6 *
7 * SPDX-License-Identifier: EPL-2.0
8 *
9 * Contributors:
10 *    Marc R. Hoffmann - initial API and implementation
11 *
12 *******************************************************************************/
13
14(function () {
15
16  /**
17   * Sets the initial sorting derived from the hash.
18   *
19   * @param linkelementids
20   *          list of element ids to search for links to add sort inidcator
21   *          hash links
22   */
23  function initialSort(linkelementids) {
24    window.linkelementids = linkelementids;
25    var hash = window.location.hash;
26    if (hash) {
27      var m = hash.match(/up-./);
28      if (m) {
29        var header = window.document.getElementById(m[0].charAt(3));
30        if (header) {
31          sortColumn(header, true);
32        }
33        return;
34      }
35      var m = hash.match(/dn-./);
36      if (m) {
37        var header = window.document.getElementById(m[0].charAt(3));
38        if (header) {
39          sortColumn(header, false);
40        }
41        return
42      }
43    }
44  }
45
46  /**
47   * Sorts the columns with the given header dependening on the current sort state.
48   */
49  function toggleSort(header) {
50    var sortup = header.className.indexOf('down ') == 0;
51    sortColumn(header, sortup);
52  }
53
54  /**
55   * Sorts the columns with the given header in the given direction.
56   */
57  function sortColumn(header, sortup) {
58    var table = header.parentNode.parentNode.parentNode;
59    var body = table.tBodies[0];
60    var colidx = getNodePosition(header);
61
62    resetSortedStyle(table);
63
64    var rows = body.rows;
65    var sortedrows = [];
66    for (var i = 0; i < rows.length; i++) {
67      r = rows[i];
68      sortedrows[parseInt(r.childNodes[colidx].id.slice(1))] = r;
69    }
70
71    var hash;
72
73    if (sortup) {
74      for (var i = sortedrows.length - 1; i >= 0; i--) {
75        body.appendChild(sortedrows[i]);
76      }
77      header.className = 'up ' + header.className;
78      hash = 'up-' + header.id;
79    } else {
80      for (var i = 0; i < sortedrows.length; i++) {
81        body.appendChild(sortedrows[i]);
82      }
83      header.className = 'down ' + header.className;
84      hash = 'dn-' + header.id;
85    }
86
87    setHash(hash);
88  }
89
90  /**
91   * Adds the sort indicator as a hash to the document URL and all links.
92   */
93  function setHash(hash) {
94    window.document.location.hash = hash;
95    ids = window.linkelementids;
96    for (var i = 0; i < ids.length; i++) {
97        setHashOnAllLinks(document.getElementById(ids[i]), hash);
98    }
99  }
100
101  /**
102   * Extend all links within the given tag with the given hash.
103   */
104  function setHashOnAllLinks(tag, hash) {
105    links = tag.getElementsByTagName("a");
106    for (var i = 0; i < links.length; i++) {
107        var a = links[i];
108        var href = a.href;
109        var hashpos = href.indexOf("#");
110        if (hashpos != -1) {
111            href = href.substring(0, hashpos);
112        }
113        a.href = href + "#" + hash;
114    }
115  }
116
117  /**
118   * Calculates the position of a element within its parent.
119   */
120  function getNodePosition(element) {
121    var pos = -1;
122    while (element) {
123      element = element.previousSibling;
124      pos++;
125    }
126    return pos;
127  }
128
129  /**
130   * Remove the sorting indicator style from all headers.
131   */
132  function resetSortedStyle(table) {
133    for (var c = table.tHead.firstChild.firstChild; c; c = c.nextSibling) {
134      if (c.className) {
135        if (c.className.indexOf('down ') == 0) {
136          c.className = c.className.slice(5);
137        }
138        if (c.className.indexOf('up ') == 0) {
139          c.className = c.className.slice(3);
140        }
141      }
142    }
143  }
144
145  window['initialSort'] = initialSort;
146  window['toggleSort'] = toggleSort;
147
148})();
149