# All files templater.js

97.85% Statements 91/93
86.36% Branches 38/44
100% Functions 11/11
98.78% Lines 81/82

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
    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
    13821x
    21x
    21x

    21x
    119x
    119x
    119x
    199x
    199x
    199x
    3x
    3x

    196x
    12x
    12x
    12x

    184x
    184x
    134x

    119x

    21x
    106x
    106x
    3x
    3x
    3x
    103x
    103x
    103x
    103x

    21x

    21x
    124x
    4x

    6x
    6x

    120x
    102x
    2x

    100x

    18x
    18x

    34x
    17x
    17x

    120x
    1x

    119x

    119x

    589x
    589x
    589x

    21x
    192x
    192x
    244x
    244x
    5x
    5x

    239x
    20x
    20x

    219x
    219x

    192x

    21x
    892x
    1550x
    1550x

    21x
    591x

    21x
    1192x
    4x

    21x
    2204x

    21x

    704x
    497x
    2062x

    21x
     var getByClass = require('./utils/get-by-class')
    var getAttribute = require('./utils/get-attribute')
    var valueNamesUtils = require('./utils/value-names')

    var createCleanTemplateItem = function (templateNode, valueNames) {
      var el = templateNode.cloneNode(true)
      el.removeAttribute('id')
      for (var i = 0, il = valueNames.length; i < il; i++) {
        var elm = undefined,
          valueName = valueNames[i]
        if (valueName.data) {
          for (var j = 0, jl = valueName.data.length; j < jl; j++) {
            el.setAttribute('data-' + valueName.data[j], '')
          }
        } else if (valueName.attr && valueName.name) {
          elm = getByClass(el, valueName.name, true)
          Eif (elm) {
            elm.setAttribute(valueName.attr, '')
          }
        } else {
          elm = getByClass(el, valueName, true)
          if (elm) {
            elm.innerHTML = ''
          }
        }
      }
      return el
    }

    var stringToDOMElement = function (itemHTML) {
      Iif (typeof itemHTML !== 'string') return undefined
      if (/<tr[\s>]/g.exec(itemHTML)) {
        var tbody = document.createElement('tbody')
        tbody.innerHTML = itemHTML
        return tbody.firstElementChild
      } else Eif (itemHTML.indexOf('<') !== -1) {
        var div = document.createElement('div')
        div.innerHTML = itemHTML
        return div.firstElementChild
      }
      return undefined
    }

    var templater = {}

    templater.getTemplate = function ({ valueNames, parentEl, template }) {
      if (typeof template === 'function') {
        return {
          valueNames,
          type: 'dynamic',
          render: function (values) {
            var item = template(values)
            return stringToDOMElement(item)
          },
        }
      }

      var itemSource
      if (typeof template === 'string') {
        if (template.indexOf('<') === -1) {
          itemSource = document.getElementById(template)
        } else {
          itemSource = stringToDOMElement(template)
        }
      } else {
        var nodes = parentEl.childNodes
        for (var i = 0, il = nodes.length; i < il; i++) {
          // Only textnodes have a data attribute
          if (nodes[i].data === undefined) {
            itemSource = nodes[i].cloneNode(true)
            break
          }
        }
      }
      if (!itemSource)
        throw new Error("The list needs to have at least one item on init otherwise you'll have to add a template.")

      itemSource = createCleanTemplateItem(itemSource, valueNames)

      return {
        valueNames,
        render: function (values) {
          var el = itemSource.cloneNode(true)
          templater.set(el, values, valueNames)
          return el
        },
      }
    }

    templater.get = function (el, valueNames) {
      var values = {}
      for (var i = 0, il = valueNames.length; i < il; i++) {
        var valueName = valueNames[i]
        if (valueName.data) {
          for (var j = 0, jl = valueName.data.length; j < jl; j++) {
            values[valueName.data[j]] = getAttribute(el, 'data-' + valueName.data[j])
          }
        } else if (valueName.attr && valueName.name) {
          var elm = getByClass(el, valueName.name, true)
          values[valueName.name] = elm ? getAttribute(elm, valueName.attr) : ''
        } else {
          var elm = getByClass(el, valueName, true)
          values[valueName] = elm ? elm.innerHTML : ''
        }
      }
      return values
    }

    templater.set = function (el, values, valueNames) {
      for (var v in values) {
        Eif (values.hasOwnProperty(v)) {
          valueNamesUtils.set(el, v, values[v], valueNames)
        }
      }
    }

    templater.create = function (values, template) {
      return template.render(values)
    }
    templater.remove = function (el, parentEl) {
      if (el !== undefined && el.parentNode === parentEl) {
        parentEl.removeChild(el)
      }
    }
    templater.show = function (el, parentEl) {
      parentEl.appendChild(el)
    }
    templater.clear = function (parentEl) {
      /* .innerHTML = ''; fucks up IE */
      if (parentEl.hasChildNodes()) {
        while (parentEl.childNodes.length >= 1) {
          parentEl.removeChild(parentEl.firstChild)
        }
      }
    }

    module.exports = templater