init
This commit is contained in:
52 files changed
+5299
No files matched your search
@@ -0,0 +1,5 @@
|
||||
import Card from './components/Card.vue'
|
||||
|
||||
Nova.booting(app => {
|
||||
app.component('nova-form-card', Card)
|
||||
})
|
||||
@@ -0,0 +1,409 @@
|
||||
<template>
|
||||
<div class="nova-form-card bg-white dark:bg-gray-800 rounded-lg shadow">
|
||||
<form class="nova-form-card__form" @submit.prevent="submit" autocomplete="off">
|
||||
<div v-if="form.title" class="nova-form-card__header">
|
||||
<h3 class="nova-form-card__title">{{ form.title }}</h3>
|
||||
</div>
|
||||
|
||||
<fieldset class="nova-form-card__fieldset" :disabled="submitDisabled">
|
||||
<div v-if="fields.length" class="nova-form-card__body">
|
||||
<div
|
||||
v-for="(field, index) in fields"
|
||||
:key="field.uniqueKey || field.attribute"
|
||||
:class="fieldWrapperClass(field)"
|
||||
>
|
||||
<component
|
||||
:is="fieldComponent(field)"
|
||||
:index="index"
|
||||
:resource="resource"
|
||||
:resource-name="resourceName"
|
||||
:resource-id="resourceId"
|
||||
:field="field"
|
||||
:errors="validationErrors"
|
||||
:form-unique-id="formUniqueId"
|
||||
:show-help-text="true"
|
||||
:sync-endpoint="syncEndpoint"
|
||||
mode="form-card"
|
||||
@field-changed="clearFieldError(field)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<div class="nova-form-card__footer">
|
||||
<component
|
||||
:is="buttonComponent"
|
||||
as="button"
|
||||
:class="{ 'nova-form-card__fallback-submit': !hasNovaButton }"
|
||||
:label="submitText"
|
||||
:loading="processing"
|
||||
size="large"
|
||||
type="submit"
|
||||
variant="solid"
|
||||
:disabled="submitDisabled"
|
||||
@click.prevent="submit"
|
||||
>
|
||||
{{ buttonText }}
|
||||
</component>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
class FallbackErrors {
|
||||
constructor(errors = {}) {
|
||||
this.record(errors)
|
||||
}
|
||||
|
||||
all() {
|
||||
return this.errors
|
||||
}
|
||||
|
||||
has(field) {
|
||||
if (!field) {
|
||||
return false
|
||||
}
|
||||
|
||||
return Object.prototype.hasOwnProperty.call(this.errors, field)
|
||||
|| Object.keys(this.errors).some(error => error.startsWith(`${field}.`) || error.startsWith(`${field}[`))
|
||||
}
|
||||
|
||||
first(field) {
|
||||
return this.get(field)[0]
|
||||
}
|
||||
|
||||
get(field) {
|
||||
return this.errors[field] || []
|
||||
}
|
||||
|
||||
record(errors = {}) {
|
||||
this.errors = errors
|
||||
}
|
||||
|
||||
clear(field = null) {
|
||||
if (!field) {
|
||||
this.errors = {}
|
||||
return
|
||||
}
|
||||
|
||||
Object.keys(this.errors)
|
||||
.filter(error => error === field || error.startsWith(`${field}.`) || error.startsWith(`${field}[`))
|
||||
.forEach(error => delete this.errors[error])
|
||||
}
|
||||
}
|
||||
|
||||
function novaErrors() {
|
||||
return typeof LaravelNova !== 'undefined' && LaravelNova.Errors
|
||||
? LaravelNova.Errors
|
||||
: FallbackErrors
|
||||
}
|
||||
|
||||
function makeErrors(errors = {}) {
|
||||
const Errors = novaErrors()
|
||||
|
||||
return new Errors(errors)
|
||||
}
|
||||
|
||||
export default {
|
||||
provide() {
|
||||
return {
|
||||
removeFile: this.removeFile,
|
||||
}
|
||||
},
|
||||
|
||||
props: {
|
||||
card: {
|
||||
type: Object,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
fields: [],
|
||||
errors: makeErrors(),
|
||||
processing: false,
|
||||
formUniqueId: `nova-form-card-${Math.random().toString(36).slice(2)}`,
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
form() {
|
||||
return this.card.formCard || {}
|
||||
},
|
||||
|
||||
resourceName() {
|
||||
return this.card.resourceName || this.form.resourceName || 'nova-form-card'
|
||||
},
|
||||
|
||||
resourceId() {
|
||||
return this.card.resourceId || this.form.resourceId || 'form-card'
|
||||
},
|
||||
|
||||
resource() {
|
||||
return this.form.resource || {}
|
||||
},
|
||||
|
||||
submitText() {
|
||||
return this.form.submitText || this.translate('nova-form-card.submit', 'Submit')
|
||||
},
|
||||
|
||||
loadingText() {
|
||||
return this.translate('nova-form-card.submitting', 'Submitting...')
|
||||
},
|
||||
|
||||
canSubmit() {
|
||||
return this.form.canSubmit !== false
|
||||
},
|
||||
|
||||
submitDisabled() {
|
||||
return this.processing || !this.canSubmit
|
||||
},
|
||||
|
||||
syncEndpoint() {
|
||||
if (!this.form.syncEndpoint || !this.form.token) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const separator = this.form.syncEndpoint.includes('?') ? '&' : '?'
|
||||
|
||||
return `${this.form.syncEndpoint}${separator}token=${encodeURIComponent(this.form.token)}`
|
||||
},
|
||||
|
||||
buttonComponent() {
|
||||
return this.resolveNovaUi('Button') || 'button'
|
||||
},
|
||||
|
||||
hasNovaButton() {
|
||||
return this.buttonComponent !== 'button'
|
||||
},
|
||||
|
||||
buttonText() {
|
||||
return this.processing && !this.hasNovaButton
|
||||
? this.loadingText
|
||||
: this.submitText
|
||||
},
|
||||
|
||||
validationErrors() {
|
||||
return this.errors
|
||||
},
|
||||
},
|
||||
|
||||
created() {
|
||||
this.syncFields()
|
||||
},
|
||||
|
||||
watch: {
|
||||
'card.formCard.fields': {
|
||||
deep: true,
|
||||
handler() {
|
||||
this.syncFields()
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
methods: {
|
||||
syncFields() {
|
||||
this.fields = (this.form.fields || []).map(field => ({ ...field }))
|
||||
},
|
||||
|
||||
componentName(field) {
|
||||
if (field.prefixComponent === false) {
|
||||
return field.component
|
||||
}
|
||||
|
||||
if (field.displayOnly) {
|
||||
return field.component === 'line-field'
|
||||
? `index-${field.component}`
|
||||
: `detail-${field.component}`
|
||||
}
|
||||
|
||||
return `form-${field.component}`
|
||||
},
|
||||
|
||||
fieldComponent(field) {
|
||||
const name = this.componentName(field)
|
||||
|
||||
return this.resolveNovaComponent(name) || name
|
||||
},
|
||||
|
||||
fieldWrapperClass(field) {
|
||||
return {
|
||||
'nova-form-card__display-field': field.displayOnly,
|
||||
}
|
||||
},
|
||||
|
||||
resolveNovaComponent(name) {
|
||||
const novaComponents = typeof Nova !== 'undefined'
|
||||
? Nova.app?._context?.components
|
||||
: null
|
||||
|
||||
const components = this.$?.appContext?.components
|
||||
|| novaComponents
|
||||
|| {}
|
||||
|
||||
return components[name]
|
||||
|| components[this.camelize(name)]
|
||||
|| components[this.upperFirst(this.camelize(name))]
|
||||
},
|
||||
|
||||
resolveNovaUi(name) {
|
||||
return typeof LaravelNovaUi !== 'undefined'
|
||||
? LaravelNovaUi[name]
|
||||
: null
|
||||
},
|
||||
|
||||
camelize(name) {
|
||||
return String(name).replace(/-(\w)/g, (_, character) => character.toUpperCase())
|
||||
},
|
||||
|
||||
upperFirst(value) {
|
||||
value = String(value)
|
||||
|
||||
return value.charAt(0).toUpperCase() + value.slice(1)
|
||||
},
|
||||
|
||||
clearFieldError(field) {
|
||||
const key = field.validationKey || field.attribute
|
||||
|
||||
if (key && this.errors && typeof this.errors.clear === 'function') {
|
||||
this.errors.clear(key)
|
||||
}
|
||||
},
|
||||
|
||||
formDataFromFields() {
|
||||
const formData = new FormData()
|
||||
|
||||
this.fields.forEach(field => {
|
||||
if (field.displayOnly) {
|
||||
return
|
||||
}
|
||||
|
||||
if (typeof field.fill === 'function') {
|
||||
field.fill(formData)
|
||||
return
|
||||
}
|
||||
|
||||
formData.append(field.attribute, field.value ?? '')
|
||||
})
|
||||
|
||||
return formData
|
||||
},
|
||||
|
||||
submissionPayload() {
|
||||
const payload = new FormData()
|
||||
|
||||
payload.append('token', this.form.token)
|
||||
|
||||
this.formDataFromFields().forEach((value, key) => {
|
||||
payload.append(`fields[${key}]`, value)
|
||||
})
|
||||
|
||||
return payload
|
||||
},
|
||||
|
||||
submit() {
|
||||
if (this.submitDisabled) {
|
||||
return
|
||||
}
|
||||
|
||||
this.processing = true
|
||||
this.errors = makeErrors()
|
||||
|
||||
Nova.request()
|
||||
.post(this.form.endpoint, this.submissionPayload())
|
||||
.then(response => {
|
||||
this.handleResponse(response.data || {})
|
||||
})
|
||||
.catch(error => {
|
||||
const response = error.response || {}
|
||||
|
||||
if (response.status === 422) {
|
||||
this.errors = makeErrors(response.data?.errors || {})
|
||||
return
|
||||
}
|
||||
|
||||
Nova.error(response.data?.message || this.couldNotSubmitMessage())
|
||||
})
|
||||
.finally(() => {
|
||||
this.processing = false
|
||||
})
|
||||
},
|
||||
|
||||
removeFile() {
|
||||
Nova.error(this.translate(
|
||||
'nova-form-card.could_not_submit',
|
||||
'The form could not be submitted.',
|
||||
))
|
||||
|
||||
return Promise.reject(new Error('Form cards do not delete existing files.'))
|
||||
},
|
||||
|
||||
handleResponse(response) {
|
||||
if (response.type === 'error') {
|
||||
Nova.error(response.message || this.couldNotSubmitMessage())
|
||||
return
|
||||
}
|
||||
|
||||
if (response.message) {
|
||||
Nova.success(response.message)
|
||||
}
|
||||
|
||||
if (response.type === 'redirect' && response.redirect?.url) {
|
||||
const redirect = response.redirect
|
||||
|
||||
if (redirect.remote) {
|
||||
Nova.visit({ url: redirect.url, remote: true })
|
||||
return
|
||||
}
|
||||
|
||||
Nova.visit(redirect.url, redirect.options || {})
|
||||
return
|
||||
}
|
||||
|
||||
if (response.refresh) {
|
||||
this.refreshPage()
|
||||
}
|
||||
},
|
||||
|
||||
refreshPage() {
|
||||
if (typeof Nova.visit === 'function') {
|
||||
Nova.visit(window.location.pathname + window.location.search, {
|
||||
preserveScroll: true,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
window.location.reload()
|
||||
},
|
||||
|
||||
couldNotSubmitMessage() {
|
||||
return this.translate('nova-form-card.could_not_submit', 'The form could not be submitted.')
|
||||
},
|
||||
|
||||
translate(key, fallback) {
|
||||
if (typeof this.__ === 'function') {
|
||||
const translated = this.__(key)
|
||||
|
||||
return translated === key ? fallback : translated
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined' && typeof window.__ === 'function') {
|
||||
const translated = window.__(key)
|
||||
|
||||
return translated === key ? fallback : translated
|
||||
}
|
||||
|
||||
if (typeof Nova !== 'undefined' && typeof Nova.__ === 'function') {
|
||||
const translated = Nova.__(key)
|
||||
|
||||
return translated === key ? fallback : translated
|
||||
}
|
||||
|
||||
return fallback
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
Reference in new issue
Block a user