Skip to content

Instantly share code, notes, and snippets.

@shilovk
Created March 8, 2024 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilovk/cb6a82bab3774192e4eb4b01d979ab38 to your computer and use it in GitHub Desktop.
Save shilovk/cb6a82bab3774192e4eb4b01d979ab38 to your computer and use it in GitHub Desktop.
// Функция для извлечения URL изображения из описания
function getImageSrc(descriptionBody) {
let parser = new DOMParser();
let doc = parser.parseFromString(descriptionBody, 'text/html');
let imageElement = doc.querySelector('action-text-attachment[content-type^="image"]');
return imageElement ? imageElement.getAttribute('url') : null;
}
$('#tree').jstree({
'core': {
"themes": { "stripes": true },
"animation": 400,
"dblclick_toggle": false,
"keep_selected_style": false,
"check_callback": true,
'data': {
'url': '/categories.json',
'data': function(node) {
return { 'id': node.id };
}
}
},
'sort': function(a, b) {
let a1 = this.get_node(a);
let b1 = this.get_node(b);
return a1.icon === b1.icon ? a1.order - b1.order : a1.icon.localeCompare(b1.icon);
},
"state" : { "key" : "#tree" },
'search' : { 'fuzzy' : false },
'plugins': ['wholerow', 'sort', 'dnd', 'state', 'search', 'themes']
}).on("select_node.jstree", function (e, data) {
let results = data.selected.map(selectedId => {
let node = data.instance.get_node(selectedId);
if (node && node.text && node.original.description && node.original.description.body) {
let descriptionBody = node.original.description.body;
let imageSrc = getImageSrc(descriptionBody);
let imageHtml = imageSrc ? `<img src="${imageSrc}" alt="Изображение">` : '';
return `<h5><small class="text-body-secondary">Выбрано:</small> ${node.text}</h5>
<p class="mt-1 description">${descriptionBody}</p>
${imageHtml}`;
}
}).filter(Boolean).join('');
$('#result').html(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment