1
0
mirror of https://github.com/github/choosealicense.com synced 2024-12-22 12:50:24 +01:00

Merge pull request #26 from elcuervo/copy_to_clipboard_without_flash

Adds a way to copy the license without using ZeroClipboard
This commit is contained in:
Phil Haack 2013-07-16 17:11:01 -07:00
commit b9560ccabc
2 changed files with 90 additions and 17 deletions

View File

@ -1,5 +1,26 @@
class Choosealicense
# Checks if Flash is available in the client.
flashAvailable: ->
if ActiveXObject?
!!(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'))
else
!!navigator.mimeTypes["application/x-shockwave-flash"]
# Selects the content of a given element
selectText: (element) ->
if document.body.createTextRange
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
else if window.getSelection
selection = window.getSelection()
range = document.createRange()
range.selectNodeContents(element)
selection.removeAllRanges();
selection.addRange(range);
# Qtip position attributes for tooltips
qtip_position:
my: "top center"
@ -14,7 +35,7 @@ class Choosealicense
# fire on document.ready
constructor: ->
@initTooltips()
@initClipboard() if ZeroClipboard?
@initClipboard()
# Init tooltip action
initTooltips: ->
@ -37,18 +58,31 @@ class Choosealicense
false
# Initializes ZeroClipboard
initZeroClipboard: ->
# Backup the clipboard button's original text.
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
# Hook up copy to clipboard buttons
clip = new ZeroClipboard $(".js-clipboard-button"),
moviePath: "/javascripts/ZeroClipboard.swf"
clip.on "mouseout", @clipboardMouseout
clip.on "complete", @clipboardComplete
clip
# Initializes an alternative way to copy the license for non-flash compatible
# browsers
initAlternativeClipboard: ->
$(".js-clipboard-button").click (e) =>
target = "#" + $(e.target).data("clipboard-target")
@selectText $(target)[0]
# if Zero Clipboard is present, bind to button and init
initClipboard: ->
# Backup the clipboard button's original text.
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
# Hook up copy to clipboard buttons
clip = new ZeroClipboard $(".js-clipboard-button"),
moviePath: "/javascripts/ZeroClipboard.swf"
clip.on "mouseout", @clipboardMouseout
clip.on "complete", @clipboardComplete
clip
if ZeroClipboard? && @flashAvailable()
@initZeroClipboard()
else
@initAlternativeClipboard()
# Callback to restore the clipboard button's original text
clipboardMouseout: (client, args) ->

View File

@ -1,8 +1,32 @@
// Generated by CoffeeScript 1.6.3
// Generated by CoffeeScript 1.4.0
(function() {
var Choosealicense;
Choosealicense = (function() {
Choosealicense.prototype.flashAvailable = function() {
if (typeof ActiveXObject !== "undefined" && ActiveXObject !== null) {
return !!(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
} else {
return !!navigator.mimeTypes["application/x-shockwave-flash"];
}
};
Choosealicense.prototype.selectText = function(element) {
var range, selection;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
return range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
return selection.addRange(range);
}
};
Choosealicense.prototype.qtip_position = {
my: "top center",
at: "bottom center"
@ -16,9 +40,7 @@
function Choosealicense() {
this.initTooltips();
if (typeof ZeroClipboard !== "undefined" && ZeroClipboard !== null) {
this.initClipboard();
}
this.initClipboard();
}
Choosealicense.prototype.initTooltips = function() {
@ -49,7 +71,7 @@
return false;
};
Choosealicense.prototype.initClipboard = function() {
Choosealicense.prototype.initZeroClipboard = function() {
var clip;
$(".js-clipboard-button").data("clipboard-prompt", $(".js-clipboard-button").text());
clip = new ZeroClipboard($(".js-clipboard-button"), {
@ -60,6 +82,23 @@
return clip;
};
Choosealicense.prototype.initAlternativeClipboard = function() {
var _this = this;
return $(".js-clipboard-button").click(function(e) {
var target;
target = "#" + $(e.target).data("clipboard-target");
return _this.selectText($(target)[0]);
});
};
Choosealicense.prototype.initClipboard = function() {
if ((typeof ZeroClipboard !== "undefined" && ZeroClipboard !== null) && this.flashAvailable()) {
return this.initZeroClipboard();
} else {
return this.initAlternativeClipboard();
}
};
Choosealicense.prototype.clipboardMouseout = function(client, args) {
return this.innerText = $(this).data("clipboard-prompt");
};