2014-07-31 18:36:56 +02:00
|
|
|
---
|
|
|
|
---
|
|
|
|
|
2013-07-13 21:18:57 +02:00
|
|
|
class Choosealicense
|
2013-07-15 22:19:31 +02:00
|
|
|
# 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()
|
2013-07-15 22:19:31 +02:00
|
|
|
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-15 22:19:31 +02:00
|
|
|
|
2013-07-13 21:18:57 +02:00
|
|
|
# Qtip position attributes for tooltips
|
|
|
|
qtip_position:
|
|
|
|
my: "top center"
|
|
|
|
at: "bottom center"
|
|
|
|
|
2015-09-09 01:24:05 +02:00
|
|
|
# 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()
|
2013-07-15 22:19:31 +02:00
|
|
|
@initClipboard()
|
2018-10-15 01:21:54 +02:00
|
|
|
@initLicenseSuggestion()
|
2013-07-13 21:18:57 +02:00
|
|
|
|
|
|
|
# Init tooltip action
|
|
|
|
initTooltips: ->
|
|
|
|
|
|
|
|
# Dynamically add annotations as title attribute to rule list items
|
2015-09-09 01:24:05 +02:00
|
|
|
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
|
2015-09-09 01:24:05 +02:00
|
|
|
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:
|
2015-09-09 01:24:05 +02:00
|
|
|
classes: "qtip-shadow qtip-#{ruletype}"
|
2013-07-13 21:18:57 +02:00
|
|
|
|
|
|
|
false
|
|
|
|
|
2015-10-25 05:47:13 +01:00
|
|
|
# Initializes Clipboard.js
|
2015-09-20 04:46:18 +02:00
|
|
|
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-07-15 22:19:31 +02:00
|
|
|
|
2013-11-30 00:08:27 +01:00
|
|
|
# Hook up copy to clipboard buttons
|
2015-10-25 05:47:13 +01:00
|
|
|
clip = new Clipboard ".js-clipboard-button"
|
2013-11-30 00:08:27 +01:00
|
|
|
clip.on "mouseout", @clipboardMouseout
|
|
|
|
clip.on "complete", @clipboardComplete
|
2013-07-15 22:19:31 +02:00
|
|
|
|
2013-07-13 21:18:57 +02:00
|
|
|
# Callback to restore the clipboard button's original text
|
|
|
|
clipboardMouseout: (client, args) ->
|
2014-12-18 20:53:45 +01:00
|
|
|
@textContent = $(this).data("clipboard-prompt")
|
2013-07-13 21:18:57 +02:00
|
|
|
|
|
|
|
# Post-copy user feedback callback
|
|
|
|
clipboardComplete: (client, args) ->
|
2014-12-18 20:53:45 +01:00
|
|
|
@textContent = "Copied!"
|
2018-10-15 01:21:54 +02:00
|
|
|
|
|
|
|
# Initializes the repository suggestion feature
|
|
|
|
initLicenseSuggestion: ->
|
|
|
|
inputEl = $("#repository-url")
|
|
|
|
licenseId = inputEl.attr("data-license-id")
|
|
|
|
statusIndicator = $(".status-indicator")
|
|
|
|
new LicenseSuggestion(inputEl, licenseId, statusIndicator)
|
2013-07-13 21:18:57 +02:00
|
|
|
|
2018-10-15 01:21:54 +02:00
|
|
|
class LicenseSuggestion
|
|
|
|
constructor: (@inputEl, @licenseId, @statusIndicator) ->
|
|
|
|
@setupTooltips()
|
|
|
|
@bindEventHandlers()
|
|
|
|
|
|
|
|
# Initializes tooltips on the input element
|
|
|
|
setupTooltips: =>
|
|
|
|
@inputEl.qtip
|
|
|
|
content:
|
|
|
|
text: false
|
|
|
|
title:
|
|
|
|
text: "message"
|
|
|
|
show: false
|
|
|
|
hide: false
|
|
|
|
position:
|
|
|
|
my: "top center"
|
|
|
|
at: "bottom center"
|
|
|
|
style:
|
|
|
|
classes: "qtip-shadow"
|
|
|
|
|
|
|
|
# Main event handlers for user input
|
|
|
|
bindEventHandlers: =>
|
|
|
|
@inputEl.on "input", (event) =>
|
|
|
|
@setStatus ""
|
|
|
|
.on "keyup", (event) =>
|
|
|
|
if event.keyCode == 13 and event.target.value
|
|
|
|
# Validate the user input first
|
|
|
|
try
|
|
|
|
repositoryFullName = @parseUserInput event.target.value
|
|
|
|
catch
|
|
|
|
@setStatus "Error", "Invalid URL."
|
|
|
|
return
|
|
|
|
|
|
|
|
@setStatus "Fetching"
|
|
|
|
@fetchInfoFromGithubAPI repositoryFullName, (err, repositoryInfo=null) =>
|
|
|
|
if (err)
|
|
|
|
@setStatus "Error", err.message
|
|
|
|
return
|
|
|
|
if repositoryInfo.license # The repository already has a license
|
|
|
|
license = repositoryInfo.license
|
|
|
|
@setStatus "Error", @repositoryLicense repositoryFullName, license
|
|
|
|
else # The repository is unlicensed
|
|
|
|
window.open "https://github.com/#{repositoryFullName}/community/license/new?template=#{@licenseId}"
|
|
|
|
@setStatus ""
|
|
|
|
@inputEl.val("")
|
|
|
|
|
|
|
|
# Try to extract the repository full name from the user input
|
|
|
|
parseUserInput: (userInput) ->
|
|
|
|
repository = /https?:\/\/github\.com\/(.*?)\/(.+)(\.git)?$/.exec userInput
|
|
|
|
[_, username, project] = repository
|
|
|
|
project = project
|
|
|
|
.split /\/|\.git/
|
|
|
|
.filter (str) -> str
|
|
|
|
.slice 0, 1
|
|
|
|
.join ""
|
|
|
|
return username + '/' + project
|
|
|
|
|
|
|
|
# Displays an indicator and tooltips to the user about the current status
|
|
|
|
setStatus: (status="", message="") =>
|
|
|
|
statusClass = status.toLowerCase()
|
|
|
|
displayQtip = (status, message) =>
|
|
|
|
@inputEl.qtip("api")
|
|
|
|
.set("content.text", message)
|
|
|
|
.set("content.title", status)
|
|
|
|
.set("style.classes", "qtip-shadow qtip-#{statusClass}")
|
|
|
|
.show()
|
|
|
|
|
|
|
|
switch status
|
|
|
|
when "Fetching"
|
|
|
|
@statusIndicator.removeClass('error').addClass(statusClass)
|
|
|
|
when "Error"
|
|
|
|
@statusIndicator.removeClass('fetching').addClass(statusClass)
|
|
|
|
displayQtip status, message
|
|
|
|
else
|
|
|
|
@inputEl.qtip("api").hide()
|
|
|
|
@statusIndicator.removeClass('fetching error')
|
|
|
|
|
|
|
|
# Fetches information about a repository from the Github API
|
|
|
|
fetchInfoFromGithubAPI: (repositoryFullName, callback) ->
|
|
|
|
$.getJSON "https://api.github.com/repos/"+repositoryFullName, (info) ->
|
|
|
|
callback null, info
|
|
|
|
.fail (e) ->
|
|
|
|
if e.status == 404
|
|
|
|
callback new Error "Repository <b>#{repositoryFullName}</b> not found."
|
|
|
|
else
|
|
|
|
callback new Error "Network error when trying to get information about <b>#{repositoryFullName}</b>."
|
|
|
|
|
|
|
|
# Generates a message showing that a repository is already licensed
|
|
|
|
repositoryLicense: (repositoryFullName, license) ->
|
|
|
|
foundLicense = window.licenses.find (lic) -> lic.spdx_id == license.spdx_id
|
|
|
|
if foundLicense # Links the license to its page on this site
|
|
|
|
"The repository <b> #{repositoryFullName}</b> is already licensed under the
|
|
|
|
<a href='/licenses/#{foundLicense.spdx_id.toLowerCase()}'><b>#{foundLicense.title}</b></a>."
|
|
|
|
else
|
|
|
|
"The repository <b> #{repositoryFullName}</b> is already licensed."
|
|
|
|
|
2013-07-13 21:18:57 +02:00
|
|
|
$ ->
|
2013-07-15 22:19:31 +02:00
|
|
|
new Choosealicense()
|