init
This commit is contained in:
52 files changed
+5299
No files matched your search
@@ -0,0 +1,72 @@
|
||||
.nova-form-card {
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.nova-form-card__form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 100%;
|
||||
}
|
||||
|
||||
.nova-form-card__header {
|
||||
border-bottom: 1px solid rgba(229, 231, 235, 1);
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
|
||||
.dark .nova-form-card__header {
|
||||
border-bottom-color: rgba(55, 65, 81, 1);
|
||||
}
|
||||
|
||||
.nova-form-card__title {
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
line-height: 1.5rem;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.nova-form-card__fieldset {
|
||||
border: 0;
|
||||
margin: 0;
|
||||
min-width: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.nova-form-card__body {
|
||||
padding: 0.75rem 0;
|
||||
}
|
||||
|
||||
.nova-form-card__display-field {
|
||||
padding-left: 1.5rem;
|
||||
padding-right: 1.5rem;
|
||||
}
|
||||
|
||||
.nova-form-card__footer {
|
||||
align-items: center;
|
||||
border-top: 1px solid rgba(229, 231, 235, 1);
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
|
||||
.dark .nova-form-card__footer {
|
||||
border-top-color: rgba(55, 65, 81, 1);
|
||||
}
|
||||
|
||||
.nova-form-card__fallback-submit {
|
||||
align-items: center;
|
||||
background-color: rgb(14, 165, 233);
|
||||
border-radius: 0.25rem;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
display: inline-flex;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 700;
|
||||
height: 2.25rem;
|
||||
justify-content: center;
|
||||
padding: 0 0.75rem;
|
||||
}
|
||||
|
||||
.nova-form-card__fallback-submit:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.65;
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,9 @@
|
||||
# Translation Notes
|
||||
|
||||
`messages.php` contains backend translation strings and supports normal PHP translator comments.
|
||||
|
||||
`card.json` files are loaded by `Nova::translations()` for the Vue card. JSON does not support comments, so keep those files as valid JSON. The current frontend keys are:
|
||||
|
||||
- `nova-form-card.submit`: default submit button text.
|
||||
- `nova-form-card.submitting`: loading text while a submission is in progress.
|
||||
- `nova-form-card.could_not_submit`: fallback error toast when a request fails without a server message.
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"nova-form-card.submit": "Submit",
|
||||
"nova-form-card.submitting": "Submitting...",
|
||||
"nova-form-card.could_not_submit": "The form could not be submitted."
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Button Labels
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| These labels are used by the backend defaults. The frontend card has
|
||||
| matching keys in card.json because Nova consumes JSON translations.
|
||||
|
|
||||
*/
|
||||
|
||||
'submit' => 'Submit',
|
||||
'submitting' => 'Submitting...',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Submission Errors
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| These messages are returned by the submit endpoint. Keep them short enough
|
||||
| to work in Nova toast notifications and validation responses.
|
||||
|
|
||||
*/
|
||||
|
||||
'cannot_submit' => 'This form card cannot be submitted.',
|
||||
'not_authorized_to_submit' => 'This form card is not authorized for submission.',
|
||||
'fields_payload_must_be_object' => 'The fields payload must be an object.',
|
||||
'invalid_data' => 'The given data was invalid.',
|
||||
'could_not_submit' => 'The form could not be submitted.',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Developer-Facing Exceptions
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The unsupported_field message is shown to developers during serialization.
|
||||
| Preserve the :field and :supported placeholders in custom translations.
|
||||
|
|
||||
*/
|
||||
|
||||
'unsupported_field' => 'The [:field] field is not supported by nova-form-card. Supported fields are: :supported.',
|
||||
'app_key_required' => 'An application key is required to sign form card submissions.',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Signed Submit Token Errors
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| These describe why a submitted form token could not be trusted. They are
|
||||
| intentionally specific for logs and debugging, but are usually returned as
|
||||
| a generic authorization failure to users.
|
||||
|
|
||||
*/
|
||||
|
||||
'token' => [
|
||||
'missing' => 'The form card token is missing.',
|
||||
'invalid_signature' => 'The form card token signature is invalid.',
|
||||
'missing_card_class' => 'The form card token does not contain a card class.',
|
||||
'expired' => 'The form card token has expired.',
|
||||
'wrong_user' => 'The form card token belongs to another user.',
|
||||
'invalid_card_class' => 'The form card token references an invalid card class.',
|
||||
'invalid_payload' => 'The form card token payload is invalid.',
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Supported Field Names
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| These names are only used in developer-facing unsupported-field messages.
|
||||
|
|
||||
*/
|
||||
|
||||
'fields' => [
|
||||
'text' => 'Text',
|
||||
'textarea' => 'Textarea',
|
||||
'select' => 'Select',
|
||||
'boolean' => 'Boolean',
|
||||
'number' => 'Number',
|
||||
'date' => 'Date',
|
||||
'file' => 'File',
|
||||
'image' => 'Image',
|
||||
'audio' => 'Audio',
|
||||
'avatar' => 'Avatar',
|
||||
'email' => 'Email',
|
||||
'url' => 'URL',
|
||||
'password' => 'Password',
|
||||
'hidden' => 'Hidden',
|
||||
'id' => 'ID',
|
||||
'date_time' => 'DateTime',
|
||||
'currency' => 'Currency',
|
||||
'color' => 'Color',
|
||||
'code' => 'Code',
|
||||
'country' => 'Country',
|
||||
'timezone' => 'Timezone',
|
||||
'multi_select' => 'MultiSelect',
|
||||
'boolean_group' => 'BooleanGroup',
|
||||
'key_value' => 'KeyValue',
|
||||
'slug' => 'Slug',
|
||||
'token' => 'Token',
|
||||
'badge' => 'Badge',
|
||||
'heading' => 'Heading',
|
||||
'line' => 'Line',
|
||||
'stack' => 'Stack',
|
||||
'sparkline' => 'Sparkline',
|
||||
'status' => 'Status',
|
||||
'gravatar' => 'Gravatar',
|
||||
'ui_avatar' => 'UiAvatar',
|
||||
'password_confirmation' => 'PasswordConfirmation',
|
||||
],
|
||||
];
|
||||
Reference in new issue
Block a user