Onionr/onionr/static-data/www/friends/friends.js

97 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2019-02-21 20:25:45 +00:00
/*
Onionr - P2P Anonymous Storage Network
This file handles the UI for managing friends/contacts
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
friendListDisplay = document.getElementById('friendList')
2019-02-22 21:04:03 +00:00
addForm = document.getElementById('addFriend')
function removeFriend(pubkey){
post_to_url('/friends/remove/' + pubkey, {'token': webpass})
}
2019-02-22 21:04:03 +00:00
addForm.onsubmit = function(){
var friend = document.getElementsByName('addKey')[0]
var alias = document.getElementsByName('data')[0]
fetch('/friends/add/' + friend.value, {
method: 'POST',
headers: {
"token": webpass
}}).then(function(data) {
if (alias.value.trim().length > 0){
2019-03-04 19:03:35 +00:00
post_to_url('/friends/setinfo/' + friend.value + '/name', {'data': alias.value, 'token': webpass})
2019-02-22 21:04:03 +00:00
}
})
return false
}
2019-02-21 20:25:45 +00:00
2019-02-22 01:55:13 +00:00
fetch('/friends/list', {
headers: {
"token": webpass
}})
.then((resp) => resp.json()) // Transform the data into json
.then(function(resp) {
var keys = [];
for(var k in resp) keys.push(k);
console.log(keys)
2019-02-22 21:04:03 +00:00
friendListDisplay.innerHTML = 'Click name to view info<br><br>'
2019-02-22 01:55:13 +00:00
for (var i = 0; i < keys.length; i++){
var peer = keys[i]
var name = resp[keys[i]]['name']
if (name === null || name === ''){
2019-02-22 21:04:03 +00:00
name = peer
2019-02-22 01:55:13 +00:00
}
var entry = document.createElement('div')
2019-02-22 21:04:03 +00:00
var nameText = document.createElement('input')
removeButton = document.createElement('button')
removeButton.classList.add('friendRemove')
removeButton.classList.add('dangerBtn')
entry.setAttribute('data-pubkey', peer)
removeButton.innerText = 'X'
nameText.value = name
nameText.readOnly = true
nameText.style.fontStyle = "italic"
2019-02-22 01:55:13 +00:00
entry.style.paddingTop = '8px'
2019-02-22 21:04:03 +00:00
entry.appendChild(removeButton)
entry.appendChild(nameText)
2019-02-22 01:55:13 +00:00
friendListDisplay.appendChild(entry)
entry.onclick = (function(entry, nameText, peer) {return function() {
2019-03-09 18:01:57 +00:00
if (nameText.length == 0){
nameText = 'Anonymous'
}
document.getElementById('friendPubkey').value = peer
document.getElementById('friendName').innerText = nameText
overlay('friendInfo')
2019-03-09 18:01:57 +00:00
};})(entry, nameText.value, peer);
2019-02-22 01:55:13 +00:00
}
// If friend delete buttons are pressed
var friendRemoveBtns = document.getElementsByClassName('friendRemove')
for (var x = 0; x < friendRemoveBtns.length; x++){
var friendKey = friendRemoveBtns[x].parentElement.getAttribute('data-pubkey')
friendRemoveBtns[x].onclick = function(){
removeFriend(friendKey)
}
}
2019-03-09 18:01:57 +00:00
})
document.getElementById('defriend').onclick = function(){
removeFriend(document.getElementById('friendPubkey').value)
}