All files / src/editor elements.js

0% Statements 0/47
0% Branches 0/1
0% Functions 0/1
0% Lines 0/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                               
export const Elements = {
  wrap(contLabel, nodes) {
    const w = typeof contLabel === 'string'
      ? document.createElement(contLabel)
      : contLabel;

    if (!(nodes instanceof Array)) {
      nodes = [nodes]; // eslint-disable-line no-param-reassign
    }
    for (let i = 0; i < nodes.length; i++) {
      if (typeof nodes[i] === 'string') {
        nodes[i] = document.createTextNode(nodes[i]); // eslint-disable-line no-param-reassign
      }
      w.appendChild(nodes[i]);
    }
    return w;
  },
  createCheckbox(name, labelText, checked) {
    const check = document.createElement('input');
    check.type = 'checkbox';
    check.name = name;
    check.id = `${name}-checkbox`;
    check.checked = !!checked;
    const label = document.createElement('label');
    const labelTextNode = document.createTextNode(labelText);
    label.htmlFor = check.id;
    label.appendChild(labelTextNode);

    const cont = document.createElement('span');
    cont.appendChild(check);
    cont.appendChild(label);

    return {
      synthetic: true,
      element: cont,
      getEventNodes: () => [check],
    };
  },
  create(contLabel, attrs = {}) {
    const el = document.createElement(contLabel);
    const keys = Object.keys(attrs) || [];
    for (let i = 0; i < keys.length; i++) {
      el.setAttribute(`${keys[i]}`, `${attrs[keys[i]]}`);
    }
    return el;
  },
};