/*global L:true, ClipperLib:true */ import ClipperLib from 'clipper-lib/clipper.js' L.Clipper = L.Evented.extend({ options: { //featureGroup: new L.FeatureGroup(), selectedPathOptions: { color: '#FF3399' } }, initialize: function (map, options) { L.Handler.prototype.initialize.call(this, map); L.Util.setOptions(this, options); if (!(this.options.featureGroup instanceof L.FeatureGroup)) { throw new Error('options.featureGroup must be a L.FeatureGroup'); } }, enable: function () { if (this._enabled || !this._hasAvailableLayers()) { return; } this.fire('enabled', {handler: this.type}); // this disable other handlers this._map.fire('draw:joinstart', { handler: this.type }); // allow drawLayer to be updated before beginning edition. L.Handler.prototype.enable.call(this); this.options.featureGroup .on('layeradd', this._enableLayerEdit, this) .on('layerremove', this._disableLayerEdit, this); }, disable: function () { if (!this._enabled) { return; } this.options.featureGroup .off('layeradd', this._enableLayerJoin, this) .off('layerremove', this._disableLayerJoin, this); L.Handler.prototype.disable.call(this); this._map.fire('draw:editstop', { handler: this.type }); this.fire('disabled', {handler: this.type}); }, addHooks: function () { var map = this._map; if (map) { map.getContainer().focus(); this.options.featureGroup.eachLayer(this._enableLayerEdit, this); // Leaflet.draw specific //this._tooltip = new L.tooltip(this._map); //this._tooltip.updateContent(this._getTooltipText()); // this._map.on('mousemove', this._onMouseMove, this); } }, removeHooks: function () { if (this._map) { // Clean up selected layers. this.options.featureGroup.eachLayer(this._disableLayerEdit, this); // Clear the backups of the original layers this._uneditedLayerProps = {}; this._tooltip.dispose(); this._tooltip = null; this._map.off('mousemove', this._onMouseMove, this); } }, revertLayers: function () { this.options.featureGroup.eachLayer(function (layer) { this._revertLayer(layer); }, this); }, save: function () { var editedLayers = new L.LayerGroup(); this.options.featureGroup.eachLayer(function (layer) { if (layer.edited) { editedLayers.addLayer(layer); layer.edited = false; } }); this._map.fire('draw:edited', {layers: editedLayers}); }, _revertLayer: function (layer) { var id = L.Util.stamp(layer); layer.edited = false; if (this._uneditedLayerProps.hasOwnProperty(id)) { // Polyline, Polygon or Rectangle if (layer instanceof L.Polyline || layer instanceof L.Polygon || layer instanceof L.Rectangle) { layer.setLatLngs(this._uneditedLayerProps[id].latlngs); } else if (layer instanceof L.Circle) { layer.setLatLng(this._uneditedLayerProps[id].latlng); layer.setRadius(this._uneditedLayerProps[id].radius); } else if (layer instanceof L.Marker) { // Marker layer.setLatLng(this._uneditedLayerProps[id].latlng); } } }, _enableLayerEdit: function (e) { var layer = e.layer || e.target || e; layer.on('click', this._subjectLayer, this); }, _disableLayerEdit: function (e) { var layer = e.layer || e.target || e; layer.off('click'); }, // Only supported in Leaflet.draw /*_getTooltipText: function () { var labelText; if (!this._subject) { labelText = { text: 'Choose a subject layer.' }; } else { labelText = { text: 'Choose a clipper layer.' }; } return labelText; },*/ _onMouseMove: function (e) { this._tooltip.updatePosition(e.latlng); }, _hasAvailableLayers: function () { return this.options.featureGroup.getLayers().length !== 0; }, _subjectLayer: function(e) { var layer = e.layer || e.target || e; if (!this._subject) { this._subject = layer; } else { this._clipperLayer(e); } layer.setStyle(this.options.selectedPathOptions); }, _clipperLayer: function(e) { var layer = e.layer || e.target || e; this._clipper = layer; layer.setStyle(this.options.selectedPathOptions); this._layerClip(); }, _layerClip: function() { var subjCoords = this._subject.getLatLngs(); var clipCoords = this._clipper.getLatLngs(); var subj = this._coordsToPoints(subjCoords); var clip = this._coordsToPoints(clipCoords); var solution = ClipperLib.Paths(); var cpr = new ClipperLib.Clipper(); for(var s = 0, slen = subj.length; s