all files / mapbox-gl-draw/src/ setup.js

91.84% Statements 45/49
62.5% Branches 5/8
87.5% Functions 7/8
93.62% Lines 44/47
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     17×   17× 17× 17× 17×   17×   17×           11× 11× 11×   11×   11× 11×     11× 11×     11× 11× 11×               11×       11×                 11×               11× 374×     11×     136×           17×   17×    
const events = require('./events');
const Store = require('./store');
const ui = require('./ui');
const Constants = require('./constants');
 
module.exports = function(ctx) {
 
  ctx.events = events(ctx);
 
  ctx.map = null;
  ctx.container = null;
  ctx.store = null;
  ctx.ui = ui(ctx);
 
  let controlContainer = null;
 
  const setup = {
    onRemove: function() {
      setup.removeLayers();
      ctx.ui.removeButtons();
      ctx.events.removeEventListeners();
      ctx.map = null;
      ctx.container = null;
      ctx.store = null;
 
      Iif (controlContainer && controlContainer.parentNode) controlContainer.parentNode.removeChild(controlContainer);
      controlContainer = null;
 
      return this;
    },
    onAdd: function(map) {
      ctx.map = map;
      ctx.container = map.getContainer();
      ctx.store = new Store(ctx);
 
      controlContainer = ctx.ui.addButtons();
 
      if (ctx.options.boxSelect) E{
        map.boxZoom.disable();
        // Need to toggle dragPan on and off or else first
        // dragPan disable attempt in simple_select doesn't work
        map.dragPan.disable();
        map.dragPan.enable();
      }
 
      if (map.loaded()) {
        setup.addLayers();
        ctx.events.addEventListeners();
      } else E{
        map.on('load', () => {
          setup.addLayers();
          ctx.events.addEventListeners();
        });
      }
 
      return controlContainer;
    },
    addLayers: function() {
      // drawn features style
      ctx.map.addSource(Constants.sources.COLD, {
        data: {
          type: Constants.geojsonTypes.FEATURE_COLLECTION,
          features: []
        },
        type: 'geojson'
      });
 
      // hot features style
      ctx.map.addSource(Constants.sources.HOT, {
        data: {
          type: Constants.geojsonTypes.FEATURE_COLLECTION,
          features: []
        },
        type: 'geojson'
      });
 
      ctx.options.styles.forEach(style => {
        ctx.map.addLayer(style);
      });
 
      ctx.store.render();
    },
    removeLayers: function() {
      ctx.options.styles.forEach(style => {
        ctx.map.removeLayer(style.id);
      });
 
      ctx.map.removeSource(Constants.sources.COLD);
      ctx.map.removeSource(Constants.sources.HOT);
    }
  };
 
  ctx.setup = setup;
 
  return setup;
};