# All files sort-listener.js

100% Statements 56/56
90% Branches 27/30
100% Functions 6/6
100% Lines 50/50

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
    7618x
    18x
    18x
    18x

    18x
    19x
    19x
    1x

    18x

    18x
    21x
    21x
    7x
    14x
    3x
    11x
    3x

    8x

    18x
    138x
    8x
    8x

    18x
    43x
    50x
    50x
    7x
    7x
    7x

    43x
    43x
    24x
    12x
    12x

    12x
    12x

    19x
    19x

    18x
    100x
    16x
    16x
    16x
    16x
    16x
    16x

    16x
    16x
    16x

    18x
     var getAttribute = require('./utils/get-attribute')
    var classes = require('./utils/classes')
    var events = require('./utils/events')
    var sorter = require('./sorter')

    var getInSensitive = function (btn) {
      var insensitive = getAttribute(btn, 'data-insensitive')
      if (insensitive === 'false') {
        return false
      } else {
        return true
      }
    }
    var getNextOrder = function (btn) {
      var predefinedOrder = getAttribute(btn, 'data-order')
      if (predefinedOrder == 'asc' || predefinedOrder == 'desc') {
        return predefinedOrder
      } else if (classes(btn).has('desc')) {
        return 'asc'
      } else if (classes(btn).has('asc')) {
        return 'desc'
      } else {
        return 'asc'
      }
    }
    var clearOrder = function (els) {
      for (var i = 0, il = els.length; i < il; i++) {
        classes(els[i]).remove('asc')
        classes(els[i]).remove('desc')
      }
    }
    var setOrder = function (els, valueName, order) {
      for (var i = 0, il = els.length; i < il; i++) {
        var btn = els[i]
        if (getAttribute(btn, 'data-sort') !== valueName) {
          classes(btn).remove('asc')
          classes(btn).remove('desc')
          continue
        }
        var predefinedOrder = getAttribute(btn, 'data-order')
        if (predefinedOrder == 'asc' || predefinedOrder == 'desc') {
          if (predefinedOrder == order) {
            classes(btn).add(order)
            classes(btn).remove(order === 'asc' ? 'desc' : 'asc')
          } else {
            classes(btn).remove('asc')
            classes(btn).remove('desc')
          }
        } else {
          classes(btn).add(order)
          classes(btn).remove(order === 'asc' ? 'desc' : 'asc')
        }
      }
    }

    var addSortListeners = function (elements, { items, sortFunction, alphabet, before, after } = {}) {
      events.bind(elements, 'click', function () {
        if (before) before()
        var target = arguments[0].currentTarget || arguments[0].srcElement || undefined
        var valueName = getAttribute(target, 'data-sort')
        var order = getNextOrder(target)
        var insensitive = getInSensitive(target)
        var options = {
          sortFunction,
          insensitive,
          alphabet,
          order,
        }
        setOrder(elements, valueName, order)
        sorter(items, valueName, options)
        if (after) after()
      })
    }

    module.exports = { addSortListeners, getInSensitive, getNextOrder, setOrder, clearOrder }