all files / mapbox-gl-draw/src/lib/ create_control_button.js

50% Statements 9/18
25% Branches 1/4
50% Functions 1/2
52.94% Lines 9/17
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                         20× 20×   20×   20×   20×                               20×      
const domUtils = require('./dom_utils');
const Constants = require('../constants');
 
/**
 * Creates a control button; adds click listeners to it; and returns it.
 *
 * @param {Object} options
 * @param {HTMLElement} options.container - Element to which the button will be appended.
 * @param {string} options.title - A title attribute value for the button.
 * @param {string} options.onActivate - A function to call when the button is activated.
 * @param {string} [options.id] - An id attribute value for the button.
 * @return {HTMLElement} The control button
 */
function createControlButton(options) {
  const attributes = { title: options.title };
  Iif (options.id) attributes.id = options.id;
 
  attributes.class = `${Constants.CONTROL_BUTTON_CLASS} ${options.className}`;
 
  const button = domUtils.create('button', options.container, attributes);
 
  button.addEventListener('click', (e) => {
    e.preventDefault();
    e.stopPropagation();
 
    const el = e.target;
 
    if (el.classList.contains('active')) {
      el.classList.remove('active');
    } else {
      domUtils.removeClass(document.querySelectorAll(`.${Constants.CONTROL_BUTTON_CLASS}`), 'active');
      el.classList.add('active');
      options.onActivate();
    }
 
  }, true);
 
  return button;
}
 
module.exports = createControlButton;