Onionr/onionr/static-data/www/ui/dist/js/timeline.js

245 lines
7.9 KiB
JavaScript
Raw Normal View History

2018-08-05 04:09:33 +00:00
/* just for testing rn */
Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, function(data) {
for(var i = 0; i < data.length; i++) {
try {
var block = data[i];
2018-09-02 22:44:23 +00:00
var finished = false;
User.getUser(new String(block.getHeader('signer', 'unknown')), function(user) {
2018-09-02 22:44:23 +00:00
var post = new Post();
var blockContent = JSON.parse(block.getContent());
2018-09-08 05:47:56 +00:00
// just ignore anything shorter than 280 characters
if(String(blockContent['content']).length <= 280) {
post.setContent(blockContent['content']);
post.setPostDate(block.getDate());
post.setUser(user);
2018-08-05 04:09:33 +00:00
2018-09-08 05:47:56 +00:00
post.setHash(block.getHash());
document.getElementById('onionr-timeline-posts').innerHTML += post.getHTML();
}
2018-08-05 04:09:33 +00:00
2018-09-02 22:44:23 +00:00
finished = true;
});
2018-08-05 04:09:33 +00:00
2018-09-02 22:44:23 +00:00
while(!finished);
2018-08-05 04:09:33 +00:00
} catch(e) {
console.log('Troublemaker block: ' + data[i].getHash());
2018-08-05 04:09:33 +00:00
console.log(e);
}
}
});
2018-08-04 02:52:45 +00:00
2018-09-08 06:45:33 +00:00
function toggleLike(hash) {
var post = getPostMap(hash);
if(post === null || !getPostMap()[hash]['liked']) {
console.log('Liking ' + hash + '...');
if(post === null)
getPostMap()[hash] = {};
getPostMap()[hash]['liked'] = true;
set('postmap', JSON.stringify(getPostMap()));
var block = new Block();
block.setType('onionr-post-like');
block.setContent(JSON.stringify({'hash' : hash}));
block.save(true, function(hash) {});
} else {
console.log('Unliking ' + hash + '...');
}
}
2018-09-08 05:47:56 +00:00
function postCreatorChange() {
var content = document.getElementById('onionr-post-creator-content').value;
var message = '';
2018-09-13 05:30:49 +00:00
var maxlength = 280;
2018-09-08 05:47:56 +00:00
var disable = true;
2018-09-13 05:30:49 +00:00
var warn = false;
2018-09-08 05:47:56 +00:00
if(content.length !== 0) {
if(content.length - content.replaceAll('\n', '').length > 16) {
// 16 max newlines
message = 'Please use less than 16 newlines';
2018-09-13 05:30:49 +00:00
} else if(content.length <= maxlength) {
2018-09-08 05:47:56 +00:00
// 280 max characters
message = '%s characters remaining'.replaceAll('%s', (280 - content.length));
disable = false;
2018-09-13 05:30:49 +00:00
if(maxlength - content.length < maxlength / 4) {
warn = true;
}
2018-09-08 05:47:56 +00:00
} else {
2018-09-13 05:30:49 +00:00
message = '%s characters over maximum'.replaceAll('%s', (content.length - maxlength));
2018-09-08 05:47:56 +00:00
}
}
var element = document.getElementById('onionr-post-creator-content-message');
var button = document.getElementById("onionr-post-creator-create");
if(message === '')
2018-09-08 06:45:33 +00:00
element.style.visibility = 'hidden';
2018-09-08 05:47:56 +00:00
else {
2018-09-08 06:45:33 +00:00
element.style.visibility = 'visible';
2018-09-08 05:47:56 +00:00
element.innerHTML = message;
if(disable)
element.style.color = 'red';
2018-09-13 05:30:49 +00:00
else if(warn)
element.style.color = '#FF8C00';
2018-09-08 05:47:56 +00:00
else
element.style.color = 'gray';
}
if(disable)
button.disabled = true;
else
button.disabled = false;
}
2018-07-30 00:43:28 +00:00
function viewProfile(id, name) {
2018-09-02 22:44:23 +00:00
id = decodeURIComponent(id);
2018-08-05 04:16:42 +00:00
document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(decodeURIComponent(name));
2018-09-02 22:44:23 +00:00
User.getUser(id, function(data) {
2018-09-02 22:44:23 +00:00
if(data !== null) {
document.getElementById("onionr-profile-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(data.getIcon());
document.getElementById("onionr-profile-user-icon").b64 = Sanitize.html(data.getIcon());
2018-09-13 05:30:49 +00:00
document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(Sanitize.username(data.getName()));
document.getElementById("onionr-profile-username").title = Sanitize.html(data.getID());
document.getElementById("onionr-profile-description").innerHTML = Sanitize.html(Sanitize.description(data.getDescription()));
2018-09-02 22:44:23 +00:00
}
});
2018-07-30 00:43:28 +00:00
}
function updateUser() {
toggleSaveButton(false);
2018-09-13 05:30:49 +00:00
// jQuery('#modal').modal('show');
var name = jQuery('#onionr-profile-username').text();
var id = document.getElementById("onionr-profile-username").title;
var icon = document.getElementById("onionr-profile-user-icon").b64;
2018-09-13 05:30:49 +00:00
var description = jQuery("#onionr-profile-description").text();
var user = new User();
user.setName(name);
user.setID(id);
user.setIcon(icon);
2018-09-13 05:30:49 +00:00
user.setDescription(Sanitize.description(description));
user.remember();
2018-09-13 05:30:49 +00:00
user.save(function() {
setCurrentUser(user);
2018-09-13 05:30:49 +00:00
window.location.reload();
});
}
function cancelUpdate() {
toggleSaveButton(false);
var name = jQuery('#onionr-profile-username').text();
var id = document.getElementById("onionr-profile-username").title;
viewProfile(id, name);
}
function toggleSaveButton(show) {
2018-09-13 05:30:49 +00:00
document.getElementById("onionr-profile-edit").style.display = (show ? 'block' : 'none');
}
function makePost() {
var content = document.getElementById("onionr-post-creator-content").value;
2018-09-07 04:37:39 +00:00
if(content.trim() !== '') {
var post = new Post();
2018-09-07 04:37:39 +00:00
post.setUser(getCurrentUser());
post.setContent(content);
post.setPostDate(new Date());
2018-09-07 04:37:39 +00:00
post.save(function(data) {}); // async, but no function
2018-09-07 04:37:39 +00:00
document.getElementById('onionr-timeline-posts').innerHTML = post.getHTML() + document.getElementById('onionr-timeline-posts').innerHTML;
2018-09-07 04:37:39 +00:00
document.getElementById("onionr-post-creator-content").value = "";
2018-09-13 05:30:49 +00:00
document.getElementById("onionr-post-creator-content").focus();
2018-09-08 06:45:33 +00:00
postCreatorChange();
2018-09-07 04:37:39 +00:00
} else {
console.log('Not making empty post.');
}
}
2018-09-27 00:40:02 +00:00
function focusPost(id) {
document.getElementById("onionr-post-focus-content").innerHTML = "";
jQuery('#onionr-post-focus').modal('show');
}
2018-09-13 05:30:49 +00:00
jQuery('body').on('click', '[data-editable]', function() {
var el = jQuery(this);
var txt = el.text();
2018-09-13 05:30:49 +00:00
var maxlength = el.attr("maxlength");
var input = jQuery('<input/>').val(txt);
2018-09-13 05:30:49 +00:00
input.attr('maxlength', maxlength);
el.replaceWith(input);
var save = function() {
var newTxt = input.val();
2018-09-13 05:30:49 +00:00
if(el.attr('id') === 'onionr-profile-username')
newTxt = Sanitize.username(newTxt);
if(el.attr('id') === 'onionr-profile-description')
newTxt = Sanitize.description(newTxt);
var p = el.text(newTxt);
input.replaceWith(p);
if(newTxt !== txt)
toggleSaveButton(true);
};
2018-09-13 05:30:49 +00:00
var saveEnter = function(event) {
console.log(event);
console.log(event.keyCode);
if (event.keyCode === 13)
save();
};
input.one('blur', save).bind('keyup', saveEnter).focus();
});
2018-09-27 00:40:02 +00:00
//viewProfile('$user-id-url', '$user-name-url')
jQuery('#onionr-post-user-id').on('click', function(e) { alert(3);});
//jQuery('#onionr-post *').on('click', function(e) { e.stopPropagation(); });
jQuery('#onionr-post').click(function(e) { alert(1); });
2018-09-08 06:45:33 +00:00
currentUser = getCurrentUser();
2018-09-13 05:30:49 +00:00
if(currentUser !== undefined && currentUser !== null) {
document.getElementById("onionr-post-creator-user-name").innerHTML = Sanitize.html(currentUser.getName());
document.getElementById("onionr-post-creator-user-id").innerHTML = "you";
document.getElementById("onionr-post-creator-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(currentUser.getIcon());
document.getElementById("onionr-post-creator-user-id").title = currentUser.getID();
document.getElementById("onionr-post-creator-content").placeholder = "Enter a message here...";
}
viewCurrentProfile = function() {
viewProfile(encodeURIComponent(currentUser.getID()), encodeURIComponent(currentUser.getName()));
}
document.getElementById("onionr-post-creator-user-id").onclick = viewCurrentProfile;
document.getElementById("onionr-post-creator-user-name").onclick = viewCurrentProfile;
2018-09-08 05:47:56 +00:00
// on some browsers it saves the user input on reload. So, it should also recheck the input.
postCreatorChange();