Skip to content

Commit

Permalink
Bug 1161797: Use document.execCommand("copy") instead of flash where …
Browse files Browse the repository at this point in the history
…it is available
  • Loading branch information
globau committed Jun 21, 2015
1 parent aa11ef5 commit 43a45ed
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
Expand Up @@ -244,6 +244,7 @@
[% IF bug.assigned_to.id == user.id || user.in_group("editbugs") %]
<button type="button" id="copy-summary" class="minor"
title="Copy [% terms.bug %] number and summary to your clipboard">Copy Summary</button>
<div id="clip-container" style="display:none"><input type="text" id="clip"></div>
[% END %]
[% IF user.id %]
<button type="button" class="minor" id="cc-btn" data-is-cced="[% is_cced ? 1 : 0 %]">
Expand Down
12 changes: 12 additions & 0 deletions extensions/BugModal/web/bug_modal.css
Expand Up @@ -887,3 +887,15 @@ div.ui-tooltip {
.search-nav-disabled {
color: #777;
}

/* clipboard shenanigans */

#clip-container {
position: fixed;
top: 0px;
left: 0px;
width: 0px;
height: 0px;
z-index: 100;
opacity: 0;
}
82 changes: 55 additions & 27 deletions extensions/BugModal/web/bug_modal.js
Expand Up @@ -162,37 +162,65 @@ $(function() {
});

// copy summary to clipboard

function clipboardSummary() {
return 'Bug ' + BUGZILLA.bug_id + ' - ' + $('#field-value-short_desc').text();
}

if ($('#copy-summary').length) {

// we don't know if flash is enabled without waiting for load to timeout
// remember the flash enabled state between pages
var hasFlash = true;
if (localStorage.getItem('hasFlash') === null) {
$('#copy-summary').hide();
// probe for document.execCommand("copy") support
var hasExecCopy = false;
try {
// on page load nothing will be selected, so we don't smash the
// clipboard doing this
document.execCommand("copy");
hasExecCopy = true;
} catch(ex) {
// ignore
}
else {
hasFlash = localStorage.getItem('hasFlash');

if (hasExecCopy) {
$('#copy-summary')
.click(function() {
// execCommand("copy") only works on selected text
$('#clip-container').show();
$('#clip').val(clipboardSummary()).select();
document.execCommand("copy");
$('#clip-container').hide();
});
}
if (hasFlash) {
ZeroClipboard.config({ flashLoadTimeout: 5000 });
var zero = new ZeroClipboard($('#copy-summary'));
zero.on({
'ready': function(event) {
$('#copy-summary').show();
localStorage.setItem('hasFlash', true);
},
'error': function(event) {
console.log(event.message);
zero.destroy();
$('#global-zeroclipboard-html-bridge').remove();
$('#copy-summary').hide();
localStorage.removeItem('hasFlash');
},
'copy': function(event) {
var clipboard = event.clipboardData;
clipboard.setData('text/plain', 'Bug ' + BUGZILLA.bug_id + ' - ' + $('#field-value-short_desc').text());
}
});
else {
// we don't know if flash is enabled without waiting for load to timeout
// remember the flash enabled state between pages
var hasFlash = true;
if (localStorage.getItem('hasFlash') === null) {
$('#copy-summary').hide();
}
else {
hasFlash = localStorage.getItem('hasFlash');
}
if (hasFlash) {
ZeroClipboard.config({ flashLoadTimeout: 5000 });
var zero = new ZeroClipboard($('#copy-summary'));
zero.on({
'ready': function(event) {
$('#copy-summary').show();
localStorage.setItem('hasFlash', true);
},
'error': function(event) {
console.log(event.message);
zero.destroy();
$('#global-zeroclipboard-html-bridge').remove();
$('#copy-summary').hide();
localStorage.removeItem('hasFlash');
},
'copy': function(event) {
var clipboard = event.clipboardData;
clipboard.setData('text/plain', clipboardSummary());
}
});
}
}
}

Expand Down

0 comments on commit 43a45ed

Please sign in to comment.