all files / js/ map.js

98.9% Statements 90/91
90% Branches 9/10
95.24% Functions 20/21
98.9% Lines 90/91
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                                                                                                                                                                  
var $ = require('jquery');
var FilterForm = require('./filterui');
var QuerybuilderAPI = require('./querybuilderapi');
var L = require('leaflet');
 
 
/** AJAX map widget.
 * @constructor
 * @param {string} mapData - JSON string representing map parameters
 * @param {string} mapData.name - widget name used as container ID and
    endpoint parameter
 * @param {string} mapData.endpoint - URL of the API endpoint
 * @param {string} mapData.params - manually defined string that can be passed
    to the endpoint
 * @param {string} mapData.filter - optional filter ID
 */
var Map = (function() {
    'use strict';
    var Map = function(mapData) {
        mapData = JSON.parse(mapData);
        this.arrayMarkers = [];
        this.map = null;
        this.layerData = null;
        this.formID = mapData.filter ? '#' + mapData.filter : null;
        this.mapWidgetId = mapData.name;
        this.endpoint = mapData.endpoint;
        this.params = mapData.params;
        var _this = this;
        new FilterForm(this.formID);
 
        setUpdateMapEventHandling('#' + _this.mapWidgetId);
        _this.init();
 
        function setUpdateMapEventHandling(widgetId) {
            $(widgetId).on('update:Map', updateMap);
        }
 
        function updateMap(event, filterData) {
            removeAllLayers();
            addMarkers(filterData);
            zoomLeafletViewport();
        }
 
        function zoomLeafletViewport() {
            if (_this.arrayMarkers.length > 0) {
                var group = L.featureGroup(_this.arrayMarkers);
                _this.map.fitBounds(group.getBounds());
            }
        }
 
        function addMarkers(filterData) {
            if (filterData) {
                $.each(filterData.data, addSingleMarker);
            }
        }
 
        function addSingleMarker(_, marker) {
            _this.addMarker(marker, _this);
        }
 
        function removeAllLayers() {
            $.each(_this.arrayMarkers, removeLayer);
            _this.arrayMarkers = [];
        }
 
        function removeLayer(num, layer) {
            _this.map.removeLayer(layer);
            _this.layerData.removeLayer(layer);
        }
    };
 
    var MapLinker = function(formID, endpointName, api, params, triggerMap,
                             mapWidgetId) {
        this.endpointName = endpointName;
        this.mapWidgetId = mapWidgetId;
        this.api = api;
        var _this = this;
        getData(formID);
 
        function getData(formID) {
            if (formID) {
                _this.form = $(formID).data('FilterForm');
                _this.form.onSubmit(getFilteredData);
            } else {
                getDefaultData();
            }
        }
 
        function getDefaultData() {
            var queryConfig = {containerID: mapWidgetId};
            _this.retrieveData(queryConfig, params, triggerMap);
        }
 
        function getFilteredData(event) {
            event.preventDefault();
            event.stopPropagation();
            var queryConfig = _this.form.serialize();
            _this.retrieveData(queryConfig, params, triggerMap);
        }
    };
 
    MapLinker.prototype = {
        retrieveData: function(queryConfig, params, callback) {
            return this.api.retrieveData(this.mapWidgetId,
                                         queryConfig, params, callback);
        }
    };
 
    function triggerMapFactory (containerID) {
        var container = containerID ? containerID : 'filter';
        return function (filteredData) {
            $('#' + container).trigger("update:Map", [filteredData]);
        };
    }
 
    function getLeafletConfig(osm, layerData) {
        return {
            center: [0.01, 51.405],
            zoom: 13,
            fullscreenControl: true,
            fullscreenOptions: {
                position: 'topleft'
            },
            layers: [osm, layerData]
        };
    }
 
    function getOsmTileLayer() {
        var osmURL = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
        var osmAttrib = '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors';
        return new L.TileLayer(osmURL, {attribution: osmAttrib});
    }
 
    function getLeafletMap(widgetId, layerData) {
        var osm = getOsmTileLayer();
        var map = L.map(widgetId, getLeafletConfig(osm, layerData));
        var baseLayers = { "OpenStreetMap": osm };
        var overlays = { "Data Layer": layerData };
        L.control.layers(baseLayers, overlays).addTo(map);
        return map;
    }
 
    Map.prototype = {
        init: function() {
            this.triggerMap = triggerMapFactory(this.mapWidgetId);
            this.map_class = new MapLinker(this.formID, this.endpoint,
                                           new QuerybuilderAPI(this.endpoint),
                                           this.params, this.triggerMap,
                                           this.mapWidgetId);
            this.layerData = new L.LayerGroup();
            this.arrayMarkers = [];
            this.map = getLeafletMap(this.mapWidgetId, this.layerData);
            this.map_class.retrieveData({containerID: this.mapWidgetId},
                                        this.params, this.triggerMap);
        },
 
        addMarker: function(obj, _this) {
            var marker = L.marker({"lat": obj.latitude, "lng": obj.longitude});
            var popupText = obj.description;
 
            marker.bindPopup(popupText);
            marker.addTo(_this.layerData);
            _this.arrayMarkers.push(marker);
        }
    };
 
    return Map;
})();
 
module.exports = Map;