From 147ee7fb79bbaf9fdd16e477522a5d4516261994 Mon Sep 17 00:00:00 2001 From: Guillaume Simon Date: Tue, 4 Aug 2015 11:27:29 +0200 Subject: [PATCH] Crappy but working flash less copy with latest browsers --- ratticweb/static/rattic/js/newcore.js | 53 ++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/ratticweb/static/rattic/js/newcore.js b/ratticweb/static/rattic/js/newcore.js index 293b59c2..268cd9d4 100644 --- a/ratticweb/static/rattic/js/newcore.js +++ b/ratticweb/static/rattic/js/newcore.js @@ -608,16 +608,12 @@ var RATTIC = (function ($, ZeroClipboard) { /* Add copy buttons to table cells */ my.controls.tableCopyButtons = function (cells) { - if (!FlashDetect.installed) { - return false; - } - cells.each(function () { // Get the players var me = $(this), button = me.children('button'), text = me.children('span'), - clip = new ZeroClipboard(button); + clip = FlashDetect.installed ? new ZeroClipboard(button) : null; // Set data for callbacks button.data('copyfrom', text); @@ -632,7 +628,21 @@ var RATTIC = (function ($, ZeroClipboard) { clip.on('mouseover', _showCopyButton); } - clip.on('dataRequested', _copyButtonGetData); + if(FlashDetect.installed) { + clip.on('dataRequested', _copyButtonGetData); + } else { + button.on('click', function(event) { + var fakeClient = { + setText: function(text) { + copyText(text); + } + } + + _copyButtonGetData.call(this, fakeClient); + + event.preventDefault(); + }); + } }); return true; @@ -781,3 +791,34 @@ $(document).ready(function () { sjcl.random.startCollectors(); }); +// https://gist.github.com/jonrohan/81085b119d16cdd7868a +var copyNode, copyText, createNode; + +createNode = function(text) { + var node; + node = document.createElement('span'); + node.style.position = 'absolute'; + node.style.left = '-10000px'; + node.textContent = text; + return node; +}; + +copyNode = function(node) { + var range, selection; + selection = getSelection(); + selection.removeAllRanges(); + range = document.createRange(); + range.selectNodeContents(node); + selection.addRange(range); + document.execCommand('copy'); + return selection.removeAllRanges(); +}; + +copyText = function(text) { + var node; + node = createNode(text); + document.body.appendChild(node); + copyNode(node); + return document.body.removeChild(node); +}; +