# All files / src sort.js

96.72% Statements 59/61
95.74% Branches 45/47
100% Functions 8/8
96.72% Lines 59/61

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

    83x

    169x
    39x
    39x

    10x
    10x
    5x
    5x
    1x
    4x
    1x

    3x

    10x
    10x

    10x

    33x
    33x
    33x

    33x
    33x
    22x
    11x

    11x

    83x
    33x
    33x

    33x

    33x
    10x
    10x
    10x

    23x
    23x
    23x
    23x

    33x
    33x

    33x
    33x

    33x
    3x
    29x

    30x
    209x
    209x
    209x
    150x

    209x

    33x
    33x
    33x

    83x
    83x

    83x
    83x
    83x
    83x

    83x

     module.exports = function(list) {

      var buttons = {
        els: undefined,
        clear: function() {
          for (var i = 0, il = buttons.els.length; i < il; i++) {
            list.utils.classes(buttons.els[i]).remove('asc');
            list.utils.classes(buttons.els[i]).remove('desc');
          }
        },
        getOrder: function(btn) {
          var predefinedOrder = list.utils.getAttribute(btn, 'data-order');
          if (predefinedOrder == "asc" || predefinedOrder == "desc") {
            return predefinedOrder;
          } else if (list.utils.classes(btn).has('desc')) {
            return "asc";
          } else if (list.utils.classes(btn).has('asc')) {
            return "desc";
          } else {
            return "asc";
          }
        },
        getInSensitive: function(btn, options) {
          var insensitive = list.utils.getAttribute(btn, 'data-insensitive');
          Iif (insensitive === "false") {
            options.insensitive = false;
          } else {
            options.insensitive = true;
          }
        },
        setOrder: function(options) {
          for (var i = 0, il = buttons.els.length; i < il; i++) {
            var btn = buttons.els[i];
            Iif (list.utils.getAttribute(btn, 'data-sort') !== options.valueName) {
              continue;
            }
            var predefinedOrder = list.utils.getAttribute(btn, 'data-order');
            if (predefinedOrder == "asc" || predefinedOrder == "desc") {
              if (predefinedOrder == options.order) {
                list.utils.classes(btn).add(options.order);
              }
            } else {
              list.utils.classes(btn).add(options.order);
            }
          }
        }
      };

      var sort = function() {
        list.trigger('sortStart');
        var options = {};

        var target = arguments[0].currentTarget || arguments[0].srcElement || undefined;

        if (target) {
          options.valueName = list.utils.getAttribute(target, 'data-sort');
          buttons.getInSensitive(target, options);
          options.order = buttons.getOrder(target);
        } else {
          options = arguments[1] || options;
          options.valueName = arguments[0];
          options.order = options.order || "asc";
          options.insensitive = (typeof options.insensitive == "undefined") ? true : options.insensitive;
        }

        buttons.clear();
        buttons.setOrder(options);

        // caseInsensitive
        // alphabet
        var customSortFunction = (options.sortFunction || list.sortFunction || null),
            multi = ((options.order === 'desc') ? -1 : 1),
            sortFunction;

        if (customSortFunction) {
          sortFunction = function(itemA, itemB) {
            return customSortFunction(itemA, itemB, options) * multi;
          };
        } else {
          sortFunction = function(itemA, itemB) {
            var sort = list.utils.naturalSort;
            sort.alphabet = list.alphabet || options.alphabet || undefined;
            if (!sort.alphabet && options.insensitive) {
              sort = list.utils.naturalSort.caseInsensitive;
            }
            return sort(itemA.values()[options.valueName], itemB.values()[options.valueName]) * multi;
          };
        }

        list.items.sort(sortFunction);
        list.update();
        list.trigger('sortComplete');
      };

      // Add handlers
      list.handlers.sortStart = list.handlers.sortStart || [];
      list.handlers.sortComplete = list.handlers.sortComplete || [];

      buttons.els = list.utils.getByClass(list.listContainer, list.sortClass);
      list.utils.events.bind(buttons.els, 'click', sort);
      list.on('searchStart', buttons.clear);
      list.on('filterStart', buttons.clear);

      return sort;
    };