# All files / src index.js

98.83% Statements 169/171
94.59% Branches 35/37
100% Functions 19/19
98.75% Lines 158/160

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
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    26417x
    17x
    17x
    17x
    17x
    17x
    17x
    17x
    17x

    17x
    91x

    91x
    91x
    91x

    91x

    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x
    91x

    91x

    91x
    91x

    91x

    91x
    91x
    90x
    90x
    90x
    90x

    90x
    90x
    90x

    90x

    90x
    720x
    1x

    90x
    90x
    71x

    90x
    4x
    1x

    4x
    3x

    4x
    4x

    91x
    1x
    1x
    1x
    1x
    1x
    1x

    91x
    2x
    2x
    8x

    2x

    91x
    238x

    238x
    2x
    2x

    236x
    236x
    236x
    161x

    236x
    717x
    717x
    717x
    717x
    717x

    236x
    236x

    91x
    79x
    79x
    79x
    79x

    91x
    5x
    5x
    20x
    10x
    10x
    10x
    10x
    10x

    5x
    5x

    91x
    251x
    251x
    1482x
    1482x
    251x

    251x

    91x
    139x

    91x
    37x
    37x
    37x

    91x
    213x
    213x

    91x
    3x
    3x
    3x
    2x

    3x

    91x
    937x
    937x
    198x

    937x

    91x

    69x
    69x
    69x
    329x

    69x

    25x
    25x
    25x
    75x

    25x

    91x
    587x
    587x

    587x
    587x
    587x
    587x
    3379x
    2462x
    2462x
    2462x
    917x
    597x
    597x

    320x

    587x
    587x

    91x

     var naturalSort = require('string-natural-compare'),
      getByClass = require('./utils/get-by-class'),
      extend = require('./utils/extend'),
      indexOf = require('./utils/index-of'),
      events = require('./utils/events'),
      toString = require('./utils/to-string'),
      classes = require('./utils/classes'),
      getAttribute = require('./utils/get-attribute'),
      toArray = require('./utils/to-array')

    module.exports = function (id, options, values) {
      var self = this,
        init,
        Item = require('./item')(self),
        addAsync = require('./add-async')(self),
        initPagination = require('./pagination')(self)

      init = {
        start: function () {
          self.listClass = 'list'
          self.searchClass = 'search'
          self.sortClass = 'sort'
          self.page = 10000
          self.i = 1
          self.items = []
          self.visibleItems = []
          self.matchingItems = []
          self.searched = false
          self.filtered = false
          self.searchColumns = undefined
          self.searchDelay = 0
          self.handlers = { updated: [] }
          self.valueNames = []
          self.utils = {
            getByClass: getByClass,
            extend: extend,
            indexOf: indexOf,
            events: events,
            toString: toString,
            naturalSort: naturalSort,
            classes: classes,
            getAttribute: getAttribute,
            toArray: toArray,
          }

          self.utils.extend(self, options)

          self.listContainer = typeof id === 'string' ? document.getElementById(id) : id
          Iif (!self.listContainer) {
            return
          }
          self.list = getByClass(self.listContainer, self.listClass, true)

          self.parse = require('./parse')(self)
          self.templater = require('./templater')(self)
          self.search = require('./search')(self)
          self.filter = require('./filter')(self)
          self.sort = require('./sort')(self)
          self.fuzzySearch = require('./fuzzy-search')(self, options.fuzzySearch)

          this.handlers()
          this.items()
          this.pagination()

          self.update()
        },
        handlers: function () {
          for (var handler in self.handlers) {
            if (self[handler] && self.handlers.hasOwnProperty(handler)) {
              self.on(handler, self[handler])
            }
          }
        },
        items: function () {
          self.parse(self.list)
          if (values !== undefined) {
            self.add(values)
          }
        },
        pagination: function () {
          if (options.pagination !== undefined) {
            if (options.pagination === true) {
              options.pagination = [{}]
            }
            if (options.pagination[0] === undefined) {
              options.pagination = [options.pagination]
            }
            for (var i = 0, il = options.pagination.length; i < il; i++) {
              initPagination(options.pagination[i])
            }
          }
        },
      }

      /*
       * Re-parse the List, use if html have changed
       */
      this.reIndex = function () {
        self.items = []
        self.visibleItems = []
        self.matchingItems = []
        self.searched = false
        self.filtered = false
        self.parse(self.list)
      }

      this.toJSON = function () {
        var json = []
        for (var i = 0, il = self.items.length; i < il; i++) {
          json.push(self.items[i].values())
        }
        return json
      }

      /*
       * Add object to list
       */
      this.add = function (values, callback) {
        Iif (values.length === 0) {
          return
        }
        if (callback) {
          addAsync(values.slice(0), callback)
          return
        }
        var added = [],
          notCreate = false
        if (values[0] === undefined) {
          values = [values]
        }
        for (var i = 0, il = values.length; i < il; i++) {
          var item = null
          notCreate = self.items.length > self.page ? true : false
          item = new Item(values[i], undefined, notCreate)
          self.items.push(item)
          added.push(item)
        }
        self.update()
        return added
      }

      this.show = function (i, page) {
        this.i = i
        this.page = page
        self.update()
        return self
      }

      /* Removes object from list.
       * Loops through the list and removes objects where
       * property "valuename" === value
       */
      this.remove = function (valueName, value, options) {
        var found = 0
        for (var i = 0, il = self.items.length; i < il; i++) {
          if (self.items[i].values()[valueName] == value) {
            self.templater.remove(self.items[i], options)
            self.items.splice(i, 1)
            il--
            i--
            found++
          }
        }
        self.update()
        return found
      }

      /* Gets the objects in the list which
       * property "valueName" === value
       */
      this.get = function (valueName, value) {
        var matchedItems = []
        for (var i = 0, il = self.items.length; i < il; i++) {
          var item = self.items[i]
          if (item.values()[valueName] == value) {
            matchedItems.push(item)
          }
        }
        return matchedItems
      }

      /*
       * Get size of the list
       */
      this.size = function () {
        return self.items.length
      }

      /*
       * Removes all items from the list
       */
      this.clear = function () {
        self.templater.clear()
        self.items = []
        return self
      }

      this.on = function (event, callback) {
        self.handlers[event].push(callback)
        return self
      }

      this.off = function (event, callback) {
        var e = self.handlers[event]
        var index = indexOf(e, callback)
        if (index > -1) {
          e.splice(index, 1)
        }
        return self
      }

      this.trigger = function (event) {
        var i = self.handlers[event].length
        while (i--) {
          self.handlers[event][i](self)
        }
        return self
      }

      this.reset = {
        filter: function () {
          var is = self.items,
            il = is.length
          while (il--) {
            is[il].filtered = false
          }
          return self
        },
        search: function () {
          var is = self.items,
            il = is.length
          while (il--) {
            is[il].found = false
          }
          return self
        },
      }

      this.update = function () {
        var is = self.items,
          il = is.length

        self.visibleItems = []
        self.matchingItems = []
        self.templater.clear()
        for (var i = 0; i < il; i++) {
          if (is[i].matching() && self.matchingItems.length + 1 >= self.i && self.visibleItems.length < self.page) {
            is[i].show()
            self.visibleItems.push(is[i])
            self.matchingItems.push(is[i])
          } else if (is[i].matching()) {
            self.matchingItems.push(is[i])
            is[i].hide()
          } else {
            is[i].hide()
          }
        }
        self.trigger('updated')
        return self
      }

      init.start()
    }