Skip to content

Commit 7a380e3

Browse files
committed
Bunch of updates
1 parent 5f05b1c commit 7a380e3

4 files changed

Lines changed: 482 additions & 7 deletions

File tree

assets/css/style.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@
2222
max-width: 300px;
2323
padding: 5px;
2424
box-sizing: border-box;
25+
}
26+
27+
#benchpress-refresh-btn,
28+
#benchpress-snapshot-btn,
29+
#benchpress-clear-snapshots-btn,
30+
#benchpress-download-snapshots-btn {
31+
line-height: 22px;
32+
float: right;
33+
margin-left: 12px;
34+
}
35+
36+
.wp-core-ui .delete-snapshot-btn,
37+
.delete-snapshot-btn,
38+
.wp-core-ui .view-data-btn,
39+
.view-data-btn {
40+
line-height: 22px;
2541
}

assets/js/benchpress-ajax.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
jQuery(document).ready(function($) {
2+
// Refresh button click handler
3+
$('#benchpress-refresh-btn').on('click', function() {
4+
$.ajax({
5+
url: benchpress_ajax.ajax_url,
6+
method: 'POST',
7+
data: {
8+
action: 'benchpress_refresh',
9+
_ajax_nonce: benchpress_ajax.nonce
10+
},
11+
success: function(response) {
12+
if (response.success) {
13+
$('#benchpress-results').html(response.data.html);
14+
}
15+
}
16+
});
17+
});
18+
19+
// Clear all snapshots click handler
20+
$('#benchpress-clear-snapshots-btn').on('click', function() {
21+
if (confirm('Are you sure you want to delete all snapshots? This action cannot be undone.')) {
22+
$.ajax({
23+
url: benchpress_ajax.ajax_url,
24+
method: 'POST',
25+
data: {
26+
action: 'benchpress_clear_all_snapshots',
27+
_ajax_nonce: benchpress_ajax.nonce
28+
},
29+
success: function(response) {
30+
if (response.success) {
31+
$('.wp-list-table tbody').empty(); // Clear table rows in the UI
32+
} else {
33+
alert(response.data.message); // Show error message if delete fails
34+
}
35+
}
36+
});
37+
}
38+
});
39+
40+
// Download snapshots as CSV
41+
$('#benchpress-download-snapshots-btn').on('click', function() {
42+
$('#benchpress-download-snapshots-btn').on('click', function() {
43+
$.ajax({
44+
url: benchpress_ajax.ajax_url,
45+
method: 'POST',
46+
data: {
47+
action: 'benchpress_download_snapshots',
48+
_ajax_nonce: benchpress_ajax.nonce
49+
},
50+
xhrFields: {
51+
responseType: 'blob' // Expect binary data as the response
52+
},
53+
success: function(response, status, xhr) {
54+
// Create a downloadable link with the CSV blob
55+
var blob = new Blob([response], { type: 'text/csv' });
56+
var downloadUrl = URL.createObjectURL(blob);
57+
58+
// Create a temporary link and click it to trigger download
59+
var link = document.createElement('a');
60+
link.href = downloadUrl;
61+
link.download = `${benchpress_ajax.site_name}-benchpress-${benchpress_ajax.datetime}.csv`;
62+
document.body.appendChild(link);
63+
link.click();
64+
65+
// Clean up
66+
document.body.removeChild(link);
67+
URL.revokeObjectURL(downloadUrl);
68+
},
69+
error: function() {
70+
alert('Failed to download snapshots. Please try again.');
71+
}
72+
});
73+
});
74+
});
75+
76+
// Snapshot button click handler
77+
$('#benchpress-snapshot-btn').on('click', function() {
78+
$.ajax({
79+
url: benchpress_ajax.ajax_url,
80+
method: 'POST',
81+
data: {
82+
action: 'benchpress_snapshot',
83+
_ajax_nonce: benchpress_ajax.nonce
84+
},
85+
success: function(response) {
86+
if (response.success) {
87+
alert(response.data.message);
88+
}
89+
}
90+
});
91+
});
92+
93+
// Open modal and populate with snapshot data
94+
$(document).on('click', '.view-data-btn', function() {
95+
var snapshotData = $(this).data('snapshot');
96+
97+
if (typeof snapshotData === 'string') {
98+
snapshotData = JSON.parse(snapshotData);
99+
}
100+
101+
var formattedData = '<ul>';
102+
snapshotData.forEach(function(benchmark) {
103+
formattedData += '<li><strong>' + benchmark.name + ':</strong> ' +
104+
benchmark.execution_time + ' seconds<br><em>' +
105+
benchmark.description + '</em></li>';
106+
});
107+
formattedData += '</ul>';
108+
109+
$('#snapshotModalData').html(formattedData);
110+
$('#snapshotModal').css('display', 'block');
111+
});
112+
113+
// Close the modal
114+
$(document).on('click', '.snapshot-modal-close', function() {
115+
$('#snapshotModal').hide();
116+
});
117+
118+
// Close modal when clicking outside the modal content
119+
$(window).on('click', function(event) {
120+
if ($(event.target).is('#snapshotModal')) {
121+
$('#snapshotModal').hide();
122+
}
123+
});
124+
125+
// Delete snapshot button click handler
126+
$(document).on('click', '.delete-snapshot-btn', function() {
127+
if (confirm('Are you sure you want to delete this snapshot?')) {
128+
var snapshotId = $(this).data('id');
129+
var row = $(this).closest('tr');
130+
131+
$.ajax({
132+
url: benchpress_ajax.ajax_url,
133+
method: 'POST',
134+
data: {
135+
action: 'benchpress_delete_snapshot',
136+
_ajax_nonce: benchpress_ajax.nonce,
137+
snapshot_id: snapshotId
138+
},
139+
success: function(response) {
140+
if (response.success) {
141+
row.remove(); // Remove the row from the table
142+
} else {
143+
alert(response.data.message); // Error alert if deletion fails
144+
}
145+
}
146+
});
147+
}
148+
});
149+
150+
});

0 commit comments

Comments
 (0)