# All files sort.js

0% Statements 0/65
0% Branches 0/47
0% Functions 0/8
0% Lines 0/61

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

     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')
          if (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]
            if (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 naturalSortOptions = {}
            naturalSortOptions.alphabet = list.alphabet || options.alphabet || undefined
            if (!naturalSortOptions.alphabet && options.insensitive) {
              naturalSortOptions.caseInsensitive = true
            }
            return (
              list.utils.naturalSort(
                '' + itemA.values()[options.valueName],
                '' + itemB.values()[options.valueName],
                naturalSortOptions
              ) * 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
    }