# All files / src templater.js

98.52% Statements 133/135
90.41% Branches 66/73
100% Functions 17/17
99.16% Lines 118/119

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
    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
    19617x

    91x

    91x

    91x
    1x
    3x
    3x

    1x

    90x
    74x
    1x

    73x

    16x

    90x
    1x

    89x

    89x
    680x

    91x
    89x
    89x

    89x
    159x
    159x
    159x
    2x
    2x

    157x
    8x
    8x
    8x

    149x
    149x
    100x

    89x

    91x
    16x

    16x

    30x
    15x

    1x

    91x
    76x
    76x
    1x
    1x
    1x
    75x
    75x
    75x
    75x

    91x
    1634x
    2118x
    2118x
    25x
    25x
    25x
    5x

    2093x
    20x
    2073x
    1359x

    91x
    1634x
    1634x
    1634x
    1384x
    5x
    1379x
    20x
    20x
    20x

    1359x
    1359x
    1005x

    91x
    190x
    190x
    190x
    236x
    236x
    236x
    4x
    4x

    232x
    16x
    16x

    216x
    216x

    190x

    91x
    1638x
    981x
    1634x
    1634x

    91x
    4291x
    3608x

    683x
    683x
    683x

    91x
    10x
    9x

    91x
    2463x
    2463x

    91x
    919x
    2x

    91x

    696x
    493x
    2311x

    91x

    17x
    91x

     var Templater = function (list) {
      var createItem,
        templater = this

      var init = function () {
        var itemSource

        if (typeof list.item === 'function') {
          createItem = function (values) {
            var item = list.item(values)
            return getItemSource(item)
          }
          return
        }

        if (typeof list.item === 'string') {
          if (list.item.indexOf('<') === -1) {
            itemSource = document.getElementById(list.item)
          } else {
            itemSource = getItemSource(list.item)
          }
        } else {
          /* If item source does not exists, use the first item in list as
          source for new items */
          itemSource = getFirstListItem()
        }

        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, list.valueNames)

        createItem = function () {
          return itemSource.cloneNode(true)
        }
      }

      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 = list.utils.getByClass(el, valueName.name, true)
            Eif (elm) {
              elm.setAttribute(valueName.attr, '')
            }
          } else {
            elm = list.utils.getByClass(el, valueName, true)
            if (elm) {
              elm.innerHTML = ''
            }
          }
        }
        return el
      }

      var getFirstListItem = function () {
        var nodes = list.list.childNodes

        for (var i = 0, il = nodes.length; i < il; i++) {
          // Only textnodes have a data attribute
          if (nodes[i].data === undefined) {
            return nodes[i].cloneNode(true)
          }
        }
        return undefined
      }

      var getItemSource = 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 getValueName = function (name) {
        for (var i = 0, il = list.valueNames.length; i < il; i++) {
          var valueName = list.valueNames[i]
          if (valueName.data) {
            var data = valueName.data
            for (var j = 0, jl = data.length; j < jl; j++) {
              if (data[j] === name) {
                return { data: name }
              }
            }
          } else if (valueName.attr && valueName.name && valueName.name == name) {
            return valueName
          } else if (valueName === name) {
            return name
          }
        }
      }

      var setValue = function (item, name, value) {
        var elm = undefined,
          valueName = getValueName(name)
        if (!valueName) return
        if (valueName.data) {
          item.elm.setAttribute('data-' + valueName.data, value)
        } else if (valueName.attr && valueName.name) {
          elm = list.utils.getByClass(item.elm, valueName.name, true)
          Eif (elm) {
            elm.setAttribute(valueName.attr, value)
          }
        } else {
          elm = list.utils.getByClass(item.elm, valueName, true)
          if (elm) {
            elm.innerHTML = value
          }
        }
      }

      this.get = function (item, valueNames) {
        templater.create(item)
        var values = {}
        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++) {
              values[valueName.data[j]] = list.utils.getAttribute(item.elm, 'data-' + valueName.data[j])
            }
          } else if (valueName.attr && valueName.name) {
            elm = list.utils.getByClass(item.elm, valueName.name, true)
            values[valueName.name] = elm ? list.utils.getAttribute(elm, valueName.attr) : ''
          } else {
            elm = list.utils.getByClass(item.elm, valueName, true)
            values[valueName] = elm ? elm.innerHTML : ''
          }
        }
        return values
      }

      this.set = function (item, values) {
        if (!templater.create(item)) {
          for (var v in values) {
            Eif (values.hasOwnProperty(v)) {
              setValue(item, v, values[v])
            }
          }
        }
      }

      this.create = function (item) {
        if (item.elm !== undefined) {
          return false
        }
        item.elm = createItem(item.values())
        templater.set(item, item.values())
        return true
      }
      this.remove = function (item) {
        if (item.elm.parentNode === list.list) {
          list.list.removeChild(item.elm)
        }
      }
      this.show = function (item) {
        templater.create(item)
        list.list.appendChild(item.elm)
      }
      this.hide = function (item) {
        if (item.elm !== undefined && item.elm.parentNode === list.list) {
          list.list.removeChild(item.elm)
        }
      }
      this.clear = function () {
        /* .innerHTML = ''; fucks up IE */
        if (list.list.hasChildNodes()) {
          while (list.list.childNodes.length >= 1) {
            list.list.removeChild(list.list.firstChild)
          }
        }
      }

      init()
    }

    module.exports = function (list) {
      return new Templater(list)
    }