All files / lib/components tabs.vue

9.72% Statements 7/72
5.36% Branches 3/56
13.64% Functions 3/22
9.72% Lines 7/72
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 26218x 18x 18x   18x 18x                     18x     18x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
<template>
    <component :is="tag" :id="id || null" class="tabs">
    I    <div v-if="bottom" :class="['tab-content',{'card-block': card}]" ref="tabsContainer">
            <slot></slot>
           E <slot name="empty" v-if="!tabs || !tabs.length"></slot>
        </div>
 
        <div :class="{'card-header': card}">
            <ul :class="['nav','nav-' + navStyle, card ? 'card-header-'+navStyle : null]"
                role="tablist"
                tabindex="0"
                :aria-setsize="tabs.length"
                :aria-posinset="currentTab + 1"
                @keydown.left="previousTab"
                @keydown.up="previousTab"
                @keydown.right="nextTab"
                @keydown.down="nextTab"
                @keydown.shift.left="setTab(-1,false,1)"
                @keydown.shift.up="setTab(-1,false,1)"
                @keydown.shift.right="setTab(tabs.length,false,-1)"
                @keydown.shift.down="setTab(tabs.length,false,-1)"
            >
                <li class="nav-item" v-for="(tab, index) in tabs" role="presentation">
                    <a :class="['nav-link',{small: small, active: tab.localActive, disabled: tab.disabled}]"
                       :href="tab.href"
                       role="tab"
                       :aria-selected="tab.localActive ? 'true' : 'false'"
                       :aria-controls="tab.id || null"
                       :id="tab.controlledBy || null"
                       @click.prevent.stop="setTab(index)"
                       @keydown.space.prevent.stop="setTab(index)"
                       @keydown.enter.prevent.stop="setTab(index)"
                       tabindex="-1"
                       v-if="!tab.headHtml"
                       v-html="tab.title"
                    ></a>
                    <div :class="['tab-head',{small: small, active: tab.localActive, disabled: tab.disabled}]"
                         role="heading"
                         tabindex="-1"
                         v-else
                         v-html="tab.headHtml"></div>
                </li>
                <slot name="tabs"></slot>
            </ul>
        </div>
 
        <div v-if="!bottom" :class="['tab-content',{'card-block': card}]" ref="tabsContainer">
            <slot></slot>
            <slot name="empty" v-if="!tabs || !tabs.length"></slot>
        </div>
    </component>
</template>
 
<script>
    import {observeDom} from '../utils';
 
    export default {
        data() {
            return {
                currentTab: this.value,
                tabs: []
            };
        },
        props: {
            id: {
                type: String,
                default: ''
            },
            tag: {
                type: String,
                default: 'div'
            },
            noFade: {
                type: Boolean,
                default: false
            },
            card: {
                type: Boolean,
                default: false
            },
            small: {
                type: Boolean,
                default: false
            },
            value: {
                type: Number,
                default: null
            },
            pills: {
                type: Boolean,
                default: false
            },
            lazy: {
                type: Boolean,
                default: false
            },
            bottom: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            currentTab(val, old) {
                if (val === old) {
                    return;
                }

                this.$root.$emit('changed::tab', this, val, this.tabs[val]);
                this.$emit('input', val);
                this.tabs[val].$emit('click');
            },
            value(val, old) {
                if (val === old) {
                    return;
                }

                this.setTab(val);
            },
            fade(val, old) {
                if (val === old) {
                    return;
                }

                this.tabs.forEach(item => {
                    this.$set(item, 'fade', val);
                });
            }
        },
        computed: {
            fade() {
                return !this.noFade;
            },
            navStyle() {
                return this.pills ? 'pills' : 'tabs';
            }
        },
        methods: {
            /**
             * Util: Return the sign of a number (as -1, 0, or 1)
             */
            sign(x) {
                return (x === 0) ? 0 : (x > 0 ? 1 : -1);
            },

            /**
             * Move to next tab
             */
            nextTab() {
                this.setTab(this.currentTab, false, 1);
            },
 
            /**
             * Move to previous tab
             */
            previousTab() {
                this.setTab(this.currentTab, false, -1);
            },

            /**
             * Set active tab on the tabs collection and the child 'tab' component
             */
            setTab(index, force, offset) {
                offset = offset || 0;

                // Prevent setting same tab!
                if (!force && (index + offset) === this.currentTab) {
                    return;
                }

                const tab = this.tabs[index + offset];
 
                // Don't go beyond indexes!
                if (!tab) {
                    return;
                }

                // Ignore or Skip disabled
                if (tab.disabled) {
                    if (offset) {
                        // Skip to next non disabled tab in offset direction (recursive)
                        this.setTab(index, force, offset + this.sign(offset));
                    }
                    return;
                }
 
                // Activate current tab, and deactivte any old tabs
                this.tabs.forEach( t => {
                    if (t === tab) {
                        // Set new tab as active
                        this.$set(t, 'localActive', true);
                    } else {
                        // Ensure non current tabs are not active
                        this.$set(t, 'localActive', false);
                    }
                });

                // Update currentTab
                this.currentTab = index + offset;
            },

            /**
             * Dynamically update tabs
             */
            updateTabs() {
                // Probe tabs
                if (this.$slots.default) {
                    this.tabs = this.$slots.default.filter(tab => tab.componentInstance || false)
                        .map(tab => tab.componentInstance);
                } else {
                    this.tabs = [];
                }

                this.tabs.forEach(tab => {
                    this.$set(tab, 'fade', this.fade);
                    this.$set(tab, 'lazy', this.lazy);
                });
 
                // Get initial active tab
                let tabIndex = this.currentTab;
 
                if (tabIndex === null || tabIndex === undefined) {
                    // Make null for easier testing further on
                    tabIndex = null;
                }
 
                if (tabIndex === null) {
                    // Find last active non-dsabled tab in current tabs
                    this.tabs.forEach((tab, index) => {
                        if (tab.active && !tab.disabled) {
                            tabIndex = index;
                        }
                    });
                }

                if (tabIndex === null) {
                    // Find first non-disabled tab in current tabs
                    this.tabs.forEach((tab, index) => {
                        if (!tab.disabled && tabIndex === null) {
                            tabIndex = index;
                        }
                    });
                }
 
                // Workaround to fix problem when currentTab is removed
                let offset = 0;
                if (tabIndex >= this.tabs.length) {
                    offset = -1;
                }
 
                this.setTab(tabIndex || 0, true, offset);
            }
        },
        mounted() {
            this.updateTabs();
 
            // Observe Child changes so we can notify tabs change
            observeDom(this.$refs.tabsContainer, this.updateTabs.bind(this), {subtree: false});
        }
    };
 
</script>