diff --git a/javascripts/app.coffee b/javascripts/app.coffee index 0dae5bb..2bb3806 100644 --- a/javascripts/app.coffee +++ b/javascripts/app.coffee @@ -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() \ No newline at end of file + new Choosealicense() diff --git a/javascripts/app.js b/javascripts/app.js index 5b6be4c..f140d3c 100644 --- a/javascripts/app.js +++ b/javascripts/app.js @@ -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"); };