all files / mapbox-gl-draw/src/feature_types/ feature.js

100% Statements 24/24
100% Branches 4/4
100% Functions 7/7
100% Lines 22/22
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   320× 320× 320× 320× 320×     392×     43×     50× 50×     335×     334×                     137×                                
const hat = require('hat');
const Constants = require('../constants');
 
const Feature = function(ctx, geojson) {
  this.ctx = ctx;
  this.properties = geojson.properties || {};
  this.coordinates = geojson.geometry.coordinates;
  this.id = geojson.id || hat();
  this.type = geojson.geometry.type;
};
 
Feature.prototype.changed = function() {
  this.ctx.store.featureChanged(this.id);
};
 
Feature.prototype.incomingCoords = function(coords) {
  this.setCoordinates(coords);
};
 
Feature.prototype.setCoordinates = function(coords) {
  this.coordinates = coords;
  this.changed();
};
 
Feature.prototype.getCoordinates = function() {
  return JSON.parse(JSON.stringify(this.coordinates));
};
 
Feature.prototype.toGeoJSON = function() {
  return JSON.parse(JSON.stringify({
    id: this.id,
    type: Constants.geojsonTypes.FEATURE,
    properties: this.properties,
    geometry: {
      coordinates: this.getCoordinates(),
      type: this.type
    }
  }));
};
 
Feature.prototype.internal = function(mode) {
  return {
    type: Constants.geojsonTypes.FEATURE,
    properties: {
      id: this.id,
      meta: Constants.meta.FEATURE,
      'meta:type': this.type,
      active: Constants.activeStates.INACTIVE,
      mode: mode
    },
    geometry: {
      coordinates: this.getCoordinates(),
      type: this.type
    }
  };
};
 
module.exports = Feature;