aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/CoursewareTabs.vue
blob: b6e2bc63b778c82708bdea8957109dd9b28d94b5 (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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template>
    <div class="cw-tabs">
        <div role="tablist" class="cw-tabs-nav">
            <button
                v-for="(tab, index) in sortedTabs"
                :key="index"
                :class="[
                    tab.isActive ? 'is-active' : '',
                    tab.icon !== '' && tab.name !== '' ? 'cw-tabs-nav-icon-text-' + tab.icon : '',
                    tab.icon !== '' && tab.name === '' ? 'cw-tabs-nav-icon-solo-' + tab.icon : '',
                ]"
                :aria-selected="tab.isActive"
                :aria-controls="tab.id"
                :id="tab.selectorId"
                :tabindex="tab.isActive ? 0 : -1"
                @click="selectTab(tab.selectorId)"
                @keydown="handleKeyEvent($event)"
                :ref="tab.selectorId"
            >
                {{ tab.name }}
            </button>
        </div>
        <div class="cw-tabs-content">
            <slot></slot>
        </div>
    </div>
</template>

<script>
export default {
    name: 'courseware-tabs',
    props: {
        setSelected: { type: Number, required: false, default: 0 }
    },
    data() {
        return {
            tabs: [],
        };
    },
    computed: {
        sortedTabs() {
            return [...this.tabs].sort((a, b) => {
                return a.index - b.index;
            });
        }
    },
    created() {
        this.tabs = this.$children;
    },
    methods: {
        selectTab(selectorId) {
            let view = this;
            this.tabs.forEach((tab) => {
                tab.isActive = false;
                if (tab.selectorId === selectorId) {
                    tab.isActive = true;
                    view.$refs[selectorId][0].focus();
                    view.$emit('selectTab', tab);
                }
            });
        },
        selectTabByIndex(selectedIndex) {
            let view = this;
            this.tabs.forEach((tab) => {
                tab.isActive = false;
                if (tab.index === selectedIndex) {
                    tab.isActive = true;
                    view.$refs[tab.selectorId][0].focus();
                    view.$emit('selectTab', tab);
                }
            });
        },
        handleKeyEvent(e) {
            let tablist = e.target.parentElement;
            switch (e.keyCode) {
                case 37: // left
                case 38: // up
                    if (tablist.firstChild.id !== e.target.id) {
                        this.selectTab(e.target.previousElementSibling.id);
                    } else {
                        this.selectTab(tablist.lastChild.id);
                    }
                    break;
                case 39: // right
                case 40: // down
                if (tablist.lastChild.id !== e.target.id) {
                    this.selectTab(e.target.nextElementSibling.id);
                } else {
                    this.selectTab(tablist.firstChild.id);
                }
                    break;
                case 36: //pos1
                    this.selectTab(tablist.firstChild.id);
                    break;
                case 35: //end
                    this.selectTab(tablist.lastChild.id);
                    break;
            }
        },
        getButtonById(id) {
            return this.$refs[id][0];
        },
        getFirstTabButton() {
            let selectorId = this.sortedTabs[0].selectorId;
            return this.$refs[selectorId][0];
        },
        getTabButtonByName(name) {
            let selectorId = null;
            this.tabs.forEach((tab) => {
                if (tab.name === name) {
                    selectorId = tab.selectorId;
                }
            });
            if (selectorId) {
                return this.$refs[selectorId][0];
            }
            return null;
        },
        getTabButtonByAlias(alias) {
            let selectorId = null;
            this.tabs.forEach((tab) => {
                if (tab.alias === alias) {
                    selectorId = tab.selectorId;
                }
            });
            if (selectorId) {
                return this.$refs[selectorId][0];
            }
            return null;
        },
    },
    watch: {
        setSelected(tab) {
            this.selectTabByIndex(tab);
        }
    }
};
</script>