summaryrefslogtreecommitdiffstats
path: root/public/ng/js/utils/tabs.js
blob: 2e2731eff041085d0b26d6316f721e04cebf3807 (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
/*
 js tabs and tabbed content plugin
 */
function Tabs(selector) {

    function hide($nav) {
        console.log("hide", $nav);
        $nav.removeClass("js-tab-nav-show");
        $($nav.data("tab-target")).removeClass("js-tab-show").hide();
    }

    function show($nav) {
        console.log("show", $nav);
        $nav.addClass("js-tab-nav-show");
        $($nav.data("tab-target")).addClass("js-tab-show").show();
    }

    var $e = $(selector);
    if ($e.length) {
        // pre-assign init index
        var $current = $e.find('.js-tab-nav-show');
        if ($current.length) {
            $($current.data("tab-target")).addClass("js-tab-show");
        }
        // bind nav click
        $e.on("click", ".js-tab-nav", function (e) {
            e.preventDefault();
            var $this = $(this);
            // is showing, not change.
            if ($this.hasClass("js-tab-nav-show")) {
                return;
            }
            $current = $e.find(".js-tab-nav-show").eq(0);
            hide($current);
            show($this);
        });
        console.log("init tabs @", selector)
    }
}

$.fn.extend({
    tabs: function () {
        Tabs(this);
    }
});