all files / src/layers/ LayerBase.ts

60.99% Statements 86/141
39.71% Branches 27/68
31.58% Functions 12/38
60.14% Lines 83/138
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442                                                                                                                                                                                                                                                   18×                                                                                                                               27×                                                                                                                                                                                                                                                                                                                                                              
import * as zoomResolutionConvert from '../olHelpers/zoomResolutionConvert';
import provide from '../util/provide';
import ol = require('custom-ol');
import makeGuid from '../util/makeGuid';
import $ = require('jquery');
 
const nm = provide('layers');
 
 
export interface LayerBaseOptions{
    id?: string;
    name?: string;
    opacity?: number;
    visible?: boolean;
    minZoom?: number;
    maxZoom?: number;
    params?: any;
    zIndex?: number;
    loadCallback?: Function;
    legendCollapse?: boolean;
    legendCheckbox?: boolean;
    legendContent?: string;
}
 
 
/**
 * The base layer class
 * @abstract
 */
export abstract class LayerBase {
 
    protected _legendCheckbox: boolean;
    protected _url: string;
    protected _opacity: number;
    protected _minZoom: number;
    protected _maxZoom: number;
    protected _visible: boolean;
    protected _loaded: boolean;
    protected _zIndex: number;
    protected _legendContent: string;
    protected _params: any;
    protected _id: string;
    protected _name: string;
    protected _source: ol.source.Source;
    protected _animate: boolean;
    protected _legendCollapse: boolean;
    protected _maxResolution: number;
    protected _minResolution: number;
    protected  _$legendDiv: JQuery;
    loadCallback: Function;
    protected _olLayer: ol.layer.Layer;
    protected _applyCollapseCalled: boolean;
 
 
 
    /**
     * The base layer for all others
     * @param {string} url - url for source
     * @param {object} options - config
     * @param {string} [options.id=makeGuid()] - layer id
     * @param {string} [options.name=Unnamed Layer] - layer name
     * @param {number} [options.opacity=1] - opacity
     * @param {boolean} [options.visible=true] - default visible
     * @param {number} [options.minZoom=undefined] - min zoom level, 0 - 28
     * @param {number} [options.maxZoom=undefined] - max zoom level, 0 - 28
     * @param {object} [options.params={}] - the get parameters to include to retrieve the layer
     * @param {number} [options.zIndex=0] - the z index for the layer
     * @param {function} [options.loadCallback] - function to call on load, context this is the layer object
     * @param {boolean} [options.legendCollapse=false] - if the legend item should be initially collapsed
     * @param {boolean} [options.legendCheckbox=true] - if the legend item should have a checkbox for visibility
     * @param {boolean} [options.legendContent=undefined] - additional content to add to the legend
     */
    constructor(url: string, Ioptions: LayerBaseOptions = {}) {
        options = options || {} as LayerBaseOptions;
 
        Iif (typeof url !== 'string') {
            throw 'Invalid URL';
        }
        this._url = url;
 
 
        this._params = typeof options.params == 'object' ? options.params : {};
        this._legendCollapse = typeof options.legendCollapse == 'boolean' ? options.legendCollapse : false;
        this._legendCheckbox = typeof options.legendCheckbox == 'boolean' ? options.legendCheckbox : true;
 
        this.id = options.id || makeGuid();
        this._name = options.name || 'Unnamed Layer';
        this.animate = false;
        this._opacity = typeof options.opacity == 'number' ? options.opacity : 1;
 
        Iif (this._opacity > 1) {
            this._opacity = 1;
        } else Iif (this._opacity < 0) {
            this._opacity = 0;
        }
 
        this._visible = typeof options.visible === 'boolean' ? options.visible : true;
 
        this._source = undefined;
 
        /**
         *
         * @protected
         */
        this._olLayer = undefined;
        this._loaded = false;
 
        this._maxResolution = zoomResolutionConvert.zoomToResolution(options.minZoom);
        Iif (typeof this._maxResolution !== 'undefined') {
            this._maxResolution += 0.00001;
        }
        this._minResolution = zoomResolutionConvert.zoomToResolution(options.maxZoom);
 
        this._minZoom = typeof options.minZoom == 'number' ? options.minZoom : undefined;
        this._maxZoom = typeof options.maxZoom == 'number' ? options.maxZoom : undefined;
        this._zIndex = typeof options.zIndex == 'number' ? options.zIndex : 0;
 
        this.loadCallback = typeof options.loadCallback == 'function' ? options.loadCallback : function () {
        };
 
 
        this._legendContent = '';
 
        Eif (this._legendCheckbox) {
            this._legendContent += `<input type="checkbox" ${this.visible ? 'checked' : ''} ` +
                `class="legend-check" id="${this.id}-legend-layer-check"><span></span>`;
            this._legendContent += `<label for="${this.id}-legend-layer-check" class="legend-layer-name">${this.name}</label>`;
        } else {
            this._legendContent += `<label class="legend-layer-name">${this.name}</label>`;
        }
 
        this._$legendDiv = null;
        this._applyCollapseCalled = false;
        this._addLegendContent(typeof options.legendContent === 'string' ? options.legendContent : undefined);
    }
 
    /**
     * base load function, sets _loaded = true if it is not already
     * @protected
     * @returns {boolean} if already loaded
     */
    _load() {
        if (this.loaded == true) {
            return true;
        } else {
            this._loaded = true;
 
            return false;
        }
    }
 
    /**
     * Get the legend html, be sure to only add to the DOM once
     * @returns {string} html for layer wrapped in a div
     */
    getLegendDiv() {
        return `<div class="legend-layer-div" id="${this.id}-legend-layer-div">${this._legendContent}</div>`;
    }
 
    /**
     *
     * @param additionalContent - additional content to add to legend
     * @private
     */
    _addLegendContent(EadditionalContent='') {
 
        let addCollapse = additionalContent.indexOf('<ul>') > -1;
 
        Iif (addCollapse) {
            additionalContent = '<span class="legend-items-expander" title="Expand/Collapse">&#9660;</span>' + additionalContent;
        }
 
        this._legendContent += additionalContent;
 
        this._$legendDiv = $(`#${this.id}-legend-layer-div`);
 
        Iif (this._$legendDiv.length > 0) {
            this._$legendDiv.append(additionalContent);
            this.applyCollapse();
        }
    }
 
    /**
     * add additional content to the legend
     * @param {string} [additionalContent=] - additonal content to add
     */
    addLegendContent(additionalContent) {
        this._addLegendContent(additionalContent);
    }
 
    applyCollapse() {
        if (this._applyCollapseCalled) {
            console.log('collapse already applied');
 
            return undefined;
        }
 
        this._$legendDiv = $(`#${this.id}-legend-layer-div`);
 
        if (this._$legendDiv.length > 0) {
 
            let $expander = this._$legendDiv.find('.legend-items-expander');
 
            if ($expander.length > 0) {
                this._applyCollapseCalled = true;
 
                $expander.click(function () {
                    let $this = $(this);
 
                    $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;');
                    }
                });
 
                if (this._legendCollapse) {
                    $expander.trigger('click');
                }
            }
        }
    }
 
    /**
     * trick to refresh the layer
     */
    refresh() {
        if (this.source) {
            this.source.refresh();
        }
    }
 
    get id(): string{
        return this._id;
    }
 
    set id(newId: string){
        this._id = newId;
    }
 
    get animate(): boolean{
        return this._animate;
    }
 
    set animate(animate: boolean){
        this._animate = animate;
    }
 
    /**
     * get the legend content
     * @type {string}
     */
    get legendContent() {
        return this._legendContent;
    }
 
    /**
     * set the legend content directly
     * @param {string} newVal - new content
     * @protected
     */
    set legendContent(newVal) {
        this._legendContent = newVal;
    }
 
    /**
     * get the map get params
     * @type {object}
     */
    get params() {
        return this._params;
    }
 
    /**
     * set the map get params
     * @param {object} newParams - new get params
     * @protected
     */
    set params(newParams) {
        this._params = newParams;
    }
 
    /**
     * get the minimum resolution
     * @type {number|*}
     */
    get minResolution() {
        return this._minResolution;
    }
 
    /**
     * get the maximum resolution
     * @type {number|*}
     */
    get maxResolution() {
        return this._maxResolution;
    }
 
    /**
     * get min zoom
     * @type {number|*}
     */
    get minZoom() {
        return this._minZoom;
    }
 
    /**
     * get max zoom
     * @type {number|*}
     */
    get maxZoom() {
        return this._maxZoom;
    }
 
    /**
     * get the url
     * @type {string}
     */
    get url() {
        return this._url;
    }
 
    /**
     * Get the layer visibility
     * @type {boolean}
     */
    get visible(): boolean {
        return this._visible;
    }
 
    /**
     * set the visibility
     * @param visibility
     */
    set visible(visibility: boolean) {
        this.setVisible(visibility);
    }
 
    protected setVisible(visibility: boolean) {
        this._visible = visibility;
        if (this.olLayer) {
            this.olLayer.setVisible(this._visible);
            if (visibility && !this._loaded) {
                this._load();
            }
        }
    }
 
 
    /**
     * Get the layer opacity
     * @type {number}
     */
    get opacity() {
        return this._opacity;
    }
 
    /**
     * Set the layer opacity
     * @param {number} opacity - layer opacity
     */
    set opacity(opacity) {
        this._opacity = opacity;
        if (this.olLayer) {
            this.olLayer.setOpacity(this._opacity);
        }
    }
 
    /**
     * Get the layer name
     * @type {string}
     */
    get name() {
        return this._name;
    }
 
    /**
     * set the layer name
     * @param {string} newName - the new name
     */
    set name(newName) {
        this._name = newName;
    }
 
    /**
     * Check if the layer is loaded
     * @type {boolean}
     */
    get loaded() {
        return this._loaded;
    }
 
    /**
     * get the layer source
     * @type {*}
     */
    get source(): ol.source.Source {
        return this.getSource();
    }
 
 
    protected getSource(): ol.source.Source{
        return this._source;
    }
 
    /**
     * get the z index
     */
    get zIndex(): number {
        return this._zIndex;
    }
 
    /**
     * set the z index
     */
    set zIndex(newZ: number) {
        this._zIndex = newZ;
    }
 
    protected setZIndex(newZ: number){
 
    }
 
    /**
     * the the ol layer
     */
    get olLayer(): ol.layer.Layer {
        return this.getOlLayer();
    }
 
    protected getOlLayer(): ol.layer.Layer{
        return this._olLayer;
    }
}
 
nm.LayerBase = LayerBase;
export default LayerBase;