summaryrefslogtreecommitdiffstats
path: root/webui_content/showhide.js
blob: d2f39e142d8bd860178134114d4dda4c8b0bd714 (plain)
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
// Javascript code by Máirín Duffy <duffy@redhat.com>


IMAGE_COLLAPSED_PATH = '/img/list-expand.gif';
IMAGE_EXPANDED_PATH  = '/img/list-collapse.gif';
IMAGE_CHILDLESS_PATH  = '/img/rhn-bullet-parentchannel.gif';

var rowHash = new Array();
var browserType;
var columnsPerRow;

// tip of the Red Hat to Mar Orlygsson for this little IE detection script
var is_ie/*@cc_on = {
   quirksmode : (document.compatMode=="BackCompat"),
   version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
}@*/;
browserType = is_ie;

function onLoadStuff(columns) {
  columnsPerRow = columns;
  var channelTable = document.getElementById('channel-list');
  createParentRows(channelTable, rowHash);
  reuniteChildrenWithParents(channelTable, rowHash);
  iconifyChildlessParents(rowHash);
}

function iconifyChildlessParents(rowHash) {
  for (var i in rowHash) {
    if (!rowHash[i].hasChildren && rowHash[i].image) { rowHash[i].image.src = IMAGE_CHILDLESS_PATH; }
  }
}

// called from clicking the show/hide button on individual rows in the page
function toggleRowVisibility(id) {
  if (!rowHash[id]) { return; }
  if (!rowHash[id].hasChildren) { return; }
  rowHash[id].toggleVisibility();
  return;
}

function showAllRows() {
  var row;
  for (var i in rowHash) {
    row = rowHash[i];
    if (!row) { continue; }
    if (!row.hasChildren) { continue; }
    row.show();
  }
  return;
}

function hideAllRows() {
  var row;
  for (var i in rowHash) {
    row = rowHash[i];
    if (!row) { continue; }
    if (!row.hasChildren) { continue; }
    row.hide();
  }
  return;
}

function Row(cells, image) {
  this.cells = new Array();
  for (var i = 0; i < cells.length; i++) { this.cells[i] = cells[i]; }
  this.image = image;
  this.hasChildren = 0;
  this.isHidden = 1; // 1 = hidden; 0 = visible.  all rows are hidden by default

// Row object methods below!
  this.toggleVisibility = function() {
    if (this.isHidden == 1) { this.show(); } 
    else if (this.isHidden == 0) { this.hide(); }
    return;
  }

  this.hide = function hide() {
    this.image.src = IMAGE_COLLAPSED_PATH;
// we start with columnsPerRow, because we want to skip the td cells of the parent tr.
    for (var i = columnsPerRow; i < this.cells.length; i++) {
      this.cells[i].parentNode.style.display = 'none';
      this.cells[i].style.display = 'none';
    }
    this.isHidden = 1;
    return;
  }

  this.show = function() {
    displayType = '';
    this.image.src = IMAGE_EXPANDED_PATH;

    for (var i = 0; i < this.cells.length; i++) {
        this.cells[i].style.display = displayType;
        this.cells[i].parentNode.style.display = displayType; 
    }
    this.isHidden = 0;
    return;
  }
}

function createParentRows(channelTable, rowHash) {
  for (var i = 0; i < channelTable.rows.length; i++) {
    tableRowNode = channelTable.rows[i];
    if (isParentRowNode(tableRowNode)) {
      if (!tableRowNode.id) { continue; }
      id = tableRowNode.id;
      var cells = tableRowNode.cells;
      var image = findRowImageFromCells(cells, id)
      if (!image) { continue; }
      rowHash[id] = new Row(cells, image);
    }
  }
  return;
}

function reuniteChildrenWithParents(channelTable, rowHash) {
  var parentNode;
  var childId;
  var tableChildRowNode;
  for (var i = 0; i < channelTable.rows.length; i++) {
    tableChildRowNode = channelTable.rows[i];
// when we find a parent, set it as parent for the children after it
    if (isParentRowNode(tableChildRowNode) && tableChildRowNode.id) {
      parentNode = tableChildRowNode;
      continue; 
    }
    if (!parentNode) { continue; }

    // it its not a child node we bail here
    if (!isChildRowNode(tableChildRowNode)) { continue; }
    // FIXME: chceck child id against parent id
    if (!rowHash[parentNode.id]) { /*alert('bailing, cant find parent in hash');*/ continue; }
    for (var j = 0; j < tableChildRowNode.cells.length; j++) {
      rowHash[parentNode.id].cells.push(tableChildRowNode.cells[j]);
      rowHash[parentNode.id].hasChildren = 1;
    }
  }
  return;
}


function getNodeTagName(node) {
  var tagName;
  var nodeId;
  tagName = new String(node.tagName);
  return tagName.toLowerCase();
}

function isParentRowNode(node) {
  var nodeInLowercase = getNodeTagName(node);
  if (nodeInLowercase != 'tr') { return 0; }
  nodeId = node.id;
  if ((nodeId.indexOf('id')) && !(nodeId.indexOf('child'))) { return 0; }
  return 1;
}

function isChildRowNode(node) {
  var nodeInLowercase = getNodeTagName(node);
  var nodeId;
  if (nodeInLowercase != 'tr') { return 0; }
  nodeId = node.id;
  if (nodeId.indexOf('child')) { return 0; }
  return 1;
}


function findRowImageFromCells(cells, id) {
  var imageId = id + '-image';
  var childNodes; // first level child
  var grandchildNodes; // second level child
  for (var i = 0; i < cells.length; i++) {
    childNodes = null;
    grandchildNodes = null;
    
    if (!cells[i].hasChildNodes()) { continue; }
    
    childNodes = cells[i].childNodes;

    for (var j = 0; j < childNodes.length; j++) {
      if (!childNodes[j].hasChildNodes()) { continue; }
      if (getNodeTagName(childNodes[j]) != 'a') { continue; }
      grandchildNodes = childNodes[j].childNodes;
      
      for (var k = 0; k < grandchildNodes.length; k++) {
        if (grandchildNodes[k].name != imageId) { continue; }
        if (grandchildNodes[k].nodeName == 'IMG' || grandchildNodes[k].nodeName == 'img') { return grandchildNodes[k]; }
      }
    }
  }
  return null;
}