all files / src/layers/ LayerEsriMapServer.ts

28.57% Statements 18/63
0% Branches 0/28
11.11% Functions 1/9
27.12% Lines 16/59
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                                                                                                                                                                                                                                                                                                                   
/**
 * Created by gavorhes on 12/7/2015.
 */
import {LayerBase, LayerBaseOptions} from './LayerBase';
import * as esriToOl from '../olHelpers/esriToOlStyle';
import mapPopup from '../olHelpers/mapPopup';
import provide from '../util/provide';
import ol = require('custom-ol');
import $ = require('jquery');
 
const nm = provide('layers');
 
 
export interface LayerEsriMapServerOptions extends LayerBaseOptions {
    addPopup?: boolean;
    showLayers?: Array<number>;
}
 
/**
 * esri mapserver layer
 * @augments LayerBase
 */
export class LayerEsriMapServer extends LayerBase {
    _esriFormat: ol.format.EsriJSON;
    _popupRequest: JQueryXHR;
 
    /**
     * The base layer for all others
     * @param {string} url - resource url
     * @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.addPopup=false] if a popup should be added
     * @param {undefined|Array<number>} [options.showLayers=undefined] if a popup should be added
     */
    constructor(url, options: LayerEsriMapServerOptions = {}) {
 
        super(url, options);
        this._source = new ol.source.TileArcGISRest(
            {
                url: this.url == '' ? undefined : this.url,
                params: typeof options.showLayers == 'undefined' ? undefined : {layers: 'show:' + options.showLayers.join(',')}
            }
        );
 
        this._olLayer = new ol.layer.Tile({
            source: this._source as ol.source.Tile,
            visible: this.visible,
            opacity: this.opacity,
            minResolution: this._minResolution,
            maxResolution: this._maxResolution
        });
 
        this._olLayer.setZIndex(this._zIndex);
 
        options.addPopup = typeof options.addPopup == 'boolean' ? options.addPopup : false;
 
        this._esriFormat = new ol.format.EsriJSON();
        this._popupRequest = null;
 
        this.addLegendContent();
 
        if (options.addPopup) {
            mapPopup.addMapServicePopup(this);
        }
    }
 
    /**
     * add additional content to the legend
     * @param {string} [additionalContent=''] additional content for legend
     */
    addLegendContent(additionalContent?: string) {
        let urlCopy = this.url;
 
        if (urlCopy[urlCopy.length - 1] !== '/') {
            urlCopy += '/';
        }
 
        urlCopy += 'legend?f=pjson&callback=?';
 
        $.get(urlCopy, {}, (d) => {
            let newHtml = esriToOl.makeMapServiceLegend(d);
            super.addLegendContent(newHtml);
        }, 'json');
    }
 
 
    getPopupInfo(queryParams) {
        if (!this.visible) {
            return;
        }
 
        let urlCopy = this.url;
 
        if (urlCopy[urlCopy.length - 1] != '/') {
            urlCopy += '/';
        }
 
        urlCopy += 'identify?callback=?';
 
        let __this = this;
 
        if (this._popupRequest != null) {
            this._popupRequest.abort();
        }
 
 
        this._popupRequest = $.get(urlCopy, queryParams, function (d) {
            for (let r of d['results']) {
 
                let popupHtml = '<table class="esri-popup-table">';
 
                for (let a in r['attributes']) {
                    if (r['attributes'].hasOwnProperty(a)) {
                        let attrVal = r['attributes'][a];
 
                        if (attrVal == null || attrVal.toString().toLowerCase() == 'null') {
                            continue;
                        }
 
                        let attr = a;
                        if (attr.length > 14) {
                            attr = attr.slice(0, 11) + '...';
                        }
 
                        popupHtml += `<tr><td>${attr}</td><td>${attrVal}</td></tr>`;
                    }
                }
 
                popupHtml += '</table>';
 
                mapPopup.addMapServicePopupContent(__this._esriFormat.readFeature(r), __this, popupHtml, r['layerName']);
            }
        }, 'json');
 
        this._popupRequest.always(function () {
            __this._popupRequest = null;
        });
 
    }
 
    /**
     *
     * @returns {ol.source.TileArcGISRest} the vector source
     */
    get source(): ol.source.TileArcGISRest {
        return super.getSource() as ol.source.TileArcGISRest;
    }
 
    /**
     *
     * @returns the ol layer
     */
    get olLayer(): ol.layer.Tile {
        return super.getOlLayer() as ol.layer.Tile;
    }
}
nm.LayerEsriMapServer = LayerEsriMapServer;
export default LayerEsriMapServer;