all files / src/components/map-controls/polygon/ polygon.controller.ts

13.27% Statements 13/98
0% Branches 0/31
4.35% Functions 1/23
13.27% Lines 13/98
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                                                                                                                                                                                                                                                                                                 
"use strict";
var ol = require("openlayers");
var PolygonToolController = (function () {
    function PolygonToolController($scope, $timeout, olData, rx) {
        var _this = this;
        this.olData = olData;
        this.rx = rx;
        this.isAddingToPolygon = false;
        this.myTimer = null;
        this.lastApplyTime = new Date();
        this.onPointermove = function (browserEvent) {
            if (_this.isToolActive()) {
                _this.addColour(_this.featureLayer);
                var now = new Date();
                if (+now - +_this.lastApplyTime > 20) {
                    clearTimeout(_this.myTimer);
                    _this.$scope.$apply(function () {
                        _this.lastApplyTime = new Date();
                        var source = _this.featureLayer.source;
                        var coordinates = source.geojson.object.features[0].geometry.geometries[1].coordinates;
                        coordinates = [browserEvent.coordinate];
                    });
                }
                else {
                    clearTimeout(_this.myTimer);
                    _this.myTimer = setTimeout(function () {
                        _this.$scope.$apply(function () {
                            _this.lastApplyTime = new Date();
                            var source = _this.featureLayer.source;
                            var coordinates = source.geojson.object.features[0].geometry.geometries[1].coordinates;
                            coordinates = [browserEvent.coordinate];
                        });
                    }, +now - +_this.lastApplyTime + 1);
                }
            }
        };
        this.$scope = $scope;
        this.$timeout = $timeout;
        this.transparentStyle = new ol.style.Style({
            fill: new ol.style.Fill({
                color: 'rgba(0,0,0, 0.0)'
            }),
            stroke: new ol.style.Stroke({
                color: 'rgba(0,0,0, 0.0)',
                width: 3
            }),
            image: new ol.style.Circle({
                radius: 7,
                fill: new ol.style.Fill({
                    color: 'rgba(0,0,0, 0.0)'
                }),
                stroke: new ol.style.Stroke({
                    color: 'rgba(0,0,0, 0.0)',
                    width: 3
                })
            })
        });
        this.features = new ol.Collection();
        this.featureSource = new ol.source.Vector({ features: this.features });
        this.featureOverlay = new ol.layer.Vector({
            source: this.featureSource,
            style: this.transparentStyle,
            updateWhileAnimating: true,
            updateWhileInteracting: true
        });
        olData.getMap().then(function (map) {
            _this.map = map;
        });
        this.pointermoveObservable = rx.Observable.create(function (observer) {
        });
    }
    PolygonToolController.prototype.toggle = function () {
        var _this = this;
        if (this.interaction && this.interaction.getActive()) {
            this.deactivate();
        }
        else {
            this.olData.getMap().then(function (newMap) {
                _this.map = newMap;
                _this.activate();
            });
        }
    };
    PolygonToolController.prototype.activate = function () {
        var _this = this;
        this.removeColour(this.featureLayer);
        this.featureOverlay.setMap(this.map);
        var draw = new ol.interaction.Draw({
            features: this.features,
            type: 'Polygon',
            style: this.transparentStyle
        });
        this.map.on('pointermove', this.onPointermove);
        draw.on('drawend', function () {
            _this.isAddingToPolygon = false;
            _this.map.on('pointermove', _this.onPointermove);
            _this.$timeout(function () {
                _this.deactivate();
            });
            _this.$scope.$apply(function () {
                _this.removeColour(_this.featureLayer);
            });
        });
        draw.on('drawstart', function (drawEvent) {
            _this.isAddingToPolygon = true;
            _this.map.un('pointermove', _this.onPointermove);
            drawEvent.feature.getGeometry().on('change', function (changeEvent) {
                var coords = changeEvent.target.getCoordinates();
                _this.addColour(_this.featureLayer);
                _this.$scope.$apply(function () {
                    _this.featureLayer.source.geojson.object.features[0].geometry.geometries[0].coordinates = coords;
                    _this.featureLayer.source.geojson.object.features[0].geometry.geometries[1].coordinates = coords[0];
                });
            });
        });
        this.interaction = draw;
        this.map.addInteraction(draw);
    };
    PolygonToolController.prototype.removeColour = function (layer) {
        layer = layer || {};
        layer.style = layer.style || {};
        layer.style.image = layer.style.image || {};
        layer.style.image.circle.fill = layer.style.image.circle.fill || {};
        layer.style.image.circle.stroke = layer.style.image.circle.stroke || {};
        layer.style.image.circle.fill.color = 'rgba(0,0,0,0)';
        layer.style.image.circle.stroke.color = 'rgba(0,0,0,0)';
    };
    PolygonToolController.prototype.addColour = function (layer) {
        layer = layer || {};
        layer.style = layer.style || {};
        layer.style.image = layer.style.image || {};
        layer.style.image.circle.fill = layer.style.image.circle.fill || {};
        layer.style.image.circle.stroke = layer.style.image.circle.stroke || {};
        layer.style.image.circle.fill.color = '#D40058';
        layer.style.image.circle.stroke.color = '#FFFFFF';
    };
    PolygonToolController.prototype.deactivate = function () {
        this.map.removeInteraction(this.interaction);
        this.interaction = null;
        this.featureOverlay.setMap(null);
        this.map.un('pointermove', this.onPointermove);
        this.isActive = false;
    };
    PolygonToolController.prototype.isToolActive = function () {
        return (this.interaction && this.interaction.getActive()) || false;
    };
    PolygonToolController.prototype.getOpenlayersLayer = function (map, layerName) {
        return map.getLayers().getArray().filter(function (layer) {
            var layerProps = layer.getProperties();
            return layerProps.name === layerName;
        })[0];
    };
    PolygonToolController.$inject = ['$scope', '$timeout', 'olData', 'rx'];
    return PolygonToolController;
}());
exports.PolygonToolController = PolygonToolController;