# All files item.js

100% Statements 23/23
95.45% Branches 21/22
100% Functions 4/4
100% Lines 21/21

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

    20x
    1012x

    1012x

    1012x
    1012x

    1012x
    1012x
    1012x
    1011x
    1011x

    1012x
    5416x
    1121x
    1716x

    1121x
    302x

    4295x

    1012x
    4634x

    1012x

     const templater = require('./templater')

    module.exports = function (initValues, { element, template } = {}) {
      var item = this

      this._values = {}

      this.found = false
      this.filtered = false

      var init = function (values, { element, template } = {}) {
        if (element) item.elm = element
        if (!template) throw new Error('missing_item_template')
        item.template = template
        item.values(values)
      }

      this.values = function (newValues) {
        if (newValues !== undefined) {
          for (var name in newValues) {
            item._values[name] = newValues[name]
          }
          if (item.elm) {
            templater.set(item.elm, item.values(), item.template.valueNames)
          }
        } else {
          return item._values
        }
      }

      this.matching = function ({ searched, filtered }) {
        return (
          (filtered && searched && item.found && item.filtered) ||
          (filtered && !searched && item.filtered) ||
          (!filtered && searched && item.found) ||
          (!filtered && !searched)
        )
      }

      init(initValues, { element, template })
    }