All files / src/components/u-grid-layout.vue column.vue

100% Statements 21/21
45.45% Branches 5/11
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      7x   7x     7x                                 7x                               7x                                                                                                                                                                                                                                                                                                       7x 7x 14x 14x     14x                                             7x 7x
<template>
<div :class="$style.root" vusion-slot-name="default" :style="[commonStyle, responsiveStyle]" :empty="!$slots.default" :nowrap="!wrap">
    <slot></slot>
    <s-empty v-if="(!$slots.default) && $env.VUE_APP_DESIGNER && !!$attrs['vusion-node-path']"></s-empty>
</div>
</template>
 
<script>
import { addResizeListener, removeResizeListener } from '../../utils/dom';
import SEmpty from '../s-empty.vue';
 
const breakpoints = [
    { name: 'Huge', width: 1440 },
    { name: 'Large', width: 1200 },
    { name: 'Medium', width: 960 },
    { name: 'Small', width: 768 },
    { name: 'Mini', width: 480 },
];
 
const GAP_CONFIGS = {
    normal: 16,
    none: 0,
    mini: 4,
    small: 8,
    large: 24,
    huge: 32,
};
 
export default {
    name: 'u-grid-layout-column',
    components: {
        SEmpty,
    },
    props: {
        span: { type: Number, default: 1 },
        pull: Number,
        push: Number,
        offset: Number,
        mediaMini: Number,
        mediaSmall: Number,
        mediaMedium: Number,
        mediaLarge: Number,
        mediaHuge: Number,
        wrap: {
            type: Boolean,
            default: true,
        },
    },
    data() {
        return {
            parentVM: this.$parent,
            currentSpan: this.span,
        };
    },
    computed: {
        stack() {
            return breakpoints
                .map((point) => ({
                    name: point.name,
                    width: point.width,
                    span: this['media' + point.name],
                }))
                .filter((point) => point.span !== undefined);
        },
        commonStyle() {
            const left = this.push ? this.getPullPush(this.push) : 'auto';
            const right = this.pull ? this.getPullPush(this.pull) : 'auto';
            const marginLeft = this.getMarginLeft(this.offset);
            return { right, left, marginLeft };
        },
        responsiveStyle() {
            const width = this.currentSpan ? this.getWidth(this.currentSpan) : 'auto';
            return { width };
        },
    },
    watch: {
        currentSpan(span, oldSpan) {
            this.$emit('responsive', { span, oldSpan }, this);
        },
        span(span) {
            this.currentSpan = span;
        },
    },
    mounted() {
        addResizeListener(this.$el, this.onResize);
        this.onResize();
    },
    destroyed() {
        removeResizeListener(this.$el, this.onResize);
    },
    methods: {
        getPullPush(span, repeat) {
            const column = this.getColumnWidth(span, repeat);
            return `calc(${column.columnWidth} + ${column.gap}px * ${span})`;
        },
        getWidth(span, repeat) {
            const column = this.getColumnWidth(span, repeat);
            return `calc(${column.columnWidth} + ${column.gap}px * ${span - 1})`;
        },
        getMarginLeft(span, repeat) {
            if (!span)
                return undefined;
            const column = this.getColumnWidth(span, repeat);
            return `calc(${column.columnWidth} + ${column.gap}px * ${span} + ${column.gap / 2}px)`;
        },
        getColumnWidth(span, repeat) {
            repeat
                = repeat || this.$parent.repeat || this.$parent.$parent.repeat;
            const gap = this.$parent.gap ? GAP_CONFIGS[this.$parent.gap] : GAP_CONFIGS.normal;
            return {
                columnWidth: `(100% - ${gap}px * ${repeat}) / ${repeat} * ${span}`,
                gap,
            };
        },
        onResize() {
            const stack = this.stack;
            if (!stack.length)
                return;
            let span = this.span;
            const width = window.innerWidth;
            stack.forEach((point, index) => {
                if (width <= point.width && point.span !== undefined)
                    span = point.span;
            });
            this.currentSpan = span;
        },
    },
};
</script>
 
<style module>
.root {
    position: relative;
}
 
.root[mode="flex"] { display: flex; text-align: inherit; flex-wrap: wrap; }
.root[mode="flex"][nowrap] {flex-wrap: nowrap;}
/* 与主轴同向用 flex,否则用 align-self */
.root[mode="flex"][direction="horizontal"] > [width-stretch="true"],
.root[mode="flex"][direction="horizontal"] > [widthstretch="true"],
.root[mode="flex"]:not([direction]) > [width-stretch="true"],
.root[mode="flex"]:not([direction]) > [widthstretch="true"],
.root[mode="flex"][direction="vertical"] > [height-stretch="true"],
.root[mode="flex"][direction="vertical"] > [heightstretch="true"] { flex: 1 0 0; }
.root[mode="flex"][direction="vertical"] > [width-stretch="true"],
.root[mode="flex"][direction="vertical"] > [widthstretch="true"],
.root[mode="flex"]:not([direction]) > [height-stretch="true"],
.root[mode="flex"]:not([direction]) > [heightstretch="true"],
.root[mode="flex"][direction="horizontal"] > [height-stretch="true"],
.root[mode="flex"][direction="horizontal"] > [heightstretch="true"] { align-self: stretch; }
/* width-stretch 强制 width 为 unset */
.root[mode="flex"] > [width-stretch="true"],
.root[mode="flex"] > [widthstretch="true"] { width: unset !important; }
/* height-stretch 强制 height 为 unset */
.root[mode="flex"] > [height-stretch="true"],
.root[mode="flex"] > [heightstretch="true"] { height: unset !important; }
 
.root[mode="flex"][direction="vertical"] { flex-direction: column; }
 
.root[mode="flex"][justify="start"] { justify-content: flex-start; }
.root[mode="flex"][justify="center"] { justify-content: center; }
.root[mode="flex"][justify="end"] { justify-content: flex-end; }
.root[mode="flex"][justify="space-between"] { justify-content: space-between; }
.root[mode="flex"][justify="space-between"]::after { display: none; }
.root[mode="flex"][justify="space-around"] { justify-content: space-around; }
 
.root[mode="flex"][alignment="start"] { align-items: flex-start; align-content: flex-start; }
.root[mode="flex"][alignment="center"] { align-items: center; align-content: center; }
.root[mode="flex"][alignment="end"] { align-items: flex-end; align-content: flex-end; }
.root[mode="flex"][alignment="baseline"] { align-items: baseline; align-content: baseline; }
.root[mode="flex"][alignment="stretch"] { align-items: stretch; align-content: stretch; }
 
 
.root[mode="flex"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-normal);
}
.root[mode="flex"][direction="vertical"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-normal);
}
 
.root[mode="flex"][gap="shrink"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-shrink);
}
.root[mode="flex"][direction="vertical"][gap="shrink"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-shrink);
}
.root[mode="flex"][gap="none"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-none);
}
.root[mode="flex"][direction="vertical"][gap="none"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-none);
}
 
.root[mode="flex"][gap="mini"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-mini);
}
.root[mode="flex"][direction="vertical"][gap="mini"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-mini);
}
 
.root[mode="flex"][gap="small"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-small);
}
.root[mode="flex"][direction="vertical"][gap="small"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-small);
}
 
.root[mode="flex"][gap="large"] > *:not(:last-child) {
    margin-right: var(--grid-layout-column-content-gap-large);
}
.root[mode="flex"][direction="vertical"][gap="large"] > *:not(:last-child) {
    margin-bottom: var(--grid-layout-column-content-gap-large);
}
.root[mode="flex"][direction="vertical"] > *:nth-child(odd),
.root[mode="flex"][direction="vertical"] > *:nth-child(even) {
    margin-right: 0;
}
 
</style>