All files index.js

98.98% Statements 195/197
95.65% Branches 44/46
100% Functions 25/25
98.92% Lines 184/186

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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 30917x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x   17x 97x   97x 97x   97x   97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x                       97x   97x 97x     97x   97x 97x 97x         96x 96x 96x   96x 96x 96x 96x   96x     96x 576x 1x         96x 96x 76x       96x 4x 1x   4x 3x   4x 4x         96x 96x 96x 33x   96x 33x 33x   96x               96x 96x 96x 70x   96x 67x   96x 23x 23x 23x 23x 23x 23x 23x 23x               97x 1x 1x 1x 1x 1x 1x     97x 2x 2x 8x   2x           97x 245x     245x 2x 2x   243x 243x 163x   243x 724x 724x 724x   243x 243x     97x 78x 78x 78x 78x             97x 5x 5x 20x 10x 10x 10x 10x 10x     5x 5x           97x 251x 251x 1482x 1482x 251x     251x           97x 139x           97x 38x 38x 38x     97x 225x 225x     97x 3x 3x 3x 2x   3x     97x 937x 937x 194x   937x     97x   67x 67x 67x 327x   67x     23x 23x 23x 73x   23x       97x 595x 595x   595x 595x 595x 595x 3384x 2203x 590x   2203x 2203x 2203x 1181x 861x 861x   320x     595x 595x     97x    
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'),
  templater = require('./templater'),
  Item = require('./item'),
  sort = require('./sort'),
  { addSortListeners, clearSortOrder, setSortOrder } = require('./sort-buttons')
 
module.exports = function (id, options, values) {
  var self = this,
    init,
    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 = templater
      self.template = self.templater.getTemplate({
        parentEl: self.list,
        valueNames: self.valueNames,
        template: self.item,
      })
      self.search = require('./search')(self)
      self.filter = require('./filter')(self)
      self.fuzzySearch = require('./fuzzy-search')(self, options.fuzzySearch)
 
      this.handlers()
      this.items()
      this.pagination()
      this.sort()
 
      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])
        }
      }
    },
    sort: function () {
      const sortButtons = self.utils.getByClass(self.listContainer, self.sortClass)
      const { items, sortFunction, alphabet } = self
      const before = function () {
        self.trigger('sortStart')
      }
      const after = function () {
        self.update()
        self.trigger('sortComplete')
      }
      addSortListeners(sortButtons, {
        items,
        sortFunction,
        alphabet,
        before,
        after,
      })
 
      self.handlers.sortStart = self.handlers.sortStart || []
      self.handlers.sortComplete = self.handlers.sortComplete || []
      self.on('searchStart', function () {
        clearSortOrder(sortButtons)
      })
      self.on('filterStart', function () {
        clearSortOrder(sortButtons)
      })
      self.sort = function (valueName, options = {}) {
        before()
        setSortOrder(sortButtons, valueName, options.order)
        options.alphabet = options.alphabet || self.alphabet
        options.sortFunction = options.sortFunction || self.sortFunction
        options.valueName = valueName
        sort(items, valueName, options)
        after()
        return items
      }
    },
  }
 
  /*
   * 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 = []
    if (values[0] === undefined) {
      values = [values]
    }
    for (var i = 0, il = values.length; i < il; i++) {
      var item = new Item(values[i], { template: self.template })
      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].elm, self.list)
        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.list)
    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(self.list)
    for (var i = 0; i < il; i++) {
      if (is[i].matching(self) && self.matchingItems.length + 1 >= self.i && self.visibleItems.length < self.page) {
        if (!is[i].elm) {
          is[i].elm = templater.create(is[i].values(), self.template)
        }
        templater.show(is[i].elm, self.list)
        self.visibleItems.push(is[i])
        self.matchingItems.push(is[i])
      } else if (is[i].matching(self)) {
        self.matchingItems.push(is[i])
        templater.remove(is[i].elm, self.list)
      } else {
        templater.remove(is[i].elm, self.list)
      }
    }
    self.trigger('updated')
    return self
  }
 
  init.start()
}