-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin-compass.js
More file actions
362 lines (318 loc) · 11.7 KB
/
admin-compass.js
File metadata and controls
362 lines (318 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
jQuery(document).ready(function($) {
var $overlay = $("#admin-compass-overlay");
var $modal = $("#admin-compass-modal");
var $input = $("#admin-compass-input");
var $results = $("#admin-compass-results");
var currentFocus = -1;
var currentRequest = null;
var searchTimer = null;
var searchCache = {};
var recentItems = JSON.parse(localStorage.getItem('adminCompassRecent') || '[]');
var indexingCheckInterval = null;
$(".admin-compass-icon").on("click", function(e) {
e.preventDefault();
toggleModal();
});
$(document).on("keydown", function(e) {
// Ctrl+K or Cmd+K
if ((e.ctrlKey || e.metaKey) && e.which === 75) {
e.preventDefault();
toggleModal();
}
// Also keep Ctrl+Shift+F for backwards compatibility
else if (e.ctrlKey && e.shiftKey && e.which === 70) {
e.preventDefault();
toggleModal();
}
});
$(document).on("keyup", function(e) {
if (e.key === "Escape") {
closeModal();
}
});
// Close modal when clicking outside
$overlay.on('click', function(e) {
if (e.target === this) {
closeModal();
}
});
// Prevent clicks inside the modal from closing it
$modal.on('click', function(e) {
e.stopPropagation();
});
function toggleModal() {
$overlay.toggleClass('admin-compass-hidden');
$modal.toggleClass('admin-compass-hidden');
if (!$modal.hasClass('admin-compass-hidden')) {
$input.focus();
showRecentItems();
checkIndexingStatus();
// Check indexing status periodically while modal is open
indexingCheckInterval = setInterval(checkIndexingStatus, 5000);
} else {
// Clear interval when modal is closed
if (indexingCheckInterval) {
clearInterval(indexingCheckInterval);
indexingCheckInterval = null;
}
}
}
function closeModal() {
$overlay.addClass('admin-compass-hidden');
$modal.addClass('admin-compass-hidden');
$input.val("");
$results.empty();
currentFocus = -1;
// Clear interval when modal is closed
if (indexingCheckInterval) {
clearInterval(indexingCheckInterval);
indexingCheckInterval = null;
}
}
$input.on("input", function() {
var query = $(this).val();
if (searchTimer) {
clearTimeout(searchTimer);
}
if (currentRequest) {
currentRequest.abort();
}
if (query.length < 2) {
showRecentItems();
return;
}
searchTimer = setTimeout(function() {
showLoading();
performSearch(query);
}, 150);
});
function checkIndexingStatus() {
$.ajax({
url: adminCompass.ajaxurl,
type: 'POST',
data: {
action: 'admin_compass_check_indexing',
nonce: adminCompass.nonce
},
success: function(response) {
if (response.success && response.data.is_indexing) {
showIndexingNotice(response.data.elapsed_time);
} else {
hideIndexingNotice();
}
}
});
}
function showIndexingNotice(elapsedTime) {
var minutes = Math.floor(elapsedTime / 60);
var seconds = elapsedTime % 60;
var timeText = minutes > 0 ? minutes + 'm ' + seconds + 's' : seconds + 's';
var $notice = $('#admin-compass-indexing-notice');
if ($notice.length === 0) {
$notice = $('<div id="admin-compass-indexing-notice" class="admin-compass-indexing-notice">' +
'<span class="spinner"></span>' +
'<span class="text">Search index is being rebuilt... (' + timeText + ')</span>' +
'<div class="subtext">Search results may be incomplete</div>' +
'</div>');
$('.admin-compass-container').prepend($notice);
} else {
$notice.find('.text').text('Search index is being rebuilt... (' + timeText + ')');
}
}
function hideIndexingNotice() {
$('#admin-compass-indexing-notice').remove();
}
function performSearch(query) {
// Check cache first
if (searchCache[query]) {
displayResults(searchCache[query]);
currentFocus = -1;
return;
}
currentRequest = $.ajax({
url: adminCompass.ajaxurl,
method: "POST",
data: {
action: "admin_compass_search",
nonce: adminCompass.nonce,
query: query
},
success: function(response) {
if (response.success) {
// Cache the results
searchCache[query] = response.data;
displayResults(response.data);
currentFocus = -1;
}
},
complete: function() {
currentRequest = null;
}
});
}
$input.on("keydown", function(e) {
var $items = $results.find(".result-item");
if (!$items.length) return;
if (e.which === 40) { // Arrow down
currentFocus++;
addActive($items);
} else if (e.which === 38) { // Arrow up
currentFocus--;
addActive($items);
} else if (e.which === 13) { // Enter
e.preventDefault();
if (currentFocus > -1) {
$items.eq(currentFocus).click();
}
}
});
function addActive($items) {
if (!$items) return false;
removeActive($items);
if (currentFocus >= $items.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = ($items.length - 1);
$items.eq(currentFocus).addClass("active");
$items.eq(currentFocus).get(0).scrollIntoView({ block: 'nearest' });
}
function removeActive($items) {
$items.removeClass("active");
}
function displayResults(items) {
$results.empty();
if (items.length === 0) {
$results.html("<div class='no-results'>No results found</div>");
return;
}
items.forEach(function(item, index) {
// Get content type icon
var icon = getContentTypeIcon(item.type);
var quickActions = $("<div class='quick-actions'>");
// Always show edit/navigate button
quickActions.append($("<button class='quick-action edit' title='Edit'>✏️</button>")
.on("click", function(e) {
e.stopPropagation();
addToRecent(item);
showNavigationLoading(item.title);
window.location.href = item.edit_url;
})
);
// Only show view button for content that can be viewed (not settings or orders)
if (item.type !== 'settings' && item.type !== 'shop_order') {
quickActions.append($("<button class='quick-action view' title='View'>👁️</button>")
.on("click", function(e) {
e.stopPropagation();
var viewUrl = getViewUrl(item);
if (viewUrl) {
window.open(viewUrl, '_blank');
}
})
);
}
var resultItem = $("<div class='result-item'>")
.append($("<span class='result-icon'>").html(icon))
.append($("<div class='result-content'>")
.append($("<div class='result-title'>").text(item.title))
.append($("<div class='result-preview'>").text(item.preview || ''))
)
.append($("<span class='result-type'>").text(formatContentType(item.type)))
.append(quickActions)
.on("click", function() {
addToRecent(item);
showNavigationLoading(item.title);
window.location.href = item.edit_url;
})
.on("mouseover", function() {
currentFocus = index;
addActive($results.find(".result-item"));
});
$results.append(resultItem);
});
}
function getContentTypeIcon(type) {
var icons = {
'post': '📝',
'page': '📄',
'product': '🛒',
'attachment': '📎',
'shop_order': '🛍️',
'settings': '⚙️'
};
return icons[type] || '📄';
}
function formatContentType(type) {
var types = {
'post': 'Post',
'page': 'Page',
'product': 'Product',
'attachment': 'Media',
'shop_order': 'Order',
'settings': 'Settings'
};
return types[type] || type.charAt(0).toUpperCase() + type.slice(1);
}
function showLoading() {
$results.html("<div class='loading-indicator'>🔍 Searching...</div>");
}
function getViewUrl(item) {
// This is simplified - in a real implementation, you'd need proper post URLs
if (item.type === 'settings') {
return null; // Settings pages don't have view URLs
}
// For now, just return the site URL + post ID for posts/pages
return window.location.origin + '/?p=' + item.id;
}
function addToRecent(item) {
// Remove if already exists
recentItems = recentItems.filter(function(recent) {
return recent.id !== item.id || recent.type !== item.type;
});
// Add to beginning
recentItems.unshift(item);
// Keep only last 10 items
recentItems = recentItems.slice(0, 10);
// Save to localStorage
localStorage.setItem('adminCompassRecent', JSON.stringify(recentItems));
}
function showRecentItems() {
if (recentItems.length === 0) {
$results.html("<div class='no-results'>Start typing to search...</div>");
return;
}
$results.html("<div class='recent-header'>Recent Items</div>");
displayResults(recentItems.slice(0, 5)); // Show only 5 most recent
}
function showNavigationLoading(title, action) {
action = action || 'Loading...';
// Replace the entire modal content with loading state
$results.html("<div class='navigation-loading'>" +
"<div class='loading-spinner'>⏳</div>" +
"<div class='loading-title'>" + action + "</div>" +
"<div class='loading-subtitle'>\"" + title + "\"</div>" +
"</div>");
// Hide the input
$input.prop('disabled', true).css('opacity', '0.5');
// Optional: Add slight delay to show the loading state
setTimeout(function() {
// This gives users visual feedback before page navigation
}, 100);
}
});
jQuery(document).ready(function($) {
$('.schedule-background-job').on('click', function(e) {
e.preventDefault();
$.ajax({
url: adminCompass.ajaxurl,
method: "POST",
data: {
action: "admin_compass_reindex",
nonce: adminCompass.nonce
},
success: function(response) {
alert('Background job scheduled successfully!');
},
error: function() {
alert('There was an error scheduling the background job.');
}
});
});
});