inertiax-svelte 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +34 -0
- package/readme.md +3 -0
- package/src/App.svelte +17 -0
- package/src/Frame.svelte +36 -0
- package/src/Link.svelte +59 -0
- package/src/Render.svelte +25 -0
- package/src/SSR.svelte +8 -0
- package/src/createInertiaApp.js +63 -0
- package/src/index.js +8 -0
- package/src/link.js +55 -0
- package/src/page.js +6 -0
- package/src/remember.js +15 -0
- package/src/server.js +1 -0
- package/src/store.js +11 -0
- package/src/useForm.js +215 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Jonathan Reinink <jonathan@reinink.ca>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "inertiax-svelte",
|
|
3
|
+
"version": "1.0.15",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "The Svelte adapter for Inertia.js",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Jonathan Reinink <jonathan@reinink.ca>",
|
|
8
|
+
"Pedro Borges <oi@pedroborg.es>"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://inertiajs.com/",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/inertiajs/inertia.git",
|
|
14
|
+
"directory": "packages/svelte"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/inertiajs/inertia/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"svelte"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./src/index.js",
|
|
24
|
+
"./server": "./src/server.js"
|
|
25
|
+
},
|
|
26
|
+
"main": "src/index.js",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"svelte": "^5.0.0-next.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"lodash.clonedeep": "^4.5.0",
|
|
32
|
+
"lodash.isequal": "^4.5.0"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/readme.md
ADDED
package/src/App.svelte
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import Render, { h } from './Render.svelte'
|
|
3
|
+
import store from './store'
|
|
4
|
+
|
|
5
|
+
$: child = $store.component && h($store.component.default, $store.page.props)
|
|
6
|
+
$: layout = $store.component && $store.component.layout
|
|
7
|
+
$: components = layout
|
|
8
|
+
? Array.isArray(layout)
|
|
9
|
+
? layout
|
|
10
|
+
.concat(child)
|
|
11
|
+
.reverse()
|
|
12
|
+
.reduce((child, layout) => h(layout, $store.page.props, [child]))
|
|
13
|
+
: h(layout, $store.page.props, [child])
|
|
14
|
+
: child
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<Render {...components} />
|
package/src/Frame.svelte
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { onMount } from 'svelte'
|
|
3
|
+
import Render, { h } from './Render.svelte'
|
|
4
|
+
import store from './store'
|
|
5
|
+
import { router } from '@inertiajs/svelte'
|
|
6
|
+
import { setContext } from 'svelte';
|
|
7
|
+
|
|
8
|
+
export let src
|
|
9
|
+
|
|
10
|
+
export let id = Math.random()
|
|
11
|
+
setContext('inertia:frame-id', id)
|
|
12
|
+
$: setContext('inertia:frame-src', src)
|
|
13
|
+
|
|
14
|
+
$: components = $store.frames?.[id] && h($store.frames[id].component.default, $store.frames[id].props)
|
|
15
|
+
|
|
16
|
+
onMount(() => {
|
|
17
|
+
router.visit(src, {
|
|
18
|
+
target: id
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<div data-inertia-frame-id={id}>
|
|
25
|
+
{#if components}
|
|
26
|
+
<Render {...components} />
|
|
27
|
+
{:else}
|
|
28
|
+
<slot />
|
|
29
|
+
{/if}
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<style>
|
|
33
|
+
div {
|
|
34
|
+
display: contents;
|
|
35
|
+
}
|
|
36
|
+
</style>
|
package/src/Link.svelte
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { beforeUpdate } from 'svelte'
|
|
3
|
+
import { default as inertia } from './link'
|
|
4
|
+
|
|
5
|
+
export let href
|
|
6
|
+
export let as = 'a'
|
|
7
|
+
export let data = {}
|
|
8
|
+
export let method = 'get'
|
|
9
|
+
export let replace = false
|
|
10
|
+
export let preserveScroll = false
|
|
11
|
+
export let preserveState = null
|
|
12
|
+
export let only = []
|
|
13
|
+
export let headers = {}
|
|
14
|
+
export let queryStringArrayFormat = 'brackets'
|
|
15
|
+
|
|
16
|
+
beforeUpdate(() => {
|
|
17
|
+
if (as === 'a' && method.toLowerCase() !== 'get') {
|
|
18
|
+
console.warn(
|
|
19
|
+
`Creating POST/PUT/PATCH/DELETE <a> links is discouraged as it causes "Open Link in New Tab/Window" accessibility issues.\n\nPlease specify a more appropriate element using the "as" attribute. For example:\n\n<Link href="${href}" method="${method}" as="button">...</Link>`,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<svelte:element
|
|
26
|
+
this={as}
|
|
27
|
+
use:inertia={{
|
|
28
|
+
...(as !== 'a' ? { href } : {}),
|
|
29
|
+
data,
|
|
30
|
+
method,
|
|
31
|
+
replace,
|
|
32
|
+
preserveScroll,
|
|
33
|
+
preserveState: preserveState ?? method !== 'get',
|
|
34
|
+
only,
|
|
35
|
+
headers,
|
|
36
|
+
queryStringArrayFormat,
|
|
37
|
+
}}
|
|
38
|
+
{...as === 'a' ? { href } : {}}
|
|
39
|
+
{...$$restProps}
|
|
40
|
+
on:focus
|
|
41
|
+
on:blur
|
|
42
|
+
on:click
|
|
43
|
+
on:dblclick
|
|
44
|
+
on:mousedown
|
|
45
|
+
on:mousemove
|
|
46
|
+
on:mouseout
|
|
47
|
+
on:mouseover
|
|
48
|
+
on:mouseup
|
|
49
|
+
on:cancel-token
|
|
50
|
+
on:before
|
|
51
|
+
on:start
|
|
52
|
+
on:progress
|
|
53
|
+
on:finish
|
|
54
|
+
on:cancel
|
|
55
|
+
on:success
|
|
56
|
+
on:error
|
|
57
|
+
>
|
|
58
|
+
<slot />
|
|
59
|
+
</svelte:element>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<script context="module">
|
|
2
|
+
export const h = (component, props, children) => {
|
|
3
|
+
return {
|
|
4
|
+
component,
|
|
5
|
+
...(props ? { props } : {}),
|
|
6
|
+
...(children ? { children } : {}),
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<script>
|
|
12
|
+
import store from './store'
|
|
13
|
+
|
|
14
|
+
export let component
|
|
15
|
+
export let props = {}
|
|
16
|
+
export let children = []
|
|
17
|
+
</script>
|
|
18
|
+
|
|
19
|
+
{#if $store.component}
|
|
20
|
+
<svelte:component this={component} {...props}>
|
|
21
|
+
{#each children as child, index (component && component.length === index ? $store.key : null)}
|
|
22
|
+
<svelte:self {...child} />
|
|
23
|
+
{/each}
|
|
24
|
+
</svelte:component>
|
|
25
|
+
{/if}
|
package/src/SSR.svelte
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { router, setupProgress } from '@inertiajs/core'
|
|
2
|
+
import { render } from 'svelte/server';
|
|
3
|
+
import App from './App.svelte'
|
|
4
|
+
import SSR from './SSR.svelte'
|
|
5
|
+
import store from './store'
|
|
6
|
+
|
|
7
|
+
export default async function createInertiaApp({ id = 'app', resolve, setup, progress = {}, page }) {
|
|
8
|
+
const isServer = typeof window === 'undefined'
|
|
9
|
+
const el = isServer ? null : document.getElementById(id)
|
|
10
|
+
const initialPage = page || JSON.parse(el.dataset.page)
|
|
11
|
+
const resolveComponent = (name) => Promise.resolve(resolve(name))
|
|
12
|
+
|
|
13
|
+
await resolveComponent(initialPage.component).then((initialComponent) => {
|
|
14
|
+
store.set({
|
|
15
|
+
component: initialComponent,
|
|
16
|
+
page: initialPage,
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
if (!isServer) {
|
|
21
|
+
router.init({
|
|
22
|
+
initialPage,
|
|
23
|
+
resolveComponent,
|
|
24
|
+
swapComponent: async ({ component, page, preserveState }) => {
|
|
25
|
+
const targetFrame = page.target
|
|
26
|
+
if (targetFrame) store.update((current) => ({
|
|
27
|
+
...current,
|
|
28
|
+
frames: { ...current.frames, [targetFrame]: {component, props: page.props} }
|
|
29
|
+
}))
|
|
30
|
+
else store.update((current) => ({
|
|
31
|
+
component,
|
|
32
|
+
page,
|
|
33
|
+
frames: current.frames,
|
|
34
|
+
key: preserveState ? current.key : Date.now(),
|
|
35
|
+
}))
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
if (progress) {
|
|
40
|
+
setupProgress(progress)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return setup({
|
|
44
|
+
el,
|
|
45
|
+
App,
|
|
46
|
+
props: {
|
|
47
|
+
initialPage,
|
|
48
|
+
resolveComponent,
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isServer) {
|
|
54
|
+
const { html, head } = render(SSR, { props: {id, initialPage} })
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
body: html,
|
|
58
|
+
head: [
|
|
59
|
+
head
|
|
60
|
+
],
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { router } from '@inertiajs/core'
|
|
2
|
+
export { default as createInertiaApp } from './createInertiaApp'
|
|
3
|
+
export { default as inertia } from './link'
|
|
4
|
+
export { default as Link } from './Link.svelte'
|
|
5
|
+
export { default as Frame } from './Frame.svelte'
|
|
6
|
+
export { default as page } from './page'
|
|
7
|
+
export { default as remember } from './remember'
|
|
8
|
+
export { default as useForm } from './useForm'
|
package/src/link.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { mergeDataIntoQueryString, router, shouldIntercept } from '@inertiajs/core'
|
|
2
|
+
|
|
3
|
+
export default (node, options = {}) => {
|
|
4
|
+
const [href, data] = hrefAndData(options)
|
|
5
|
+
node.href = href
|
|
6
|
+
options.data = data
|
|
7
|
+
|
|
8
|
+
function fireEvent(name, eventOptions = {}) {
|
|
9
|
+
return node.dispatchEvent(new CustomEvent(name, eventOptions))
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function hrefAndData(options) {
|
|
13
|
+
return mergeDataIntoQueryString(
|
|
14
|
+
options.method || 'get',
|
|
15
|
+
node.href || options.href || '',
|
|
16
|
+
options.data || {},
|
|
17
|
+
options.queryStringArrayFormat || 'brackets',
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function visit(event) {
|
|
22
|
+
if (!node.href) {
|
|
23
|
+
throw new Error('Option "href" is required')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (shouldIntercept(event)) {
|
|
27
|
+
event.preventDefault()
|
|
28
|
+
|
|
29
|
+
router.visit(node.href, {
|
|
30
|
+
onCancelToken: () => fireEvent('cancel-token'),
|
|
31
|
+
onBefore: (visit) => fireEvent('before', { detail: { visit } }),
|
|
32
|
+
onStart: (visit) => fireEvent('start', { detail: { visit } }),
|
|
33
|
+
onProgress: (progress) => fireEvent('progress', { detail: { progress } }),
|
|
34
|
+
onFinish: (visit) => fireEvent('finish', { detail: { visit } }),
|
|
35
|
+
onCancel: () => fireEvent('cancel'),
|
|
36
|
+
onSuccess: (page) => fireEvent('success', { detail: { page } }),
|
|
37
|
+
onError: (errors) => fireEvent('error', { detail: { errors } }),
|
|
38
|
+
...options,
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
node.addEventListener('click', visit)
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
update(newOptions) {
|
|
47
|
+
const [href, data] = hrefAndData(newOptions)
|
|
48
|
+
node.href = href
|
|
49
|
+
options = { ...newOptions, data }
|
|
50
|
+
},
|
|
51
|
+
destroy() {
|
|
52
|
+
node.removeEventListener('click', visit)
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/page.js
ADDED
package/src/remember.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { router } from '@inertiajs/core'
|
|
2
|
+
import { onDestroy } from 'svelte'
|
|
3
|
+
import { writable } from 'svelte/store'
|
|
4
|
+
|
|
5
|
+
function useRemember(initialState, key) {
|
|
6
|
+
const restored = router.restore(key)
|
|
7
|
+
const store = writable(restored !== undefined ? restored : initialState)
|
|
8
|
+
const unsubscribe = store.subscribe((state) => router.remember(state, key))
|
|
9
|
+
|
|
10
|
+
onDestroy(unsubscribe)
|
|
11
|
+
|
|
12
|
+
return store
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default useRemember
|
package/src/server.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as default } from '@inertiajs/core/server'
|
package/src/store.js
ADDED
package/src/useForm.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { router } from '@inertiajs/core'
|
|
2
|
+
import isEqual from 'lodash.isequal'
|
|
3
|
+
import cloneDeep from 'lodash.clonedeep'
|
|
4
|
+
import { writable } from 'svelte/store'
|
|
5
|
+
import { getContext } from 'svelte'
|
|
6
|
+
|
|
7
|
+
function useForm(...args) {
|
|
8
|
+
const rememberKey = typeof args[0] === 'string' ? args[0] : null
|
|
9
|
+
const data = (typeof args[0] === 'string' ? args[1] : args[0]) || {}
|
|
10
|
+
const restored = rememberKey ? router.restore(rememberKey) : null
|
|
11
|
+
const frameId = getContext('inertia:frame-id')
|
|
12
|
+
const frameSrc = getContext('inertia:frame-src')
|
|
13
|
+
let defaults = cloneDeep(data)
|
|
14
|
+
let cancelToken = null
|
|
15
|
+
let recentlySuccessfulTimeoutId = null
|
|
16
|
+
let transform = (data) => data
|
|
17
|
+
|
|
18
|
+
const store = writable({
|
|
19
|
+
...(restored ? restored.data : data),
|
|
20
|
+
isDirty: false,
|
|
21
|
+
errors: restored ? restored.errors : {},
|
|
22
|
+
hasErrors: false,
|
|
23
|
+
progress: null,
|
|
24
|
+
wasSuccessful: false,
|
|
25
|
+
recentlySuccessful: false,
|
|
26
|
+
processing: false,
|
|
27
|
+
setStore(key, value) {
|
|
28
|
+
store.update((store) => {
|
|
29
|
+
return Object.assign(store, typeof key === 'string' ? { [key]: value } : key)
|
|
30
|
+
})
|
|
31
|
+
},
|
|
32
|
+
data() {
|
|
33
|
+
return Object.keys(data).reduce((carry, key) => {
|
|
34
|
+
carry[key] = this[key]
|
|
35
|
+
return carry
|
|
36
|
+
}, {})
|
|
37
|
+
},
|
|
38
|
+
transform(callback) {
|
|
39
|
+
transform = callback
|
|
40
|
+
|
|
41
|
+
return this
|
|
42
|
+
},
|
|
43
|
+
defaults(key, value) {
|
|
44
|
+
if (typeof key === 'undefined') {
|
|
45
|
+
defaults = Object.assign(defaults, cloneDeep(this.data()))
|
|
46
|
+
|
|
47
|
+
return this
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
defaults = Object.assign(defaults, cloneDeep(value ? { [key]: value } : key))
|
|
51
|
+
|
|
52
|
+
return this
|
|
53
|
+
},
|
|
54
|
+
reset(...fields) {
|
|
55
|
+
let clonedDefaults = cloneDeep(defaults)
|
|
56
|
+
if (fields.length === 0) {
|
|
57
|
+
this.setStore(clonedDefaults)
|
|
58
|
+
} else {
|
|
59
|
+
this.setStore(
|
|
60
|
+
Object.keys(clonedDefaults)
|
|
61
|
+
.filter((key) => fields.includes(key))
|
|
62
|
+
.reduce((carry, key) => {
|
|
63
|
+
carry[key] = clonedDefaults[key]
|
|
64
|
+
return carry
|
|
65
|
+
}, {}),
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return this
|
|
70
|
+
},
|
|
71
|
+
setError(key, value) {
|
|
72
|
+
this.setStore('errors', {
|
|
73
|
+
...this.errors,
|
|
74
|
+
...(value ? { [key]: value } : key),
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return this
|
|
78
|
+
},
|
|
79
|
+
clearErrors(...fields) {
|
|
80
|
+
this.setStore(
|
|
81
|
+
'errors',
|
|
82
|
+
Object.keys(this.errors).reduce(
|
|
83
|
+
(carry, field) => ({
|
|
84
|
+
...carry,
|
|
85
|
+
...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}),
|
|
86
|
+
}),
|
|
87
|
+
{},
|
|
88
|
+
),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
return this
|
|
92
|
+
},
|
|
93
|
+
submit(method, url, options = {}) {
|
|
94
|
+
const data = transform(this.data())
|
|
95
|
+
if (frameSrc) data.frameSrc = frameSrc
|
|
96
|
+
const _options = {
|
|
97
|
+
...options,
|
|
98
|
+
target: typeof(options.target) !== 'undefined' ? options.target : frameId,
|
|
99
|
+
onCancelToken: (token) => {
|
|
100
|
+
cancelToken = token
|
|
101
|
+
|
|
102
|
+
if (options.onCancelToken) {
|
|
103
|
+
return options.onCancelToken(token)
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
onBefore: (visit) => {
|
|
107
|
+
this.setStore('wasSuccessful', false)
|
|
108
|
+
this.setStore('recentlySuccessful', false)
|
|
109
|
+
clearTimeout(recentlySuccessfulTimeoutId)
|
|
110
|
+
|
|
111
|
+
if (options.onBefore) {
|
|
112
|
+
return options.onBefore(visit)
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
onStart: (visit) => {
|
|
116
|
+
this.setStore('processing', true)
|
|
117
|
+
|
|
118
|
+
if (options.onStart) {
|
|
119
|
+
return options.onStart(visit)
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
onProgress: (event) => {
|
|
123
|
+
this.setStore('progress', event)
|
|
124
|
+
|
|
125
|
+
if (options.onProgress) {
|
|
126
|
+
return options.onProgress(event)
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
onSuccess: async (page) => {
|
|
130
|
+
this.setStore('processing', false)
|
|
131
|
+
this.setStore('progress', null)
|
|
132
|
+
this.clearErrors()
|
|
133
|
+
this.setStore('wasSuccessful', true)
|
|
134
|
+
this.setStore('recentlySuccessful', true)
|
|
135
|
+
recentlySuccessfulTimeoutId = setTimeout(() => this.setStore('recentlySuccessful', false), 2000)
|
|
136
|
+
|
|
137
|
+
if (options.onSuccess) {
|
|
138
|
+
return options.onSuccess(page)
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
onError: (errors) => {
|
|
142
|
+
this.setStore('processing', false)
|
|
143
|
+
this.setStore('progress', null)
|
|
144
|
+
this.clearErrors().setError(errors)
|
|
145
|
+
|
|
146
|
+
if (options.onError) {
|
|
147
|
+
return options.onError(errors)
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
onCancel: () => {
|
|
151
|
+
this.setStore('processing', false)
|
|
152
|
+
this.setStore('progress', null)
|
|
153
|
+
|
|
154
|
+
if (options.onCancel) {
|
|
155
|
+
return options.onCancel()
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
onFinish: () => {
|
|
159
|
+
this.setStore('processing', false)
|
|
160
|
+
this.setStore('progress', null)
|
|
161
|
+
cancelToken = null
|
|
162
|
+
|
|
163
|
+
if (options.onFinish) {
|
|
164
|
+
return options.onFinish()
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (method === 'delete') {
|
|
170
|
+
router.delete(url, { ..._options, data })
|
|
171
|
+
} else {
|
|
172
|
+
router[method](url, data, _options)
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
get(url, options) {
|
|
176
|
+
this.submit('get', url, options)
|
|
177
|
+
},
|
|
178
|
+
post(url, options) {
|
|
179
|
+
this.submit('post', url, options)
|
|
180
|
+
},
|
|
181
|
+
put(url, options) {
|
|
182
|
+
this.submit('put', url, options)
|
|
183
|
+
},
|
|
184
|
+
patch(url, options) {
|
|
185
|
+
this.submit('patch', url, options)
|
|
186
|
+
},
|
|
187
|
+
delete(url, options) {
|
|
188
|
+
this.submit('delete', url, options)
|
|
189
|
+
},
|
|
190
|
+
cancel() {
|
|
191
|
+
if (cancelToken) {
|
|
192
|
+
cancelToken.cancel()
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
store.subscribe((form) => {
|
|
198
|
+
if (form.isDirty === isEqual(form.data(), defaults)) {
|
|
199
|
+
form.setStore('isDirty', !form.isDirty)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const hasErrors = Object.keys(form.errors).length > 0
|
|
203
|
+
if (form.hasErrors !== hasErrors) {
|
|
204
|
+
form.setStore('hasErrors', !form.hasErrors)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (rememberKey) {
|
|
208
|
+
router.remember({ data: form.data(), errors: form.errors }, rememberKey)
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
return store
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export default useForm
|