# All files / src parse.js

100% Statements 27/27
100% Branches 8/8
100% Functions 6/6
100% Lines 27/27

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

    83x

    83x
    84x
    84x
    84x

    309x
    189x

    84x

    83x
    87x
    189x

    83x
    4x
    4x
    4x
    3x
    3x

    1x
    1x

    83x

    83x
    84x
    84x

    84x
    1x

    83x

     module.exports = function(list) {

      var Item = require('./item')(list);

      var getChildren = function(parent) {
        var nodes = parent.childNodes,
          items = [];
        for (var i = 0, il = nodes.length; i < il; i++) {
          // Only textnodes have a data attribute
          if (nodes[i].data === undefined) {
            items.push(nodes[i]);
          }
        }
        return items;
      };

      var parse = function(itemElements, valueNames) {
        for (var i = 0, il = itemElements.length; i < il; i++) {
          list.items.push(new Item(valueNames, itemElements[i]));
        }
      };
      var parseAsync = function(itemElements, valueNames) {
        var itemsToIndex = itemElements.splice(0, 50); // TODO: If < 100 items, what happens in IE etc?
        parse(itemsToIndex, valueNames);
        if (itemElements.length > 0) {
          setTimeout(function() {
            parseAsync(itemElements, valueNames);
          }, 1);
        } else {
          list.update();
          list.trigger('parseComplete');
        }
      };

      list.handlers.parseComplete = list.handlers.parseComplete || [];

      return function() {
        var itemsToIndex = getChildren(list.list),
          valueNames = list.valueNames;

        if (list.indexAsync) {
          parseAsync(itemsToIndex, valueNames);
        } else {
          parse(itemsToIndex, valueNames);
        }
      };
    };