All files / src/components WorkflowChart.vue

87.1% Statements 27/31
0% Branches 0/4
100% Functions 2/2
87.1% Lines 27/31
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                                                  1x 1x 1x 1x                                       7x                 7x   7x   7x             1x 1x     2x 2x       7x       10x     10x           3x 6x   3x     44x     16x         16x     8x         8x     10x   10x 10x   10x                          
<template>
    <div class="workflow-chart">
        <state
            v-for="state in layoutStates"
            :id="state.id"
            :ref="state.id"
            :key="state.id"
            :label="state.label"
            :center="state.center"
            :stylingClass="state.stylingClass"
            @click="$emit('state-click', $event)" />
        <chart-transition
            v-for="transition in layoutTransitions"
            :id="transition.id"
            :ref="transition.id"
            :key="transition.id"
            :transitionPath="transition.path"
            :transitionPathRadius="transitionPathRadius"
            :label="transition.label"
            :stylingClass="transition.stylingClass"
            @click="$emit('transition-click', $event)" />
    </div>
</template>
 
<script>
import Transition from './Transition.vue';
import State from './State.vue';
import Layout from '../lib/layout';
import { size } from '../lib/DivElement';
 
 
export default {
    name: 'WorkflowChart',
    components: {
        State,
        chartTransition: Transition,
    },
    props: {
        transitions: {
            type: Array,
            required: true,
        },
        states: {
            type: Array,
            required: true,
        },
        stateSemantics: {
            type: Array,
            default: () => [],
        },
        transitionPathRadius: {
            type: Number,
            required: false,
            default: 12,
        },
    },
    data() {
        const layout = new Layout();
 
        this.setup(layout);
 
        return {
            layout,
            ...this.propsOf(layout),
        };
    },
    watch: {
        transitions() {
            this.setup(this.layout);
            this.updateComputedLayout();
        },
        states() {
            this.setup(this.layout);
            this.updateComputedLayout();
        },
    },
    mounted() {
        return this.emitSize();
    },
    methods: {
        emitSize() {
            this.$emit('size-change', this.layout.size);
        },
        propsOf(layout) {
            return {
                layoutTransitions: layout.transitions,
                layoutStates: layout.states,
            };
        },
        updateComputedLayout() {
            for (const [key, value] of Object.entries(this.propsOf(this.layout))) {
                this[key] = value;
            }
            this.emitSize();
        },
        includeSizeWithClasses(classes) {
            return item => ({ ...item, ...size.ofDivWith(item, classes) });
        },
        addStateStylingClass(state) {
            for(const semantic of this.stateSemantics){
                if(semantic.id === state.id){
                    return { ...state, stylingClass:semantic.classname };
                }
            }
            return state;
        },
        addTransitionStylingClass(transition){
            for(const semantic of this.stateSemantics){
                if(semantic.id === transition.target){
                    return { ...transition, stylingClass:semantic.classname };
                }
            }
            return transition;
        },
        setup(layout) {
            const states = this.states
                .map(this.includeSizeWithClasses('vue-workflow-chart-state')).map(this.addStateStylingClass);
            layout.setStates(states);
            const transitions = this.transitions.
                map(this.includeSizeWithClasses('vue-workflow-chart-transition-label')).map(this.addTransitionStylingClass);
            layout.setTransitions(transitions);
        },
    },
};
</script>
<style lang='scss'>
@import '../styling.scss';
</style>
<style lang='scss' scoped>
div {
    position: absolute;
}
</style>