| 1 |
"use strict"; |
| 2 |
(function ($) { |
| 3 |
const FLAG = "kingAddonsDataTablePreviewBound"; |
| 4 |
let activeView = null; |
| 5 |
|
| 6 |
/** |
| 7 |
* Export currently active table as CSV. |
| 8 |
* |
| 9 |
* @returns {void} |
| 10 |
*/ |
| 11 |
const exportActiveTable = () => { |
| 12 |
if (!activeView) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
const rows = activeView.$el.find(".king-addons-data-table .king-addons-table-row"); |
| 17 |
const data = []; |
| 18 |
|
| 19 |
rows.each((_, row) => { |
| 20 |
const cols = row.querySelectorAll(".king-addons-table-text"); |
| 21 |
const rowData = Array.from(cols, (col) => col.innerText).join(","); |
| 22 |
data.push(rowData); |
| 23 |
}); |
| 24 |
|
| 25 |
const csvContent = data.join("\n"); |
| 26 |
const blob = new Blob([csvContent], { type: "text/csv" }); |
| 27 |
const url = URL.createObjectURL(blob); |
| 28 |
|
| 29 |
const downloadLink = document.createElement("a"); |
| 30 |
downloadLink.download = "placeholder.csv"; |
| 31 |
downloadLink.href = url; |
| 32 |
downloadLink.style.display = "none"; |
| 33 |
|
| 34 |
document.body.appendChild(downloadLink); |
| 35 |
downloadLink.click(); |
| 36 |
document.body.removeChild(downloadLink); |
| 37 |
URL.revokeObjectURL(url); |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* Bind editor listeners once. |
| 42 |
* |
| 43 |
* @returns {void} |
| 44 |
*/ |
| 45 |
const bindOnce = () => { |
| 46 |
if (window[FLAG]) { |
| 47 |
return; |
| 48 |
} |
| 49 |
window[FLAG] = true; |
| 50 |
|
| 51 |
$(window).on("elementor:init", () => { |
| 52 |
elementor.channels.editor.off("king-addons-data-table-export.kingAddonsDataTable"); |
| 53 |
elementor.channels.editor.on( |
| 54 |
"king-addons-data-table-export.kingAddonsDataTable", |
| 55 |
exportActiveTable |
| 56 |
); |
| 57 |
|
| 58 |
elementor.hooks.addAction( |
| 59 |
"panel/open_editor/widget/king-addons-data-table", |
| 60 |
(panel, model, view) => { |
| 61 |
activeView = view; |
| 62 |
} |
| 63 |
); |
| 64 |
}); |
| 65 |
}; |
| 66 |
|
| 67 |
bindOnce(); |
| 68 |
})(jQuery); |