| 1 |
/** |
| 2 |
* Downloads List JavaScript |
| 3 |
* Handles download functionality for the list view with visibility checks |
| 4 |
*/ |
| 5 |
|
| 6 |
// Translation helpers — idempotent shim matching the other frontend bundles. |
| 7 |
(function () { |
| 8 |
if (typeof window.__ === 'function') return; |
| 9 |
if (window.wp && window.wp.i18n && typeof window.wp.i18n.__ === 'function') { |
| 10 |
window.__ = function (text, domain) { return window.wp.i18n.__(text, domain || 'yatra'); }; |
| 11 |
window._n = function (s, p, n, domain) { return window.wp.i18n._n(s, p, n, domain || 'yatra'); }; |
| 12 |
window._x = function (text, ctx, domain) { return window.wp.i18n._x(text, ctx, domain || 'yatra'); }; |
| 13 |
window.sprintf = window.sprintf || (window.wp.i18n.sprintf || function (fmt) { return fmt; }); |
| 14 |
} else { |
| 15 |
window.__ = function (text) { return text; }; |
| 16 |
window._n = function (s, p, n) { return n === 1 ? s : p; }; |
| 17 |
window._x = function (text) { return text; }; |
| 18 |
window.sprintf = window.sprintf || function (fmt) { return fmt; }; |
| 19 |
} |
| 20 |
})(); |
| 21 |
|
| 22 |
document.addEventListener('DOMContentLoaded', function() { |
| 23 |
// Use centralized API helper |
| 24 |
if (typeof window.YatraApiHelper === 'undefined') { |
| 25 |
console.error('YatraApiHelper not loaded'); |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
// Handle download button clicks |
| 30 |
const downloadButtons = document.querySelectorAll('.yatra-download-btn:not(.yatra-download-btn-disabled)'); |
| 31 |
|
| 32 |
downloadButtons.forEach(button => { |
| 33 |
button.addEventListener('click', function(e) { |
| 34 |
e.preventDefault(); |
| 35 |
|
| 36 |
const downloadId = this.getAttribute('data-download-id'); |
| 37 |
const visibility = this.getAttribute('data-visibility'); |
| 38 |
|
| 39 |
if (!downloadId) { |
| 40 |
console.error('No download ID found'); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Show loading state |
| 45 |
const originalContent = this.innerHTML; |
| 46 |
this.innerHTML = '<span class="yatra-download-spinner"></span> ' + __('Getting download...', 'yatra'); |
| 47 |
this.disabled = true; |
| 48 |
|
| 49 |
// For booked_only downloads, we need to get booking ID |
| 50 |
// In a real implementation, you'd get this from user's bookings |
| 51 |
if (visibility === 'booked_only') { |
| 52 |
// For demo purposes, we'll try without booking ID first |
| 53 |
// The API will tell us if booking is required |
| 54 |
} |
| 55 |
|
| 56 |
// Make API request to get download URL using centralized helper |
| 57 |
window.YatraApiHelper.getDownloadUrl(downloadId) |
| 58 |
.then(response => { |
| 59 |
if (!response.ok) { |
| 60 |
return response.json().then(err => { |
| 61 |
throw new Error(err.message || __('Download failed', 'yatra')); |
| 62 |
}); |
| 63 |
} |
| 64 |
return response.json(); |
| 65 |
}) |
| 66 |
.then(data => { |
| 67 |
if (data.download_url) { |
| 68 |
// Create temporary link and trigger download |
| 69 |
const link = document.createElement('a'); |
| 70 |
link.href = data.download_url; |
| 71 |
link.download = data.filename || 'download'; |
| 72 |
link.style.display = 'none'; |
| 73 |
document.body.appendChild(link); |
| 74 |
link.click(); |
| 75 |
document.body.removeChild(link); |
| 76 |
} else { |
| 77 |
throw new Error(__('No download URL provided', 'yatra')); |
| 78 |
} |
| 79 |
}) |
| 80 |
.catch(error => { |
| 81 |
console.error('Download error:', error); |
| 82 |
|
| 83 |
// Show user-friendly error message |
| 84 |
let errorMessage = __('Download failed. Please try again.', 'yatra'); |
| 85 |
if (error.message.includes('booking')) { |
| 86 |
errorMessage = __('This download requires a booking. Please book this trip first.', 'yatra'); |
| 87 |
} else if (error.message.includes('login')) { |
| 88 |
errorMessage = __('Please log in to download this file.', 'yatra'); |
| 89 |
} else if (error.message.includes('permission')) { |
| 90 |
errorMessage = __('You do not have permission to download this file.', 'yatra'); |
| 91 |
} |
| 92 |
|
| 93 |
alert(errorMessage); |
| 94 |
}) |
| 95 |
.finally(() => { |
| 96 |
// Restore button state |
| 97 |
this.innerHTML = originalContent; |
| 98 |
this.disabled = false; |
| 99 |
}); |
| 100 |
}); |
| 101 |
}); |
| 102 |
|
| 103 |
// Handle disabled button clicks - show appropriate message |
| 104 |
const disabledButtons = document.querySelectorAll('.yatra-download-btn-disabled'); |
| 105 |
|
| 106 |
disabledButtons.forEach(button => { |
| 107 |
button.addEventListener('click', function(e) { |
| 108 |
e.preventDefault(); |
| 109 |
|
| 110 |
const visibility = this.getAttribute('data-visibility'); |
| 111 |
let message = __('Download not available.', 'yatra'); |
| 112 |
|
| 113 |
if (visibility === 'logged_in') { |
| 114 |
message = __('Please log in to download this file.', 'yatra'); |
| 115 |
} else if (visibility === 'booked_only') { |
| 116 |
message = __('This download requires a booking for this trip.', 'yatra'); |
| 117 |
} |
| 118 |
|
| 119 |
alert(message); |
| 120 |
}); |
| 121 |
}); |
| 122 |
|
| 123 |
// Add CSS for spinner if not already present |
| 124 |
if (!document.querySelector('#yatra-downloads-spinner-styles')) { |
| 125 |
const style = document.createElement('style'); |
| 126 |
style.id = 'yatra-downloads-spinner-styles'; |
| 127 |
style.textContent = ` |
| 128 |
.yatra-download-spinner { |
| 129 |
display: inline-block; |
| 130 |
width: 16px; |
| 131 |
height: 16px; |
| 132 |
border: 2px solid rgba(255, 255, 255, 0.3); |
| 133 |
border-top: 2px solid white; |
| 134 |
border-radius: 50%; |
| 135 |
animation: yatra-spin 1s linear infinite; |
| 136 |
} |
| 137 |
|
| 138 |
@keyframes yatra-spin { |
| 139 |
0% { transform: rotate(0deg); } |
| 140 |
100% { transform: rotate(360deg); } |
| 141 |
} |
| 142 |
`; |
| 143 |
document.head.appendChild(style); |
| 144 |
} |
| 145 |
}); |
| 146 |
|