mirror of
https://github.com/github/choosealicense.com
synced 2024-12-22 12:50:24 +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
|
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 attributes for tooltips
|
||||||
qtip_position:
|
qtip_position:
|
||||||
my: "top center"
|
my: "top center"
|
||||||
@ -14,7 +35,7 @@ class Choosealicense
|
|||||||
# fire on document.ready
|
# fire on document.ready
|
||||||
constructor: ->
|
constructor: ->
|
||||||
@initTooltips()
|
@initTooltips()
|
||||||
@initClipboard() if ZeroClipboard?
|
@initClipboard()
|
||||||
|
|
||||||
# Init tooltip action
|
# Init tooltip action
|
||||||
initTooltips: ->
|
initTooltips: ->
|
||||||
@ -37,9 +58,8 @@ class Choosealicense
|
|||||||
|
|
||||||
false
|
false
|
||||||
|
|
||||||
# if Zero Clipboard is present, bind to button and init
|
# Initializes ZeroClipboard
|
||||||
initClipboard: ->
|
initZeroClipboard: ->
|
||||||
|
|
||||||
# Backup the clipboard button's original text.
|
# Backup the clipboard button's original text.
|
||||||
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
|
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
|
||||||
|
|
||||||
@ -50,6 +70,20 @@ class Choosealicense
|
|||||||
clip.on "complete", @clipboardComplete
|
clip.on "complete", @clipboardComplete
|
||||||
clip
|
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
|
# Callback to restore the clipboard button's original text
|
||||||
clipboardMouseout: (client, args) ->
|
clipboardMouseout: (client, args) ->
|
||||||
@innerText = $(this).data("clipboard-prompt")
|
@innerText = $(this).data("clipboard-prompt")
|
||||||
|
@ -1,8 +1,32 @@
|
|||||||
// Generated by CoffeeScript 1.6.3
|
// Generated by CoffeeScript 1.4.0
|
||||||
(function() {
|
(function() {
|
||||||
var Choosealicense;
|
var Choosealicense;
|
||||||
|
|
||||||
Choosealicense = (function() {
|
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 = {
|
Choosealicense.prototype.qtip_position = {
|
||||||
my: "top center",
|
my: "top center",
|
||||||
at: "bottom center"
|
at: "bottom center"
|
||||||
@ -16,10 +40,8 @@
|
|||||||
|
|
||||||
function Choosealicense() {
|
function Choosealicense() {
|
||||||
this.initTooltips();
|
this.initTooltips();
|
||||||
if (typeof ZeroClipboard !== "undefined" && ZeroClipboard !== null) {
|
|
||||||
this.initClipboard();
|
this.initClipboard();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Choosealicense.prototype.initTooltips = function() {
|
Choosealicense.prototype.initTooltips = function() {
|
||||||
var category, label, rules, text, _ref;
|
var category, label, rules, text, _ref;
|
||||||
@ -49,7 +71,7 @@
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
Choosealicense.prototype.initClipboard = function() {
|
Choosealicense.prototype.initZeroClipboard = function() {
|
||||||
var clip;
|
var clip;
|
||||||
$(".js-clipboard-button").data("clipboard-prompt", $(".js-clipboard-button").text());
|
$(".js-clipboard-button").data("clipboard-prompt", $(".js-clipboard-button").text());
|
||||||
clip = new ZeroClipboard($(".js-clipboard-button"), {
|
clip = new ZeroClipboard($(".js-clipboard-button"), {
|
||||||
@ -60,6 +82,23 @@
|
|||||||
return clip;
|
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) {
|
Choosealicense.prototype.clipboardMouseout = function(client, args) {
|
||||||
return this.innerText = $(this).data("clipboard-prompt");
|
return this.innerText = $(this).data("clipboard-prompt");
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user