100 lines
3.3 KiB
JavaScript
100 lines
3.3 KiB
JavaScript
// File: admin/js/admin-script.js
|
|
|
|
jQuery(document).ready(function($) {
|
|
'use strict';
|
|
|
|
// Confirm delete actions
|
|
$('.pc-hfap-snippets-table .button-link-delete').on('click', function(e) {
|
|
if (!confirm(pc_hfap_admin.confirm_delete)) {
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
});
|
|
|
|
// Form validation
|
|
$('.pc-hfap-snippet-form').on('submit', function(e) {
|
|
var title = $('#pc_hfap_title').val().trim();
|
|
var code = $('#pc_hfap_code').val().trim();
|
|
var location = $('#pc_hfap_location').val();
|
|
|
|
if (!title) {
|
|
alert('Please enter a title for the snippet.');
|
|
$('#pc_hfap_title').focus();
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
|
|
if (!location || (location !== 'header' && location !== 'body' && location !== 'footer')) {
|
|
alert('Please select a valid location for the snippet.');
|
|
$('#pc_hfap_location').focus();
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
|
|
if (!code) {
|
|
alert('Please enter the code for the snippet.');
|
|
$('#pc_hfap_code').focus();
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
});
|
|
|
|
// Code editor enhancement (if CodeMirror is available)
|
|
if (typeof wp !== 'undefined' && wp.codeEditor) {
|
|
var codeEditorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
|
|
codeEditorSettings.codemirror = _.extend({}, codeEditorSettings.codemirror, {
|
|
lineNumbers: true,
|
|
lineWrapping: true,
|
|
mode: 'htmlmixed',
|
|
indentUnit: 4,
|
|
tabSize: 4,
|
|
theme: 'default'
|
|
});
|
|
var editor = wp.codeEditor.initialize($('#pc_hfap_code'), codeEditorSettings);
|
|
}
|
|
|
|
// Character counter for code preview
|
|
$('.pc-hfap-code-preview').each(function() {
|
|
var text = $(this).text();
|
|
var charCount = text.length;
|
|
var $counter = $('<span class="pc-hfap-char-count"> (' + charCount + ' chars)</span>');
|
|
$(this).after($counter);
|
|
});
|
|
|
|
// Highlight new rows
|
|
if (window.location.hash === '#new') {
|
|
var $newRow = $('.pc-hfap-snippets-table tbody tr:first-child');
|
|
$newRow.addClass('highlight');
|
|
|
|
// Scroll to the new row
|
|
$('html, body').animate({
|
|
scrollTop: $newRow.offset().top - 100
|
|
}, 500);
|
|
|
|
// Remove hash without scrolling
|
|
history.replaceState(null, null, ' ');
|
|
}
|
|
|
|
// Toggle for code snippet preview
|
|
$('.pc-hfap-code-preview').on('click', function() {
|
|
var $preview = $(this);
|
|
var fullCode = $preview.data('full-code');
|
|
|
|
if (!fullCode) {
|
|
fullCode = $preview.text();
|
|
$preview.data('full-code', fullCode);
|
|
$preview.data('is-expanded', false);
|
|
}
|
|
|
|
if ($preview.data('is-expanded')) {
|
|
// Collapse
|
|
$preview.text(fullCode.length > 100 ? fullCode.substring(0, 100) + '...' : fullCode);
|
|
$preview.data('is-expanded', false);
|
|
} else {
|
|
// Expand
|
|
$preview.text(fullCode);
|
|
$preview.data('is-expanded', true);
|
|
}
|
|
});
|
|
});
|