rename template

This commit is contained in:
Pete Matsyburka committed 2026-09-23 10:59:54 +03:00
1 parent 9678cd1286
commit ca009252ac
8 files changed
+159 -34

No files matched your search

+42 -26
View File
@@ -13,6 +13,7 @@ import NativeAction from './elements/native_action'
import NativeSearch from './elements/native_search'
import NativeModal from './elements/native_modal'
import NativeEvent from './elements/native_event'
import FormPrompt from './elements/form_prompt'
import InfiniteScroll from './elements/infinite_scroll'
import ModalButton from './elements/modal_button'
import FileDropzone from './elements/file_dropzone'
@@ -128,6 +129,7 @@ safeRegisterElement('native-action', NativeAction)
safeRegisterElement('native-search', NativeSearch)
safeRegisterElement('native-modal', NativeModal)
safeRegisterElement('native-event', NativeEvent)
safeRegisterElement('form-prompt', FormPrompt)
safeRegisterElement('infinite-scroll', InfiniteScroll)
safeRegisterElement('modal-button', ModalButton)
safeRegisterElement('file-dropzone', FileDropzone)
@@ -186,6 +188,7 @@ safeRegisterElement('date-placeholder', DatePlaceholder)
safeRegisterElement('template-builder', class extends HTMLElement {
connectedCallback () {
document.addEventListener('turbo:submit-end', this.onSubmit)
document.addEventListener('template-builder:update', this.onSheetSubmit)
document.addEventListener('turbo:before-cache', this.onBeforeCache)
this.appElem = document.createElement('div')
@@ -238,36 +241,48 @@ safeRegisterElement('template-builder', class extends HTMLElement {
this.appendChild(this.appElem)
}
onSubmit = (e) => {
if (e.detail.success) {
if (e.detail?.formSubmission?.formElement?.id === 'submitters_form') {
e.detail.fetchResponse.response.json().then((data) => {
this.component.template.submitters = data.submitters
})
}
onSubmit = async (e) => {
const form = e.detail.formSubmission?.formElement
if (e.detail?.formSubmission?.formElement?.action?.endsWith('/prefillable_fields')) {
e.detail.fetchResponse.response.text().then((data) => {
const doc = new DOMParser().parseFromString(data, 'text/html')
const fragment = doc.querySelector('turbo-stream template').content
if (!e.detail.success || !form) return
const prefillableUuidsIndex = {}
const data = {}
fragment.querySelectorAll('[name="field_uuid"]').forEach((field) => {
prefillableUuidsIndex[field.value] = true
})
new FormData(form).forEach((value, key) => { data[key] = value })
this.component.template.fields.forEach((field) => {
if (prefillableUuidsIndex[field.uuid]) {
field.prefillable = true
field.readonly = true
} else if (field.prefillable) {
delete field.prefillable
delete field.readonly
}
})
})
}
this.applySubmission(form.id, form.action, await e.detail.fetchResponse.responseText, data)
}
onSheetSubmit = (e) => {
this.applySubmission(e.detail.form, e.detail.action, e.detail.body, {})
}
applySubmission (formId, action, body, data) {
if (formId === 'submitters_form') {
this.component.template.submitters = JSON.parse(body).submitters
} else if (action.endsWith('/prefillable_fields')) {
const doc = new DOMParser().parseFromString(body, 'text/html')
const fragment = doc.querySelector('turbo-stream template').content
const prefillableUuidsIndex = {}
fragment.querySelectorAll('[name="field_uuid"]').forEach((field) => {
prefillableUuidsIndex[field.value] = true
})
this.component.template.fields.forEach((field) => {
if (prefillableUuidsIndex[field.uuid]) {
field.prefillable = true
field.readonly = true
} else if (field.prefillable) {
delete field.prefillable
delete field.readonly
}
})
} else if (data['template[name]']) {
this.component.template.name = data['template[name]']
document.title = data['template[name]']
}
}
@@ -278,6 +293,7 @@ safeRegisterElement('template-builder', class extends HTMLElement {
disconnectedCallback () {
document.removeEventListener('turbo:submit-end', this.onSubmit)
document.removeEventListener('template-builder:update', this.onSheetSubmit)
document.removeEventListener('turbo:before-cache', this.onBeforeCache)
this.app?.unmount()
+33
View File
@@ -0,0 +1,33 @@
export default class extends HTMLElement {
connectedCallback () {
this.addEventListener('click', this.prompt)
document.addEventListener('turbo:submit-end', this.onSubmitEnd)
}
disconnectedCallback () {
this.removeEventListener('click', this.prompt)
document.removeEventListener('turbo:submit-end', this.onSubmitEnd)
}
get form () {
return this.querySelector('form')
}
prompt = () => {
const label = this.form.querySelector('label')
const input = this.form.querySelector(`#${CSS.escape(label.htmlFor)}`)
const value = window.prompt(label.textContent.trim(), input.value)?.trim()
if (!value || value === input.value) return
input.value = value
this.form.requestSubmit()
}
onSubmitEnd = (e) => {
if (e.detail.success && e.detail.formSubmission?.formElement === this.form && this.dataset.refresh !== 'false') {
window.Turbo.visit(window.location.href, { action: 'replace' })
}
}
}
+20 -5
View File
@@ -1,16 +1,27 @@
export default class extends HTMLElement {
connectedCallback () {
this.addEventListener('click', this.dispatch)
if (this.dataset.on === 'submit') {
document.addEventListener('turbo:submit-end', this.onSubmitEnd)
} else {
this.addEventListener('click', this.dispatch)
}
}
disconnectedCallback () {
document.removeEventListener('turbo:submit-end', this.onSubmitEnd)
this.removeEventListener('click', this.dispatch)
}
dispatch = async () => {
const bridge = window.webkit?.messageHandlers?.modal
onSubmitEnd = async (e) => {
const form = e.detail.formSubmission?.formElement
if (!bridge || this.dataset.loading) return
if (!e.detail.success || !form || !this.contains(form)) return
this.post(JSON.stringify({ form: form.id, action: form.action, body: await e.detail.fetchResponse.responseText }))
}
dispatch = async () => {
if (this.dataset.loading) return
let detail = this.dataset.detail || 'null'
@@ -30,6 +41,10 @@ export default class extends HTMLElement {
}
}
bridge.postMessage({ action: 'dispatch', name: this.dataset.name, detail })
this.post(detail)
}
post (detail) {
window.webkit?.messageHandlers?.modal?.postMessage({ action: 'dispatch', name: this.dataset.name, detail, dismiss: this.dataset.dismiss || 'true' })
}
}
+1
View File
@@ -13,6 +13,7 @@ export default class extends HTMLElement {
if (e.detail.formSubmission?.formElement?.method === 'get') return
if (this.dataset.closeAfterSubmit === 'false') return
if (e.detail.formSubmission?.formElement?.dataset?.closeOnSubmit === 'false') return
if (e.detail.formSubmission?.formElement?.closest('native-event[data-on="submit"]')) return
window.webkit?.messageHandlers?.modal?.postMessage({ action: 'submitted' })
}
@@ -103,6 +103,42 @@
name="buttons"
/>
<template v-else>
<form-prompt
v-if="nativeApp && editable"
data-refresh="false"
>
<native-action
data-placement="title"
:data-label="t('name')"
>
<form
class="hidden"
method="post"
:action="`/templates/${template.id}`"
>
<input
type="hidden"
name="_method"
value="put"
autocomplete="off"
>
<input
type="hidden"
name="authenticity_token"
:value="authenticityToken"
autocomplete="off"
>
<label for="template_name_prompt">{{ t('name') }}</label>
<input
id="template_name_prompt"
type="hidden"
name="template[name]"
:value="template.name"
autocomplete="off"
>
</form>
</native-action>
</form-prompt>
<native-action
v-if="withSendButton && nativeApp"
:data-label="t('send')"
+7
View File
@@ -1,4 +1,5 @@
const en = {
name: 'Name',
fixed: 'Fixed',
default: 'Default',
save_as_custom_field: 'Save as custom field',
@@ -243,6 +244,7 @@ const en = {
}
const es = {
name: 'Nombre',
fixed: 'Fijo',
default: 'Predeterminado',
save_as_custom_field: 'Guardar como personalizado',
@@ -487,6 +489,7 @@ const es = {
}
const it = {
name: 'Nome',
fixed: 'Fisso',
default: 'Predefinito',
save_as_custom_field: 'Salva come personalizzato',
@@ -731,6 +734,7 @@ const it = {
}
const pt = {
name: 'Nome',
fixed: 'Fixo',
default: 'Padrão',
save_as_custom_field: 'Salvar como personalizado',
@@ -975,6 +979,7 @@ const pt = {
}
const fr = {
name: 'Nom',
fixed: 'Fixe',
default: 'Par défaut',
save_as_custom_field: 'Enregistrer comme personnalisé',
@@ -1219,6 +1224,7 @@ const fr = {
}
const de = {
name: 'Name',
fixed: 'Fest',
default: 'Standard',
save_as_custom_field: 'Als benutzerdefiniert speichern',
@@ -1463,6 +1469,7 @@ const de = {
}
const nl = {
name: 'Naam',
fixed: 'Vast',
default: 'Standaard',
save_as_custom_field: 'Opslaan als aangepast',