@rocketsearch/vue-uappi 0.1.7 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocketsearch/vue-uappi",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Rocket Search para storefronts Uappi em Vue 3 + Vite + Pinia: busca, autocomplete, vitrines, banners e eventos",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/runtime/banner.js CHANGED
@@ -125,6 +125,12 @@ export function pickVideoUrl (content, win) {
125
125
  return probe.canPlayType && probe.canPlayType('video/' + ext[1].toLowerCase()) ? url : '';
126
126
  }
127
127
 
128
+ // Mesmo slug do painel (slugifyMenuKey): "Refrigeração Comercial" -> "refrigeracao-comercial".
129
+ export function slugKey (value) {
130
+ return String(value || '').trim().toLowerCase().normalize('NFD').replace(/\p{M}/gu, '')
131
+ .replace(/\s+/g, '-').replace(/[^a-z0-9_-]/g, '').replace(/^[-_]+/, '').slice(0, 63);
132
+ }
133
+
128
134
  export function isHeroSlot (position) {
129
135
  return /^(hero|fullbanner|header|top)$/i.test(String(position || ''));
130
136
  }
@@ -28,16 +28,22 @@
28
28
  import '../css/common.css';
29
29
  import '../css/banner.css';
30
30
  import RocketBannerItem from './banner/RocketBannerItem.vue';
31
- import { exactWidth, isHeroSlot } from '../banner.js';
31
+ import { MOBILE_MEDIA, exactWidth, isHeroSlot, slugKey } from '../banner.js';
32
32
 
33
33
  const positionOf = b => (b && ((b.placement && b.placement.position) || b.position)) || '';
34
34
 
35
35
  export default {
36
36
  name: 'RocketBanner',
37
37
  components: { RocketBannerItem },
38
+ // loaded(banners): a lista que o slot vai mostrar ([] = nada na posição) — para o tema ajustar o
39
+ // layout em volta (ex.: colunas do megamenu com ou sem banner).
40
+ emits: ['loaded'],
38
41
  props: {
39
42
  // placement.position cadastrada no painel (hero, top, middle...). Obrigatória fora da landing.
40
43
  position: { type: String, default: '' },
44
+ // Posição menu: aba do megamenu (banner.menuKey no painel). Aceita o rótulo da aba
45
+ // ("Refrigeração Comercial"), comparado pelo mesmo slug do painel.
46
+ menuKey: { type: String, default: '' },
41
47
  // eager: busca assim que a página tem contexto (hero/LCP). ondemand: só quando o slot
42
48
  // chega perto da viewport.
43
49
  loading: {
@@ -56,6 +62,9 @@ export default {
56
62
  // Mais de um banner na posição: carrossel (senão empilham na ordem do BE).
57
63
  carousel: { type: Boolean, default: false },
58
64
  sizes: { type: String, default: '100vw' },
65
+ // Onde vale a imagem mobile do painel. Slot que só existe no layout mobile do tema (ex.: menu
66
+ // mobile até 1340px) passa "all"; o padrão segue o breakpoint de 768px.
67
+ mobileMedia: { type: String, default: MOBILE_MEDIA },
59
68
  rootMargin: { type: String, default: '200px' },
60
69
  imageResizing: { type: Boolean, default: true }
61
70
  },
@@ -84,6 +93,12 @@ export default {
84
93
  this.requested = false;
85
94
  this.tryLoad();
86
95
  },
96
+ // Outra aba no mesmo slot (menu mobile): refiltra — a posição vem do cache.
97
+ menuKey () {
98
+ if (!this.requested) return;
99
+ this.requested = false;
100
+ this.tryLoad();
101
+ },
87
102
  // O contexto da página chega pelo currentPageData do tema (o plugin traduz para $rocket.setPage).
88
103
  '$rocket.currentPage.value' () {
89
104
  if (this.loading === 'eager' || this.visible) this.tryLoad();
@@ -111,19 +126,24 @@ export default {
111
126
  if (this.requested) return;
112
127
  if (!this.pageType && !this.$rocket.getPage().section) return;
113
128
  this.requested = true;
114
- // Largura exata do slot; sem medida a imagem sai com srcset.
115
- if (this.$el && this.$el.clientWidth) {
129
+ // Largura exata do slot (um banner ocupando o slot todo); sem medida a imagem sai com srcset.
130
+ // Com sizes próprio (vários banners lado a lado, ex.: cartões de categoria), fica o srcset e o
131
+ // navegador escolhe pela largura de cada um — medir o slot pediria a imagem larga para todos.
132
+ if (this.sizes === '100vw' && this.$el && this.$el.clientWidth) {
116
133
  this.width = exactWidth(this.$el.clientWidth, window.devicePixelRatio);
117
134
  }
118
135
  const key = this.landingKey;
136
+ const menuKey = this.menuKey;
119
137
  const list = this.pageType === 'custom'
120
138
  ? (key ? await this.$rocket.getBannersByContext('custom', { customKey: key }) : [])
121
139
  : await this.$rocket.getBannersByContext(this.pageType || undefined, { position: this.position });
122
- // Troca rápida de landing: a resposta da anterior não sobrescreve a atual.
123
- if (key !== this.landingKey) return;
124
- const mine = this.pageType === 'custom' ? list : list.filter(b => positionOf(b) === this.position);
140
+ // Troca rápida de landing/aba: a resposta da anterior não sobrescreve a atual.
141
+ if (key !== this.landingKey || menuKey !== this.menuKey) return;
142
+ let mine = this.pageType === 'custom' ? list : list.filter(b => positionOf(b) === this.position);
143
+ if (menuKey) mine = mine.filter(b => slugKey(b.banner && b.banner.menuKey) === slugKey(menuKey));
125
144
  this.banners = this.limit > 0 ? mine.slice(0, this.limit) : mine;
126
145
  this.waiting = false;
146
+ this.$emit('loaded', this.banners);
127
147
  },
128
148
 
129
149
  itemProps (index) {
@@ -138,7 +158,8 @@ export default {
138
158
  sizes: this.sizes,
139
159
  width: this.width,
140
160
  imageResizing: this.imageResizing,
141
- aspectRatio: this.ratio ? this.ratioCss : ''
161
+ aspectRatio: this.ratio ? this.ratioCss : '',
162
+ mobileMedia: this.mobileMedia
142
163
  };
143
164
  },
144
165
 
@@ -75,10 +75,12 @@ export default {
75
75
  // Largura exata (px) medida do slot; 0 = usa srcset.
76
76
  width: { type: Number, default: 0 },
77
77
  imageResizing: { type: Boolean, default: true },
78
- aspectRatio: { type: String, default: '' }
78
+ aspectRatio: { type: String, default: '' },
79
+ // Media query em que vale a imagem mobile (ver RocketBanner).
80
+ mobileMedia: { type: String, default: MOBILE_MEDIA }
79
81
  },
80
82
  data () {
81
- return { naturalRatio: null, resizeFailed: false, mobileMedia: MOBILE_MEDIA, videoSrc: '', videoPlaying: false };
83
+ return { naturalRatio: null, resizeFailed: false, videoSrc: '', videoPlaying: false };
82
84
  },
83
85
  computed: {
84
86
  content () {
@@ -221,7 +223,7 @@ export default {
221
223
  },
222
224
 
223
225
  imageVariant () {
224
- const narrow = window.matchMedia && window.matchMedia(MOBILE_MEDIA).matches;
226
+ const narrow = window.matchMedia && window.matchMedia(this.mobileMedia).matches;
225
227
  if (narrow && this.content.mobileImageUrl) return 'mobile';
226
228
  if (this.content.imageUrl) return 'desktop';
227
229
  return this.content.mobileImageUrl ? 'mobile' : undefined;
package/runtime/rocket.js CHANGED
@@ -337,24 +337,44 @@ export function createRocket (config) {
337
337
 
338
338
  // Só banners (DTO banner-native). Gated pela flag `banners`: loja sem ela recebe [].
339
339
  // options.customKey: identificador da landing (pageType=custom); sem ele a API responde 400.
340
- rocket.getBannersByContext = async function (type, options) {
340
+ // A resposta não depende da categoria/produto da página, só de pageType + posição + landing: fica
341
+ // 5 min em cache. O slot que remonta (dropdown do menu, que monta a cada hover) não chama a API de
342
+ // novo, e o banner agendado que acaba ainda sai do ar sem recarregar a página. Falha não fica.
343
+ const BANNER_TTL = 5 * 60 * 1000;
344
+ const bannerCache = {};
345
+ rocket.getBannersByContext = function (type, options) {
341
346
  const pageType = resolvePageType(type);
342
- if (!pageType) return [];
347
+ if (!pageType) return Promise.resolve([]);
343
348
  const opts = options || {};
349
+ const customKey = pageType === 'custom' ? opts.customKey : undefined;
350
+ const key = pageType + '|' + (opts.position || '') + '|' + (customKey || '');
351
+ const hit = bannerCache[key];
352
+ if (hit && Date.now() - hit.at < BANNER_TTL) return hit.list;
353
+ const list = fetchBanners(pageType, opts.position, customKey).then((banners) => {
354
+ if (banners) return banners;
355
+ delete bannerCache[key];
356
+ return [];
357
+ });
358
+ bannerCache[key] = { list, at: Date.now() };
359
+ return list;
360
+ };
361
+
362
+ // null = falhou (não entra no cache).
363
+ async function fetchBanners (pageType, position, customKey) {
344
364
  try {
345
365
  const sessionId = await getSessionIdWithTimeout();
346
- const params = { storeId: config.storeId, pageType, sessionId, position: opts.position };
347
- if (pageType === 'custom') params.customKey = opts.customKey;
366
+ const params = { storeId: config.storeId, pageType, sessionId, position };
367
+ if (pageType === 'custom') params.customKey = customKey;
348
368
  const res = await fetch(config.apiUrl + '/banners/context?' + query(params), { headers: headers() });
349
- if (!res.ok) return [];
369
+ if (!res.ok) return null;
350
370
  const banners = await res.json();
351
371
  return Array.isArray(banners) ? banners : [];
352
372
  }
353
373
  catch (e) {
354
374
  // Banner é conteúdo promocional — falha não pode quebrar a página.
355
- return [];
375
+ return null;
356
376
  }
357
- };
377
+ }
358
378
 
359
379
  // ---- Atribuição (sessionStorage) ----
360
380