2022-09-20 19:59:52 +00:00
|
|
|
<template>
|
|
|
|
<div class="flex flex-wrap flex-col">
|
|
|
|
<transition v-if="stateReady" name="fade" mode="out-in">
|
2022-10-26 14:44:47 +00:00
|
|
|
<div key="2">
|
2023-10-14 16:24:44 +00:00
|
|
|
<create-form-base-modal :show="showInitialFormModal" @form-generated="formGenerated"
|
|
|
|
@close="showInitialFormModal=false"
|
|
|
|
/>
|
2022-09-20 19:59:52 +00:00
|
|
|
<form-editor v-if="!workspacesLoading" ref="editor"
|
2022-10-26 14:44:47 +00:00
|
|
|
class="w-full flex flex-grow"
|
2023-09-05 14:25:33 +00:00
|
|
|
:error="error"
|
2023-05-21 17:34:47 +00:00
|
|
|
@on-save="formInitialHash=null"
|
2022-09-20 19:59:52 +00:00
|
|
|
/>
|
|
|
|
<div v-else class="text-center mt-4 py-6">
|
2023-10-14 16:24:44 +00:00
|
|
|
<loader class="h-6 w-6 text-nt-blue mx-auto" />
|
2022-09-20 19:59:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-10-02 18:38:41 +00:00
|
|
|
import store from '~/store'
|
2022-09-20 19:59:52 +00:00
|
|
|
import Form from 'vform'
|
2023-10-14 16:24:44 +00:00
|
|
|
import { mapState, mapActions } from 'vuex'
|
|
|
|
import initForm from '../../mixins/form_editor/initForm.js'
|
2023-01-21 11:57:37 +00:00
|
|
|
import SeoMeta from '../../mixins/seo-meta.js'
|
2023-10-14 16:24:44 +00:00
|
|
|
import CreateFormBaseModal from '../../components/pages/forms/create/CreateFormBaseModal.vue'
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2022-10-02 18:38:41 +00:00
|
|
|
const loadTemplates = function () {
|
|
|
|
store.commit('open/templates/startLoading')
|
|
|
|
store.dispatch('open/templates/loadIfEmpty').then(() => {
|
2022-11-16 10:56:49 +00:00
|
|
|
store.commit('open/templates/stopLoading')
|
2022-10-02 18:38:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
export default {
|
|
|
|
name: 'CreateForm',
|
2023-10-14 16:24:44 +00:00
|
|
|
components: { CreateFormBaseModal },
|
2022-11-16 10:56:49 +00:00
|
|
|
|
2022-12-22 10:55:17 +00:00
|
|
|
mixins: [initForm, SeoMeta],
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2023-10-14 16:24:44 +00:00
|
|
|
beforeRouteEnter (to, from, next) {
|
2022-10-02 18:38:41 +00:00
|
|
|
loadTemplates()
|
|
|
|
next()
|
|
|
|
},
|
|
|
|
|
2023-05-21 17:34:47 +00:00
|
|
|
beforeRouteLeave (to, from, next) {
|
|
|
|
if (this.isDirty()) {
|
|
|
|
return this.alertConfirm('Changes you made may not be saved. Are you sure want to leave?', () => {
|
|
|
|
window.onbeforeunload = null
|
|
|
|
next()
|
|
|
|
}, () => {})
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
},
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
middleware: 'auth',
|
|
|
|
|
2023-10-14 16:24:44 +00:00
|
|
|
data () {
|
2022-09-20 19:59:52 +00:00
|
|
|
return {
|
2022-12-22 10:55:17 +00:00
|
|
|
metaTitle: 'Create a new Form',
|
2022-09-20 19:59:52 +00:00
|
|
|
stateReady: false,
|
|
|
|
loading: false,
|
|
|
|
error: '',
|
2023-05-21 17:34:47 +00:00
|
|
|
showInitialFormModal: false,
|
|
|
|
formInitialHash: null
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
...mapState({
|
|
|
|
workspaces: state => state['open/workspaces'].content,
|
|
|
|
workspacesLoading: state => state['open/workspaces'].loading,
|
|
|
|
user: state => state.auth.user
|
|
|
|
}),
|
|
|
|
form: {
|
2023-10-14 16:24:44 +00:00
|
|
|
get () {
|
2022-09-20 19:59:52 +00:00
|
|
|
return this.$store.state['open/working_form'].content
|
|
|
|
},
|
|
|
|
/* We add a setter */
|
2023-10-14 16:24:44 +00:00
|
|
|
set (value) {
|
2022-09-20 19:59:52 +00:00
|
|
|
this.$store.commit('open/working_form/set', value)
|
|
|
|
}
|
|
|
|
},
|
2023-10-14 16:24:44 +00:00
|
|
|
workspace () {
|
2022-09-20 19:59:52 +00:00
|
|
|
return this.$store.getters['open/workspaces/getCurrent']()
|
2023-10-14 16:24:44 +00:00
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
2023-10-14 16:24:44 +00:00
|
|
|
workspace () {
|
2022-09-20 19:59:52 +00:00
|
|
|
if (this.workspace) {
|
|
|
|
this.form.workspace_id = this.workspace.id
|
|
|
|
}
|
|
|
|
},
|
2023-10-14 16:24:44 +00:00
|
|
|
user () {
|
2022-09-20 19:59:52 +00:00
|
|
|
this.stateReady = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-10-14 16:24:44 +00:00
|
|
|
mounted () {
|
2023-05-21 17:34:47 +00:00
|
|
|
window.onbeforeunload = () => {
|
|
|
|
if (this.isDirty()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:56:49 +00:00
|
|
|
this.initForm()
|
2023-05-21 17:34:47 +00:00
|
|
|
this.formInitialHash = this.hashString(JSON.stringify(this.form.data()))
|
2022-11-16 10:56:49 +00:00
|
|
|
if (this.$route.query.template !== undefined && this.$route.query.template) {
|
|
|
|
const template = this.$store.getters['open/templates/getBySlug'](this.$route.query.template)
|
|
|
|
if (template && template.structure) {
|
2023-10-14 16:24:44 +00:00
|
|
|
this.form = new Form({ ...this.form.data(), ...template.structure })
|
2022-10-02 18:38:41 +00:00
|
|
|
}
|
2023-03-26 10:54:12 +00:00
|
|
|
} else {
|
|
|
|
// No template loaded, ask how to start
|
|
|
|
this.showInitialFormModal = true
|
2022-10-02 18:38:41 +00:00
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
this.closeAlert()
|
|
|
|
this.loadWorkspaces()
|
|
|
|
|
|
|
|
this.stateReady = this.user !== null
|
|
|
|
},
|
|
|
|
|
2023-10-14 16:24:44 +00:00
|
|
|
created () {},
|
|
|
|
unmounted () {},
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
methods: {
|
|
|
|
...mapActions({
|
|
|
|
loadWorkspaces: 'open/workspaces/loadIfEmpty'
|
|
|
|
}),
|
2023-10-14 16:24:44 +00:00
|
|
|
formGenerated (form) {
|
|
|
|
this.form = new Form({ ...this.form.data(), ...form })
|
2023-05-21 17:34:47 +00:00
|
|
|
},
|
|
|
|
isDirty () {
|
|
|
|
return !this.loading && this.formInitialHash && this.formInitialHash !== this.hashString(JSON.stringify(this.form.data()))
|
|
|
|
},
|
|
|
|
hashString (str, seed = 0) {
|
|
|
|
let h1 = 0xdeadbeef ^ seed
|
|
|
|
let h2 = 0x41c6ce57 ^ seed
|
|
|
|
for (let i = 0, ch; i < str.length; i++) {
|
|
|
|
ch = str.charCodeAt(i)
|
|
|
|
h1 = Math.imul(h1 ^ ch, 2654435761)
|
|
|
|
h2 = Math.imul(h2 ^ ch, 1597334677)
|
|
|
|
}
|
|
|
|
|
|
|
|
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
|
|
|
|
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
|
|
|
|
|
|
|
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|