|
1 | 1 | <div wire:ignore |
2 | 2 | {{ $extraAttributes }} |
3 | | - x-data="{ pond: null, @if ($shouldWatch($attributes)) value: @entangle($attributes->wire('model')), oldValue: undefined @endif }" |
| 3 | + x-data="{ pond: null, wireId: null, @if ($shouldWatch($attributes)) value: @entangle($attributes->wire('model')), oldValue: undefined @endif }" |
4 | 4 | x-cloak |
5 | 5 | x-on:file-pond-clear.window=" |
6 | | - const id = $wire && $wire.__instance.id; |
7 | | - const sentId = $event.detail.id; |
8 | | - if (id && (sentId !== id)) { |
9 | | - return; |
| 6 | + if (! this.wireId || $event.detail.id !== this.wireId) { |
| 7 | + return; |
10 | 8 | } |
| 9 | +
|
11 | 10 | @if ($multiple) |
12 | 11 | pond.getFiles().forEach(file => pond.removeFile(file.id)); |
13 | 12 | @else |
14 | 13 | pond.removeFile(); |
15 | 14 | @endif |
16 | 15 | " |
17 | 16 | x-init=" |
18 | | - {{ $plugins ?? '' }} |
19 | | -
|
20 | | - @if ($shouldWatch($attributes)) |
21 | | - $watch('value', value => { |
22 | | - @if ($multiple) |
23 | | - const removeOldFiles = (newValue, oldValue) => { |
24 | | - if (newValue.length < oldValue.length) { |
25 | | - const difference = oldValue.filter(i => ! newValue.includes(i)); |
| 17 | + {{ $plugins ?? '' }} |
26 | 18 |
|
27 | | - difference.forEach(serverId => { |
28 | | - const file = pond.getFiles().find(f => f.serverId === serverId); |
29 | | -
|
30 | | - file && pond.removeFile(file.id); |
31 | | - }); |
32 | | - } |
33 | | - }; |
| 19 | + @if ($shouldWatch($attributes)) |
| 20 | + $watch('value', value => { |
| 21 | + @if ($multiple) |
| 22 | + const removeOldFiles = (newValue, oldValue) => { |
| 23 | + if (newValue.length < oldValue.length) { |
| 24 | + const difference = oldValue.filter(i => ! newValue.includes(i)); |
34 | 25 |
|
35 | | - if (this.oldValue !== undefined) { |
36 | | - try { |
37 | | - const files = Array.isArray(value) ? value : JSON.parse(String(value).split('livewire-files:')[1]); |
38 | | - const oldFiles = Array.isArray(this.oldValue) ? this.oldValue : JSON.parse(String(this.oldValue).split('livewire-files:')[1]); |
| 26 | + difference.forEach(serverId => { |
| 27 | + const file = pond.getFiles().find(f => f.serverId === serverId); |
39 | 28 |
|
40 | | - if (Array.isArray(files) && Array.isArray(oldFiles)) { |
41 | | - removeOldFiles(files, oldFiles); |
42 | | - } |
43 | | - } catch (e) {} |
| 29 | + file && pond.removeFile(file.id); |
| 30 | + }); |
44 | 31 | } |
| 32 | + }; |
45 | 33 |
|
46 | | - this.oldValue = value; |
47 | | - @else |
48 | | - ! value && pond.removeFile(); |
49 | | - @endif |
50 | | - }); |
51 | | - @endif |
| 34 | + if (this.oldValue !== undefined) { |
| 35 | + try { |
| 36 | + const files = Array.isArray(value) ? value : JSON.parse(String(value).split('livewire-files:')[1]); |
| 37 | + const oldFiles = Array.isArray(this.oldValue) ? this.oldValue : JSON.parse(String(this.oldValue).split('livewire-files:')[1]); |
52 | 38 |
|
53 | | - pond = FilePond.create($refs.input, { |
54 | | - {{ $jsonOptions() }} |
55 | | - {{-- Enhance for livewire support --}} |
56 | | - @if ($attributes->whereStartsWith('wire:model')->first()) |
57 | | - server: { |
58 | | - process: (fieldName, file, metadata, load, error, progress, abort, transfer, options) => { |
59 | | - @this.upload('{{ $attributes->wire('model')->value() }}', file, load, error, progress); |
60 | | - }, |
61 | | - revert: (filename, load) => { |
62 | | - @this.removeUpload('{{ $attributes->wire('model')->value() }}', filename, load); |
63 | | - }, |
64 | | - }, |
| 39 | + if (Array.isArray(files) && Array.isArray(oldFiles)) { |
| 40 | + removeOldFiles(files, oldFiles); |
| 41 | + } |
| 42 | + } catch (e) {} |
| 43 | + } |
| 44 | +
|
| 45 | + this.oldValue = value; |
| 46 | + @else |
| 47 | + ! value && pond.removeFile(); |
65 | 48 | @endif |
66 | | - {{ $optionsSlot ?? '' }} |
67 | 49 | }); |
| 50 | + @endif |
| 51 | +
|
| 52 | + $nextTick(function() { |
| 53 | + pond = FilePond.create($refs.input, { |
| 54 | + {{ $jsonOptions() }}, |
| 55 | + @if ($attributes->whereStartsWith('wire:model')->first()) |
| 56 | + server: { |
| 57 | + process: (fieldName, file, metadata, load, error, progress, abort, transfer, options) => { |
| 58 | + @this.upload('{{ $attributes->wire('model')->value() }}', file, load, error, progress); |
| 59 | + }, |
| 60 | + revert: (filename, load) => { |
| 61 | + @this.removeUpload('{{ $attributes->wire('model')->value() }}', filename, load); |
| 62 | + }, |
| 63 | + }, |
| 64 | + @endif |
| 65 | + {{ $optionsSlot ?? '' }} |
| 66 | + }); |
| 67 | + )); |
68 | 68 | " |
69 | 69 | > |
70 | 70 | <input x-ref="input" |
|
0 commit comments