2014-07-31 12:36:56 -04:00
|
|
|
---
|
|
|
|
---
|
|
|
|
|
2013-07-13 15:18:57 -04:00
|
|
|
class Choosealicense
|
2013-07-15 17:19:31 -03:00
|
|
|
# Selects the content of a given element
|
|
|
|
selectText: (element) ->
|
|
|
|
if document.body.createTextRange
|
2013-11-29 18:08:27 -05:00
|
|
|
range = document.body.createTextRange()
|
|
|
|
range.moveToElementText(element)
|
|
|
|
range.select()
|
2013-07-15 17:19:31 -03:00
|
|
|
else if window.getSelection
|
|
|
|
selection = window.getSelection()
|
|
|
|
range = document.createRange()
|
|
|
|
|
|
|
|
range.selectNodeContents(element)
|
2013-11-29 18:08:27 -05:00
|
|
|
selection.removeAllRanges()
|
|
|
|
selection.addRange(range)
|
2013-07-15 17:19:31 -03:00
|
|
|
|
2023-08-27 18:17:13 +05:00
|
|
|
tooltipAttributesMapperByRuleType:
|
|
|
|
permissions:
|
2023-08-31 12:38:53 +05:00
|
|
|
heading: 'Permission'
|
2023-08-31 13:06:26 +05:00
|
|
|
color: 'tooltip--permissions'
|
2023-08-27 18:17:13 +05:00
|
|
|
conditions:
|
2023-08-31 12:38:53 +05:00
|
|
|
heading: 'Condition'
|
2023-08-31 13:06:26 +05:00
|
|
|
color: 'tooltip--conditions'
|
2023-08-27 18:17:13 +05:00
|
|
|
limitations:
|
2023-08-31 12:38:53 +05:00
|
|
|
heading: 'Limitation'
|
2023-08-31 13:06:26 +05:00
|
|
|
color: 'tooltip--limitations'
|
2023-08-27 18:17:13 +05:00
|
|
|
|
2013-07-13 15:18:57 -04:00
|
|
|
|
|
|
|
# fire on document.ready
|
|
|
|
constructor: ->
|
|
|
|
@initTooltips()
|
2013-07-15 17:19:31 -03:00
|
|
|
@initClipboard()
|
2018-10-14 20:21:54 -03:00
|
|
|
@initLicenseSuggestion()
|
2013-07-13 15:18:57 -04:00
|
|
|
|
|
|
|
# Init tooltip action
|
|
|
|
initTooltips: ->
|
|
|
|
|
|
|
|
# Dynamically add annotations as title attribute to rule list items
|
2015-09-09 00:24:05 +01:00
|
|
|
for ruletype, rules of window.annotations
|
2014-09-05 11:26:54 -04:00
|
|
|
for rule in rules
|
2024-11-25 22:52:01 +00:00
|
|
|
# Exclude license elements in the legend
|
|
|
|
licenseLiElement = $(".license-#{ruletype} .#{rule["tag"]}").not("dd.license-#{ruletype} .#{rule["tag"]}")
|
2023-08-27 18:17:13 +05:00
|
|
|
tooltipAttr = @tooltipAttributesMapperByRuleType[ruletype]
|
2024-11-25 22:52:13 +00:00
|
|
|
licenseLiElement.attr "aria-label", "#{rule.label} #{tooltipAttr.heading.toLowerCase()}: #{rule.description}"
|
2023-08-27 18:17:13 +05:00
|
|
|
licenseLiElement.addClass("hint--bottom
|
|
|
|
hint--large
|
|
|
|
hint--no-animate
|
|
|
|
#{tooltipAttr.color}
|
2024-11-25 21:04:41 +00:00
|
|
|
override-hint-inline")
|
2013-07-13 15:18:57 -04:00
|
|
|
|
2015-10-25 15:17:13 +10:30
|
|
|
# Initializes Clipboard.js
|
2015-09-19 22:46:18 -04:00
|
|
|
initClipboard: ->
|
2013-11-29 18:08:27 -05:00
|
|
|
# Backup the clipboard button's original text.
|
|
|
|
$(".js-clipboard-button").data "clipboard-prompt", $(".js-clipboard-button").text()
|
2013-07-15 17:19:31 -03:00
|
|
|
|
2013-11-29 18:08:27 -05:00
|
|
|
# Hook up copy to clipboard buttons
|
2015-10-25 15:17:13 +10:30
|
|
|
clip = new Clipboard ".js-clipboard-button"
|
2013-11-29 18:08:27 -05:00
|
|
|
clip.on "mouseout", @clipboardMouseout
|
|
|
|
clip.on "complete", @clipboardComplete
|
2013-07-15 17:19:31 -03:00
|
|
|
|
2013-07-13 15:18:57 -04:00
|
|
|
# Callback to restore the clipboard button's original text
|
|
|
|
clipboardMouseout: (client, args) ->
|
2014-12-18 11:53:45 -08:00
|
|
|
@textContent = $(this).data("clipboard-prompt")
|
2013-07-13 15:18:57 -04:00
|
|
|
|
|
|
|
# Post-copy user feedback callback
|
|
|
|
clipboardComplete: (client, args) ->
|
2014-12-18 11:53:45 -08:00
|
|
|
@textContent = "Copied!"
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2018-10-14 20:21:54 -03: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 15:18:57 -04:00
|
|
|
|
2018-10-14 20:21:54 -03:00
|
|
|
class LicenseSuggestion
|
|
|
|
constructor: (@inputEl, @licenseId, @statusIndicator) ->
|
|
|
|
@bindEventHandlers()
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2023-08-27 22:11:52 +05:00
|
|
|
inputWraper: $('.input-wrapper')
|
2023-08-31 16:26:39 +05:00
|
|
|
tooltipErrorClasses: 'hint--bottom tooltip--error hint--always'
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2018-10-14 20:21:54 -03:00
|
|
|
# Main event handlers for user input
|
2019-12-25 10:58:01 -08:00
|
|
|
bindEventHandlers: =>
|
2018-10-14 20:21:54 -03:00
|
|
|
@inputEl.on "input", (event) =>
|
|
|
|
@setStatus ""
|
2019-12-25 10:58:01 -08:00
|
|
|
.on "keyup", (event) =>
|
2018-10-14 20:21:54 -03:00
|
|
|
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
|
2019-12-25 10:58:01 -08:00
|
|
|
|
|
|
|
@setStatus "Fetching"
|
2018-10-14 20:21:54 -03:00
|
|
|
@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
|
2018-10-19 08:23:57 +02:00
|
|
|
else # The repository is not licensed
|
2018-10-15 09:57:14 -03:00
|
|
|
licenseUrl = encodeURIComponent "https://github.com/#{repositoryFullName}/community/license/new?template=#{@licenseId}"
|
|
|
|
# Provide the chance to the user log-in, since the URL to suggest a license is restricted
|
|
|
|
window.location.href = "https://github.com/login?return_to=#{licenseUrl}"
|
2018-10-14 20:21:54 -03:00
|
|
|
@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
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2018-10-14 20:21:54 -03:00
|
|
|
# Displays an indicator and tooltips to the user about the current status
|
|
|
|
setStatus: (status="", message="") =>
|
|
|
|
statusClass = status.toLowerCase()
|
2023-08-27 22:11:52 +05:00
|
|
|
displayTooltip = (status, message) =>
|
|
|
|
@inputWraper.attr('aria-label', "#{status}: #{message}")
|
|
|
|
@inputWraper.addClass(@tooltipErrorClasses)
|
2018-10-14 20:21:54 -03:00
|
|
|
|
|
|
|
switch status
|
|
|
|
when "Fetching"
|
2023-08-27 22:11:52 +05:00
|
|
|
@statusIndicator.removeClass("error #{@tooltipErrorClasses}").addClass(statusClass)
|
2018-10-14 20:21:54 -03:00
|
|
|
when "Error"
|
|
|
|
@statusIndicator.removeClass('fetching').addClass(statusClass)
|
2023-08-27 22:11:52 +05:00
|
|
|
displayTooltip status, message
|
2018-10-14 20:21:54 -03:00
|
|
|
else
|
|
|
|
@statusIndicator.removeClass('fetching error')
|
2023-08-27 22:11:52 +05:00
|
|
|
@inputWraper.removeClass(@tooltipErrorClasses)
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2018-10-14 20:21:54 -03:00
|
|
|
# Fetches information about a repository from the Github API
|
|
|
|
fetchInfoFromGithubAPI: (repositoryFullName, callback) ->
|
|
|
|
$.getJSON "https://api.github.com/repos/"+repositoryFullName, (info) ->
|
|
|
|
callback null, info
|
2019-12-25 10:58:01 -08:00
|
|
|
.fail (e) ->
|
2018-10-14 20:21:54 -03:00
|
|
|
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>."
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2018-10-14 20:21:54 -03:00
|
|
|
# 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
|
2023-08-27 22:11:52 +05:00
|
|
|
"The repository #{repositoryFullName} is already licensed under the #{foundLicense.title}."
|
2018-10-14 20:21:54 -03:00
|
|
|
else
|
2023-08-27 22:11:52 +05:00
|
|
|
"The repository #{repositoryFullName} is already licensed."
|
2019-12-25 10:58:01 -08:00
|
|
|
|
2013-07-13 15:18:57 -04:00
|
|
|
$ ->
|
2013-07-15 17:19:31 -03:00
|
|
|
new Choosealicense()
|