All files / lib/components collapse.vue

67.69% Statements 44/65
60% Branches 24/40
76.19% Functions 16/21
67.69% Lines 44/65
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 17418x 18x 18x   18x 18x                     18x     18x     99x                                                           1x 1x       5x 5x           108x                 4x     2x 2x 2x 2x 2x               3x 3x 3x 3x 3x 3x 3x                 5x     104x 104x 104x   12x                     27x 24x   3x     108x 72x   36x   12x         24x 1x                   99x 99x     99x           99x                              
<template>
    <transition
    I        enter-class=""
            enter-active-class="collapsing"
           E enter-to-class=""
            leave-class=""
            leave-active-class="collapsing"
            leave-to-class=""
            @enter="onEnter"
            @after-enter="onAfterEnter"
            @leave="onLeave"
            @after-leave="onAfterLeave"
    >
        <div v-show="show"
             :id="id || null"
             :class="classObject"
             @click.native="clickHandler"
        ><slot></slot></div>
    </transition>
</template>
 
<script>
    import { listenOnRootMixin } from '../mixins';
 
    export default {
        mixins: [listenOnRootMixin],
        data() {
            return {
                show: this.visible,
                transitioning: false
            };
        },
        model: {
            prop: 'visible',
            event: 'input'
        },
        props: {
            id: {
                type: String,
                required: true
            },
            isNav: {
                type: Boolean,
                default: false
            },
            accordion: {
                type: String,
                default: null
            },
            visible: {
                type: Boolean,
                default: false
            }E
        },
        watch: {
            visible(newVal) {
                if (newVal !== this.show) {
                E    this.show = newVal;
                }
            },
            show(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.emitState();
                }
            }
        },
        computed: {
            classObject() {
                return {
                    'navbar-collapse': this.isNav,
                    'collapse': !this.transitioning,
                    'show': this.show && !this.transitioning
                };
            }
        },
        methods: {
            toggle() {
                this.show = !this.show;
            },
            onEnter(el) {
                el.style.height = 0;
                this.reflow(el);
                el.style.height = el.scrollHeight + 'px';
                this.transitioning = true;
                this.$emit('show');
            },
            onAfterEnter(el) {
                el.style.height = null;
                this.transitioning = false;
                this.$emit('shown');
            },
            onLeave(el) {
                el.style.height = 'auto';
                el.style.display = 'block';
                el.style.height = el.getBoundingClientRect().height + 'px';
                this.reflow(el);
                this.transitioning = true;
                el.style.height = 0;
                this.$emit('hide');
            },
            onAfterLeave(el) {
                el.style.height = null;
                this.transitioning = false;
                this.$emit('hidden');
            },
            reflow(el) {
                /* eslint-disable no-unused-expressions */
                el.offsetHeight; // Force repaint
            },
            emitState() {
                this.$emit('input', this.show);
                this.$root.$emit('collapse::toggle::state', this.id, this.show);
                if (this.accordion && this.show) {
                    // Tell the other collapses in this accordion to close
                    this.$root.$emit('accordion::toggle', this.id, this.accordion);
                }
            },
            clickHandler() {
                if (!this.isNav || typeof document === 'undefined') {
                    return;
                }
                // If we are in a nav/navbar, close the collapse when clicked
                this.toggle();
            },
            handleToggleEvt(target) {
                if (target !== this.id) {
                    return;
                }
                this.toggle();
            },
            handleAccordionEvt(openedId, accordion) {
                if (!this.accordion || accordion !== this.accordion) {
                    Ireturn;
                }
                if (openedId === this.id) {
                    // Open this collapse if not shown
                    if (!this.show) {
                        this.toggle();
                    }
                } else {
                    // Close this collapse if shown
                    if (this.show) {
                        this.toggle();
                    }
                }
            },
            handleResize() {
                // Handler for orientation/resize to set collapsed state
              this.show = (getComputedStyle(this.$el).display === 'block');
            },
        },
        created() {
            Ithis.listenOnRoot('collapse::toggle', this.handleToggleEvt);
            this.listenOnRoot('accordion::toggle', this.handleAccordionEvt);
        },
        mounted() {
            if (this.isNav && typeof document !== 'undefined') {
                // Set up handlers
                window.addEventListener('resize', this.handleResize, false);
                window.addEventListener('orientationchange', this.handleResize, false);
                this.handleResize();
            }
            this.emitState();
        },
        destroyed() {
            if (this.isNav && typeof document !== 'undefined') {
                window.removeEventListener('resize', this.handleResize, false);
                window.removeEventListener('orientationchange', this.handleResize, false);
            }
        }
    };
 
</script>