| 1 |
|
| 2 |
(function ($) { |
| 3 |
$(document).ready(function ($) { |
| 4 |
function appendImage(event = null, attachment = null, remove = false, previewClass = '.doc-category-image-wrapper', imgID = '.doc-category-image-id') { |
| 5 |
if (event === null) { |
| 6 |
document.querySelectorAll(previewClass).forEach(node => { |
| 7 |
let customImage = node.querySelector('.custom_media_image'); |
| 8 |
let displayedImage = node.querySelector('img'); |
| 9 |
|
| 10 |
if( customImage ) { |
| 11 |
customImage.remove(); |
| 12 |
} |
| 13 |
|
| 14 |
if( displayedImage ) { |
| 15 |
displayedImage.style.display = 'block'; |
| 16 |
} |
| 17 |
}); |
| 18 |
|
| 19 |
document.querySelectorAll(imgID).forEach(node => { |
| 20 |
node.value = ''; |
| 21 |
}); |
| 22 |
|
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
const closestParent = event.target.closest('.form-field'); |
| 27 |
const node = closestParent.querySelector(previewClass); |
| 28 |
const imageIDNode = closestParent.querySelector(imgID); |
| 29 |
|
| 30 |
if (!remove) { |
| 31 |
let alreadySetImage = node.querySelector('.custom_media_image'); |
| 32 |
if (alreadySetImage) { |
| 33 |
alreadySetImage.src = attachment?.url ?? ''; |
| 34 |
} else { |
| 35 |
const tempImage = document.createElement('img'); |
| 36 |
tempImage.src = attachment?.url ?? ''; |
| 37 |
tempImage.style.margin = '0px'; |
| 38 |
tempImage.style.padding = '0px'; |
| 39 |
tempImage.style.maxHeight = '100px'; |
| 40 |
tempImage.style.float = 'none'; |
| 41 |
tempImage.classList.add('custom_media_image'); |
| 42 |
|
| 43 |
node.querySelector('img').style.display = 'none'; |
| 44 |
node.append(tempImage); |
| 45 |
} |
| 46 |
imageIDNode.value = attachment?.id ?? ''; |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
imageIDNode.value = ''; |
| 51 |
node.querySelector('.custom_media_image')?.remove(); |
| 52 |
node.querySelector('img').style.display = 'block'; |
| 53 |
} |
| 54 |
|
| 55 |
function betterdocs_media_upload(button_class) { |
| 56 |
var _custom_media = true, |
| 57 |
_betterdocs_send_attachment = wp.media.editor.send.attachment; |
| 58 |
|
| 59 |
$('body').on('click', button_class, function (e) { |
| 60 |
let button_id = '#' + $(this).attr('id'); |
| 61 |
let button = $(button_id); |
| 62 |
_custom_media = true; |
| 63 |
|
| 64 |
// Reset the image selection |
| 65 |
wp.media.frame?.state()?.get('selection')?.reset(); |
| 66 |
wp.media.editor.send.attachment = function (props, attachment) { |
| 67 |
if (_custom_media) { |
| 68 |
appendImage(e, attachment) |
| 69 |
} else { |
| 70 |
return _betterdocs_send_attachment.apply(button_id, [props, attachment]); |
| 71 |
} |
| 72 |
} |
| 73 |
wp.media.editor.open(button); |
| 74 |
return false; |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
betterdocs_media_upload('.betterdocs_tax_media_button.button'); |
| 79 |
|
| 80 |
$('body').on('click', '.doc_tax_media_remove', e => appendImage(e, null, true)); |
| 81 |
|
| 82 |
$(document).ajaxComplete(function (event, xhr, settings) { |
| 83 |
var queryStringArr = settings.data.split('&'); |
| 84 |
if ($.inArray('action=add-tag', queryStringArr) !== -1) { |
| 85 |
var xml = xhr.responseXML; |
| 86 |
$response = $(xml).find('term_id').text(); |
| 87 |
if ($response != "") { |
| 88 |
// Clear the thumb image |
| 89 |
appendImage(); |
| 90 |
} |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* DRAG and DROP sortable categories |
| 97 |
*/ |
| 98 |
function betterdocsCategorySorter(config, className = '.taxonomy-doc_category') { |
| 99 |
if (config === undefined) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
var $ = jQuery; |
| 104 |
let _paged = parseInt(config.paged); |
| 105 |
let _per_page_id = parseInt($("#" + config.per_page_id).val()); |
| 106 |
|
| 107 |
const base_index = _paged > 0 ? (_paged - 1) * _per_page_id : 0; |
| 108 |
const tableList = document.querySelector(className + " #the-list"); |
| 109 |
|
| 110 |
if (tableList != null && !tableList.querySelector('tr:first-child').classList.contains('no-items')) { |
| 111 |
$(tableList).sortable({ |
| 112 |
placeholder: "betterdocs-drag-drop-item-placeholder", |
| 113 |
axis: "y", |
| 114 |
|
| 115 |
// On start, set a height for the placeholder to prevent table jumps. |
| 116 |
start: function (event, ui) { |
| 117 |
const item = $(ui.item[0]); |
| 118 |
$(".betterdocs-drag-drop-item-placeholder").css("height", item.css("height")); |
| 119 |
}, |
| 120 |
// Update callback. |
| 121 |
update: function (event, ui) { |
| 122 |
const taxonomy_ordering_data = []; |
| 123 |
|
| 124 |
$(tableList) |
| 125 |
.find("tr.ui-sortable-handle") |
| 126 |
.each(function () { |
| 127 |
const ele = $(this); |
| 128 |
const term_data = { |
| 129 |
term_id: ele.attr("id").replace("tag-", ""), |
| 130 |
order: parseInt(ele.index()) + 1, |
| 131 |
}; |
| 132 |
taxonomy_ordering_data.push(term_data); |
| 133 |
}); |
| 134 |
|
| 135 |
// AJAX Data. |
| 136 |
const data = { |
| 137 |
action: config.action, |
| 138 |
data: taxonomy_ordering_data, |
| 139 |
base_index, |
| 140 |
nonce: config.nonce |
| 141 |
}; |
| 142 |
|
| 143 |
// Run the ajax request. |
| 144 |
$.ajax({ |
| 145 |
type: "POST", |
| 146 |
url: config.ajaxurl, |
| 147 |
data, |
| 148 |
dataType: "JSON", |
| 149 |
success: function (response) { }, |
| 150 |
error: function (error) { |
| 151 |
console.error("Ordering Error: ", error) |
| 152 |
} |
| 153 |
}); |
| 154 |
}, |
| 155 |
}); |
| 156 |
} |
| 157 |
} |
| 158 |
if (window?.betterdocsCategorySorting != undefined) { |
| 159 |
betterdocsCategorySorter( |
| 160 |
window.betterdocsCategorySorting, |
| 161 |
window?.betterdocsCategorySorting?.selector ?? '.taxonomy-doc_category' |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
}); |
| 166 |
})(jQuery); |
| 167 |
|