1
0
mirror of https://github.com/github/choosealicense.com synced 2024-12-21 20:30:10 +01:00

Adds a way to copy the license without using ZeroClipboard

* Selects the license when the browser does not have Flash installed so people
 can easily copy & paste it.
This commit is contained in:
Bruno Aguirre 2013-07-15 17:19:31 -03:00
parent 565d35107e
commit 60f9ab0ab7
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,19 +58,32 @@ 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: ->
if ZeroClipboard? && @flashAvailable()
@initZeroClipboard()
else
@initAlternativeClipboard()
# 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
# Callback to restore the clipboard button's original text
clipboardMouseout: (client, args) ->
@innerText = $(this).data("clipboard-prompt")
@ -59,4 +93,4 @@ class Choosealicense
@innerText = "Copied!"
$ ->
new Choosealicense()
new Choosealicense()

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");
};