media_player_client/src/components/ModeDialog.vue

324 lines
9.7 KiB
Vue
Raw Normal View History

<template>
<q-dialog persistent v-model="show_dialog" @before-hide="resetData">
<q-card class="overflow-hidden" style="overflow-y: scroll; max-width: 45vw">
<q-form @submit="onSubmit">
<q-card-section class="q-ma-none q-pa-sm">
<div class="row">
2021-08-17 17:17:51 +08:00
<div class="col-auto text-h6">
{{
type == 1
2021-08-24 15:20:01 +08:00
? $t("add mode")
: type == 2
2021-08-24 15:20:01 +08:00
? $t("edit mode")
2021-08-25 17:30:02 +08:00
: $t("add mode")
}}
</div>
2021-08-17 17:17:51 +08:00
<q-space />
<div>
<q-btn
:loading="loading"
flat
round
icon="close"
color="red"
v-close-popup
>
<q-tooltip>
{{ $t("close") }}
</q-tooltip>
</q-btn>
</div>
</div>
</q-card-section>
<q-separator />
<q-card-section style="max-height: 50vh; width: 45vw" class="scroll">
<q-list>
2021-08-24 15:20:01 +08:00
<q-item>
<q-item-label>{{ $t("parent group") }}:</q-item-label>
</q-item>
2021-08-24 15:20:01 +08:00
<q-item class="q-pa-none q-ma-none">
<q-item-section style="padding-right: 10px">
<q-tree
ref="tree"
class="scroll"
:class="loading ? 'disable_tree' : ''"
v-model:selected="selected"
:nodes="tree_nodes"
default-expand-all
node-key="uuid"
labelKey="name"
filter="group filter"
:filter-method="treeNodesFilter"
>
<template v-slot:default-header="prop">
<q-item
class="full-width"
:class="
prop.tree.selected == prop.key ? 'item-selected-bg' : ''
"
>
<q-item-section avatar>
<q-icon
:name="'img:source_icon/group.png'"
color="orange"
size="28px"
class="q-mr-sm"
/>
</q-item-section>
<q-item-section>
<div class="text-weight-bold text-primary">
{{ prop.node.name }}
</div>
</q-item-section>
</q-item>
</template>
</q-tree>
</q-item-section>
</q-item>
<q-item>
<q-item-section>
<q-input
autofocus
:loading="loading"
:disable="loading"
filled
v-model="name"
2021-08-24 15:20:01 +08:00
:label="$t('mode name')"
:hint="$t('please input mode name')"
lazy-rules
:rules="[
(val) =>
(val && val.length > 0) || $t('Please type something'),
]"
@keydown="
(evt) => {
if (evt.keyCode == 13) {
$refs?.accept?.click();
}
}
"
>
<template v-if="name" v-slot:append>
<q-icon
name="cancel"
@click.stop="name = null"
class="cursor-pointer"
/>
</template>
</q-input>
</q-item-section>
</q-item>
2021-09-07 18:03:50 +08:00
<q-item>
<q-item-section>
<q-input
autofocus
:loading="loading"
:disable="loading"
filled
type="number"
min="0"
max="65535"
v-model.number="index"
:label="$t('mode index')"
:hint="$t('please input mode index')"
lazy-rules
:rules="[checkIndex]"
@keydown="
(evt) => {
if (evt.keyCode == 13) {
$refs?.accept?.click();
}
}
"
>
</q-input>
</q-item-section>
</q-item>
</q-list>
</q-card-section>
<q-separator />
<q-card-actions align="right">
<q-btn
:loading="loading"
flat
:label="$t('Cancel')"
color="primary"
v-close-popup
/>
<q-btn
ref="accept"
flat
:label="$t('Accept')"
:loading="loading"
type="submit"
color="primary"
/>
</q-card-actions>
</q-form>
</q-card>
</q-dialog>
</template>
<style scoped>
.disable_tree {
background: #9e9e9e;
cursor: wait;
pointer-events: none;
}
</style>
<script lang="ts">
import { defineComponent, ref, watch, computed } from "vue";
import { useStore } from "src/store";
import GlobalData from "src/common/GlobalData";
import { useQuasar } from "quasar";
import { useI18n } from "vue-i18n";
2021-09-07 18:03:50 +08:00
import { api } from "boot/axios";
import { HttpProtocol } from "src/entities/HttpProtocol";
export default defineComponent({
2021-08-24 15:20:01 +08:00
name: "ComponentModeDialog",
setup() {
let $store = useStore();
let $q = useQuasar();
let $t = useI18n();
let show_dialog = ref(false);
let type = ref(1);
let name = ref(null);
2021-09-07 18:03:50 +08:00
let index = ref(0);
2021-08-10 15:09:11 +08:00
let uuid = ref("");
const selected: any = ref(null);
let loading = ref(false);
const tree_nodes = computed({
2021-08-24 15:20:01 +08:00
get: () => $store.state.mode_tree,
2021-08-10 15:09:11 +08:00
set: (val) => {},
});
watch(
() => selected.value,
(newValue, oldValue) => {
if (newValue == null) {
selected.value = "";
}
}
);
2021-08-24 15:20:01 +08:00
const requestAddMode = async () => {
2021-08-10 15:09:11 +08:00
let response = await GlobalData.getInstance()
.getCurrentClient()
2021-09-07 18:03:50 +08:00
?.addMode(selected.value, name.value ?? "", index.value);
2021-08-10 15:09:11 +08:00
if (response) {
$q.notify({
color: response.success ? "positive" : "negative",
icon: response.success ? "done" : "warning",
message:
2021-08-24 15:20:01 +08:00
$t.t("add mode") +
2021-08-10 15:09:11 +08:00
(response.success ? $t.t("success") : $t.t("fail")) +
"!",
position: "top",
2021-12-22 11:25:04 +08:00
timeout: 1500,
2021-08-10 15:09:11 +08:00
});
}
};
2021-08-24 15:20:01 +08:00
const requestEditMode = async () => {
2021-08-10 15:09:11 +08:00
let response = await GlobalData.getInstance()
.getCurrentClient()
2021-09-07 18:03:50 +08:00
?.editMode(uuid.value, name.value ?? "", index.value);
2021-08-10 15:09:11 +08:00
if (response) {
$q.notify({
color: response.success ? "positive" : "negative",
icon: response.success ? "done" : "warning",
message:
2021-08-24 15:20:01 +08:00
$t.t("edit mode") +
2021-08-10 15:09:11 +08:00
(response.success ? $t.t("success") : $t.t("fail")) +
"!",
position: "top",
2021-12-22 11:25:04 +08:00
timeout: 1500,
2021-08-10 15:09:11 +08:00
});
}
};
return {
show_dialog,
type,
name,
2021-09-07 18:03:50 +08:00
index,
2021-08-10 15:09:11 +08:00
uuid,
selected,
loading,
tree_nodes,
showDialog(options: any) {
if (options) {
type.value = options.type ?? 1;
2021-08-10 15:09:11 +08:00
if (type.value == 2) {
name.value = options.data?.name ?? null;
2021-08-24 15:20:01 +08:00
selected.value = options.data?.item_data?.group_uuid ?? null;
uuid.value = options.data?.item_data?.uuid ?? null;
2021-09-07 18:03:50 +08:00
index.value = options.data?.item_data?.number ?? 0;
2021-08-24 15:20:01 +08:00
} else {
selected.value = options.data?.uuid ?? null;
uuid.value = options.data?.uuid ?? null;
2021-08-10 15:09:11 +08:00
}
}
show_dialog.value = true;
},
resetData() {
loading.value = false;
(selected.value = null), (name.value = null);
type.value = 1;
2021-08-10 15:09:11 +08:00
},
treeNodesFilter(node: any, filter: any) {
2021-08-24 15:20:01 +08:00
return node.is_group;
},
2021-09-07 18:03:50 +08:00
checkIndex(val: number) {
return new Promise(async (resolve, reject) => {
if (val < 0) {
resolve($t.t("Please type 0~65535"));
} else {
try {
let url = GlobalData.getInstance().createCurrentRequestUrl();
if (!url) {
url = new URL(window.location.hostname);
}
url.pathname = HttpProtocol.RequestCheckModeIndex;
url.searchParams.append("index", index.value.toString());
let response = (await api.get(url.toString())).data as boolean;
if (typeof response != "boolean") {
try {
response = JSON.parse(response);
} catch {
response = false;
}
}
if (!response) {
resolve($t.t("index exised!"));
} else {
resolve(true);
}
} catch (e) {
resolve($t.t("check error"));
}
}
});
},
async onSubmit() {
loading.value = true;
try {
2021-08-24 15:20:01 +08:00
await (type.value == 2 ? requestEditMode() : requestAddMode());
show_dialog.value = false;
} catch {}
loading.value = false;
},
};
},
});
</script>