diff --git a/www/assets/css/main.css b/www/assets/css/main.css index a8460e1..7cfd184 100644 --- a/www/assets/css/main.css +++ b/www/assets/css/main.css @@ -242,3 +242,72 @@ img, video { background-color: rgba(255, 255, 255, .95); } +/** + * Generate links to be copied to anchor targets: + * + * First, make the target stand out to the left a bit, so that hovering gets activated more easily + */ +H1[id], +H2[id], +H3[id], +H4[id], +H5[id], +H6[id], +[id] > H1, +[id] > H2, +[id] > H3, +[id] > H4, +[id] > H5, +[id] > H6, +[id].copy-link, +[id].copy-link-child > :first-child { + z-index: 99; + margin-left: -1em; + padding-left: 1em; + position: relative; +} + +/* Then, on hovering, make it go even further to the left, so there is room for the link icon */ +H1[id]:hover, +H2[id]:hover, +H3[id]:hover, +H4[id]:hover, +H5[id]:hover, +H6[id]:hover, +[id] > H1:hover, +[id] > H2:hover, +[id] > H3:hover, +[id] > H4:hover, +[id] > H5:hover, +[id] > H6:hover, +[id].copy-link:hover, +[id].copy-link-child > :first-child:hover { + z-index: 99; + margin-left: -2em; + padding-left: 0.3em; + position: relative; +} + +/* And last, make the link icon appear to the left. */ +H1[id]:hover::before, +H2[id]:hover::before, +H3[id]:hover::before, +H4[id]:hover::before, +H5[id]:hover::before, +H6[id]:hover::before, +[id] > H1:hover::before, +[id] > H2:hover::before, +[id] > H3:hover::before, +[id] > H4:hover::before, +[id] > H5:hover::before, +[id] > H6:hover::before, +[id].copy-link:hover::before, +[id].copy-link-child > :first-child:hover::before { + content: '\01f517 \a0'; + cursor: pointer; +} + +/* Make the text in the toast align to the left, as it looks more smoothly. */ +.toast > li { + text-align: right; +} diff --git a/www/assets/js/main.js b/www/assets/js/main.js index 906810a..8332e9c 100644 --- a/www/assets/js/main.js +++ b/www/assets/js/main.js @@ -62,4 +62,141 @@ $(async => { } })(); }; + + /** + * Function to copy a text to the clipboard + **/ + function copyTextToClipboard(text) { + const textarea = document.createElement('textarea'); + const selection = document.getSelection(); + const range = document.createRange(); + + textarea.textContent = text; + document.body.appendChild(textarea); + + range.selectNode(textarea); + selection.removeAllRanges(); + selection.addRange(range); + + const result = document.execCommand('copy'); + + document.body.removeChild(textarea); + + return result; + } + + /** + * Function to dynamically load CSS with a single request. + * + * @see: https://stackoverflow.com/a/46984311/3102305 + */ + $.extend({ + getCss: function (url, success) { + + if ($("head").children("style[data-url='" + url + "']").length) { + console.warn("CSS file already loaded: " + url); + } + + var deferred = $.Deferred(function (defer) { + $.ajax({ + url: url + , context: {defer: defer, url: url} + , dataType: 'text' + , cache: (function () { + let cache = $.ajaxSetup().cache; + if (cache === undefined) { + cache = false; + } + return cache; + })() + }) + .then( + function (css, textStatus, jqXHR) { + css = "\n\n/* CSS dynamically loaded by jQuery */\n\n" + css; + $('').appendTo("head"); + this.defer.resolve(css, textStatus, jqXHR); + } + , function (jqXHR, textStatus, errorThrown) { + this.defer.reject(jqXHR, textStatus, errorThrown); + } + ); + }); + if ($.isFunction(success)) { + deferred.done(success); + } + return deferred; + } + }); + + /** + * Append event listener to the elements that shall get the icon to copy the anchored link + */ + document.querySelectorAll( + 'h1[id], h2[id], h3[id], h4[id], h5[id], h6[id], ' + + '[id]>h1, [id]>h2, [id]>h3, [id]>h4, [id]>h5, [id]>h6, ' + + '[id].copy-link-child > :first-child' + ).forEach(function (element) { + element.addEventListener("click", function (event) { + + const id = event.target.id !== '' + ? event.target.id + : event.target.parentElement.id + + const page = window.location.href + .replace(/#.*$/, ''); + + const link = page + '#' + id; + const nilesoft = link + .replace(/^https?:\/\/[^/]+/, 'https://nilesoft.org'); + + const emAsPx = parseFloat(getComputedStyle(event.target).fontSize); + const em2 = 2 * emAsPx + + const hitLinkIcon = event.layerX < em2; + + /*console.log({ + emAsPx: emAsPx, + em2: em2, + layerX: event.layerX, + clientX: event.clientX, + doCopy: hitLinkIcon, + link: link, + offsetLeft: event.target.offsetLeft, + parentOffsetLeft: event.target.parentElement.offsetLeft, + parentClass: event.target.parentElement.className, + event: event + })*/ + + if (hitLinkIcon) { + const result = copyTextToClipboard(nilesoft); + if (result) { + console.log('copied: ' + nilesoft); + + const toast = function () { + $.toast.config.align = 'right'; + $.toast('' + nilesoft + '
copied to clipboard!', { + type: 'info', + duration: 3000, + }); + } + + if ($.toast === undefined) { + console.log('loading jquery.toast ...') + // @see: https://github.com/Soldier-B/jquery.toast + $.getCss( + "/assets/lib/jquery.toast/jquery.toast.min.css", + $.getScript( + "/assets/lib/jquery.toast/jquery.toast.min.js" + , toast + ) + ); + } else { + toast(); + } + + window.location.href = link; + } + } + }); + }); }); \ No newline at end of file diff --git a/www/assets/lib/jquery.toast/jquery.toast.css b/www/assets/lib/jquery.toast/jquery.toast.css new file mode 100644 index 0000000..646819d --- /dev/null +++ b/www/assets/lib/jquery.toast/jquery.toast.css @@ -0,0 +1,8 @@ +.toast{ position:fixed; list-style: none; padding: 0; top:0; z-index: 999999; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size:14px; line-height:20px; } +.toast li{ margin: 10px 0 0 0; display:block; background-color:#fcf8e3; color:#c09853; border:1px solid #fbeed5; padding:5px 10px; border-radius: 4px; -webkit-border-radius: 4px; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5); box-shadow: 0 2px 5px rgba(0,0,0,.15); -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.15); } +.toast li:first-child{ margin-top:0; } +.toast li.danger{ color: #b94a48; background-color: #f2dede; border-color: #eed3d7; } +.toast li.info{ color: #3a87ad; background-color: #d9edf7; border-color: #bce8f1; } +.toast li.success{ color: #468847; background-color: #dff0d8; border-color: #d6e9c6; } +.toast button.close{ background: none; border: none; font-weight: bold; font-size: 20px; line-height: 20px; float: right; padding: 0; margin: 0 0 0 5px; color: rgba(0,0,0,.25); cursor: pointer; } +.toast h1, .toast h2, .toast h3, .toast h4 { display: inline; } \ No newline at end of file diff --git a/www/assets/lib/jquery.toast/jquery.toast.js b/www/assets/lib/jquery.toast/jquery.toast.js new file mode 100644 index 0000000..2b4cba5 --- /dev/null +++ b/www/assets/lib/jquery.toast/jquery.toast.js @@ -0,0 +1,41 @@ +(function($){ + var th = null, cf = null, toast = function(m,o){ + // fix option type + o = $.extend({ duration: 5000, sticky: false, 'type': ''}, o); + typeof o.duration === 'number' || (o.duration = 5000); + typeof o.sticky === 'boolean' || (o.sticky = false); + typeof o.type === 'string' || (o.type = ''); + // create host on first call + if(!th){ + // get/fix config + cf = toast.config; + th = $('').addClass('toast').appendTo(document.body).hide(); + typeof cf.width === 'number' || (cf.width = 500); + typeof cf.align === 'string' || (cf.align = 'center'); + typeof cf.closeForStickyOnly === 'boolean' || (cf.closeForStickyOnly = false); + th.width(cf.width); + (cf.align === 'left' || cf.align === 'right') && th.css('margin','5px').css(cf.align, '0') || th.css({left: '50%', margin: '5px 0 0 -' + (cf.width / 2) + 'px'}); + } + // create toast + var ti = $('
  • ').hide().html(m).appendTo(th), cb = $('').addClass('close').prependTo(ti), to = null; + // setup close button + cb.click(function(){ + clearTimeout(to); + ti.animate({ height: 0, opacity: 0}, 'fast', function(){ + ti.remove(); + th.children().length || th.removeClass('active').hide(); + }); + }); + cf.closeForStickyOnly && !o.sticky && cb.hide(); + // add type class + o.type !== '' && ti.addClass(o.type); + // show host if necessary + !th.hasClass('active') && th.addClass('active').show(); + // setup timeout unless sticky + !o.sticky && o.duration > 0 && (to = setTimeout(function(){ cb.click(); }, o.duration)); + // show toast + ti.fadeIn('normal'); + }; + toast.config = { width: 500, align: 'center', closeForStickyOnly: true }; + $.extend({ toast: toast }); +})(jQuery); \ No newline at end of file diff --git a/www/assets/lib/jquery.toast/jquery.toast.min.css b/www/assets/lib/jquery.toast/jquery.toast.min.css new file mode 100644 index 0000000..85c6ab7 --- /dev/null +++ b/www/assets/lib/jquery.toast/jquery.toast.min.css @@ -0,0 +1 @@ +.toast{position:fixed;list-style:none;padding:0;top:0;z-index:999999;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:14px;line-height:20px}.toast li{margin:10px 0 0 0;display:block;background-color:#fcf8e3;color:#c09853;border:1px solid #fbeed5;padding:5px 10px;border-radius:4px;-webkit-border-radius:4px;text-shadow:0 1px 0 rgba(255,255,255,0.5);box-shadow:0 2px 5px rgba(0,0,0,.15);-webkit-box-shadow:0 2px 5px rgba(0,0,0,.15)}.toast li:first-child{margin-top:0}.toast li.danger{color:#b94a48;background-color:#f2dede;border-color:#eed3d7}.toast li.info{color:#3a87ad;background-color:#d9edf7;border-color:#bce8f1}.toast li.success{color:#468847;background-color:#dff0d8;border-color:#d6e9c6}.toast button.close{background:none;border:none;font-weight:bold;font-size:20px;line-height:20px;float:right;padding:0;margin:0 0 0 5px;color:rgba(0,0,0,.25);cursor:pointer}.toast h1,.toast h2,.toast h3,.toast h4{display:inline} \ No newline at end of file diff --git a/www/assets/lib/jquery.toast/jquery.toast.min.js b/www/assets/lib/jquery.toast/jquery.toast.min.js new file mode 100644 index 0000000..907faae --- /dev/null +++ b/www/assets/lib/jquery.toast/jquery.toast.min.js @@ -0,0 +1 @@ +(function(a){var b=null,c=null,d=function(e,f){f=a.extend({duration:5e3,sticky:!1,type:""},f),"number"==typeof f.duration||(f.duration=5e3),"boolean"==typeof f.sticky||(f.sticky=!1),"string"==typeof f.type||(f.type=""),b||(c=d.config,b=a("").addClass("toast").appendTo(document.body).hide(),"number"==typeof c.width||(c.width=500),"string"==typeof c.align||(c.align="center"),"boolean"==typeof c.closeForStickyOnly||(c.closeForStickyOnly=!1),b.width(c.width),("left"===c.align||"right"===c.align)&&b.css("margin","5px").css(c.align,"0")||b.css({left:"50%",margin:"5px 0 0 -"+c.width/2+"px"}));var g=a("
  • ").hide().html(e).appendTo(b),h=a("").addClass("close").prependTo(g),i=null;h.click(function(){clearTimeout(i),g.animate({height:0,opacity:0},"fast",function(){g.remove(),b.children().length||b.removeClass("active").hide()})}),c.closeForStickyOnly&&!f.sticky&&h.hide(),""!==f.type&&g.addClass(f.type),!b.hasClass("active")&&b.addClass("active").show(),!f.sticky&&f.duration>0&&(i=setTimeout(function(){h.click()},f.duration)),g.fadeIn("normal")};d.config={width:500,align:"center",closeForStickyOnly:!0},a.extend({toast:d})})(jQuery); \ No newline at end of file