', {
'class': 'btn-clipboard',
'data-clipboard-text': $(this).text(),
'title': 'Click to copy'
}).append('');
if ($(this).parent().width() > 100) {
copyButton.append('Copy');
}
$(this).before($('', {'class': 'copy-clipboard'})
.append(copyButton));
$(copyButton.get(0)).mouseleave(function () {
$(this).attr('class', 'btn-clipboard');
$(this).removeAttr('aria-label');
});
var curClipboard = new Clipboard(copyButton.get(0));
curClipboard.on('success', function (e) {
e.clearSelection();
showTooltip(e.trigger, 'Copied!');
});
curClipboard.on('error', function (e) {
showTooltip(e.trigger, fallbackMessage(e.action));
});
});
}
function register_copy_clipboard($elements, callback) {
$elements.on('paste', function(event) {
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (const index in items) {
const item = items[index];
if (item.kind === 'file' && item.type.indexOf('image') !== -1) {
const blob = item.getAsFile();
const formData = new FormData();
formData.append('image', blob);
$(this).prop('disabled', true);
$.ajax({
url: '/pagedown/image-upload/',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data) {
// Assuming the server returns the URL of the image
const imageUrl = data.url;
const editor = $(event.target); // Get the textarea where the event was triggered
let currentMarkdown = editor.val();
const markdownImageText = '![](' + imageUrl + ')'; // Markdown for an image
if (currentMarkdown) currentMarkdown += "\n";
currentMarkdown += markdownImageText;
editor.val(currentMarkdown);
callback?.();
},
error: function() {
alert('There was an error uploading the image.');
},
complete: () => {
// Re-enable the editor
$(this).prop('disabled', false).focus();
}
});
// We only handle the first image in the clipboard data
break;
}
}
});
}
function activateBlogBoxOnClick() {
$('.blog-box').on('click', function () {
var $description = $(this).children('.blog-description');
var max_height = $description.css('max-height');
if (max_height !== 'fit-content') {
$description.css('max-height', 'fit-content');
$(this).css('cursor', 'auto');
$(this).removeClass('pre-expand-blog');
$(this).children().children('.show-more').hide();
}
});
$('.blog-box').each(function () {
var $precontent = $(this).children('.blog-description').height();
var $content = $(this).children().children('.content-description').height();
if ($content == undefined) {
$content = $(this).children().children('.md-typeset').height()
}
if ($content > $precontent - 30) {
$(this).addClass('pre-expand-blog');
$(this).css('cursor', 'pointer');
} else {
$(this).children().children('.show-more').hide();
}
});
}
function onWindowReady() {
// http://stackoverflow.com/a/1060034/1090657
var hidden = 'hidden';
// Standards:
if (hidden in document)
document.addEventListener('visibilitychange', onchange);
else if ((hidden = 'mozHidden') in document)
document.addEventListener('mozvisibilitychange', onchange);
else if ((hidden = 'webkitHidden') in document)
document.addEventListener('webkitvisibilitychange', onchange);
else if ((hidden = 'msHidden') in document)
document.addEventListener('msvisibilitychange', onchange);
// IE 9 and lower:
else if ('onfocusin' in document)
document.onfocusin = document.onfocusout = onchange;
// All others:
else
window.onpageshow = window.onpagehide
= window.onfocus = window.onblur = onchange;
function onchange(evt) {
var v = 'window-visible', h = 'window-hidden', evtMap = {
focus: v, focusin: v, pageshow: v, blur: h, focusout: h, pagehide: h
};
evt = evt || window.event;
if (evt.type in evtMap)
document.body.className = evtMap[evt.type];
else
document.body.className = this[hidden] ? 'window-hidden' : 'window-visible';
if ('$' in window)
$(window).trigger('dmoj:' + document.body.className);
}
$('.tabs').each(function () {
var $this = $(this), $h2 = $(this).find('h2'), $ul = $(this).find('ul');
var cutoff = ($h2.textWidth() || 400) + 20, handler;
$ul.children().each(function () {
cutoff += $(this).width();
});
$(window).resize(handler = function () {
$this.toggleClass('tabs-no-flex', $this.width() < cutoff);
});
handler();
});
// set the initial state (but only if browser supports the Page Visibility API)
if (document[hidden] !== undefined)
onchange({type: document[hidden] ? 'blur' : 'focus'});
$("a.close").click(function () {
var $closer = $(this);
$closer.parent().fadeOut(200);
});
register_time($('.time-with-rel'));
if (typeof window.orientation !== 'undefined') {
$(window).resize(function () {
var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
// $('#viewport').attr('content', width > 480 ? 'initial-scale=1' : 'width=480');
});
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!(/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type)) && !this.crossDomain)
xhr.setRequestHeader('X-CSRFToken', $.cookie('csrftoken'));
}
});
register_copy_clipboard($('textarea.wmd-input'));
$('form').submit(function (evt) {
// Prevent multiple submissions of forms, see #565
$("input[type='submit']").prop('disabled', true);
});
$('.lang-dropdown-item').click(function() {
$('select[name="language"]').val($(this).attr('value'));
$('#form-lang').submit();
})
$('#logout').on('click', () => $('#logout-form').submit());
populateCopyButton();
$('a').click(function() {
var href = $(this).attr('href');
var target = $(this).attr('target');
if (!href || href === '#' || href.startsWith("javascript") ||
$(this).attr("data-featherlight") ||
target === "_blank"
) {
return;
}
$("#loading-bar").show();
$("#loading-bar").animate({ width: "100%" }, 2000, function() {
$(this).stop(true, true);
$(this).hide().css({ width: 0});
});
});
$('.errorlist').each(function() {
var errorList = $(this);
errorList.nextAll('input, select, textarea').first().after(errorList);
});
register_all_toggles();
activateBlogBoxOnClick();
}
$(function() {
onWindowReady();
registerPopper($('#nav-lang-icon'), $('#lang-dropdown'));
registerPopper($('#user-links'), $('#userlink_dropdown'));
var $nav_list = $('#nav-list');
$('#navicon').click(function (event) {
event.stopPropagation();
$nav_list.toggle();
if ($nav_list.is(':hidden'))
$(this).blur().removeClass('hover');
else {
$(this).addClass('hover');
$nav_list.find('li ul').css('left', $('#nav-list').width()).hide();
}
}).hover(function () {
$(this).addClass('hover');
}, function () {
$(this).removeClass('hover');
});
$nav_list.find('li a .nav-expand').click(function (event) {
event.preventDefault();
$(this).parent().siblings('ul').css('display', 'block');
});
$nav_list.find('li a').each(function () {
if (!$(this).siblings('ul').length)
return;
$(this).on('contextmenu', function (event) {
event.preventDefault();
}).on('taphold', function () {
$(this).siblings('ul').css('display', 'block');
});
});
$nav_list.click(function (event) {
event.stopPropagation();
});
$('html').click(function () {
$nav_list.hide();
});
$(window).on('beforeunload', function() {
let key = `oj-content-${window.location.href}`;
let $contentClone = $('#content').clone();
$contentClone.find('.select2').remove();
$contentClone.find('.select2-hidden-accessible').removeClass('select2-hidden-accessible');
$contentClone.find('.noUi-base').remove();
$contentClone.find('.wmd-button-row').remove();
sessionStorage.setItem(key, JSON.stringify({
"html": $contentClone.html(),
"page": window.page,
"has_next_page": window.has_next_page,
"scrollOffset": $(window).scrollTop(),
}));
});
if (window.performance &&
window.performance.navigation.type
=== window.performance.navigation.TYPE_BACK_FORWARD) {
let key = `oj-content-${window.location.href}`;
let content = sessionStorage.getItem(key);
if (content) {
content = JSON.parse(content);
$('#content').html(content.html);
onWindowReady();
window.PAGE_FROM_BACK_BUTTON_CACHE = true;
$(window).scrollTop(content.scrollOffset - 100);
window.page = content.page;
window.has_next_page = content.has_next_page;
}
}
});