Initial import of 404 Enhacer
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
/* 404 Enhancer admin script: featured image picker + featured content AJAX picker. */
|
||||
jQuery(document).ready(function ($) {
|
||||
'use strict';
|
||||
|
||||
/* Featured image via the media library frame. */
|
||||
var frame;
|
||||
$('#fe-upload-btn').on('click', function (e) {
|
||||
e.preventDefault();
|
||||
if (!frame) {
|
||||
frame = wp.media({
|
||||
title: FEAdmin.i18n.chooseImage,
|
||||
button: { text: FEAdmin.i18n.useImage },
|
||||
library: { type: 'image' },
|
||||
multiple: false
|
||||
});
|
||||
frame.on('select', function () {
|
||||
var attachment = frame.state().get('selection').first().toJSON();
|
||||
$('#fe_featured_image_id').val(attachment.id);
|
||||
$('#fe-featured-preview').attr('src', attachment.url).show();
|
||||
$('#fe-remove-btn').show();
|
||||
});
|
||||
}
|
||||
frame.open();
|
||||
});
|
||||
|
||||
$('#fe-remove-btn').on('click', function (e) {
|
||||
e.preventDefault();
|
||||
$('#fe_featured_image_id').val('');
|
||||
$('#fe-featured-preview').attr('src', '').hide();
|
||||
$(this).hide();
|
||||
});
|
||||
|
||||
/* Featured content picker: debounced AJAX search + chips. */
|
||||
var $search = $('#fe-post-search');
|
||||
var $results = $('#fe-search-results');
|
||||
var $list = $('#fe-featured-list');
|
||||
var timer = null;
|
||||
|
||||
function selectedIds() {
|
||||
return $list.find('li').map(function () { return String($(this).data('id')); }).get();
|
||||
}
|
||||
|
||||
function addChip(post) {
|
||||
if (-1 !== $.inArray(String(post.id), selectedIds())) {
|
||||
return;
|
||||
}
|
||||
var $chip = $('<li class="fe-chip"></li>').attr('data-id', post.id);
|
||||
$chip.append(document.createTextNode(post.title + ' '));
|
||||
$chip.append($('<span class="fe-chip-type"></span>').text(post.type));
|
||||
$chip.append($('<input type="hidden">').attr('name', 'fe_featured_post_ids[]').val(post.id));
|
||||
$chip.append($('<button type="button" class="fe-chip-remove" aria-label="Remove">×</button>'));
|
||||
$list.append($chip);
|
||||
}
|
||||
|
||||
function renderResults(posts) {
|
||||
$results.empty();
|
||||
var ids = selectedIds();
|
||||
var visible = posts.filter(function (p) { return -1 === $.inArray(String(p.id), ids); });
|
||||
if (!visible.length) {
|
||||
$results.append($('<li></li>').text(FEAdmin.i18n.noResults)).show();
|
||||
return;
|
||||
}
|
||||
visible.forEach(function (post) {
|
||||
var $item = $('<li></li>').append(
|
||||
$('<button type="button" class="fe-result"></button>').text(post.title + ' (' + post.type + ')')
|
||||
);
|
||||
$item.find('button').on('click', function () {
|
||||
addChip(post);
|
||||
$search.val('');
|
||||
$results.hide().empty();
|
||||
});
|
||||
$results.append($item);
|
||||
});
|
||||
$results.show();
|
||||
}
|
||||
|
||||
$search.on('input', function () {
|
||||
var term = $search.val();
|
||||
clearTimeout(timer);
|
||||
if (!term || term.length < 2) {
|
||||
$results.hide().empty();
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(function () {
|
||||
$.post(FEAdmin.ajaxUrl, {
|
||||
action: FEAdmin.action,
|
||||
nonce: FEAdmin.nonce,
|
||||
s: term
|
||||
}, function (response) {
|
||||
if (response && response.success && response.data && response.data.posts) {
|
||||
renderResults(response.data.posts);
|
||||
}
|
||||
});
|
||||
}, 300);
|
||||
});
|
||||
|
||||
$results.on('mousedown', function (e) { e.preventDefault(); }); // Keep input focus while clicking a result.
|
||||
$(document).on('click', function (e) {
|
||||
if (!$(e.target).closest('#fe-featured-picker').length) {
|
||||
$results.hide();
|
||||
}
|
||||
});
|
||||
$list.on('click', '.fe-chip-remove', function () {
|
||||
$(this).closest('li').remove();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user