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

Merge pull request #610 from felubra/suggest-license

feat: suggest the license to a repository from the license page
This commit is contained in:
Mike Linksvayer 2018-10-21 14:39:36 -07:00 committed by GitHub
commit 8d579321cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 169 additions and 2 deletions

View File

@ -21,6 +21,16 @@
<script src="/assets/vendor/clipboard/dist/clipboard.min.js"></script>
<script>
window.annotations = {{ site.data.rules | jsonify }};
{% if page.collection == "licenses" %}
window.licenses = [
{% for license in site.licenses %}
{
"title": "{{ license.title | escape }}",
"spdx_id": "{{ license.spdx-id | escape }}"
}{% unless forloop.last %},{% endunless %}
{% endfor %}
];
{% endif %}
</script>
<script src="/assets/js/app.js"></script>
{% endif %}

View File

@ -1,6 +1,14 @@
<div class="sidebar">
<a href="#" data-clipboard-target="#license-text" data-proofer-ignore="true" class="js-clipboard-button button">Copy license text to clipboard</a>
<h3 id="suggest-this-license">Suggest this license</h3>
<div class="repository-suggestion">
<p>Make a pull request to suggest this license fo a project that is <a href="/no-permission/">not licensed</a>. Please be polite: see if a license has already been suggested, try to suggest a license fitting for the project's <a href="/community/">community</a>, and keep your communicaton with project maintainers friendly.</p>
<div class="input-wrapper">
<input type="text" data-license-id="{{ page.spdx-id }}" placeholder="Enter GitHub repository URL" id="repository-url" title="status" />
<div class="status-indicator"></div>
</div>
</div>
<div class="how-to-apply">
<h3>How to apply this license</h3>

View File

@ -294,6 +294,43 @@ strong {
width: 100%;
}
.sidebar input#repository-url {
width: 100%;
padding: 5px 20px 5px 10px;
box-sizing: border-box;
margin-right: 12px;
}
.input-wrapper {
position: relative;
}
.input-wrapper .status-indicator {
position: absolute;
right: 5px;
top: 6px;
height: 8px;
width: 8px;
border-radius: 50%;
background: #fff;
animation: none;
border: 4px solid #ddd;
}
.input-wrapper .status-indicator.fetching {
border-right-color: transparent;
animation: rotate 0.8s infinite linear;
}
.input-wrapper .status-indicator.error {
border: 4px solid #c6403d;
}
@keyframes rotate {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.note {
color: #687072;
}
@ -386,7 +423,9 @@ strong {
.qtip-conditions,
.qtip-permissions,
.qtip-limitations {
.qtip-limitations,
.qtip-fetching,
.qtip-error {
font-size: 12px;
line-height: 1.3;
}
@ -402,23 +441,27 @@ strong {
color: #fff;
}
.qtip-fetching,
.qtip-permissions {
background-color: #d8f4d7;
border-color: #3dc637;
color: #298625;
}
.qtip-fetching .qtip-titlebar,
.qtip-permissions .qtip-titlebar {
background-color: #3dc637;
color: #fff;
}
.qtip-error,
.qtip-limitations {
background-color: #f4d9d8;
border-color: #c6403d;
color: #812a28;
}
.qtip-error .qtip-titlebar,
.qtip-limitations .qtip-titlebar {
background-color: #c6403d;
color: #fff;

View File

@ -31,6 +31,7 @@ class Choosealicense
constructor: ->
@initTooltips()
@initClipboard()
@initLicenseSuggestion()
# Init tooltip action
initTooltips: ->
@ -70,6 +71,111 @@ class Choosealicense
# Post-copy user feedback callback
clipboardComplete: (client, args) ->
@textContent = "Copied!"
# Initializes the repository suggestion feature
initLicenseSuggestion: ->
inputEl = $("#repository-url")
licenseId = inputEl.attr("data-license-id")
statusIndicator = $(".status-indicator")
new LicenseSuggestion(inputEl, licenseId, statusIndicator)
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 not licensed
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}"
@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."
$ ->
new Choosealicense()

View File

@ -22,7 +22,7 @@ If you find software that doesn't have a license, that generally means you have
Your options:
**Ask the maintainers nicely to add a license.** Unless the software includes strong indications to the contrary, lack of a license is probably an oversight. If the software is hosted on a site like GitHub, open an issue requesting a license and include a link to this site, or if you're bold and it's fairly obvious what license is most appropriate, open a pull request to add a license.
**Ask the maintainers nicely to add a license.** Unless the software includes strong indications to the contrary, lack of a license is probably an oversight. If the software is hosted on a site like GitHub, open an issue requesting a license and include a link to this site. If you're bold and it's fairly obvious what license is most appropriate, open a pull request to add a license see "suggest this license" in the sidebar of the page for each license on this site (e.g., [MIT](/licenses/mit/#suggest-this-license)).
**Don't use the software.** Find or create an alternative that is under an open source license.