all files / src/layers/ LayerBaseVector.ts

58.7% Statements 54/92
29.55% Branches 13/44
36.36% Functions 8/22
57.3% Lines 51/89
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                                                                                                                                                   10×                                                                                                                                                                                                                                                                                                                             27×                                                                                  
import {LayerBase, LayerBaseOptions} from './LayerBase';
import mapMove from '../olHelpers/mapMove';
import MapMoveCls from '../olHelpers/mapMoveCls'
import provide from '../util/provide';
import ol = require('custom-ol');
import $ = require('jquery');
 
const nm = provide('layers');
 
export interface makeMapMoveParams {
    /**
     *
     * @param lyr
     * @param extent
     * @param zoomLevel
     */
    (lyr: LayerBaseVector, extent: Array<number>, zoomLevel?: number): any
}
 
 
 
 
export interface LayerBaseVectorOptions extends LayerBaseOptions{
    autoLoad?: boolean;
    style?: ol.style.Style|Array<ol.style.Style>|ol.StyleFunction;
    onDemand?: boolean;
    onDemandDelay?: number;
    mapMoveMakeGetParams?: makeMapMoveParams;
    mapMoveObj?: MapMoveCls;
    renderOrder?: (a: ol.Feature, b: ol.Feature) => number;
 
}
 
 
 
/**
 * The Vector layer base
 * @augments LayerBase
 * @abstract
 */
export class LayerBaseVector extends LayerBase {
    _olLayer: ol.layer.Vector;
    _source: ol.source.Vector;
    _style: ol.style.Style|Array<ol.style.Style>|ol.StyleFunction;
    _autoLoad: boolean;
    _onDemand: boolean;
    _onDemandDelay: number;
    _mapMoveMakeGetParams: makeMapMoveParams;
    _mapMoveParams: any;
    _mapMove: MapMoveCls;
    _projectionMap: ol.proj.Projection;
    _projection4326: ol.proj.Projection;
 
 
 
    /**
     * The base vector layer
     * @param {string} url - pass an empty string to prevent default load and add from a json source
     * @param {object} options - config
     * @param {string} [options.id] - 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] additional content to add to the legend
     *
     * @param {boolean} [options.autoLoad=false] if the layer should auto load if not visible
     * @param {object} [options.style=undefined] the layer style, use openlayers default style if not defined
     * @param {boolean} [options.onDemand=false] if the layer should be loaded by extent on map move
     * @param {number} [options.onDemandDelay=300] delay before the map move callback should be called
     * @param {mapMoveMakeGetParams} [options.mapMoveMakeGetParams=function(lyr, extent, zoomLevel){}] function to create additional map move params
     * @param {MapMoveCls} [options.mapMoveObj=mapMove] alternate map move object for use with multi map pages
     *
     */
    constructor(url: string, Ioptions: LayerBaseVectorOptions = {}) {
        super(url, options);
 
        options = options as LayerBaseVectorOptions;
 
        //prevent regular load if no url has been provided
        if (this.url.trim() == '') {
            this._loaded = true;
        }
 
        this._style = typeof options.style == 'undefined' ? undefined : options.style;
 
        Eif (this.visible) {
            this._autoLoad = true;
        } else {
            this._autoLoad = (typeof options['autoLoad'] == 'boolean' ? options['autoLoad'] : false);
        }
 
        this._onDemand = typeof options.onDemand == 'boolean' ? options.onDemand : false;
        this._onDemandDelay = typeof options.onDemandDelay == 'number' ? options.onDemandDelay : 300;
 
        Iif (options.mapMoveObj) {
            this._mapMove = options.mapMoveObj;
        } else {
            this._mapMove = this._onDemand ? mapMove : undefined;
        }
 
 
        this._mapMoveMakeGetParams = typeof options.mapMoveMakeGetParams == 'function' ? options.mapMoveMakeGetParams :
            function () {return {};};
 
        Iif (this._onDemand) {
            this._loaded = true;
            this._mapMoveParams = {};
            this._mapMove.checkInit();
            this._mapMove.addVectorLayer(this);
        }
 
        this._source = new ol.source.Vector();
 
 
        this._olLayer = new ol.layer.Vector(
            {
                source: this._source,
                visible: this.visible,
                style: this.style,
                minResolution: this._minResolution,
                maxResolution: this._maxResolution,
                renderOrder: options.renderOrder
            }
        );
 
        this.olLayer.setZIndex(this._zIndex);
 
 
        this._projectionMap = null;
        this._projection4326 = new ol.proj.Projection({code: "EPSG:4326"});
    }
 
    /**
     * dummy to be overridden
     * @param {object} featureCollection - geojson or esrijson object
     */
    addFeatures(featureCollection) {
        console.log('Layer vector base addFeatures is a placeholder and does nothing');
    }
 
    /**
     * Before call to map move callback, can prevent call by returning false
     * @param {number} zoom - zoom level
     * @param {string} [evtType=undefined] undefined for initial load, otherwise one of 'change:center', 'change:resolution'
     * @returns {boolean} if the call should proceed
     */
    mapMoveBefore(zoom, evtType) {
        if (this.minZoom !== undefined) {
            if (zoom < this.minZoom) {
                return false;
            }
        }
 
        if (this.maxZoom !== undefined) {
            if (zoom > this.maxZoom) {
                return false;
            }
        }
 
        return this.visible;
    }
 
    /**
     * callback to generate the parameters passed in the get request
     * @param {object} extent - extent object
     * @param {number} extent.minX - minX
     * @param {number} extent.minY - minY
     * @param {number} extent.maxX - maxX
     * @param {number} extent.maxY - maxY
     * @param {number} zoomLevel - zoom level
     */
    mapMoveMakeGetParams(extent, zoomLevel) {
        this._mapMoveParams = {};
        $.extend(this._mapMoveParams, this.params);
        $.extend(this._mapMoveParams, this._mapMoveMakeGetParams(this, extent, zoomLevel));
    }
 
    /**
     * callback function on map move
     * @param {object} d - the json response
     */
    mapMoveCallback(d) {
        if (this.source) {
            this._source.clear();
        }
    }
 
    /**
     * clear features in the layer
     */
    clear() {
        if (this._source) {
            this._source.clear();
        }
    }
 
    /**
     * get on demand delay in miliseconds
     */
    get onDemandDelay(): number {
        return this._onDemandDelay;
    }
 
    /**
     * get if the layer is autoloaded
     */
    get autoLoad(): boolean {
        return this._autoLoad;
    }
 
    /**
     * get the style definition
     */
    get style(): ol.StyleFunction|Array<ol.style.Style>|ol.style.Style {
        return this._style;
    }
 
    /**
     * set the style
     * @param style - the style or function
     */
    set style(style: ol.StyleFunction|Array<ol.style.Style>|ol.style.Style) {
        this._style = style;
        this.olLayer.setStyle(this._style as ol.style.Style);
    }
 
    /**
     * get the map CRS if it is defined by the map move object
     */
    get mapCrs(): string {
        return this.mapProj == null ? null : this.mapProj.getCode();
    }
 
    get mapProj(): ol.proj.Projection{
        if (this._projectionMap != null){
            return this._projectionMap;
        }
 
        if (this._mapMove) {
            this._projectionMap = this._mapMove.map.getView().getProjection();
            return this._projectionMap;
        } else {
            return null;
        }
 
    }
 
    /**
     * get the map move object
     * @type {MapMoveCls|*}
     */
    get mapMove(): MapMoveCls {
        return this._mapMove;
    }
 
    /**
     * map move params
     * @type {object}
     */
    get mapMoveParams() {
        return this._mapMoveParams;
    }
 
    get visible(): boolean{
        return this._visible;
    }
 
    /**
     * Set the layer visibility
     * @type {boolean}
     * @override
     */
    set visible(visibility) {
        super.setVisible(visibility);
 
        if (this._onDemand) {
            this.mapMove.triggerLyrLoad(this);
        }
    }
 
    /**
     * get the layer vector source
     * @override
     */
    get source(): ol.source.Vector {
        return this.getSource() as ol.source.Vector;
    }
 
    /**
     * array of ol features
     */
    get features(): Array<ol.Feature> {
        return this.source.getFeatures();
    }
 
    /**
     *
     */
    get olLayer(): ol.layer.Vector {
        return super.getOlLayer() as ol.layer.Vector;
    }
 
    protected setZIndex(newZ: number) {
        this.olLayer.setZIndex(newZ);
    }
}
 
nm.LayerBaseVector = LayerBaseVector;
export default LayerBaseVector;