summaryrefslogtreecommitdiff
path: root/src/lib/flextabs.js
blob: 12842c5ff84ae55adcebb78d49e7892213cc1c18 (plain)
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
(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define([], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory();
  } else {
    root.flextabs = factory();
  }
}(this, function() {

  var flextabs = function(target) {

    var _ = {};

    _.flextabs = target;

    _.toggle = _.flextabs.querySelectorAll('.flextabs__toggle');

    _.content = _.flextabs.querySelectorAll('.flextabs__content');

    _.reset = function() {
      for (var i = 0; i < _.toggle.length; i += 1) {
        _.toggle[i].classList.remove('flextabs__toggle--active--last');
        _.content[i].classList.remove('flextabs__content--active--last');
      }
    };

    _.activate = function() {
      var i = Array.prototype.indexOf.call(_.toggle, this);
      _.toggle[i].classList.toggle('flextabs__toggle--active');
      _.toggle[i].classList.add('flextabs__toggle--active--last');
      _.content[i].classList.toggle('flextabs__content--active');
      _.content[i].classList.add('flextabs__content--active--last');
    };

    _.aria = function() {
      for (var i = 0; i < _.toggle.length; i += 1) {
        var style = getComputedStyle(_.content[i]);
        if (style.getPropertyValue('display') !== 'none') {
          _.toggle[i].setAttribute('aria-expanded', true);
        } else {
          _.toggle[i].setAttribute('aria-expanded', false);
        }
      }
    };

    _.click = function(e) {
      e.preventDefault();
      _.reset();
      _.activate.call(this);
      _.aria();
    };

    _.init = function() {
      for (var i = 0; i < _.toggle.length; i += 1) {
        window.addEventListener('load', _.aria);
        window.addEventListener('resize', _.aria);
        _.toggle[i].addEventListener('click', _.click);
      }
    };

    return _;

  };

  return flextabs;

}));