| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="UTF-8"> |
| 5 |
<title>Kontroll.digital</title> |
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> |
| 7 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> |
| 8 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script> |
| 9 |
<script src="https://code.jquery.com/jquery-3.6.3.min.js"></script> |
| 10 |
<script> |
| 11 |
$(document).ready(function () { |
| 12 |
|
| 13 |
// Text format functions |
| 14 |
|
| 15 |
$('.size_in_bytes').each(function(i, obj) { |
| 16 |
$(this).text(formatBytes($(this).text())); |
| 17 |
console.log($(this).text()); |
| 18 |
}); |
| 19 |
|
| 20 |
$('.date_in_iso').each(function(i, obj) { |
| 21 |
$(this).text(formatDate($(this).text())); |
| 22 |
console.log($(this).text()); |
| 23 |
}); |
| 24 |
|
| 25 |
// Event functions |
| 26 |
|
| 27 |
{% for project in projects %} |
| 28 |
|
| 29 |
$('#button_{{project.project_id}}').on('click', function (event) { |
| 30 |
let form_data = new FormData(); |
| 31 |
let files = $('#file_{{project.project_id}}')[0].files; |
| 32 |
if(files.length > 0) { |
| 33 |
form_data.append('file', files[0]); |
| 34 |
form_data.append('project', '{{project.project_id}}'); |
| 35 |
form_data.append('selection_session', $('#selection_session').val()); |
| 36 |
$.ajax({ |
| 37 |
url: './upload_file_to_project', |
| 38 |
type: 'POST', |
| 39 |
data: form_data, |
| 40 |
dataType: 'json', |
| 41 |
contentType: false, |
| 42 |
processData: false, |
| 43 |
success:function (response) { |
| 44 |
if (response.document_id) { |
| 45 |
$('#span_{{project.project_id}}').text(response.document_id); |
| 46 |
} else { |
| 47 |
$('#span_{{project.project_id}}').text('Not uploaded'); |
| 48 |
} |
| 49 |
} |
| 50 |
}); |
| 51 |
} else { |
| 52 |
$('#span_{{project.project_id}}').text('Please select a file.') |
| 53 |
} |
| 54 |
event.preventDefault(); |
| 55 |
}); |
| 56 |
|
| 57 |
{% endfor %} |
| 58 |
|
| 59 |
$('#select_documents_button').on('click', function (event) { |
| 60 |
select_documents(); |
| 61 |
}) |
| 62 |
|
| 63 |
}); |
| 64 |
|
| 65 |
// Download functions |
| 66 |
|
| 67 |
function select_documents () { |
| 68 |
event.preventDefault(); |
| 69 |
let form_values = $('#select_documents_form').serialize(); |
| 70 |
console.log('Form values: ' + form_values); |
| 71 |
$.ajax ({ |
| 72 |
type: 'POST', |
| 73 |
url: './mark-documents-as-selected', |
| 74 |
data: form_values, |
| 75 |
dataType: 'json', |
| 76 |
success: function (msg) { |
| 77 |
if (msg) { |
| 78 |
console.log('Result of mark-some-documents-as-selected: ' + msg); |
| 79 |
callback(msg) |
| 80 |
} |
| 81 |
}, |
| 82 |
error: function () { |
| 83 |
let errors = ['An unknown error occured.']; |
| 84 |
console.log(errors); |
| 85 |
} |
| 86 |
}); |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
function callback (msg) { |
| 91 |
let callback_url = $('#callback_url').val(); |
| 92 |
let session_id = $('#selection_session').val(); |
| 93 |
let server_context = $('#server_context').val(); |
| 94 |
let selected_documents_url = 'https://api.kontroll.digital/documents/1.0/download-instructions' |
| 95 |
+ '?session_id=' + session_id |
| 96 |
+ '&server_context=' + server_context |
| 97 |
|
| 98 |
callback_url = callback_url + '?selected_documents_url=' + encodeURIComponent(selected_documents_url) |
| 99 |
|
| 100 |
console.log('Selected documents url: ' + selected_documents_url); |
| 101 |
console.log('Callback url: ' + callback_url); |
| 102 |
|
| 103 |
$.get ({ |
| 104 |
url: callback_url, |
| 105 |
crossDomain: true, |
| 106 |
dataType: 'html', |
| 107 |
success: function (msg) { |
| 108 |
if (msg) { |
| 109 |
console.log('Result text: ' + msg); |
| 110 |
$('#result_message').text('You may now close this window.'); |
| 111 |
} |
| 112 |
}, |
| 113 |
error: function () { |
| 114 |
let errors = ['An unknown error occured.']; |
| 115 |
console.log(errors); |
| 116 |
} |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
// Helper functions |
| 121 |
|
| 122 |
function formatBytes(bytes, decimals = 2) { |
| 123 |
bytes = parseInt(bytes) |
| 124 |
const k = 1024 |
| 125 |
if (!+bytes) return '0 Bytes' |
| 126 |
const dm = decimals < 0 ? 0 : decimals |
| 127 |
const sizes = ['Bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'] |
| 128 |
const i = Math.floor(Math.log(bytes) / Math.log(k)) |
| 129 |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}` |
| 130 |
} |
| 131 |
|
| 132 |
function formatDate(iso_date) { |
| 133 |
let d = new Date(iso_date) |
| 134 |
let curr_date = d.getDate(); |
| 135 |
let curr_month = d.getMonth() + 1; |
| 136 |
let curr_year = d.getFullYear(); |
| 137 |
return curr_year + "-" + curr_month + "-" + curr_date |
| 138 |
} |
| 139 |
|
| 140 |
</script> |
| 141 |
<style> |
| 142 |
body { |
| 143 |
align-content: center; |
| 144 |
justify-content: center; |
| 145 |
padding-top: 40px; |
| 146 |
padding-bottom: 40px; |
| 147 |
background-color: #f5f5f5; |
| 148 |
} |
| 149 |
.page_layout { |
| 150 |
width: 100%; |
| 151 |
max-width: 780px; |
| 152 |
padding: 15px; |
| 153 |
margin: 0 auto; |
| 154 |
} |
| 155 |
</style> |
| 156 |
</head> |
| 157 |
<body> |
| 158 |
<div id="page_layout" class="page_layout"> |
| 159 |
<form action="" id="select_documents_form" name="select_documents_form"> |
| 160 |
<h1 class="h3 mb-3">kontroll.digital</h1> |
| 161 |
<div id="select_documents_div"> |
| 162 |
|
| 163 |
<table class="table"> |
| 164 |
<thead> |
| 165 |
<tr> |
| 166 |
<th scope="col"></th> |
| 167 |
<th scope="col">ID</th> |
| 168 |
<th scope="col">Title</th> |
| 169 |
<th scope="col">Version</th> |
| 170 |
<th scope="col">File type</th> |
| 171 |
<th scope="col">Size</th> |
| 172 |
<th scope="col">Created</th> |
| 173 |
</tr> |
| 174 |
</thead> |
| 175 |
<tbody> |
| 176 |
|
| 177 |
{% for project in projects %} |
| 178 |
|
| 179 |
<tr> |
| 180 |
<td colspan="7" class="project_upload"> |
| 181 |
|
| 182 |
<h2>{{project.name}}</h2> |
| 183 |
|
| 184 |
<span> |
| 185 |
<input type="file" id="file_{{project.project_id}}" name="file_{{project.project_id}}" /> |
| 186 |
<input type="button" id="button_{{project.project_id}}" name="button_{{project.project_id}}" value="Upload"> |
| 187 |
</span> |
| 188 |
|
| 189 |
<span id="span_{{project.project_id}}"></span> |
| 190 |
|
| 191 |
</td> |
| 192 |
<tr> |
| 193 |
|
| 194 |
{% for document in project.documents %} |
| 195 |
|
| 196 |
<tr> |
| 197 |
<td><input type="checkbox" id="document_{{document.document_id}}" name="document_{{document.document_id}}"></td> |
| 198 |
<td><span>{{document.document_id}}</span></td> |
| 199 |
<td><span>{{document.title}}</span></td> |
| 200 |
<td><span>{{document.version_number}}</span></td> |
| 201 |
<td><span>{{document.file_type}}</span></td> |
| 202 |
<td><span class="size_in_bytes">{{document.file_description.size_in_bytes}}</span></td> |
| 203 |
<td><span>{{document.creation_date}}</span></td> |
| 204 |
<tr> |
| 205 |
|
| 206 |
{% endfor %} |
| 207 |
|
| 208 |
{% endfor %} |
| 209 |
|
| 210 |
</tbody> |
| 211 |
</table> |
| 212 |
|
| 213 |
<input type="hidden" id="username" name="username" value="{{current_user}}"> |
| 214 |
<input type="hidden" id="selection_session" name="selection_session" value="{{selection_session}}"> |
| 215 |
<input type="hidden" id="server_context" name="server_context" value="{{server_context}}"> |
| 216 |
<input type="hidden" id="callback_url" name="callback_url" value="{{callback_url}}"> |
| 217 |
<input type="hidden" id="callback_expires_in" name="callback_expires_in" value="{{callback_expires_in}}"> |
| 218 |
|
| 219 |
<button class="btn btn-lg btn-primary btn-block" id="select_documents_button" type="button">Select documents</button> |
| 220 |
|
| 221 |
</div> |
| 222 |
</form> |
| 223 |
</div> |
| 224 |
</body> |
| 225 |
</html> |