-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathchoose-a-license.js
166 lines (154 loc) · 5.06 KB
/
choose-a-license.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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"
}