david-PC\david
2018-06-12 f240ac3ccd37c541cab2c21cfc433d3510999a3c
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
 
(function( window, undefined ) {
    "use strict";
    var Tab = function(params,callback) {
        this.extend(this.params, params);
        this._init(callback);
    };
    var tabItems;
    Tab.prototype = {
        params: {
            element: false,
            index: 1, //默认选中
            repeatClick: false //是否允许重复点击
        },
        _init: function(callback) {
            var self = this;
            if(!self.params.element || self.params.element.nodeType!=1){
                return;
            }
            tabItems = self.params.element.children;
            if(tabItems){
                self.setActive();
                for(var i=0; i<tabItems.length; i++){
                    tabItems[i].setAttribute("tapmode","");
                    tabItems[i].setAttribute("data-item-order",i);
                    tabItems[i].onclick = function(e){
                        if(!self.params.repeatClick){
                            if(this.className.indexOf("aui-active") > -1)return;
                        }
                        if(callback){
                            callback({
                                index: parseInt(this.getAttribute("data-item-order"))+1,
                                dom:this
                            })
                        };
                        this.parentNode.querySelector(".aui-active").classList.remove("aui-active");
                        this.classList.add("aui-active");
                    }
                }
            }
        },
        setRepeat:function(value){
            var self = this;
            self.params.repeatClick = value ? value : false;
        },
        setActive: function(index){
            var self = this;
            index = index ? index : self.params.index;
            var _tab = tabItems[index-1];
            if(_tab.parentNode.querySelector(".aui-active"))_tab.parentNode.querySelector(".aui-active").classList.remove("aui-active");
            _tab.classList.add("aui-active");
        },
        extend: function(a, b) {
            for (var key in b) {
                  if (b.hasOwnProperty(key)) {
                      a[key] = b[key];
                  }
              }
              return a;
        }
    };
    window.auiTab = auiTab;
})(window);