Move all js to a source file

This commit is contained in:
Adrià Casajús 2022-02-25 14:58:38 +01:00
parent 3d498b4eae
commit 61d1655529
No known key found for this signature in database
GPG Key ID: F0033226A5AFC9B9
3 changed files with 35 additions and 67 deletions

View File

@ -0,0 +1,28 @@
const MAX_BYTES = 1240; // 10KiB
function enableDragDropForPGPKeys(inputID) {
function drop(event) {
event.stopPropagation();
event.preventDefault();
let files = event.dataTransfer.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
if(file.type !== 'text/plain'){
toastr.warning(`File ${file.name} is not a public key file`);
continue;
}
let reader = new FileReader();
reader.onloadend = onFileLoaded;
reader.readAsBinaryString(file);
}
}
function onFileLoaded(event) {
const initialData = event.currentTarget.result.substr(0, MAX_BYTES);
$(inputID).val(initialData);
}
const dropArea = $(inputID).get(0);
dropArea.addEventListener("drop", drop, false);
}

View File

@ -51,7 +51,7 @@
{% if not current_user.is_premium() %} disabled {% endif %}
class="form-control" rows=10 id="pgp-public-key"
placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----">{{ contact.pgp_public_key or "" }}</textarea>
<div class="text-body p-2">You can drag and drop the pgp key into the text area</div>
<div class="alert alert-info mt-2">You can drag and drop the pgp key file into the text area</div>
</div>
@ -73,36 +73,8 @@
{% endblock %}
{% block script %}
{% if current_user.is_premium() %}
<script src="/static/js/utils/drag-drop-into-text.js"></script>
<script>
const MAX_BYTES = 2048; // 2KiB
function drop(event) {
event.stopPropagation();
event.preventDefault();
let files = event.dataTransfer.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
console.log(file, file.name);
if(file.type !== 'text/plain'){
toastr.warning(`File ${file.name} is not a public key file`);
continue;
}
let reader = new FileReader();
reader.onloadend = onFileLoaded;
reader.readAsBinaryString(file);
}
}
function onFileLoaded(event) {
const initialData = event.currentTarget.result.substr(0, MAX_BYTES);
$('#pgp-public-key').val(initialData);
console.log($('#pgp-public-key'));
}
const dropArea = $("#pgp-public-key").get(0);
dropArea.addEventListener("drop", drop, false);
enableDragDropForPGPKeys('#pgp-public-key');
</script>
{% endif %}
{% endblock %}

View File

@ -125,7 +125,7 @@
{% if not current_user.is_premium() %} disabled {% endif %}
class="form-control" rows=10 id="pgp-public-key"
placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----">{{ mailbox.pgp_public_key or "" }}</textarea>
<div class="text-body p-2">You can drag and drop the pgp key into the text area</div>
<div class="alert alert-info mt-2">You can drag and drop the pgp key file into the text area</div>
</div>
<input type="hidden" name="form-name" value="pgp">
@ -264,44 +264,12 @@
{% endblock %}
{% block script %}
<oscript>
<script src="/static/js/utils/drag-drop-into-text.js"></script>
<script>
$(".custom-switch-input").change(function (e) {
$(this).closest("form").submit();
});
enableDragDropForPGPKeys('#pgp-public-key');
</script>
{% if current_user.is_premium() %}
<script>
const MAX_BYTES = 2048; // 2KiB
function drop(event) {
event.stopPropagation();
event.preventDefault();
let files = event.dataTransfer.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
console.log(file, file.name);
if(file.type !== 'text/plain'){
toastr.warning(`File ${file.name} is not a public key file`);
continue;
}
let reader = new FileReader();
reader.onloadend = onFileLoaded;
reader.readAsBinaryString(file);
}
}
function onFileLoaded(event) {
const initialData = event.currentTarget.result.substr(0, MAX_BYTES);
$('#pgp-public-key').val(initialData);
console.log($('#pgp-public-key'));
}
const dropArea = $("#pgp-public-key").get(0);
dropArea.addEventListener("drop", drop, false);
</script>
{% endif %}
{% endblock %}