mirror of
https://github.com/github/choosealicense.com
synced 2024-12-22 04:40:09 +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:
parent
565d35107e
commit
60f9ab0ab7
@ -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,9 +58,8 @@ class Choosealicense
|
||||
|
||||
false
|
||||
|
||||
# if Zero Clipboard is present, bind to button and init
|
||||
initClipboard: ->
|
||||
|
||||
# Initializes ZeroClipboard
|
||||
initZeroClipboard: ->
|
||||
# Backup the clipboard button's original text.
|
||||
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
|
||||
|
||||
@ -50,6 +70,20 @@ class Choosealicense
|
||||
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()
|
||||
|
||||
# Callback to restore the clipboard button's original text
|
||||
clipboardMouseout: (client, args) ->
|
||||
@innerText = $(this).data("clipboard-prompt")
|
||||
|
@ -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,10 +40,8 @@
|
||||
|
||||
function Choosealicense() {
|
||||
this.initTooltips();
|
||||
if (typeof ZeroClipboard !== "undefined" && ZeroClipboard !== null) {
|
||||
this.initClipboard();
|
||||
}
|
||||
}
|
||||
|
||||
Choosealicense.prototype.initTooltips = function() {
|
||||
var category, label, rules, text, _ref;
|
||||
@ -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");
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user