@worksafevictoria/wcl7.5 1.13.0 → 1.14.0-beta.1

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.
@@ -1,284 +0,0 @@
1
- <template>
2
- <card-group
3
- :title="title"
4
- :title-tag="titleTag"
5
- :background="background"
6
- :cards="displayedCards"
7
- :loading="loading"
8
- :columns="columns"
9
- :show-load-more="showMore && hasMorePages"
10
- :class="`wcl-browse-content card_group__card--col-${columns}`"
11
- :card-browse-content="true"
12
- @loadMore="() => loadMoreCards(false)"
13
- >
14
- <template
15
- v-if="enableBundleFilter && bundle.length > 1"
16
- v-slot:cardGroupFilter
17
- >
18
- <switcher
19
- v-if="enableBundleFilter"
20
- :options="bundle"
21
- class="wcl-browse-content__switcher"
22
- :get-option-alias="getBundleAlias"
23
- @switched="onSwitcher"
24
- @reset="onSwitcher"
25
- />
26
- </template>
27
-
28
- <template v-if="filters.taxonomies.length > 0" v-slot:cardGroupPreContent>
29
- <directory-filters
30
- class="wcl-browse-content__filter"
31
- :filters="filters"
32
- :results="allCards.total"
33
- :is-filtering="loading"
34
- @onFilter="onFilter"
35
- @onReset="reset"
36
- />
37
- </template>
38
- </card-group>
39
- </template>
40
-
41
- <script setup>
42
- import CardGroup from '../../SubComponents/CardGroup/index.vue'
43
- import DirectoryFilters from '../../Global/DirectoryFilters/index.vue'
44
- import Switcher from './switcher.vue'
45
-
46
- const nuxtApp = useNuxtApp()
47
-
48
- const hasMorePages = ref(false)
49
- const displayedCards = ref([])
50
- const loading = ref(true)
51
- const bundleCache = ref({})
52
- const displayLimit = ref(0)
53
- const allCards = ref(undefined)
54
- const filters = ref({
55
- taxonomies: [],
56
- selected: {},
57
- availableBundles: [],
58
- })
59
- const props = defineProps({
60
- background: {
61
- type: String,
62
- default: 'grey',
63
- },
64
- title: {
65
- type: String,
66
- default: '',
67
- },
68
- titleTag: {
69
- type: String,
70
- default: 'h2',
71
- },
72
- bundle: {
73
- type: Array,
74
- default: () => [''],
75
- },
76
- columns: {
77
- type: Number,
78
- default: 0,
79
- },
80
- initialDisplyLimit: {
81
- type: Number,
82
- default: 0,
83
- },
84
- fetchContent: {
85
- type: Function,
86
- required: true,
87
- },
88
- fetchFilters: {
89
- type: Function,
90
- required: false,
91
- default: null,
92
- },
93
- enableBundleFilter: {
94
- type: Boolean,
95
- },
96
- getBundleAlias: {
97
- type: Function,
98
- required: false,
99
- default: null,
100
- },
101
- showMore: {
102
- type: Boolean,
103
- },
104
- type: {
105
- type: String,
106
- required: true,
107
- },
108
- })
109
-
110
- await useAsyncData('fetchData', async () => {
111
- try {
112
- await Promise.all([
113
- props.fetchFilters ? loadFilters() : Promise.resolve(),
114
- loadMoreCards(true),
115
- ])
116
- } catch (err) {
117
- console.log('🚀 ~ err:', err)
118
- }
119
- })
120
-
121
- function initial() {
122
- allCards.value = {
123
- total: 0,
124
- cards: [],
125
- }
126
- displayLimit.value = 0
127
- hasMorePages.value = false
128
- displayedCards.value = []
129
- bundleCache.value = {}
130
- }
131
- async function fetchCards(currentDisplayedCards, allCards, addMoreBy, bundles) {
132
- const shouldMakeNewFetchCall =
133
- currentDisplayedCards.length + addMoreBy > allCards.cards.length
134
- if (shouldMakeNewFetchCall) {
135
- const cardBundles = await Promise.all(
136
- bundles.map((bundle, index) => getBundle(bundle, addMoreBy, index)),
137
- )
138
- allCards = getShuffledCards(cardBundles)
139
-
140
- if (
141
- props.type === 'paragraph--cards_group_latest' ||
142
- props.type === 'paragraph--browse_content'
143
- ) {
144
- allCards.cards = sortByDate(allCards.cards)
145
- }
146
- }
147
- return allCards
148
- }
149
-
150
- async function loadMoreCards(init) {
151
- const attrs = {
152
- group: nuxtApp.$pageStore?.content?.title,
153
- }
154
- if (nuxtApp.$gtm && !init) {
155
- nuxtApp.$gtm.push({ event: 'custom.interaction.showmore.click', ...attrs })
156
- }
157
- if (init) {
158
- initial()
159
- }
160
- loading.value = true
161
- const addMoreBy = props.initialDisplyLimit
162
- displayLimit.value += addMoreBy
163
- const bundles =
164
- filters.value.availableBundles.length > 0
165
- ? filters.value.availableBundles
166
- : props.bundle
167
- allCards.value = await fetchCards(
168
- displayedCards.value,
169
- allCards.value,
170
- addMoreBy,
171
- bundles,
172
- )
173
- hasMorePages.value = allCards.value.total > allCards.value.cards.length
174
- displayedCards.value = allCards.value.cards.slice(0, displayLimit.value)
175
- loading.value = false
176
- }
177
- async function getBundle(bundle, addMoreBy, index) {
178
- let start
179
- const rows = addMoreBy
180
- const cache = (bundleCache.value[bundle] = bundleCache.value[bundle] || {
181
- response: {
182
- results: [],
183
- numFound: 0,
184
- },
185
- qs: {
186
- start,
187
- },
188
- })
189
- if (cache.qs.start === undefined) {
190
- start = 0
191
- } else if (cache.response.numFound > cache.response.results.length) {
192
- start = cache.response.results.length
193
- } else {
194
- return Promise.resolve(cache.response)
195
- }
196
- const qs = {
197
- bundle,
198
- rows,
199
- start,
200
- }
201
- let response = await props.fetchContent(qs, filters.value.selected)
202
- cache.qs.start = qs.start
203
- cache.response.numFound = response.numFound
204
- cache.response.results.push(...response.results)
205
- return cache.response
206
- }
207
- function getShuffledCards(cardBundles) {
208
- const shuffled = []
209
- const bundleWithMostCards = cardBundles.reduce(
210
- (a, b) => (a.results.length > b.results.length ? a : b),
211
- { results: [] },
212
- ).results.length
213
-
214
- for (let i = 0; i < bundleWithMostCards; i++) {
215
- cardBundles.forEach((bundle) => {
216
- if (bundle.results[i]) {
217
- shuffled.push(bundle.results[i])
218
- }
219
- })
220
- }
221
-
222
- const total = cardBundles.reduce((acc, bundle) => {
223
- return acc + bundle.numFound
224
- }, 0)
225
-
226
- return { cards: shuffled, total }
227
- }
228
- function loadFilters() {
229
- return props.fetchFilters().then((taxonomies) => {
230
- filters.value.taxonomies = taxonomies
231
-
232
- return Promise.resolve(taxonomies)
233
- })
234
- }
235
- const onFilter = (fltrs) => {
236
- bundleCache.value = {}
237
- Object.keys(fltrs).forEach((filter) => {
238
- filters.values.selected[filter] = fltrs[filter].map((obj) => obj.tid)
239
- })
240
- loadMoreCards(true)
241
- }
242
- const onSwitcher = (switchToBundle) => {
243
- filters.value.selected = {}
244
- filters.value.availableBundles.length = 0
245
- if (switchToBundle) {
246
- filters.value.availableBundles.push(switchToBundle)
247
- }
248
- loadMoreCards(true)
249
- }
250
- const reset = async () => {
251
- bundleCache.value = {}
252
- filters.value.selected = {}
253
- filters.value.availableBundles.length = 0
254
- await loadMoreCards(true)
255
- }
256
- function sortByDate(cards) {
257
- const sorted = cards.sort((a, b) => {
258
- return new Date(b.dateSort) - new Date(a.dateSort)
259
- })
260
- return sorted
261
- }
262
- </script>
263
- <style lang="scss" scoped>
264
- .wcl-browse-content {
265
- &__filter {
266
- align-items: flex-end;
267
- place-content: flex-end;
268
- }
269
-
270
- &.card_group__card--col-4 {
271
- :deep(.card-grid__column .card-grid-item__description) {
272
- display: none;
273
- }
274
- }
275
-
276
- :deep(.card_group__card--blue .card_item__desc p) {
277
- margin-top: 12px;
278
- }
279
-
280
- :deep(.card_group__card--red .card_item__desc p) {
281
- margin-top: 12px;
282
- }
283
- }
284
- </style>