|
| 1 | + |
| 2 | +function add_legend(map_id, layer_id, legendValues) { |
| 3 | + |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + var i = 0; |
| 7 | + |
| 8 | + for (i = 0; i < legendValues.length; i++) { |
| 9 | + if (legendValues[i].type === "category") { |
| 10 | + add_legend_category(map_id, layer_id, legendValues[i]); |
| 11 | + } else { |
| 12 | + if (legendValues[i].legend.colour.length === 1) { |
| 13 | + add_legend_category(map_id, layer_id, legendValues[i]); |
| 14 | + } else { |
| 15 | + add_legend_gradient(map_id, layer_id, legendValues[i]); |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function add_legend_gradient(map_id, layer_id, legendValues) { |
| 22 | + // fill gradient |
| 23 | + 'use strict'; |
| 24 | + var legendContent, |
| 25 | + legendTitle, |
| 26 | + tickContainer, |
| 27 | + labelContainer = document.createElement("div"), |
| 28 | + legendColours = document.createElement('div'), |
| 29 | + jsColours = [], |
| 30 | + colours = '', |
| 31 | + i = 0, |
| 32 | + legendTextColour = '#828282', |
| 33 | + style = '', |
| 34 | + isUpdating = false; |
| 35 | + |
| 36 | + |
| 37 | + if (window[map_id + 'legend' + layer_id + legendValues.colourType] == null) { |
| 38 | + window[map_id + 'legend' + layer_id + legendValues.colourType] = document.createElement("div"); |
| 39 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('id', map_id + 'legend' + layer_id + legendValues.colourType); |
| 40 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('class', 'legend'); |
| 41 | + } else { |
| 42 | + isUpdating = true; |
| 43 | + |
| 44 | + while (window[map_id + 'legend' + layer_id + legendValues.colourType].hasChildNodes()) { |
| 45 | + window[map_id + 'legend' + layer_id + legendValues.colourType].removeChild(window[map_id + 'legend' + layer_id + legendValues.colourType].lastChild); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + legendContent = document.createElement("div"), |
| 50 | + legendTitle = document.createElement("div"), |
| 51 | + tickContainer = document.createElement("div"), |
| 52 | + labelContainer = document.createElement("div"), |
| 53 | + legendColours = document.createElement('div'), |
| 54 | + |
| 55 | + legendContent.setAttribute('class', 'legendContent'); |
| 56 | + |
| 57 | + legendTitle.setAttribute('class', 'legendTitle'); |
| 58 | + legendTitle.innerHTML = legendValues.title; |
| 59 | + window[map_id + 'legend' + layer_id + legendValues.colourType].appendChild(legendTitle); |
| 60 | + |
| 61 | + tickContainer.setAttribute('class', 'tickContainer'); |
| 62 | + labelContainer.setAttribute('class', 'labelContainer'); |
| 63 | + |
| 64 | + if (legendValues.css !== null) { |
| 65 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('style', legendValues.css); |
| 66 | + } |
| 67 | + |
| 68 | + for (i = 0; i < legendValues.legend.colour.length; i++) { |
| 69 | + jsColours.push(legendValues.legend.colour[i]); |
| 70 | + } |
| 71 | + |
| 72 | + colours = '(' + jsColours.join() + ')'; |
| 73 | + |
| 74 | + style = 'display: inline-block; height: ' + jsColours.length * 20 + 'px; width: 15px;'; |
| 75 | + style += 'background: ' + jsColours[1] + ';'; |
| 76 | + style += 'background: -webkit-linear-gradient' + colours + ';'; |
| 77 | + style += 'background: -o-linear-gradient' + colours + ';'; |
| 78 | + style += 'background: -moz-linear-gradient' + colours + ';'; |
| 79 | + style += 'background: linear-gradient' + colours + ';'; |
| 80 | + |
| 81 | + legendColours.setAttribute('style', style); |
| 82 | + legendContent.appendChild(legendColours); |
| 83 | + |
| 84 | + for (i = 0; i < legendValues.legend.colour.length; i++) { |
| 85 | + |
| 86 | + var legendValue = 'text-align: left; color: ' + legendTextColour + '; font-size: 12px; height: 20px;', |
| 87 | + divTicks = document.createElement('div'), |
| 88 | + divVal = document.createElement('div'); |
| 89 | + |
| 90 | + divTicks.setAttribute('style', legendValue); |
| 91 | + divTicks.innerHTML = '-'; |
| 92 | + tickContainer.appendChild(divTicks); |
| 93 | + |
| 94 | + divVal.setAttribute('style', legendValue); |
| 95 | + divVal.innerHTML = legendValues.legend.variable[i]; |
| 96 | + labelContainer.appendChild(divVal); |
| 97 | + } |
| 98 | + |
| 99 | + legendContent.appendChild(tickContainer); |
| 100 | + legendContent.appendChild(labelContainer); |
| 101 | + |
| 102 | + window[map_id + 'legend' + layer_id + legendValues.colourType].appendChild(legendContent); |
| 103 | + |
| 104 | + if (isUpdating === false) { |
| 105 | + placeControl(map_id, window[map_id + 'legend' + layer_id + legendValues.colourType], legendValues.position); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function generateColourBox(colourType, colour) { |
| 110 | + 'use strict'; |
| 111 | + |
| 112 | + if (colourType === "fill_colour") { |
| 113 | + return ('height: 20px; width: 15px; background: ' + colour); |
| 114 | + } else { |
| 115 | + // http://jsfiddle.net/UES6U/2/ |
| 116 | + return ('height: 20px; width: 15px; background: linear-gradient(to bottom, white 25%, ' + colour + ' 25%, ' + colour + ' 45%, ' + 'white 45%)'); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function add_legend_category(map_id, layer_id, legendValues) { |
| 121 | + |
| 122 | + 'use strict'; |
| 123 | + |
| 124 | + var legendContent, |
| 125 | + legendTitle, |
| 126 | + colourContainer, |
| 127 | + tickContainer, |
| 128 | + labelContainer, |
| 129 | + legendColours, |
| 130 | + colourBox = '', |
| 131 | + //colourAttribute = '', |
| 132 | + i = 0, |
| 133 | + legendTextColour = '#828282', |
| 134 | + isUpdating = false; |
| 135 | + |
| 136 | + // catch undefined OR null |
| 137 | + // https://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null |
| 138 | + if (window[map_id + 'legend' + layer_id + legendValues.colourType] == null) { |
| 139 | + window[map_id + 'legend' + layer_id + legendValues.colourType] = document.createElement("div"); |
| 140 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('id', map_id + 'legend' + layer_id + legendValues.colourType); |
| 141 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('class', 'legend'); |
| 142 | + |
| 143 | + } else { |
| 144 | + isUpdating = true; |
| 145 | + |
| 146 | + while (window[map_id + 'legend' + layer_id + legendValues.colourType].hasChildNodes()) { |
| 147 | + window[map_id + 'legend' + layer_id + legendValues.colourType].removeChild(window[map_id + 'legend' + layer_id + legendValues.colourType].lastChild); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + legendContent = document.createElement("div"); |
| 152 | + legendTitle = document.createElement("div"); |
| 153 | + colourContainer = document.createElement("div"); |
| 154 | + tickContainer = document.createElement("div"); |
| 155 | + labelContainer = document.createElement("div"); |
| 156 | + legendColours = document.createElement('div'); |
| 157 | + |
| 158 | + legendContent.setAttribute('class', 'legendContent'); |
| 159 | + legendContent.setAttribute('id', 'legendContentId' + map_id + layer_id); |
| 160 | + |
| 161 | + legendTitle.setAttribute('class', 'legendTitle'); |
| 162 | + legendTitle.innerHTML = legendValues.title; |
| 163 | + window[map_id + 'legend' + layer_id + legendValues.colourType].appendChild(legendTitle); |
| 164 | + |
| 165 | + colourContainer.setAttribute('class', 'labelContainer'); |
| 166 | + colourContainer.setAttribute('id', 'colourContainerId' + map_id + layer_id); |
| 167 | + |
| 168 | + tickContainer.setAttribute('class', 'tickContainer'); |
| 169 | + tickContainer.setAttribute('id', 'tickContainerId' + map_id + layer_id); |
| 170 | + |
| 171 | + labelContainer.setAttribute('class', 'labelContainer'); |
| 172 | + labelContainer.setAttribute('id', 'labelContainerId' + map_id + layer_id); |
| 173 | + |
| 174 | + if (legendValues.css !== null) { |
| 175 | + window[map_id + 'legend' + layer_id + legendValues.colourType].setAttribute('style', legendValues.css); |
| 176 | + } |
| 177 | + |
| 178 | + for (i = 0; i < legendValues.legend.colour.length; i++) { |
| 179 | + |
| 180 | + var tickVal = 'text-left: center; color: ' + legendTextColour + '; font-size: 12px; height: 20px;', |
| 181 | + divCol = document.createElement('div'), |
| 182 | + divTicks = document.createElement('div'), |
| 183 | + divVal = document.createElement('div'); |
| 184 | + |
| 185 | + //colourBox = 'height: 20px; width: 15px; background: ' + legendValues.legend.colour[i]; |
| 186 | + colourBox = generateColourBox(legendValues.colourType, legendValues.legend.colour[i]); |
| 187 | + divCol.setAttribute('style', colourBox); |
| 188 | + colourContainer.appendChild(divCol); |
| 189 | + |
| 190 | + divTicks.setAttribute('style', tickVal); |
| 191 | + divTicks.innerHTML = '-'; |
| 192 | + tickContainer.appendChild(divTicks); |
| 193 | + |
| 194 | + divVal.setAttribute('style', tickVal); |
| 195 | + divVal.innerHTML = legendValues.legend.variable[i]; |
| 196 | + labelContainer.appendChild(divVal); |
| 197 | + } |
| 198 | + |
| 199 | + legendContent.appendChild(colourContainer); |
| 200 | + legendContent.appendChild(tickContainer); |
| 201 | + legendContent.appendChild(labelContainer); |
| 202 | + |
| 203 | + window[map_id + 'legend' + layer_id + legendValues.colourType].appendChild(legendContent); |
| 204 | + |
| 205 | + if (isUpdating === false) { |
| 206 | + placeControl(map_id, window[map_id + 'legend' + layer_id + legendValues.colourType], legendValues.position); |
| 207 | + } |
| 208 | + |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +function clear_legend(map_id, layer_id){ |
| 213 | + |
| 214 | + // find reference to this layer in the legends |
| 215 | + var id = map_id + 'legend' + layer_id + 'fill_colour'; |
| 216 | + var objIndex = findById(window[map_id + 'legendPositions'], id, "index" ); |
| 217 | + |
| 218 | + if(objIndex != null) { |
| 219 | + removeControl(map_id, id, window[map_id + 'legendPositions'][objIndex].position); |
| 220 | + window[map_id + 'legendPositions'].splice(objIndex, 1); |
| 221 | + window[id] = null; |
| 222 | + } |
| 223 | + |
| 224 | + id = map_id + 'legend' + layer_id + 'stroke_colour'; |
| 225 | + objIndex = findById(window[map_id + 'legendPositions'], id, "index" ); |
| 226 | + |
| 227 | + if(objIndex != null) { |
| 228 | + removeControl(map_id, id, window[map_id + 'legendPositions'][objIndex].position); |
| 229 | + window[map_id + 'legendPositions'].splice(objIndex, 1); |
| 230 | + window[id] = null; |
| 231 | + } |
| 232 | +} |
| 233 | + |
0 commit comments