# All files / src index.js

98.74% Statements 157/159
91.43% Branches 32/35
100% Functions 19/19
99.37% Lines 157/158

    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
    26317x
    17x
    17x
    17x
    17x
    17x
    17x
    17x
    17x

    17x

    83x

    83x
    83x
    83x

    83x

    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x
    83x

    83x

    83x
    83x
    83x

    83x
    83x
    83x
    83x
    83x
    83x

    83x
    83x
    83x

    83x

    83x
    664x
    1x

    83x
    83x
    64x

    83x
    3x
    1x

    3x
    2x

    3x
    3x

    83x
    1x
    1x
    1x
    1x
    1x
    1x

    83x
    2x
    2x
    8x

    2x

    83x
    190x

    190x
    2x
    2x

    188x
    188x
    188x
    120x

    188x
    627x
    627x
    627x
    626x
    626x

    187x
    187x

    83x
    71x
    71x
    71x
    71x

    83x
    5x
    5x
    20x
    10x
    10x
    10x
    10x
    10x

    5x
    5x

    83x
    221x
    221x
    1302x
    1302x
    221x

    221x

    83x
    79x

    83x
    29x
    29x
    29x

    83x
    198x
    198x

    83x
    3x
    3x
    3x
    2x

    3x

    83x
    858x
    858x
    186x

    858x

    83x

    69x
    69x
    69x
    329x

    69x

    25x
    25x
    25x
    75x

    25x

    83x
    518x
    518x

    518x
    518x
    518x
    518x
    2974x
    2246x
    2246x
    2246x
    728x
    431x
    431x

    297x

    518x
    518x

    83x

     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.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.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, 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();
    };