Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Avoid multiple deletion of Pairing Keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-martynovich committed Aug 27, 2019
1 parent ef81845 commit 476db1d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
2 changes: 1 addition & 1 deletion backend/backend/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ require('exports-loader?window.select23!../../staticfiles/tagulous/adaptor/selec
// Copy to clipboard
import * as clipboard from "clipboard-polyfill"
window.copyToClipboard = function(text) {
return clipboard.writeText(text).then(console.log, console.error);
return clipboard.writeText(text);
}
2 changes: 1 addition & 1 deletion backend/device_registry/templates/credentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ <h6 class="card-subtitle text-muted">View credentials.</h6>
});


$('#confirm-btn').on('click', (e) => {
$('#confirm-btn').click(e => {
console.log('del');

$.ajax({
Expand Down
49 changes: 15 additions & 34 deletions backend/device_registry/templates/pairing_keys.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,16 @@ <h6 class="card-subtitle text-muted">List of keys not yet applied.</h6>
{ title:"Revoke the enrollment key"} )
}

function copy_key(a){
copyToClipboard(
"enroll_token = " + a
);
}

var deleted_pk;
function revoke_key(a){
wott_alert_hide("main-alert");
$('#confirm-btn').on("click", {key: a.parentElement.parentElement.parentElement.id}, delete_key );
deleted_pk = a.parentElement.parentElement.parentElement.id;
show_dialog("Confirm Revoke Key",
"Are you sure you want to revoke?",
"Revoke"
Expand All @@ -247,35 +254,30 @@ <h6 class="card-subtitle text-muted">List of keys not yet applied.</h6>

function copy_key(btn, key){
let key_text = "enroll_token = " + key;
navigator.clipboard.writeText(key_text).then(function() {
show_popover($(btn), 'Copied', key_text);
}, function() {
let successful = copyToClipboard(key_text);
let title = successful ? 'Copied' : 'Could not copy to clipboard. Check the browser permissions.';
let msg = successful ? key_text : '';
show_popover($(btn), title, msg);
copyToClipboard(key_text).then(() => {
show_popover($(btn), 'Copied', key_text);
}, () => {
show_popover($(btn), 'Could not copy to clipboard. Check the browser permissions.', '');
});
}

/***
* on confirm delete
* @param e - e.data.key <- key to delete
*/
function delete_key(e)
{
$('#confirm-btn').click(e => {
hide_dialog();
$.ajax({
url: `/ajax-pairing-keys/${e.data.key}/delete/`,
url: `/ajax-pairing-keys/${deleted_pk}/delete/`,
type: "DELETE",
}).done(function () {
console.log('success');
table.ajax.reload();
}).fail(function (a) {
console.log('error');
console.log(a.status, a.responseText);
alertbox_on_ajax_fail(a, "confirm-alert");
});
}
});

/**
* id - key/row_id (they are the same)
Expand Down Expand Up @@ -369,27 +371,6 @@ <h6 class="card-subtitle text-muted">List of keys not yet applied.</h6>
wott_alert_strings(errMsgs, alert_box_id);
}

const copyToClipboard = str => {
const el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
const selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously
? document.getSelection().getRangeAt(0) // Store selection if found
: false; // Mark as false to know no selection existed before
el.select(); // Select the <textarea> content
let res = document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); // Remove the <textarea> element
if (selected) { // If a selection existed before copying
document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
document.getSelection().addRange(selected); // Restore the original selection
}
return res;
};

function htmlEntities(str) {
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
Expand Down

0 comments on commit 476db1d

Please sign in to comment.