mkfashion-sdk 2.3.5 → 2.3.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/mkfashion-sdk-2.3.8.tgz +0 -0
- package/package.json +1 -1
- package/src/mkfashion.js +71 -17
|
Binary file
|
package/package.json
CHANGED
package/src/mkfashion.js
CHANGED
|
@@ -33,6 +33,8 @@ const mkfashion = {
|
|
|
33
33
|
_config: null,
|
|
34
34
|
_lastCartData: null,
|
|
35
35
|
_lastCartProductData: null, // cache completo do produto (para getProduct)
|
|
36
|
+
_lastConfig: null, // config do ultimo open (para reutilizar iframe)
|
|
37
|
+
_container: null,
|
|
36
38
|
_escHandler: null,
|
|
37
39
|
_messageHandler: null,
|
|
38
40
|
|
|
@@ -96,22 +98,35 @@ const mkfashion = {
|
|
|
96
98
|
height: options.height || 800
|
|
97
99
|
}
|
|
98
100
|
|
|
99
|
-
|
|
100
|
-
this.
|
|
101
|
+
// Reutiliza modal/iframe existente se ja foi criado para o mesmo produto
|
|
102
|
+
if (this._modal && this._iframe && this._lastConfig &&
|
|
103
|
+
this._lastConfig.projectId === projectId &&
|
|
104
|
+
this._lastConfig.identifier === identifier) {
|
|
105
|
+
this._showModal()
|
|
106
|
+
this._setupMessageListener()
|
|
107
|
+
} else {
|
|
108
|
+
// Destroi modal anterior se existir (produto diferente)
|
|
109
|
+
if (this._modal) {
|
|
110
|
+
this._removeMessageListener()
|
|
111
|
+
this._destroyModal()
|
|
112
|
+
}
|
|
113
|
+
this._setupMessageListener()
|
|
114
|
+
this._openModal()
|
|
115
|
+
}
|
|
116
|
+
this._lastConfig = { projectId, identifier }
|
|
101
117
|
|
|
102
118
|
this._isOpen = true
|
|
103
119
|
this._log('Aberto', this._config)
|
|
104
120
|
},
|
|
105
121
|
|
|
106
|
-
/** Fecha o modal do provador virtual */
|
|
122
|
+
/** Fecha o modal do provador virtual (esconde sem destruir a sessao) */
|
|
107
123
|
close() {
|
|
108
124
|
if (!this._isOpen) {
|
|
109
125
|
this._log('Ja esta fechado')
|
|
110
126
|
return
|
|
111
127
|
}
|
|
112
128
|
|
|
113
|
-
this.
|
|
114
|
-
this._closeModal()
|
|
129
|
+
this._hideModal()
|
|
115
130
|
this._triggerCallback('onClose')
|
|
116
131
|
this._isOpen = false
|
|
117
132
|
this._log('Fechado')
|
|
@@ -183,8 +198,8 @@ const mkfashion = {
|
|
|
183
198
|
/** Fecha modal, remove listeners, limpa cache e reseta callbacks */
|
|
184
199
|
destroy() {
|
|
185
200
|
this.close()
|
|
186
|
-
this.
|
|
187
|
-
this.
|
|
201
|
+
this._removeMessageListener()
|
|
202
|
+
this._destroyModal()
|
|
188
203
|
this._config = null
|
|
189
204
|
this._lastCartData = null
|
|
190
205
|
this._lastCartProductData = null
|
|
@@ -236,7 +251,7 @@ const mkfashion = {
|
|
|
236
251
|
this._log('Verificando disponibilidade', { projectId: resolved, identifier })
|
|
237
252
|
|
|
238
253
|
try {
|
|
239
|
-
const url = `${this.apiUrl}/availability/${resolved}/${identifier}`
|
|
254
|
+
const url = `${this.apiUrl}/availability/${resolved}/${encodeURIComponent(identifier)}`
|
|
240
255
|
const response = await fetch(url)
|
|
241
256
|
|
|
242
257
|
if (!response.ok) {
|
|
@@ -326,14 +341,14 @@ const mkfashion = {
|
|
|
326
341
|
this._log('Buscando produto', { projectId: resolved, identifier })
|
|
327
342
|
|
|
328
343
|
try {
|
|
329
|
-
const url = `${this.apiUrl}/products/${resolved}/${identifier}`
|
|
344
|
+
const url = `${this.apiUrl}/products/${resolved}/${encodeURIComponent(identifier)}`
|
|
330
345
|
const response = await fetch(url)
|
|
331
346
|
|
|
332
347
|
if (!response.ok) {
|
|
333
348
|
// Fallback: tenta com productId do config caso caller tenha passado variantSku
|
|
334
349
|
if (response.status === 404 && this._config?.identifier && this._config.identifier !== identifier) {
|
|
335
350
|
this._log(`Identifier ${identifier} não encontrado, tentando com productId: ${this._config.identifier}`)
|
|
336
|
-
const fallbackUrl = `${this.apiUrl}/products/${resolved}/${this._config.identifier}`
|
|
351
|
+
const fallbackUrl = `${this.apiUrl}/products/${resolved}/${encodeURIComponent(this._config.identifier)}`
|
|
337
352
|
const fallbackResponse = await fetch(fallbackUrl)
|
|
338
353
|
if (fallbackResponse.ok) {
|
|
339
354
|
const data = await fallbackResponse.json()
|
|
@@ -394,7 +409,7 @@ const mkfashion = {
|
|
|
394
409
|
},
|
|
395
410
|
|
|
396
411
|
_buildUrl() {
|
|
397
|
-
return `${this.appUrl}/${this._config.projectId}/${this._config.identifier}`
|
|
412
|
+
return `${this.appUrl}/${this._config.projectId}/${encodeURIComponent(this._config.identifier)}`
|
|
398
413
|
},
|
|
399
414
|
|
|
400
415
|
_openModal() {
|
|
@@ -515,18 +530,19 @@ const mkfashion = {
|
|
|
515
530
|
document.addEventListener('keydown', this._escHandler)
|
|
516
531
|
},
|
|
517
532
|
|
|
518
|
-
|
|
533
|
+
/** Esconde o modal sem remover do DOM (preserva sessao do iframe) */
|
|
534
|
+
_hideModal() {
|
|
519
535
|
if (!this._modal) return
|
|
520
536
|
|
|
521
537
|
this._modal.style.opacity = '0'
|
|
522
|
-
|
|
538
|
+
if (this._container) {
|
|
539
|
+
this._container.style.transform = 'scale(0.95)'
|
|
540
|
+
}
|
|
523
541
|
|
|
524
542
|
setTimeout(() => {
|
|
525
|
-
if (this._modal
|
|
526
|
-
this._modal.
|
|
543
|
+
if (this._modal) {
|
|
544
|
+
this._modal.style.display = 'none'
|
|
527
545
|
}
|
|
528
|
-
this._modal = null
|
|
529
|
-
this._iframe = null
|
|
530
546
|
}, 300)
|
|
531
547
|
|
|
532
548
|
if (this._escHandler) {
|
|
@@ -534,6 +550,44 @@ const mkfashion = {
|
|
|
534
550
|
}
|
|
535
551
|
},
|
|
536
552
|
|
|
553
|
+
/** Mostra o modal que estava escondido */
|
|
554
|
+
_showModal() {
|
|
555
|
+
if (!this._modal) return
|
|
556
|
+
|
|
557
|
+
this._modal.style.display = 'flex'
|
|
558
|
+
|
|
559
|
+
requestAnimationFrame(() => {
|
|
560
|
+
this._modal.style.opacity = '1'
|
|
561
|
+
if (this._container) {
|
|
562
|
+
this._container.style.transform = 'scale(1)'
|
|
563
|
+
}
|
|
564
|
+
})
|
|
565
|
+
|
|
566
|
+
this._escHandler = (e) => {
|
|
567
|
+
if (e.key === 'Escape') {
|
|
568
|
+
this.close()
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
document.addEventListener('keydown', this._escHandler)
|
|
572
|
+
},
|
|
573
|
+
|
|
574
|
+
/** Remove o modal do DOM completamente (usado por destroy) */
|
|
575
|
+
_destroyModal() {
|
|
576
|
+
if (!this._modal) return
|
|
577
|
+
|
|
578
|
+
if (this._modal.parentNode) {
|
|
579
|
+
this._modal.parentNode.removeChild(this._modal)
|
|
580
|
+
}
|
|
581
|
+
this._modal = null
|
|
582
|
+
this._iframe = null
|
|
583
|
+
this._container = null
|
|
584
|
+
this._lastConfig = null
|
|
585
|
+
|
|
586
|
+
if (this._escHandler) {
|
|
587
|
+
document.removeEventListener('keydown', this._escHandler)
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
|
|
537
591
|
_createIframe() {
|
|
538
592
|
const { width, height } = this._config
|
|
539
593
|
const iframe = document.createElement('iframe')
|