Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SliderField.vue « Form « Components « vue « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efa0db8aa7d6a84d71121fa9d53ca09a5c4aa213 (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
<template>
    <span :class="getClassNames" @click.stop="toggleSwitch" :title="getTitle">
        <span class="input-slider-bar"></span>
        <span class="input-slider-button"></span>
        <input type="checkbox"
               ref="checkbox"
               :id="id"
               :name="name"
               v-model="model"
               v-on="listeners"/>
    </span>
</template>

<script>
    import LocalisationService from '@js/Services/LocalisationService';

    export default {
        props: {
            value: {
                type   : Boolean,
                default: false
            },
            title: {
                type   : String,
                default: ''
            },
            id   : {
                type   : String,
                default: undefined
            },
            name : {
                type   : String,
                default: undefined
            }
        },

        data() {
            return {
                model: this.value
            };
        },

        computed: {
            getClassNames() {
                return 'input-slider ' + (this.value ? 'on':'off');
            },
            getTitle() {
                if(this.title.length === 0) return;

                return LocalisationService.translate(this.title);
            },
            listeners() {
                let listeners = {};

                for(let key in this.$listeners) {
                    if(this.$listeners.hasOwnProperty(key) && key !== 'input' && key !== 'change') {
                        listeners[key] = this.$listeners[key];
                    }
                }

                return listeners;
            }
        },

        methods: {
            toggleSwitch() {
                this.model = !this.model;
            }
        },

        watch: {
            value(value) {
                if(this.model !== value) this.model = value;
            },
            model(value) {
                if(this.value !== value) {
                    this.$emit('change', value);
                    this.$emit('input', value);
                }
            }
        }
    };
</script>

<style lang="scss">
.input-slider {
    position    : relative;
    display     : inline-flex;
    align-items : center;
    width       : 1.5em;
    height      : 1em;
    top         : -1px;
    cursor      : pointer;

    .input-slider-bar {
        margin           : .25em 0;
        background-color : var(--element-active-fg-color);
        border-radius    : var(--button-border-radius);
        width            : 100%;
        display          : inline-block;
        height           : .5em;
    }

    .input-slider-button {
        background-color : var(--element-active-bg-color);
        border-radius    : var(--button-border-radius);
        border           : 1px solid var(--element-active-fg-color);
        position         : absolute;
        top              : .1em;
        left             : .1em;
        height           : .8em;
        width            : .8em;
        transition       : left .15s ease-in-out;
    }

    &.on {
        .input-slider-button {
            left : .55rem;
        }
    }

    input {
        display : none;
    }
}
</style>