Code coverage report for src/js/base/renderer.js

# all files / src/js/base/ renderer.js

100% Statements 37/37
94.44% Branches 34/36
100% Functions 7/7
100% Lines 37/37

    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
    671×

    1×
    1×
    8170×
    8170×
    8170×
    8170×

    1×
    8110×

    8110×
    3642×

    8110×
    4202×

    8110×
    540×
    540×

    8110×
    3638×

    8110×
    8110×
    8110×
    2381×

    8110×
    5598×

    8110×
    600×

    8110×
    2381×

    8110×

    1×

    1×

    3881×
    8170×
    8170×
    8170×
    332×

    8170×

     import $ from 'jquery';

    class Renderer {
      constructor(markup, children, options, callback) {
        this.markup = markup;
        this.children = children;
        this.options = options;
        this.callback = callback;
      }

      render($parent) {
        const $node = $(this.markup);

        if (this.options && this.options.contents) {
          $node.html(this.options.contents);
        }

        if (this.options && this.options.className) {
          $node.addClass(this.options.className);
        }

        if (this.options && this.options.data) {
          $.each(this.options.data, (k, v) => {
            $node.attr('data-' + k, v);
          });
        }

        if (this.options && this.options.click) {
          $node.on('click', this.options.click);
        }

        Eif (this.children) {
          const $container = $node.find('.note-children-container');
          this.children.forEach((child) => {
            child.render($container.length ? $container : $node);
          });
        }

        if (this.callback) {
          this.callback($node, this.options);
        }

        if (this.options && this.options.callback) {
          this.options.callback($node);
        }

        if ($parent) {
          $parent.append($node);
        }

        return $node;
      }
    }

    export default {
      create: (markup, callback) => {
        return function() {
          const options = typeof arguments[1] === 'object' ? arguments[1] : arguments[0];
          let children = $.isArray(arguments[0]) ? arguments[0] : [];
          if (options && options.children) {
            children = options.children;
          }
          return new Renderer(markup, children, options, callback);
        };
      },
    };