Skip to content

[WIP] Add chooser functionality #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
</style>
</noscript>
</head>
<body>
<body onload="set_license()">
<header style="padding-bottom: 1%">
<br>
<img src="assets/cc-logo_white.png" style="width: 20%; margin-left: 5%">
</header>
<div class="container site-container" id="chooser-app">
<br>
<div class="container" id="education-content">
<div class="container" id="education-content" hidden>
<h1 class="title is-1">[Some Title]</h1>
<div class="columns">
<div class="column is-one-third" id="icons-explanation">
Expand Down Expand Up @@ -140,7 +140,7 @@ <h4 class="title is-4">
</label> <br>
</div>
</div>
</div>--><!--
</div>-->
<div class="column" id="chooser-controls-switch">
<p align="center">Changes to the options below will affect the license choice to the right.</p> <br><br>
<div id="license-chooser-allow-adaptations">
Expand All @@ -154,13 +154,15 @@ <h4 class="title is-4">
<div class="columns is-gapless">
<div class="field column is-one-fifth">
<label for="allow-adaptations-switch">No</label>
<input id="allow-adaptations-switch" type="checkbox" name="allow-adaptations-switch" class="switch is-thin" checked="checked">
<input id="allow-adaptations-switch" type="checkbox"
name="allow-adaptations-switch" class="switch is-thin"
checked="checked" onchange="switch_callback(this)">
<label for="allow-adaptations-switch">Yes</label>
</div>
<div class="field column is-pulled-left" style="display: inline;">
<p class="control is-pulled-left">
<div class="b-checkbox is-primary" style="display: inline;">
<input id="checkbox" class="styled" checked type="checkbox">
<div class="b-checkbox is-primary" id="sa-checkbox" style="display: inline;">
<input id="checkbox" class="styled" type="checkbox" onchange="sa_check_callback(this)">
<label for="checkbox">
SA Checkbox
</label>
Expand All @@ -180,12 +182,14 @@ <h4 class="title is-4">
<div class="control" style="margin-left: 9%; padding-top: 1%;">
<div class="field">
<label for="allow-commercial-switch">No</label>
<input id="allow-commercial-switch" type="checkbox" name="allow-commercial-switch" class="switch is-thin" checked="checked">
<input id="allow-commercial-switch" type="checkbox"
name="allow-commercial-switch" class="switch is-thin"
checked="checked" onchange="switch_callback(this)">
<label for="allow-commercial-switch">Yes</label>
</div>
</div>
</div> <br><br>
</div>-->
</div><!--
<div class="column" id="chooser-controls-images">
<p align="center">Changes to the options below will affect the license choice to the right.</p> <br><br>
<div id="license-chooser-allow-adaptations">
Expand Down Expand Up @@ -232,13 +236,17 @@ <h4 class="title is-4">
</div>
</div>
</div> <br><br>
</div>
<div class="column">
<h3 class="title is-3">Your License!</h3>
<p align="center">{{chooser.selected_license}}</p> <br><br>
<div id="license-chooser-icons" align="center">
<img src="assets/cc.svg" class="chooser-license-icon">
</div>
</div>-->
<div class="column" align="center">
<h3 class="title is-3">Your License!</h3> <br><br>
<a v-bind:href="chooser.selected_license_link" align="center">{{chooser.selected_license}}</a> <br>
<p>{{chooser.selected_license_short}}</p> <br>
<span id="license-chooser-icons">
<img src="assets/cc.svg" class="chooser-license-icon" id="cc-logo-icon">
<img src="assets/license-icons/cc-by_icon.svg" class="chooser-license-icon" id="attribution-icon">
<img src="assets/license-icons/cc-nc_icon.svg" class="chooser-license-icon" id="commercial-icon">
<img v-bind:src="'assets/license-icons/' + chooser.icons.nd_sa_src" class="chooser-license-icon" id="adaptations-icon">
</span>
</div>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ var app_state = new Vue({ // eslint-disable-line
},
chooser: {
selected_license: "[License Name]",
selected_license_short: "[License Short]",
selected_license_link: "",
icons: {
nc_shown: false,
sa_shown: false,
nd_shown: false,
nd_sa_src: "cc-sa_icon.svg",
},
inputs: {
share_alike: false,
allow_adaptations: true,
Expand Down
166 changes: 166 additions & 0 deletions src/scripts/choose-a-license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
function set_license() {
set_license_text()
set_license_icons()
}

function set_license_text() {
/*
var state = app_state.chooser
if (state.inputs.allow_adaptations == null &&
state.inputs.allow_commercial_uses == null) {
state.selected_license = "To Choose a License, Please Make Some Selections Using the Controls to the Left."
} else if (state.inputs.allow_adaptations == null &&
state.inputs.allow_commercial_uses != null) {
state.selected_license = "Please Select Whether or not to Allow Adaptations of Your Work."
}*/
app_state.chooser.selected_license = gen_license_name()
app_state.chooser.selected_license_short = gen_shortened_name()
set_license_link()
}

function set_license_icons() {
var state = app_state.chooser
if (state.inputs.allow_adaptations) {
state.icons.nd_shown = false
state.icons.sa_shown = false
toggle_license_icon("nd-sa", false)
if (state.inputs.share_alike) {
state.icons.sa_shown = true
state.icons.nd_shown = false
state.icons.nd_sa_src = "cc-sa_icon.svg"
toggle_license_icon("nd-sa", true)
}
}
else if (!state.inputs.allow_adaptations) {
state.icons.nd_shown = true
state.icons.sa_shown = false
state.icons.nd_sa_src = "cc-nd_icon.svg"
toggle_license_icon("nd-sa", true)
}

if (state.inputs.allow_commercial_uses) {
state.icons.nc_shown = false
toggle_license_icon("nc", false)
} else {
state.icons.nc_shown = true
toggle_license_icon("nc", true)
}
}

function gen_license_name() {
var state = app_state.chooser
var license_base = "Atribution"
if (!state.inputs.allow_commercial_uses) {
license_base += "-NonCommercial"
}
if (state.inputs.allow_adaptations) {
if (state.inputs.share_alike) {
license_base += "-ShareAlike"
}
}
else if (!state.inputs.allow_adaptations) {
license_base += "-NoDerivatives"
}
license_base += " 4.0 International"
app_state.chooser.selected_license = license_base
return license_base
}

/**
*
* @param {boolean} url_version If the shortened name need to be slugified for a URL
*/
function gen_shortened_name(url_version = false) {
const license = app_state.chooser.selected_license
var short = "CC BY"
if (license.includes("NonCommercial")) {
short += "-NC"
}
if (license.includes("NoDerivatives")) {
short += "-ND"
} else if(license.includes("ShareAlike")) {
short += "-SA"
}
return (url_version ? short.slice(3).toLowerCase() : short += " 4.0")
}

function set_license_link() {
const short_license = gen_shortened_name(true)
var url = "https://creativecommons.org/licenses/{0}/4.0".format(short_license)
app_state.chooser.selected_license_link = url
}

/**
*
* @param {object} check The HTML SA Checkbox object
*/
function sa_check_callback(check) {
console.log("Checkbox Toggled")
app_state.chooser.inputs.share_alike = check.checked
set_license()
}

/**
*
* @param {object} cb The HTML switch object
*/
function switch_callback(cb) {
console.log("Switch Toggled - " + cb.id)
var state = app_state.chooser.inputs
state.selected_license = ""
switch (cb.id) {
case "allow-adaptations-switch":
state.allow_adaptations = cb.checked
if (cb.checked) { // If allow adaptations
console.log("Is Allow Adaptations - " + cb.checked)
state.allow_adaptations = true
show_sa_check()
set_license()
}
else { // If NOT allow adaptations
state.allow_adaptations = false
hide_sa_check()
}
break;
case "allow-commercial-switch":
state.allow_commercial_uses = cb.checked
break;
default:
console.log("Whoops! This function isn't designed to handle that parameter.")
break;
}
set_license()
}

function toggle_license_icon(icon, is_show) {
switch (icon) {
case "nd-sa":
if (!is_show) document.getElementById("adaptations-icon").style.display = "none"
else document.getElementById("adaptations-icon").style.display = "inline"
break;
case "nc":
if (!is_show) document.getElementById("commercial-icon").style.display = "none"
else document.getElementById("commercial-icon").style.display = "inline"
break;
default:
console.log("Whoops! This function isn't designed to handle that parameter.")
break;
}
}

function hide_sa_check() {
console.log("SA Hidden")
document.getElementById("sa-checkbox").style.display = "none"
}

function show_sa_check() {
console.log("SA Shown")
var element = document.getElementById("sa-checkbox")
if (app_state.chooser.inputs.share_alike) {
element.checked = "true"
} else {
element.checked = "false"
}

element.style.display = "block"
}
7 changes: 7 additions & 0 deletions src/scripts/page-controls.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import set_license from "../scripts/choose-a-license.js"

function spawn_modal(modal_id) {
app_state.modals.modal_content = app_state.modals.modal_packs[modal_id].content // Update modal content
app_state.modals.modal_title = app_state.modals.modal_packs[modal_id].title // Update modal title
Expand All @@ -18,6 +20,11 @@ function destroy_modal() {
app_state.modals.modal_toggle = "modal"
}

function on_page_load() {
set_license()
document.getElementById('sa-checkbox').checked = app_state.chooser.inputs.share_alike
}

function copy_text_to_clipboard() {

}
7 changes: 7 additions & 0 deletions src/scripts/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
29 changes: 2 additions & 27 deletions src/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,15 @@
display: inline;
}

/* The next three classes were copied from w3schools */
/* Style the button that is used to open and close the collapsible content */
.collapsible {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active, .collapsible:hover {
background-color: #ccc;
}

/* Style the collapsible content. Note: hidden by default */
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #f1f1f1;
}

a.modal-icon {
vertical-align: middle;
display: inline;
}

img.chooser-license-icon {
width: 15%;
margin-right: 1.4%;
display: inline;
}

a.generator-copy-button {
Expand Down