File size: 5,134 Bytes
6d12932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let fhirTableLoading = false;

function getCollapsed(store, row) {
  return sessionStorage.getItem("ft-"+store+row);
}

function setCollapsed(store, row, value) {
  if (!fhirTableLoading) {
    if (value == 'collapsed') {
      sessionStorage.setItem("ft-"+store+row, value);
    } else {
      sessionStorage.removeItem("ft-"+store+row);
    }
  }
}
  
function fhirTableRowExpand(table, id) {
  var rows = table.getElementsByTagName('tr');
  var row, i;
  var noex = null;
  for (i = 0; i < rows.length; i++) {
    row = rows[i];
    if (row.id.startsWith(id)) {
      if (noex && row.id.startsWith(noex)) {
        // do nothing
      } else {
        noex = null;
        if (row.id != id) {
          row.style.display = "";
          if (row.className == 'closed') {
            noex = row.id;
          }
        }
      }
    }
  }
}

function fhirTableRowCollapse(table, id) {
  var rows = table.getElementsByTagName('tr');
  var row, i;
  for (i = 0; i < rows.length; i++) {
    row = rows[i];
    if (row.id.startsWith(id) && row.id != id) {
      row.style.display = "none";
    }
  }
}

function findElementFromFocus(src, name) {
  e = src;
  while (e && e.tagName != name) {
    e = e.parentNode;
  }
  return e;
}

// src - a handle to an element in a row in the table 
function tableRowAction(src) {
  let table = findElementFromFocus(src, "TABLE");
  let row = findElementFromFocus(src, "TR");
  let td = row.firstElementChild;
  let state = row.className;
  if (state == "closed") {
    fhirTableRowExpand(table, row.id);
    row.className = "open";
    src.src  = src.src.replace("-closed", "-open");
    td.style.backgroundImage = td.style.backgroundImage.replace('0.png', '1.png');
    setCollapsed(table.id, row.id, 'expanded');
  } else {
    fhirTableRowCollapse(table, row.id);
    row.className = "closed";
    src.src  = src.src.replace("-open", "-closed");
    td.style.backgroundImage = td.style.backgroundImage.replace('1.png', '0.png');
    setCollapsed(table.id, row.id, 'collapsed');
  }
}

// src - a handle to an element in a row in the table 
function fhirTableInit(src) {
  let table = findElementFromFocus(src, "TABLE");
  var rows = table.getElementsByTagName('tr');
  var row, i;
  fhirTableLoading = true;
  for (i = 0; i < rows.length; i++) {
    row = rows[i];
    var id = row.id;
    if (getCollapsed(table.id, id) == 'collapsed') {
      let td = row.firstElementChild;
      let e = td.firstElementChild;
      while (e.tagName != "IMG" || !(e.src.includes("join"))) {
        e = e.nextSibling;
      }
      tableRowAction(e);
    }
  }
  fhirTableLoading = false;
}

function filterTree(table, text) {
  if (!text) {
     for (let i = 1; i < table.rows.length-1; i++) {
      const row = table.rows[i];
      row.style.display = '';
      const cell = row.cells[4];
      cell.style.display = '';
    }
  } else if (text.startsWith('.')) {
    text = text.substring(1);
    for (let i = 1; i < table.rows.length-1; i++) {
      const row = table.rows[i];
      let rowText = row.textContent || row.innerText
      rowText = rowText.toLowerCase();

      // Check if row contains the search text
      if (rowText.includes(text)) {
        let id = row.id;
        while (id) {
          document.getElementById(id).style.display = '';
          id = id.substring(0, id.length - 1);
        }
      } else {
        // Hide the row
        row.style.display = 'none';
      }
    }
  } else {
    for (let i = 1; i < table.rows.length-1; i++) {
      const row = table.rows[i];
      const cell = row.cells[4];
      let cellText = cell.textContent || cell.innerText
      cellText = cellText.toLowerCase();

      // Check if row contains the search text
      if (cellText.includes(text)) {
        // Show the row
        cell.style.display = '';
      } else {
        // Hide the row
        cell.style.display = 'none';
      }
    }
  }
}

function filterDesc(table, prop, value, panel) {
  let v = 'none';
  if (value) {
    v = '';
  }
  
  panel.style.display = 'none';
  localStorage.setItem('ht-table-states-'+prop, value);

  for (let i = 1; i < table.rows.length-1; i++) {
    const row = table.rows[i];
    const cell = row.cells[4];
    if (cell) {
      for (let i = 0; i < cell.children.length; i++) {
        const childElement = cell.children[i];
        let classes = childElement.getAttribute('class');
        if (classes.includes(prop)) {
          childElement.style.display = v;
        }
      }
    }
  }
}

function hide() {
  if (visiblePanel) {
    visiblePanel.style.display = 'none';
    visiblePanel = null;
  }
}

function showPanel(button, table, panel) {
  const rect1 = button.getBoundingClientRect();
  panel.style.top = (rect1.bottom+10) + 'px';
  panel.style.left = (rect1.left) + 'px';
  panel.style.display = 'block';
  visiblePanel = panel;
  window.addEventListener('scroll', hide);
  window.addEventListener('click', hide);
  event.stopPropagation();
}