1
0
mirror of https://github.com/github/choosealicense.com synced 2024-06-29 15:13:04 +02:00
choosealicense.com/assets/js/app.coffee

97 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2014-07-31 18:36:56 +02:00
---
---
2013-07-13 21:18:57 +02:00
class Choosealicense
# Selects the content of a given element
selectText: (element) ->
if document.body.createTextRange
2013-11-30 00:08:27 +01:00
range = document.body.createTextRange()
range.moveToElementText(element)
range.select()
else if window.getSelection
selection = window.getSelection()
range = document.createRange()
range.selectNodeContents(element)
2013-11-30 00:08:27 +01:00
selection.removeAllRanges()
selection.addRange(range)
2013-07-13 21:18:57 +02:00
# Qtip position attributes for tooltips
qtip_position:
my: "top center"
at: "bottom center"
# Annotation rule types as defined in `_config.yml`
ruletypes:
2016-04-16 18:58:54 +02:00
permissions: "Permission"
conditions: "Condition"
limitations: "Limitation"
2013-07-13 21:18:57 +02:00
# fire on document.ready
constructor: ->
@initTooltips()
@initClipboard()
@initAutocomplete()
2013-07-13 21:18:57 +02:00
# Init tooltip action
initTooltips: ->
# Dynamically add annotations as title attribute to rule list items
for ruletype, rules of window.annotations
2014-09-05 17:26:54 +02:00
for rule in rules
2016-05-10 19:43:44 +02:00
$(".license-#{ruletype} .#{rule["tag"]}").attr "title", rule["description"]
2013-07-13 21:18:57 +02:00
# Init tooltips on all rule list items
for ruletype, label of @ruletypes
2016-05-10 19:43:44 +02:00
$(".license-#{ruletype} li, .license-#{ruletype} .license-sprite").qtip
2013-07-13 21:18:57 +02:00
content:
text: false
title:
text: label
position: @qtip_position
style:
classes: "qtip-shadow qtip-#{ruletype}"
2013-07-13 21:18:57 +02:00
false
# Initializes Clipboard.js
initClipboard: ->
2013-11-30 00:08:27 +01:00
# Backup the clipboard button's original text.
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
2013-11-30 00:08:27 +01:00
# Hook up copy to clipboard buttons
clip = new Clipboard ".js-clipboard-button"
2013-11-30 00:08:27 +01:00
clip.on "mouseout", @clipboardMouseout
clip.on "complete", @clipboardComplete
2013-07-13 21:18:57 +02:00
# Callback to restore the clipboard button's original text
clipboardMouseout: (client, args) ->
@textContent = $(this).data("clipboard-prompt")
2013-07-13 21:18:57 +02:00
# Post-copy user feedback callback
clipboardComplete: (client, args) ->
@textContent = "Copied!"
2013-07-13 21:18:57 +02:00
# Initializes JavaScript-autoComplete plugin
initAutocomplete: ->
new autoComplete {
selector: "#reposiory-search",
delay: 300,
source: (term, response) ->
$.getJSON "https://api.github.com/search/repositories", {q: term}, (data) ->
if data and data.total_count > 0
response (data.items
.filter (item) -> item.archived == false && !item.license
.map (item) -> item.full_name)
else
response([])
onSelect: (event, repository, item) ->
2018-10-09 20:45:03 +02:00
licenseId = document.getElementById("reposiory-search").getAttribute("data-license-id")
if licenseId
window.open 'https://github.com/'+repository+'/community/license/new?template='+licenseId
else
window.open 'https://github.com/'+repository+'/community/license/new'
}
2013-07-13 21:18:57 +02:00
$ ->
new Choosealicense()