summaryrefslogtreecommitdiffstats
path: root/src/components/DockerHostDialog.vue
blob: d2cde8186e0379b862ee536858a7b8a1d997200b (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
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
<template>
    <form @submit.prevent="submit">
        <div ref="modal" class="modal fade" tabindex="-1" data-bs-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 id="exampleModalLabel" class="modal-title">
                            {{ $t("Setup Docker Host") }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" />
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="docker-name" class="form-label">{{ $t("Friendly Name") }}</label>
                            <input id="docker-name" v-model="dockerHost.name" type="text" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label for="docker-type" class="form-label">{{ $t("Connection Type") }}</label>
                            <select id="docker-type" v-model="dockerHost.dockerType" class="form-select">
                                <option v-for="type in connectionTypes" :key="type" :value="type">{{ $t(type) }}</option>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="docker-daemon" class="form-label">{{ $t("Docker Daemon") }}</label>
                            <input id="docker-daemon" v-model="dockerHost.dockerDaemon" type="text" class="form-control" required>

                            <div class="form-text">
                                {{ $t("Examples") }}:
                                <ul>
                                    <li>/var/run/docker.sock</li>
                                    <li>http://localhost:2375</li>
                                    <li>https://localhost:2376 (TLS)</li>
                                </ul>
                            </div>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button v-if="id" type="button" class="btn btn-danger" :disabled="processing" @click="deleteConfirm">
                            {{ $t("Delete") }}
                        </button>
                        <button type="button" class="btn btn-warning" :disabled="processing" @click="test">
                            {{ $t("Test") }}
                        </button>
                        <button type="submit" class="btn btn-primary" :disabled="processing">
                            <div v-if="processing" class="spinner-border spinner-border-sm me-1"></div>
                            {{ $t("Save") }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <Confirm ref="confirmDelete" btn-style="btn-danger" :yes-text="$t('Yes')" :no-text="$t('No')" @yes="deleteDockerHost">
        {{ $t("deleteDockerHostMsg") }}
    </Confirm>
</template>

<script lang="ts">
import { Modal } from "bootstrap";
import Confirm from "./Confirm.vue";

export default {
    components: {
        Confirm,
    },
    props: {},
    emits: [ "added", "deleted" ],
    data() {
        return {
            modal: null,
            processing: false,
            id: null,
            connectionTypes: [ "socket", "tcp" ],
            dockerHost: {
                name: "",
                dockerDaemon: "",
                dockerType: "",
                // Do not set default value here, please scroll to show()
            }
        };
    },

    mounted() {
        this.modal = new Modal(this.$refs.modal);
    },
    methods: {

        /**
         * Confirm deletion of docker host
         * @returns {void}
         */
        deleteConfirm() {
            this.modal.hide();
            this.$refs.confirmDelete.show();
        },

        /**
         * Show specified docker host
         * @param {number} dockerHostID ID of host to show
         * @returns {void}
         */
        show(dockerHostID) {
            if (dockerHostID) {
                let found = false;

                this.id = dockerHostID;

                for (let n of this.$root.dockerHostList) {
                    if (n.id === dockerHostID) {
                        this.dockerHost = n;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    this.$root.toastError("Docker Host not found!");
                }

            } else {
                this.id = null;
                this.dockerHost = {
                    name: "",
                    dockerType: "socket",
                    dockerDaemon: "/var/run/docker.sock",
                };
            }

            this.modal.show();
        },

        /**
         * Add docker host
         * @returns {void}
         */
        submit() {
            this.processing = true;
            this.$root.getSocket().emit("addDockerHost", this.dockerHost, this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.modal.hide();

                    // Emit added event, doesn't emit edit.
                    if (! this.id) {
                        this.$emit("added", res.id);
                    }

                }
            });
        },

        /**
         * Test the docker host
         * @returns {void}
         */
        test() {
            this.processing = true;
            this.$root.getSocket().emit("testDockerHost", this.dockerHost, (res) => {
                this.$root.toastRes(res);
                this.processing = false;
            });
        },

        /**
         * Delete this docker host
         * @returns {void}
         */
        deleteDockerHost() {
            this.processing = true;
            this.$root.getSocket().emit("deleteDockerHost", this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.$emit("deleted", this.id);
                    this.modal.hide();
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
@import "../assets/vars.scss";

.dark {
    .modal-dialog .form-text, .modal-dialog p {
        color: $dark-font-color;
    }
}
</style>