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

88.89% Statements 104/117
75.93% Branches 41/54
83.33% Functions 20/24
89.42% Lines 93/104
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                     17× 17× 17× 17×   17× 116×       43×   73× 73×       17× 254× 116×   138× 138× 138×     17× 155×       155× 155× 155×     17× 157× 157×   157×       136×   21×       17×         24×   17×   10×         17× 15× 15×       17×       17×                       48× 142×   142× 142×     142× 142× 141×   141× 48×     141× 141×     17×             260× 260× 780× 780× 780×   260×     17×       569×     136×               11× 11× 11× 11×   11×   11× 11× 11×                             149×       17×    
const setupModeHandler = require('./lib/mode_handler');
const getFeaturesAndSetCursor = require('./lib/get_features_and_set_cursor');
const isClick = require('./lib/is_click');
const Constants = require('./constants');
 
const modes = {
  [Constants.modes.SIMPLE_SELECT]: require('./modes/simple_select'),
  [Constants.modes.DIRECT_SELECT]: require('./modes/direct_select'),
  [Constants.modes.DRAW_POINT]: require('./modes/draw_point'),
  [Constants.modes.DRAW_LINE_STRING]: require('./modes/draw_line_string'),
  [Constants.modes.DRAW_POLYGON]: require('./modes/draw_polygon'),
  [Constants.modes.STATIC]: require('./modes/static')
};
 
module.exports = function(ctx) {
 
  let mouseDownInfo = {};
  const events = {};
  let currentModeName = Constants.modes.SIMPLE_SELECT;
  let currentMode = setupModeHandler(modes.simple_select(ctx), ctx);
 
  events.drag = function(event) {
    if (isClick(mouseDownInfo, {
      point: event.point,
      time: new Date().getTime()
    })) {
      event.originalEvent.stopPropagation();
    } else {
      ctx.ui.queueMapClasses({ mouse: Constants.cursors.DRAG });
      currentMode.drag(event);
    }
  };
 
  events.mousemove = function(event) {
    if (event.originalEvent.which === 1) {
      return events.drag(event);
    }
    const target = getFeaturesAndSetCursor(event, ctx);
    event.featureTarget = target;
    currentMode.mousemove(event);
  };
 
  events.mousedown = function(event) {
    mouseDownInfo = {
      time: new Date().getTime(),
      point: event.point
    };
    const target = getFeaturesAndSetCursor(event, ctx);
    event.featureTarget = target;
    currentMode.mousedown(event);
  };
 
  events.mouseup = function(event) {
    const target = getFeaturesAndSetCursor(event, ctx);
    event.featureTarget = target;
 
    if (isClick(mouseDownInfo, {
      point: event.point,
      time: new Date().getTime()
    })) {
      currentMode.click(event);
    } else {
      currentMode.mouseup(event);
    }
  };
 
  events.mouseout = function(event) {
    currentMode.mouseout(event);
  };
 
  // 8 - Backspace
  // 46 - Delete
  const isKeyModeValid = (code) => !(code === 8 || code === 46 || (code >= 48 && code <= 57));
 
  events.keydown = function(event) {
 
    if ((event.keyCode === 8 || event.keyCode === 46) && ctx.options.controls.trash) {
      event.preventDefault();
      currentMode.trash();
    } else if (isKeyModeValid(event.keyCode)) I{
      currentMode.keydown(event);
    } else if (event.keyCode === 49 && ctx.options.controls.point) {
      changeMode(Constants.modes.DRAW_POINT);
    } else if (event.keyCode === 50 && ctx.options.controls.line_string) {
      changeMode(Constants.modes.DRAW_LINE_STRING);
    } else if (event.keyCode === 51 && ctx.options.controls.polygon) E{
      changeMode(Constants.modes.DRAW_POLYGON);
    }
  };
 
  events.keyup = function(event) {
    if (isKeyModeValid(event.keyCode)) E{
      currentMode.keyup(event);
    }
  };
 
  events.zoomend = function() {
    ctx.store.changeZoom();
  };
 
  events.data = function(event) {
    if (event.dataType === 'style') {
      const { setup, map, options, store } = ctx;
      const hasLayers = !!options.styles.find(style => map.getLayer(style.id));
      if (!hasLayers) {
        setup.addLayers();
        store.setDirty();
        store.render();
      }
    }
  };
 
  function changeMode(modename, nextModeOptions, eventOptions = {}) {
    currentMode.stop();
 
    const modebuilder = modes[modename];
    if (modebuilder === undefined) I{
      throw new Error(`${modename} is not valid`);
    }
    currentModeName = modename;
    const mode = modebuilder(ctx, nextModeOptions);
    currentMode = setupModeHandler(mode, ctx);
 
    if (!eventOptions.silent) {
      ctx.map.fire(Constants.events.MODE_CHANGE, { mode: modename});
    }
 
    ctx.store.setDirty();
    ctx.store.render();
  }
 
  const actionState = {
    trash: false,
    combineFeatures: false,
    uncombineFeatures: false
  };
 
  function actionable(actions) {
    let changed = false;
    Object.keys(actions).forEach(action => {
      Iif (actionState[action] === undefined) throw new Error('Invalid action type');
      if (actionState[action] !== actions[action]) changed = true;
      actionState[action] = actions[action];
    });
    if (changed) ctx.map.fire(Constants.events.ACTIONABLE, { actions: actionState });
  }
 
  const api = {
    changeMode,
    actionable,
    currentModeName: function() {
      return currentModeName;
    },
    currentModeRender: function(geojson, push) {
      return currentMode.render(geojson, push);
    },
    fire: function(name, event) {
      if (events[name]) {
        events[name](event);
      }
    },
    addEventListeners: function() {
      ctx.map.on('mousemove', events.mousemove);
      ctx.map.on('mousedown', events.mousedown);
      ctx.map.on('mouseup', events.mouseup);
      ctx.map.on('data', events.data);
 
      ctx.container.addEventListener('mouseout', events.mouseout);
 
      if (ctx.options.keybindings) E{
        ctx.container.addEventListener('keydown', events.keydown);
        ctx.container.addEventListener('keyup', events.keyup);
      }
    },
    removeEventListeners: function() {
      ctx.map.off('mousemove', events.mousemove);
      ctx.map.off('mousedown', events.mousedown);
      ctx.map.off('mouseup', events.mouseup);
      ctx.map.off('data', events.data);
 
      ctx.container.removeEventListener('mouseout', events.mouseout);
 
      if (ctx.options.keybindings) E{
        ctx.container.removeEventListener('keydown', events.keydown);
        ctx.container.removeEventListener('keyup', events.keyup);
      }
    },
    trash: function(options) {
      currentMode.trash(options);
    },
    combineFeatures: function() {
      currentMode.combineFeatures();
    },
    uncombineFeatures: function() {
      currentMode.uncombineFeatures();
    },
    getMode: function() {
      return currentModeName;
    }
  };
 
  return api;
};