Nils Diewald | 1eba657 | 2014-06-17 19:49:53 +0000 | [diff] [blame] | 1 | // Store Table object in global object |
| 2 | |
| 3 | var splitRegex = /^([^\/]+?)(?:\/(.+?))?:([^:]+?)$/; |
| 4 | |
| 5 | var textFoundry = "Foundry"; |
| 6 | var textLayer = "Layer"; |
| 7 | |
| 8 | |
| 9 | // SnippetTable constructor |
| 10 | function SnippetTable (snippet) { |
| 11 | this.info = []; |
| 12 | this.foundry = {}; |
| 13 | this.layer = {}; |
| 14 | this.pos = 0; |
| 15 | |
| 16 | this.load = function (children) { |
| 17 | for (var i in children) { |
| 18 | var c = children[i]; |
| 19 | |
| 20 | // element with title |
| 21 | if (c.nodeType === 1) { |
| 22 | if (c.getAttribute("title")) { |
| 23 | if (splitRegex.exec(c.getAttribute("title"))) { |
| 24 | |
| 25 | // Fill position with info |
| 26 | var foundry, layer; |
| 27 | if (RegExp.$2) { |
| 28 | foundry = RegExp.$1; |
| 29 | layer = RegExp.$2; |
| 30 | } |
| 31 | else { |
| 32 | foundry = "base"; |
| 33 | layer = RegExp.$1 |
| 34 | }; |
| 35 | |
| 36 | // Create object on position unless it exists |
| 37 | if (!this.info[this.pos]) |
| 38 | this.info[this.pos] = {}; |
| 39 | |
| 40 | this.info[this.pos][foundry + "/" + layer] = RegExp.$3; |
| 41 | |
| 42 | // Set foundry |
| 43 | if (!this.foundry[foundry]) |
| 44 | this.foundry[foundry] = {}; |
| 45 | this.foundry[foundry][layer] = 1; |
| 46 | |
| 47 | // Set layer |
| 48 | if (!this.layer[layer]) |
| 49 | this.layer[layer] = {}; |
| 50 | this.layer[layer][foundry] = 1; |
| 51 | }; |
| 52 | }; |
| 53 | |
| 54 | // depth search |
| 55 | if (c.hasChildNodes()) |
| 56 | this.load(c.childNodes); |
| 57 | } |
| 58 | |
| 59 | // Leaf node - store string on position and go to next string |
| 60 | else if (c.nodeType === 3) |
| 61 | if (c.nodeValue.match(/[-a-z0-9]/i)) |
| 62 | this.info[this.pos++]["-s"] = c.nodeValue; |
| 63 | }; |
| 64 | return this; |
| 65 | }; |
| 66 | |
| 67 | this.toTable = function (base) { |
| 68 | var i, f, l; |
| 69 | |
| 70 | // Create HTML based on info |
| 71 | var d = document; |
| 72 | var table = d.createElement('table'); |
| 73 | var tr = d.createElement('tr'); |
| 74 | table.appendChild(tr); |
| 75 | var th = d.createElement('th'); |
| 76 | th.appendChild(document.createTextNode(base === "layer" ? textLayer : textFoundry)); |
| 77 | |
| 78 | // Add icon to switch sorting |
| 79 | var span = document.createElement("span"); |
| 80 | |
| 81 | // Add switch event |
| 82 | var that = this; |
| 83 | span.addEventListener("click", function (obj) { |
| 84 | var x = that.toTable(base === "layer" ? "foundry" : "layer"); |
| 85 | table.parentNode.replaceChild(x, table); |
| 86 | }, false); |
| 87 | |
| 88 | span.setAttribute("class", "switchSort"); |
| 89 | var icon = document.createElement("i"); |
| 90 | icon.setAttribute("class", "fa fa-arrows-h"); |
| 91 | span.appendChild(icon); |
| 92 | th.appendChild(span); |
| 93 | |
| 94 | tr.appendChild(th); |
| 95 | th = d.createElement('th'); |
| 96 | th.appendChild(document.createTextNode(base === "layer" ? textFoundry : textLayer)); |
| 97 | tr.appendChild(th); |
| 98 | |
| 99 | // Header line with surface strings |
| 100 | for (i in this.info) { |
| 101 | th = d.createElement('th'); |
| 102 | tr.appendChild(th); |
| 103 | th.appendChild(d.createTextNode(this.info[i]["-s"])); |
| 104 | }; |
| 105 | |
| 106 | // Sort keys |
| 107 | var baseArray = []; |
| 108 | if (base === "layer") { |
| 109 | for (i in this.layer) { |
| 110 | baseArray.push(i); |
| 111 | }; |
| 112 | } |
| 113 | else { |
| 114 | for (i in this.foundry) { |
| 115 | baseArray.push(i); |
| 116 | }; |
| 117 | }; |
| 118 | baseArray.sort(); |
| 119 | |
| 120 | // Annotations |
| 121 | for (f in baseArray) { |
| 122 | f = baseArray[f]; |
| 123 | var thBase = d.createElement('th'); |
| 124 | thBase.appendChild(d.createTextNode(f)); |
| 125 | |
| 126 | var rowSpan = 0; |
| 127 | |
| 128 | // Sort keys |
| 129 | var subArray = []; |
| 130 | if (base === "layer") { |
| 131 | for (i in this.layer[f]) { |
| 132 | subArray.push(i); |
| 133 | }; |
| 134 | } |
| 135 | else { |
| 136 | for (i in this.foundry[f]) { |
| 137 | subArray.push(i); |
| 138 | }; |
| 139 | }; |
| 140 | subArray.sort(); |
| 141 | |
| 142 | for (l in subArray) { |
| 143 | l = subArray[l]; |
| 144 | tr = d.createElement('tr'); |
| 145 | table.appendChild(tr); |
| 146 | |
| 147 | if (rowSpan === 0) |
| 148 | tr.appendChild(thBase); |
| 149 | |
| 150 | th = d.createElement('th'); |
| 151 | tr.appendChild(th); |
| 152 | th.appendChild(d.createTextNode(l)); |
| 153 | |
| 154 | var infoString = base === "layer" ? l + '/' + f : f + '/' + l; |
| 155 | |
| 156 | for (t in this.info) { |
| 157 | var td = d.createElement('td'); |
| 158 | tr.appendChild(td); |
| 159 | |
| 160 | if (this.info[t][infoString] !== undefined) |
| 161 | td.appendChild(d.createTextNode(this.info[t][infoString])); |
| 162 | }; |
| 163 | rowSpan++; |
| 164 | }; |
| 165 | thBase.setAttribute("rowspan", rowSpan); |
| 166 | }; |
| 167 | // return HTML object |
| 168 | return table; |
| 169 | }; |
| 170 | |
| 171 | // Create wrapper element |
| 172 | var html = document.createElement("table"); |
| 173 | html.innerHTML = snippet; |
| 174 | |
| 175 | // Create table object and load data from HTML |
| 176 | this.load(html.childNodes); |
| 177 | }; |
| 178 | |
| 179 | |