| 1 |
export const Paginator = (taxonomy) => ({ |
| 2 |
loading: true, |
| 3 |
current: 1, |
| 4 |
selected: {}, |
| 5 |
data: [], |
| 6 |
selectedstring: '', |
| 7 |
selectedCount: 0, |
| 8 |
total: 0, |
| 9 |
pages: 0, |
| 10 |
taxonomy, |
| 11 |
objects: 0, |
| 12 |
formattedTotal: 0, |
| 13 |
searchTerm: null, |
| 14 |
searchText: '', |
| 15 |
initialised: false, |
| 16 |
async page(page) { |
| 17 |
this.loading = true; |
| 18 |
|
| 19 |
const { rest, nonce } = ag_admin; |
| 20 |
|
| 21 |
const data = new FormData; |
| 22 |
data.append('taxonomy', this.taxonomy); |
| 23 |
data.append('page', page); |
| 24 |
data.append('t', Date.now()); |
| 25 |
|
| 26 |
if (this.searchTerm) { |
| 27 |
data.append('search', this.searchTerm); |
| 28 |
} |
| 29 |
|
| 30 |
const q = new URLSearchParams(data).toString(); |
| 31 |
|
| 32 |
const response = await ( |
| 33 |
await ( |
| 34 |
fetch( |
| 35 |
`${rest}?${q}`, |
| 36 |
{ |
| 37 |
headers: { |
| 38 |
'X-WP-Nonce': nonce, |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
) |
| 43 |
) |
| 44 |
).json(); |
| 45 |
|
| 46 |
const { |
| 47 |
data: terms, |
| 48 |
page: current, |
| 49 |
total, |
| 50 |
pages, |
| 51 |
objects, |
| 52 |
} = response; |
| 53 |
|
| 54 |
this.objects = objects; |
| 55 |
this.data = terms; |
| 56 |
this.current = current; |
| 57 |
this.total = total; |
| 58 |
this.pages = pages; |
| 59 |
|
| 60 |
this.formattedTotal = Intl.NumberFormat().format(objects * total); |
| 61 |
|
| 62 |
this.loading = false; |
| 63 |
this.initialised = true; |
| 64 |
}, |
| 65 |
async init() { |
| 66 |
|
| 67 |
this.$watch('selected', value => console.log(value)); |
| 68 |
|
| 69 |
this.$watch('selected', (value) => { |
| 70 |
this.selectedstring = JSON.stringify(Object.fromEntries(Object.entries(value).filter(([, v]) => v))); |
| 71 |
this.selectedCount = Object.keys(Object.fromEntries(Object.entries(value).filter(([, v]) => v))).length; |
| 72 |
}); |
| 73 |
|
| 74 |
const { rest, nonce } = ag_admin; |
| 75 |
const data = new FormData; |
| 76 |
data.append('taxonomy', this.taxonomy); |
| 77 |
data.append('t', Date.now()); |
| 78 |
|
| 79 |
const q = new URLSearchParams(data).toString(); |
| 80 |
|
| 81 |
try { |
| 82 |
const selected = await ( |
| 83 |
await ( |
| 84 |
fetch( |
| 85 |
`${rest}/selected?${q}`, |
| 86 |
{ |
| 87 |
headers: { |
| 88 |
'X-WP-Nonce': nonce, |
| 89 |
} |
| 90 |
} |
| 91 |
) |
| 92 |
) |
| 93 |
).json(); |
| 94 |
|
| 95 |
this.selected = Object.keys(selected).length ? selected : {}; |
| 96 |
} catch (error) { |
| 97 |
const { load_error } = ag_content_params; |
| 98 |
|
| 99 |
document.querySelector('.wrap h2').insertAdjacentHTML('afterend', `<div class="notice notice-error"><p><strong>${load_error}</strong></p></div>`); |
| 100 |
|
| 101 |
window.scrollTo({ |
| 102 |
top: 0, |
| 103 |
left: 0, |
| 104 |
behavior: "smooth", |
| 105 |
}) |
| 106 |
this.selected = {}; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
this.$watch('searchTerm', (value) => this.page(1)); |
| 111 |
|
| 112 |
this.page(1); |
| 113 |
}, |
| 114 |
async all() { |
| 115 |
if (Object.keys(this.selected).length) { |
| 116 |
this.selected = {}; |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
this.loading = true; |
| 121 |
|
| 122 |
const { rest, nonce } = ag_admin; |
| 123 |
const data = new FormData; |
| 124 |
data.append('taxonomy', this.taxonomy); |
| 125 |
data.append('t', Date.now()); |
| 126 |
|
| 127 |
if (this.searchTerm) { |
| 128 |
data.append('search', this.searchTerm); |
| 129 |
} |
| 130 |
|
| 131 |
const q = new URLSearchParams(data).toString(); |
| 132 |
|
| 133 |
const selected = await( |
| 134 |
await( |
| 135 |
fetch( |
| 136 |
`${rest}/select-all?${q}`, |
| 137 |
{ |
| 138 |
headers: { |
| 139 |
'X-WP-Nonce': nonce, |
| 140 |
} |
| 141 |
} |
| 142 |
) |
| 143 |
) |
| 144 |
).json(); |
| 145 |
|
| 146 |
this.selected = Object.keys(selected).length ? selected : {}; |
| 147 |
|
| 148 |
this.loading = false; |
| 149 |
}, |
| 150 |
search(term) { |
| 151 |
this.searchTerm = term; |
| 152 |
|
| 153 |
if (!term) { |
| 154 |
this.searchText = ''; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
}) |
| 159 |
|