MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* ========================= | /* ========================= | ||
GLOBAL CLICK HANDLER (SAFE) | |||
========================= */ | ========================= */ | ||
$(document).on('click | $(document).on('click', function (e) { | ||
var $ | var $target = $(e.target); | ||
var $ | /* ========================= | ||
COMMENTARY CLICK | |||
========================= */ | |||
var $commentBtn = $target.closest('.verse-action-commentary'); | |||
if ($commentBtn.length) { | |||
e.preventDefault(); | |||
var verseId = $commentBtn.data('verse'); | |||
if (!verseId) return; | |||
var $bodies = $('.commentary-body[data-verse="' + verseId + '"]'); | |||
var isOpen = !$bodies.first().hasClass('open'); | |||
// close others (optional UX) | |||
$('.commentary-body.open').removeClass('open'); | |||
$('.verse-action-commentary.active').removeClass('active'); | |||
// toggle | |||
$bodies.toggleClass('open', isOpen); | |||
$commentBtn.toggleClass('active', isOpen); | |||
return; | |||
} | |||
/* ========================= | |||
COPY VERSE CLICK | |||
========================= */ | |||
var $copyBtn = $target.closest('.verse-action-copy'); | |||
if ($copyBtn.length) { | |||
e.preventDefault(); | |||
var line1 = $copyBtn.data('line1') || ''; | |||
var line2 = $copyBtn.data('line2') || ''; | |||
var text = line2 ? line1 + '\n' + line2 : line1; | |||
if (!text) return; | |||
copyText(text, $copyBtn); | |||
return; | |||
} | |||
/* ========================= | |||
COPY ID CLICK | |||
========================= */ | |||
var $idBtn = $target.closest('.copy-id-btn'); | |||
if ($idBtn.length) { | |||
e.preventDefault(); | |||
var id = $idBtn.data('copyid'); | |||
if (!id) return; | |||
copyText(id, $idBtn); | |||
return; | |||
} | |||
}); | }); | ||
/* ========================= | /* ========================= | ||
COPY | COPY FUNCTION + TOOLTIP | ||
========================= */ | ========================= */ | ||
function copyText(text, $btn) { | |||
function showTooltip() { | |||
var $tip = $('<span class="copy-tooltip">Copied ✓</span>'); | |||
$btn.append($tip); | |||
setTimeout(function () { | setTimeout(function () { | ||
$ | $tip.fadeOut(200, function () { $(this).remove(); }); | ||
}, | }, 1200); | ||
} | } | ||
if (navigator.clipboard && window.isSecureContext) { | if (navigator.clipboard && window.isSecureContext) { | ||
navigator.clipboard.writeText(text).then( | navigator.clipboard.writeText(text).then(showTooltip); | ||
} else { | } else { | ||
var $temp = $('<textarea>').val(text).appendTo('body'); | var $temp = $('<textarea>').val(text).appendTo('body'); | ||
| Line 50: | Line 88: | ||
document.execCommand('copy'); | document.execCommand('copy'); | ||
$temp.remove(); | $temp.remove(); | ||
showTooltip(); | |||
} | } | ||
} | } | ||