all files / src/collections/ LayerLegend.ts

11.46% Statements 18/157
0% Branches 0/60
20% Functions 3/15
11.69% Lines 18/154
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * Created by gavorhes on 12/16/2015.
 */
 
import provide from '../util/provide';
import makeGuid from '../util/makeGuid';
import mapMove from '../olHelpers/mapMove';
 
let nm = provide('collections');
import $ = require('jquery');
 
class LayerGroup {
    groupLayers: any;
    groupLayersLookup: any;
    groupGroupsLookup: any;
    groupGroups: any;
    itemIdArray: any;
    groupId: any;
    groupName: any;
    allLayerArray: any;
    parent: any;
    allGroupArray: any;
    allGroupLookup: any;
    allLayerLookup: any;
    collapse: any;
    addCheck: any;
    layerParentLookup: any;
 
    /**
     *
     * @param {object} [groupConfig={}] - group configuration object
     * @param {string} groupConfig.groupName - the group name
     * @param {boolean} [groupConfig.collapse=false] - if the group should be collapsed initially
     * @param {boolean} [groupConfig.addCheck=true] - if the group should have a checkbox controlling visibility of all layers
     * @param {LayerGroup} [parent=undefined] - the parent group
     */
    constructor(groupConfig?, parent?) {
        this.groupLayers = [];
        this.groupLayersLookup = {};
        this.groupGroups = [];
        this.groupGroupsLookup = {};
        this.itemIdArray = [];
 
        if (typeof groupConfig == 'undefined') {
            this.parent = null;
            this.groupId = 'root';
            this.groupName = 'root';
            this.allGroupLookup = {root: this};
            this.allGroupArray = [this];
            this.allLayerArray = [];
            this.allLayerLookup = {};
            this.layerParentLookup = {};
            this.collapse = false;
            this.addCheck = false;
        } else {
            this.groupId = makeGuid();
            this.parent = parent;
            this.groupName = groupConfig.groupName;
            this.collapse = typeof  groupConfig.collapse == 'boolean' ? groupConfig.collapse : false;
            this.addCheck = typeof  groupConfig.addCheck == 'boolean' ? groupConfig.addCheck : true;
        }
    }
 
    /**
     *
     * @param {object} groupConfig - configuration object
     * @param {string} groupConfig.groupName - the group name
     * @param {boolean} groupConfig.collapse if the group should be collapsed initially
     * @param {boolean} groupConfig.addCheck if the group should have a checkbox controlling visibility of all layers
     * @param {Array<LayerGroup>} parents parent groups
     * @returns {LayerGroup} the layer group just added
     */
    addGroup(groupConfig, parents) {
        let parent;
        if (parents.length > 0) {
            parent = parents[parents.length - 1];
        } else {
            parent = 'root';
        }
 
 
        /**
         * @type {LayerGroup}
         */
        let parentGroup = this.allGroupLookup[parent];
        let newGroup = new LayerGroup(groupConfig, parentGroup);
        this.allGroupLookup[newGroup.groupId] = newGroup;
        this.allGroupArray.push(newGroup);
 
        parentGroup.groupGroups.push(newGroup);
        parentGroup.groupGroupsLookup[newGroup.groupId] = newGroup;
 
        if (parentGroup.itemIdArray.indexOf(newGroup.groupId) > 0) {
            console.log(newGroup.groupId);
            throw 'layer and group ids must be unique';
        }
        parentGroup.itemIdArray.push(newGroup.groupId);
 
        return newGroup;
    }
 
    /**
     *
     * @param {LayerBase} newLayer the layer to be added
     * @param {Array} parents array
     */
    addLegendLayer(newLayer, parents) {
        let parent;
        if (parents.length > 0) {
            parent = parents[parents.length - 1];
        } else {
            parent = 'root';
        }
 
        this.allLayerLookup[newLayer.id] = newLayer;
        this.allLayerArray.push(newLayer);
 
        /**
         * @type {LayerGroup}
         */
        let parentGroup = this.allGroupLookup[parent];
 
        parentGroup.groupLayers.push(newLayer);
        parentGroup.groupLayersLookup[newLayer.id] = newLayer;
        if (parentGroup.itemIdArray.indexOf(newLayer.id) > 0) {
            console.log(newLayer.id);
            throw 'layer and group ids must be unique';
        }
        parentGroup.itemIdArray.push(newLayer.id);
 
        this.layerParentLookup[newLayer.id] = parentGroup;
 
    }
 
    getLegendHtml(legendId, options) {
 
 
        let legendHtml = `<ul id="${legendId}" class="legend-container">`;
 
        legendHtml += `<li>${options.legendTitle}<input type="checkbox" checked id="suppress-by-extent-${legendId}" class="suppress-by-extent">` +
            `<label title="Suppress layers not visible at this zoom level" for="suppress-by-extent-${legendId}">` +
            `<span></span>` +
            `</label></li>`;
 
        legendHtml += this._buildLegend(this.itemIdArray, this, options.layerDivClasses) + '</ul>';
 
        return legendHtml;
    }
 
    /**
     * @param {Array} itemIds the items to process
     * @param {LayerGroup} theGroup new group
     * @param {Array} [layerDivClasses=[]] optional classes to apply to the layer divs
     * @static
     * @returns {string} html string
     */
    _buildLegend(itemIds, theGroup, layerDivClasses) {
 
        if (itemIds.length == 0) {
            return '';
        }
 
        let theHml = '';
 
        let itemId = itemIds[0];
 
        if (theGroup.groupLayersLookup[itemId]) {
 
            /**
             * @type {LayerBase}
             */
            let lyr = theGroup.groupLayersLookup[itemId];
            theHml += `<li id="${lyr.id}-layer-li" class="legend-layer-li ${layerDivClasses.join(' ')}">` + lyr.getLegendDiv() + '</li>';
 
 
        } else if (theGroup.groupGroupsLookup[itemId]) {
            /**
             * type {LayerGroup}
             */
            let otherGroup = theGroup.groupGroupsLookup[itemId];
 
            theHml += `<li>`;
            theHml += `<div id="${otherGroup.groupId}-legend-layer-div" ` +
                `class="legend-layer-group  ${layerDivClasses.join(' ')}">`;
 
            if (otherGroup.addCheck) {
                theHml += `<input type="checkbox" checked id="${otherGroup.groupId}-group-chck">` +
                    `<label for="${otherGroup.groupId}-group-chck" title="Click arrow to expand or collapse">${otherGroup.groupName}</label>`;
            } else {
                theHml += `<label title="Click arrow to expand or collapse">${otherGroup.groupName}</label>`;
            }
 
            theHml += `<span title="Expand/Collapse" class="layer-group-expander`;
            theHml += `${otherGroup.collapse ? ' legend-layer-group-initial-collapse' : ''}">`;
            theHml += otherGroup.collapse ? '&#9654;' : '&#9660;';
            theHml += '</span>';
 
            //parents.push(groupId);
            theHml += '<ul>' + this._buildLegend(otherGroup.itemIdArray, otherGroup, layerDivClasses) + '</ul>';
            theHml += '</div>';
            theHml += '</li>';
        }
 
        return theHml + this._buildLegend(itemIds.slice(1), theGroup, layerDivClasses);
    }
}
 
/**
 * a wrapper to make a legend
 */
class LayerLegend {
 
    $divElement: any;
    _legendItems: any;
    layerGroup: any;
    legendId: any;
 
    /**
     *
     * @param {Array} legendItems array of layers or objects with {groupName:  {string}, collapse: {boolean}, addCheck: {boolean}, items: {Array}}
     * @param {string} divId the div where the legend should be added
     * @param {object} options for legend
     * @param {Array} [options.layerDivClasses=[]] optional array of classes to be applied to the layer legend divs for custom styling
     * @param {string} [options.legendTitle=Legend] the legend title
     * @param {boolean} [options.scaleDependent=true] if legend display is scale dependent
     */
    constructor(legendItems, divId, options) {
        for (let i of legendItems) {
            if (typeof i == 'undefined') {
                throw 'undefined item passed in array to legend constructor';
            }
        }
 
        options = options || {};
 
        options.legendTitle = typeof options.legendTitle == 'string' ? options.legendTitle : 'Legend';
        options.scaleDependent = typeof options.scaleDependent == 'boolean' ? options.scaleDependent : true;
        options.layerDivClasses = options.layerDivClasses || [];
 
        // if legend display is scale dependent, make sure the mapMove object is initialized first
        if (options.scaleDependent) {
            mapMove.checkInit();
        }
 
        this.$divElement = $('#' + divId);
 
        this._legendItems = legendItems;
 
        this.layerGroup = new LayerGroup();
 
        this._buildTree(legendItems);
 
        this.legendId = makeGuid();
 
        this.$divElement.append(this.layerGroup.getLegendHtml(this.legendId, options));
 
        for (let l of this.layerGroup.allLayerArray){
            l.applyCollapse();
        }
 
        let _this = this;
        //// if legend display is scale dependent, make sure the mapMove object is initialized first
        if (options.scaleDependent) {
            mapMove.checkInit();
 
            mapMove.addCallback(function (ext, zoom, evt) {
 
                if (typeof evt == 'undefined' || evt == 'change:resolution') {
                    for (let lyr of this.layerGroup.allLayerArray) {
                        let $lyrLi = $('#' + lyr.id + '-layer-li');
                        if (zoom > lyr.maxZoom || zoom < lyr.minZoom) {
                            $lyrLi.addClass('layer-not-visible');
                        } else {
                            $lyrLi.removeClass('layer-not-visible');
                        }
                    }
                }
            }, this, 100, true, 'legend1');
        }
 
        // <editor-fold desc="add event listeners">
 
        this.$divElement.find(".suppress-by-extent").change(function () {
            let legendLayerLis = $('.legend-layer-li');
            if (this.checked) {
                legendLayerLis.removeClass('layer-force-show');
            } else {
                legendLayerLis.addClass('layer-force-show');
            }
        });
 
 
        this.$divElement.find('.legend-check').change(function () {
            let lyrId = this.id.replace('-legend-layer-check', '');
            _this.layerGroup.allLayerLookup[lyrId].visible = this.checked;
        });
 
        this.$divElement.find('.legend-layer-group > input[type=checkbox]').change(function () {
            $(this).siblings('ul').find('input[type=checkbox]').prop('checked', this.checked).trigger('change');
        });
 
        this.$divElement.find('.layer-group-expander').click(function () {
            let $this = $(this);
            $this.removeClass('legend-layer-group-initial-collapse');
 
            $this.siblings('ul').slideToggle();
 
            if ($this.hasClass('legend-layer-group-collapsed')){
                $this.removeClass('legend-layer-group-collapsed');
                $this.html('&#9660;');
            } else {
                $this.addClass('legend-layer-group-collapsed');
                $this.html('&#9654;');
            }
        });
 
        this.$divElement.find('.legend-layer-group-initial-collapse').trigger('click');
        // </editor-fold>
    }
 
 
    /**
     * @param {Array} [legendItems=this._layerConfig] the legend items
     * @param {Array} [parents=[]] the ordered list of groups in which this item is a member
     * @private
     */
    _buildTree(legendItems, parents?) {
 
        if (legendItems.length == 0) {
            return;
        }
 
        let oneItem = legendItems[0];
 
        //reset the parent if the item is in the base array
        if (this._legendItems.indexOf(oneItem) > -1 || typeof parents == 'undefined') {
            parents = [];
        }
 
        if (typeof oneItem['groupName'] !== 'undefined') {
            let groupItem = legendItems[0];
            let newGroup = this.layerGroup.addGroup(groupItem, parents);
            parents.push(newGroup.groupId);
            this._buildTree(groupItem.items, parents);
        } else {
            /**
             * @type {LayerBase}
             */
            let layerItem = legendItems[0];
 
            this.layerGroup.addLegendLayer(layerItem, parents);
        }
 
        this._buildTree(legendItems.slice(1), parents);
    }
 
}
 
nm.LayerLegend = LayerLegend;
export default LayerLegend;