Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 1 | define(function () { |
| 2 | var _TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
| 3 | |
| 4 | return { |
| 5 | create : function (snippet) { |
| 6 | return Object.create(this)._init(snippet); |
| 7 | }, |
| 8 | _init : function (snippet) { |
| 9 | // Create html for traversal |
| 10 | var html = document.createElement("div"); |
| 11 | html.innerHTML = snippet; |
| 12 | |
| 13 | this._pos = 0; |
| 14 | this._token = []; |
| 15 | this._info = []; |
| 16 | this._foundry = {}; |
| 17 | this._layer = {}; |
| 18 | |
| 19 | // Parse the snippet |
| 20 | this._parse(html.childNodes); |
| 21 | |
| 22 | html.innerHTML = ''; |
| 23 | return this; |
| 24 | }, |
| 25 | |
| 26 | length : function () { |
| 27 | return this._pos; |
| 28 | }, |
| 29 | |
| 30 | getToken : function (pos) { |
| 31 | if (pos === undefined) |
| 32 | return this._token; |
| 33 | return this._token[pos]; |
| 34 | }, |
| 35 | |
| 36 | getValue : function (pos, foundry, layer) { |
| 37 | return this._info[pos][foundry + '/' + layer] |
| 38 | }, |
| 39 | |
| 40 | getLayerPerFoundry : function (foundry) { |
| 41 | return this._foundry[foundry] |
| 42 | }, |
| 43 | |
| 44 | getFoundryPerLayer : function (layer) { |
| 45 | return this._layer[layer]; |
| 46 | }, |
| 47 | |
| 48 | // Parse the snippet |
| 49 | _parse : function (children) { |
| 50 | |
| 51 | // Get all children |
| 52 | for (var i in children) { |
| 53 | var c = children[i]; |
| 54 | |
| 55 | // Create object on position unless it exists |
| 56 | if (this._info[this._pos] === undefined) |
| 57 | this._info[this._pos] = {}; |
| 58 | |
| 59 | // Store at position in foundry/layer as array |
| 60 | var found = this._info[this._pos]; |
| 61 | |
| 62 | // Element with title |
| 63 | if (c.nodeType === 1) { |
| 64 | if (c.getAttribute("title") && |
| 65 | _TermRE.exec(c.getAttribute("title"))) { |
| 66 | |
| 67 | // Fill position with info |
| 68 | var foundry, layer, value; |
| 69 | if (RegExp.$2) { |
| 70 | foundry = RegExp.$1; |
| 71 | layer = RegExp.$2; |
| 72 | } |
| 73 | else { |
| 74 | foundry = "base"; |
| 75 | layer = RegExp.$1 |
| 76 | }; |
| 77 | |
| 78 | value = RegExp.$3; |
| 79 | |
| 80 | if (found[foundry + "/" + layer] === undefined) |
| 81 | found[foundry + "/" + layer] = []; |
| 82 | |
| 83 | // Push value to foundry/layer at correct position |
| 84 | found[foundry + "/" + layer].push(RegExp.$3); |
| 85 | |
| 86 | // Set foundry |
| 87 | if (this._foundry[foundry] === undefined) |
| 88 | this._foundry[foundry] = {}; |
| 89 | this._foundry[foundry][layer] = 1; |
| 90 | |
| 91 | // Set layer |
| 92 | if (this._layer[layer] === undefined) |
| 93 | this._layer[layer] = {}; |
| 94 | this._layer[layer][foundry] = 1; |
| 95 | }; |
| 96 | |
| 97 | // depth search |
| 98 | if (c.hasChildNodes()) |
| 99 | this._parse(c.childNodes); |
| 100 | } |
| 101 | |
| 102 | // Leaf node |
| 103 | // store string on position and go to next string |
| 104 | else if (c.nodeType === 3) { |
| 105 | if (c.nodeValue.match(/[a-z0-9]/i)) |
| 106 | this._token[this._pos++] = c.nodeValue; |
| 107 | }; |
| 108 | }; |
| 109 | |
| 110 | delete this._info[this._pos]; |
| 111 | }, |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Get HTML table view of annotations. |
| 116 | */ |
| 117 | element : function () { |
| 118 | if (this._element !== undefined) |
| 119 | return this._element; |
| 120 | |
| 121 | // First the legend table |
| 122 | var d = document; |
| 123 | var table = d.createElement('table'); |
| 124 | |
| 125 | // Single row in head |
| 126 | var tr = table.appendChild(d.createElement('thead')) |
| 127 | .appendChild(d.createElement('tr')); |
| 128 | |
| 129 | // Add cell to row |
| 130 | var addCell = function (type, name) { |
| 131 | var c = this.appendChild(d.createElement(type)) |
| 132 | if (name === undefined) |
| 133 | return c; |
| 134 | |
| 135 | if (name instanceof Array) { |
| 136 | for (var n = 0; n < name.length; n++) { |
| 137 | c.appendChild(d.createTextNode(name[n])); |
| 138 | if (n !== name.length - 1) { |
| 139 | c.appendChild(d.createElement('br')); |
| 140 | }; |
| 141 | }; |
| 142 | } |
| 143 | else { |
| 144 | c.appendChild(d.createTextNode(name)); |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | tr.addCell = addCell; |
| 149 | |
| 150 | // Add header information |
| 151 | tr.addCell('th', 'Foundry'); |
| 152 | tr.addCell('th', 'Layer'); |
| 153 | |
| 154 | // Add tokens |
| 155 | for (var i in this._token) { |
| 156 | tr.addCell('th', this.getToken(i)); |
| 157 | }; |
| 158 | |
| 159 | var tbody = table.appendChild( |
| 160 | d.createElement('tbody') |
| 161 | ); |
| 162 | |
| 163 | var foundryList = Object.keys(this._foundry).sort(); |
| 164 | |
| 165 | for (var f = 0; f < foundryList.length; f++) { |
| 166 | var foundry = foundryList[f]; |
| 167 | var layerList = |
| 168 | Object.keys(this._foundry[foundry]).sort(); |
| 169 | |
| 170 | for (var l = 0; l < layerList.length; l++) { |
| 171 | var layer = layerList[l]; |
| 172 | tr = tbody.appendChild( |
| 173 | d.createElement('tr') |
| 174 | ); |
| 175 | tr.setAttribute('tabindex', 0); |
| 176 | tr.addCell = addCell; |
| 177 | |
| 178 | tr.addCell('th', foundry); |
| 179 | tr.addCell('th', layer); |
| 180 | |
| 181 | for (var v = 0; v < this.length(); v++) { |
| 182 | tr.addCell( |
| 183 | 'td', |
| 184 | this.getValue(v, foundry, layer) |
| 185 | ); |
| 186 | }; |
| 187 | }; |
| 188 | }; |
| 189 | |
| 190 | return this._element = table; |
| 191 | } |
| 192 | }; |
| 193 | }); |