342 lines
8.3 KiB
Vue
342 lines
8.3 KiB
Vue
<template>
|
|
<v-row v-if="formState.ready">
|
|
<v-col>
|
|
<v-form ref="form">
|
|
<v-row>
|
|
<!-- {{ toUsers }} -->
|
|
<gz-error :error-box-message="formState.errorBoxMessage"></gz-error>
|
|
<v-col cols="12">
|
|
<gz-pick-list
|
|
:allow-no-selection="false"
|
|
:can-clear="false"
|
|
:aya-type="ayaTypes().User"
|
|
:show-edit-icon="false"
|
|
v-model="pickListSelectedUserId"
|
|
:label="$ay.t('UserList')"
|
|
ref="userPickList"
|
|
@input="addSelected()"
|
|
data-cy="pickListSelectedUserId"
|
|
></gz-pick-list>
|
|
<template v-for="item in toUsers">
|
|
<v-chip
|
|
color="primary"
|
|
outlined
|
|
:key="item.id"
|
|
class="ma-2"
|
|
close
|
|
@click:close="closeChip(item)"
|
|
>
|
|
{{ item.name }}
|
|
</v-chip>
|
|
</template>
|
|
</v-col>
|
|
|
|
<v-col cols="12">
|
|
<v-textarea
|
|
v-model="message"
|
|
:label="$ay.t('MemoMessage')"
|
|
ref="message"
|
|
:rules="[form().required(this, 'message')]"
|
|
auto-grow
|
|
@input="fieldValueChanged('message')"
|
|
></v-textarea>
|
|
</v-col>
|
|
</v-row>
|
|
</v-form>
|
|
</v-col>
|
|
</v-row>
|
|
</template>
|
|
|
|
<script>
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/* Xeslint-disable */
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
const FORM_KEY = "home-notify-direct";
|
|
|
|
export default {
|
|
async created() {
|
|
let vm = this;
|
|
try {
|
|
await initForm(vm);
|
|
|
|
vm.rights = window.$gz.role.fullRightsObject();
|
|
generateMenu(vm);
|
|
|
|
vm.formState.ready = true;
|
|
window.$gz.form.setFormState({
|
|
vm: vm,
|
|
dirty: false,
|
|
valid: true,
|
|
loading: false,
|
|
readOnly: false
|
|
});
|
|
window.$gz.eventBus.$on("menu-click", clickHandler);
|
|
//-------------
|
|
|
|
//------------------
|
|
} catch (err) {
|
|
vm.formState.ready = true;
|
|
window.$gz.errorHandler.handleFormError(err, vm);
|
|
}
|
|
},
|
|
async beforeRouteLeave(to, from, next) {
|
|
if (!this.formState.dirty) {
|
|
next();
|
|
return;
|
|
}
|
|
if ((await window.$gz.dialog.confirmLeaveUnsaved()) === true) {
|
|
next();
|
|
} else {
|
|
next(false);
|
|
}
|
|
},
|
|
beforeDestroy() {
|
|
window.$gz.eventBus.$off("menu-click", clickHandler);
|
|
},
|
|
components: {},
|
|
data() {
|
|
return {
|
|
pickListSelectedUserId: null,
|
|
items: [],
|
|
toUsers: [],
|
|
message: null,
|
|
formState: {
|
|
ready: false,
|
|
dirty: false,
|
|
valid: true,
|
|
readOnly: false,
|
|
loading: true,
|
|
errorBoxMessage: null,
|
|
appError: null,
|
|
serverError: {}
|
|
},
|
|
rights: window.$gz.role.fullRightsObject()
|
|
};
|
|
},
|
|
//WATCHERS
|
|
watch: {
|
|
formState: {
|
|
handler: function(val) {
|
|
//,oldval is available here too if necessary
|
|
if (this.formState.loading) {
|
|
return;
|
|
}
|
|
|
|
//enable / disable save button
|
|
|
|
let hasSelection = this.toUsers.length > 0;
|
|
if (val.dirty && val.valid && !val.readOnly && hasSelection) {
|
|
window.$gz.eventBus.$emit("menu-enable-item", FORM_KEY + ":save");
|
|
} else {
|
|
window.$gz.eventBus.$emit("menu-disable-item", FORM_KEY + ":save");
|
|
}
|
|
},
|
|
deep: true
|
|
}
|
|
},
|
|
computed: {
|
|
canSave: function() {
|
|
return this.formState.valid && this.formState.dirty;
|
|
}
|
|
},
|
|
methods: {
|
|
updateSave: function() {
|
|
let hasSelection = this.toUsers.length > 0;
|
|
//enable / disable save button
|
|
if (
|
|
this.formState.dirty &&
|
|
this.formState.valid &&
|
|
!this.formState.readOnly &&
|
|
hasSelection
|
|
) {
|
|
window.$gz.eventBus.$emit("menu-enable-item", FORM_KEY + ":save");
|
|
} else {
|
|
window.$gz.eventBus.$emit("menu-disable-item", FORM_KEY + ":save");
|
|
}
|
|
},
|
|
closeChip(item) {
|
|
let i = this.toUsers.findIndex(z => z.id == item.id);
|
|
if (i != -1) {
|
|
this.toUsers.splice(i, 1);
|
|
}
|
|
this.updateSave();
|
|
},
|
|
addSelected() {
|
|
let selected = this.$refs.userPickList.getFullSelectionValue();
|
|
if (selected == null) {
|
|
return;
|
|
}
|
|
//already in the list?
|
|
if (this.toUsers.find(z => z.id == selected.id)) {
|
|
return;
|
|
}
|
|
|
|
this.toUsers.push(selected);
|
|
this.pickListSelectedUserId = 0;
|
|
this.updateSave();
|
|
},
|
|
ayaTypes: function() {
|
|
return window.$gz.type;
|
|
},
|
|
|
|
form() {
|
|
return window.$gz.form;
|
|
},
|
|
fieldValueChanged(ref) {
|
|
if (
|
|
this.formState.ready &&
|
|
!this.formState.loading &&
|
|
!this.formState.readOnly
|
|
) {
|
|
window.$gz.form.fieldValueChanged(this, ref);
|
|
}
|
|
},
|
|
async submit() {
|
|
let vm = this;
|
|
|
|
vm.formState.loading = true;
|
|
|
|
//always submit from this form for the current logged in user id
|
|
let url = "notify/direct-message";
|
|
|
|
//clear any errors vm might be around from previous submit
|
|
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
|
try {
|
|
let userIdList = [];
|
|
if (vm.toUsers.length > 0) {
|
|
vm.toUsers.forEach(z => {
|
|
userIdList.push(z.id);
|
|
});
|
|
} else {
|
|
//just accept the single userid in the pickLkist
|
|
userIdList.push(vm.pickListSelectedUserId);
|
|
}
|
|
let res = await window.$gz.api.upsert(url, {
|
|
users: userIdList,
|
|
message: this.message
|
|
});
|
|
|
|
if (res.error) {
|
|
vm.formState.serverError = res.error;
|
|
window.$gz.form.setErrorBoxErrors(vm);
|
|
} else {
|
|
vm.pickListSelectedUserId = null;
|
|
vm.items = [];
|
|
vm.toUsers = [];
|
|
vm.message = null;
|
|
//Only a post, no data returned
|
|
window.$gz.form.setFormState({
|
|
vm: vm,
|
|
dirty: false
|
|
});
|
|
vm.updateSave();
|
|
}
|
|
} catch (error) {
|
|
window.$gz.errorHandler.handleFormError(error, vm);
|
|
} finally {
|
|
vm.loading = false;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
/////////////////////////////
|
|
//
|
|
//
|
|
function clickHandler(menuItem) {
|
|
if (!menuItem) {
|
|
return;
|
|
}
|
|
let m = window.$gz.menu.parseMenuItem(menuItem);
|
|
if (m.owner == FORM_KEY && !m.disabled) {
|
|
switch (m.key) {
|
|
case "save":
|
|
m.vm.submit();
|
|
break;
|
|
|
|
default:
|
|
window.$gz.eventBus.$emit(
|
|
"notify-warning",
|
|
FORM_KEY + "::context click: [" + m.key + "]"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
//////////////////////
|
|
//
|
|
//
|
|
function generateMenu(vm) {
|
|
let menuOptions = {
|
|
isMain: true,
|
|
icon: "$ayiCommentAlt",
|
|
title: "DirectNotification",
|
|
helpUrl: "home-notify-direct",
|
|
|
|
menuItems: []
|
|
};
|
|
|
|
if (vm.rights.change) {
|
|
menuOptions.menuItems.push({
|
|
title: "Save",
|
|
icon: "$ayiSave",
|
|
surface: true,
|
|
key: FORM_KEY + ":save",
|
|
vm: vm
|
|
});
|
|
}
|
|
|
|
window.$gz.eventBus.$emit("menu-change", menuOptions);
|
|
}
|
|
|
|
/////////////////////////////////
|
|
//
|
|
//
|
|
async function initForm(vm) {
|
|
await fetchTranslatedText(vm);
|
|
if (vm.$route.params.userIdList != undefined) {
|
|
await preFillSelection(vm);
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////
|
|
//
|
|
// Ensures UI translated text is available
|
|
//
|
|
async function fetchTranslatedText(vm) {
|
|
await window.$gz.translation.cacheTranslations([
|
|
"DirectNotification",
|
|
"UserList",
|
|
"MemoMessage",
|
|
"MemoToID"
|
|
]);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////
|
|
//
|
|
// Pre fill if pre-selected users
|
|
//
|
|
async function preFillSelection(vm) {
|
|
var idParam = vm.$route.params.userIdList;
|
|
let l = [];
|
|
if (typeof idParam == "string" && idParam.includes(",")) {
|
|
//just a random list of user ids seperated by commas
|
|
l = idParam.split(",").map(z => parseInt(z, 10));
|
|
} else {
|
|
//single value
|
|
l.push(parseInt(idParam, 10));
|
|
}
|
|
|
|
for (let i of l) {
|
|
if (vm.toUsers.find(z => z.id == i)) {
|
|
continue;
|
|
}
|
|
let res = await window.$gz.api.get("pick-list/list?ayaType=3&preId=" + i);
|
|
if (res.data && res.data.length > 0) {
|
|
vm.toUsers.push(res.data[0]);
|
|
}
|
|
}
|
|
}
|
|
</script>
|