# All files / src search.js

94.03% Statements 63/67
80.95% Branches 34/42
92.31% Functions 12/13
95.45% Lines 63/66

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

    83x

    67x
    67x
    67x

    67x
    6x
    61x
    3x
    3x
    58x
    4x
    4x

    54x

    67x
    67x
    60x

    67x
    67x
    67x

    58x
    58x
    128x

    58x

    83x

    35x
    192x

    192x
    192x
    312x
    49x
    49x

    312x
    306x
    306x
    49x

    263x

    25x
    25x

    83x
    67x

    67x
    67x
    67x
    67x

    67x
    25x

    42x
    42x
    7x

    35x

    67x
    67x
    67x

    83x
    83x

    83x
    2x
    2x
    2x
    2x

    83x

    83x

     module.exports = function(list) {
      var item,
        text,
        columns,
        searchString,
        customSearch;

      var prepare = {
        resetList: function() {
          list.i = 1;
          list.templater.clear();
          customSearch = undefined;
        },
        setOptions: function(args) {
          if (args.length == 2 && args[1] instanceof Array) {
            columns = args[1];
          } else if (args.length == 2 && typeof(args[1]) == "function") {
            columns = undefined;
            customSearch = args[1];
          } else if (args.length == 3) {
            columns = args[1];
            customSearch = args[2];
          } else {
            columns = undefined;
          }
        },
        setColumns: function() {
          Iif (list.items.length === 0) return;
          if (columns === undefined) {
            columns = (list.searchColumns === undefined) ? prepare.toArray(list.items[0].values()) : list.searchColumns;
          }
        },
        setSearchString: function(s) {
          s = list.utils.toString(s).toLowerCase();
          s = s.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"); // Escape regular expression characters
          searchString = s;
        },
        toArray: function(values) {
          var tmpColumn = [];
          for (var name in values) {
            tmpColumn.push(name);
          }
          return tmpColumn;
        }
      };
      var search = {
        list: function() {
          for (var k = 0, kl = list.items.length; k < kl; k++) {
            search.item(list.items[k]);
          }
        },
        item: function(item) {
          item.found = false;
          for (var j = 0, jl = columns.length; j < jl; j++) {
            if (search.values(item.values(), columns[j])) {
              item.found = true;
              return;
            }
          }
        },
        values: function(values, column) {
          if (values.hasOwnProperty(column)) {
            text = list.utils.toString(values[column]).toLowerCase();
            if ((searchString !== "") && (text.search(searchString) > -1)) {
              return true;
            }
          }
          return false;
        },
        reset: function() {
          list.reset.search();
          list.searched = false;
        }
      };

      var searchMethod = function(str) {
        list.trigger('searchStart');

        prepare.resetList();
        prepare.setSearchString(str);
        prepare.setOptions(arguments); // str, cols|searchFunction, searchFunction
        prepare.setColumns();

        if (searchString === "" ) {
          search.reset();
        } else {
          list.searched = true;
          if (customSearch) {
            customSearch(searchString, columns);
          } else {
            search.list();
          }
        }

        list.update();
        list.trigger('searchComplete');
        return list.visibleItems;
      };

      list.handlers.searchStart = list.handlers.searchStart || [];
      list.handlers.searchComplete = list.handlers.searchComplete || [];

      list.utils.events.bind(list.utils.getByClass(list.listContainer, list.searchClass), 'keyup', function(e) {
        var target = e.target || e.srcElement, // IE have srcElement
          alreadyCleared = (target.value === "" && !list.searched);
        Eif (!alreadyCleared) { // If oninput already have resetted the list, do nothing
          searchMethod(target.value);
        }
      });

      // Used to detect click on HTML5 clear button
      list.utils.events.bind(list.utils.getByClass(list.listContainer, list.searchClass), 'input', function(e) {
        var target = e.target || e.srcElement;
        if (target.value === "") {
          searchMethod('');
        }
      });

      return searchMethod;
    };