protect users from accidentally loosing unposted queets, also make localStorageObjectCache_GET less insane

This commit is contained in:
Hannes Mannerheim 2015-07-02 16:57:51 +02:00
parent 0c79026f77
commit 972f37cd7f
4 changed files with 124 additions and 82 deletions

View File

@ -387,11 +387,10 @@ function unRequeet(this_stream_item, this_action, my_rq_id) {
function getFavsAndRequeetsForQueet(q,qid) {
// get immediately from localstorage cache
localStorageObjectCache_GET('favsAndRequeets',qid,function(data){
if(data) {
showFavsAndRequeetsInQueet(q, data);
}
});
var cacheData = localStorageObjectCache_GET('favsAndRequeets',qid);
if(cacheData) {
showFavsAndRequeetsInQueet(q, cacheData);
}
$.ajax({ url: window.apiRoot + "qvitter/favs_and_repeats/" + qid + ".json?t=" + timeNow(),
type: "GET",

View File

@ -1114,21 +1114,20 @@ function expand_queet(q,doScrolling) {
var attachmentId = q.children('.queet').find('span.attachment.more').attr('data-attachment-id');
// get full html for queet, first try localstorage cache
localStorageObjectCache_GET('fullQueetHtml',qid,function(data){
if(data) {
q.children('.queet').find('.queet-text').html(data);
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
}
else {
getFromAPI("attachment/" + attachmentId + ".json",function(data){
if(data) {
localStorageObjectCache_STORE('fullQueetHtml',qid,data);
q.children('.queet').find('.queet-text').html($.trim(data));
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
}
});
}
});
var cacheData = localStorageObjectCache_GET('fullQueetHtml',qid);
if(cacheData) {
q.children('.queet').find('.queet-text').html(cacheData);
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
}
else {
getFromAPI("attachment/" + attachmentId + ".json",function(data){
if(data) {
localStorageObjectCache_STORE('fullQueetHtml',qid,data);
q.children('.queet').find('.queet-text').html($.trim(data));
q.children('.queet').outerHTML(detectRTL(q.children('.queet').outerHTML()));
}
});
}
}
// add expanded container
@ -1226,8 +1225,18 @@ function expand_queet(q,doScrolling) {
// show inline reply form if logged in
if(typeof window.loggedIn.screen_name != 'undefined') {
q.children('.queet').append(replyFormHtml(q,qid));
}
// if we have cached text, expand the reply form and add that
var queetBox = q.children('.queet').find('.queet-box');
var cachedText = decodeURIComponent(queetBox.attr('data-cached-text'));
var cachedTextText = $('<div/>').html(cachedText).text();
if(cachedText != 'undefined') {
queetBox.click();
queetBox.html(cachedText);
setSelectionRange(queetBox[0], cachedTextText.length, cachedTextText.length);
queetBox.trigger('input');
}
}
}
}
}
@ -1277,7 +1286,14 @@ function queetBoxHtml() {
· · · · · · · · · */
function replyFormHtml(q,qid) {
// get all @:s
// if we have cached text in localstorage
var data = localStorageObjectCache_GET('queetBoxInput','queet-box-' + qid);
if(data) {
var cachedText = encodeURIComponent(data);
}
// get all @:s
var user_screen_name = q.children('.queet').find('.screen-name').html().substring(1);
var user_screen_name_html = '<a>@' + user_screen_name + '</a>';
var user_screen_name_text = '@' + user_screen_name;
@ -1302,9 +1318,12 @@ function replyFormHtml(q,qid) {
}
});
var startText = encodeURIComponent(window.sL.replyTo + ' ' + user_screen_name_html + reply_to_screen_name_html + more_reply_tos + '&nbsp;<br>');
var repliesText = encodeURIComponent(user_screen_name_text + reply_to_screen_name_text + more_reply_tos_text + '&nbsp;');
return '<div class="inline-reply-queetbox"><span class="inline-reply-caret"><span class="caret-inner"></span></span><img class="reply-avatar" src="' + $('#user-avatar').attr('src') + '" /><div class="queet-box queet-box-syntax" id="queet-box-' + qid + '" data-start-text="' + startText + '" data-replies-text="' + repliesText + '">' + decodeURIComponent(startText) + '</div><div class="syntax-middle"></div><div class="syntax-two" contenteditable="true"></div><div class="mentions-suggestions"></div><div class="queet-toolbar toolbar-reply"><div class="queet-box-extras"><button class="upload-image"></button><button class="shorten disabled">URL</button></div><div class="queet-button"><span class="queet-counter"></span><button>' + window.sL.queetVerb + '</button></div></div></div>';
var startText = window.sL.replyTo + ' ' + user_screen_name_html + reply_to_screen_name_html + more_reply_tos + '&nbsp;<br>';
var repliesText = user_screen_name_text + reply_to_screen_name_text + more_reply_tos_text + '&nbsp;';
startText = encodeURIComponent(startText);
repliesText = encodeURIComponent(repliesText);
return '<div class="inline-reply-queetbox"><span class="inline-reply-caret"><span class="caret-inner"></span></span><img class="reply-avatar" src="' + $('#user-avatar').attr('src') + '" /><div class="queet-box queet-box-syntax" id="queet-box-' + qid + '" data-start-text="' + startText + '" data-replies-text="' + repliesText + '" data-cached-text="' + cachedText + '">' + decodeURIComponent(startText) + '</div><div class="syntax-middle"></div><div class="syntax-two" contenteditable="true"></div><div class="mentions-suggestions"></div><div class="queet-toolbar toolbar-reply"><div class="queet-box-extras"><button class="upload-image"></button><button class="shorten disabled">URL</button></div><div class="queet-button"><span class="queet-counter"></span><button>' + window.sL.queetVerb + '</button></div></div></div>';
}
@ -1363,12 +1382,10 @@ function centerPopUp(thisPopUp) {
function getConversation(q, qid) {
// check if we have a conversation for this notice cached in localstorage
localStorageObjectCache_GET('conversation',q.attr('data-conversation-id'), function(data){
if(data) {
showConversation(q, qid, data);
}
});
var cacheData = localStorageObjectCache_GET('conversation',q.attr('data-conversation-id'));
if(cacheData) {
showConversation(q, qid, cacheData);
}
// always get most recent conversation from server
getFromAPI('statusnet/conversation/' + q.attr('data-conversation-id') + '.json?count=100', function(data){ if(data) {
@ -1873,11 +1890,10 @@ function addToFeed(feed, after, extraClasses, isReply) {
function buildQueetHtml(obj, idInStream, extraClassesThisRun, requeeted_by, isConversation) {
// if we have the full html for a truncated notice cached in localstorage, we use that
localStorageObjectCache_GET('fullQueetHtml',obj.id,function(data){
if(data) {
obj.statusnet_html = data;
}
});
var cacheData = localStorageObjectCache_GET('fullQueetHtml',obj.id);
if(cacheData) {
obj.statusnet_html = cacheData;
}
// we don't want to print 'null' in in_reply_to_screen_name-attribute, someone might have that username!
var in_reply_to_screen_name = '';

View File

@ -53,8 +53,8 @@ function localStorageObjectCache_STORE(name, unique_id, object) {
if(localStorageIsEnabled()) {
if(object.length < 1) {
// an empty object means we remove this entry
if(object === false || object.length < 1) {
// false or an empty object means we remove this entry
if(typeof localStorage[name + '-' + unique_id] != 'undefined' && localStorage[name + '-' + unique_id] !== null) {
delete localStorage[name + '-' + unique_id];
}
@ -127,11 +127,10 @@ function removeOldestLocalStorageEntries(callback) {
·
· @param name: the name of this type of object
· @param unique_id: some unique_id the key in localStorage will be name-unique_id
· @param callback: callback function, returns false if not found
·
· · · · · · · · · */
function localStorageObjectCache_GET(name, unique_id, callback) {
function localStorageObjectCache_GET(name, unique_id) {
if(localStorageIsEnabled()) {
if(typeof localStorage[name + '-' + unique_id] != 'undefined' && localStorage[name + '-' + unique_id] !== null) {
@ -139,18 +138,18 @@ function localStorageObjectCache_GET(name, unique_id, callback) {
if(typeof parsedObject.modified == 'undefined' || parsedObject.modified === null) {
// invalid or old localstorage object found, check the whole localstorage!
checkLocalStorage();
callback(false);
return false;
}
else {
callback(parsedObject.data);
return parsedObject.data;
}
}
else {
callback(false);
return false;
}
}
else {
callback(false);
return false;
}
}
@ -185,7 +184,8 @@ function checkLocalStorage() {
'favsAndRequeets',
'languageData',
'fullQueetHtml',
'selectedLanguage'
'selectedLanguage',
'queetBoxInput'
];
var thisDataType = k.substring(0,k.indexOf('-'));
if($.inArray(thisDataType, validDataTypes) == -1 || k.indexOf('-') == -1) {
@ -1073,15 +1073,14 @@ function updateHistoryLocalStorage() {
function loadHistoryFromLocalStorage() {
if(localStorageIsEnabled()) {
localStorageObjectCache_GET('browsingHistory', window.loggedIn.screen_name,function(data){
if(data) {
$('#history-container').css('display','block');
$('#history-container').html('');
$.each(data, function(key,obj) {
$('#history-container').append('<a class="stream-selection" data-stream-header="' + obj.dataStreamHeader + '" href="' + obj.dataStreamHref + '">' + obj.dataStreamHeader + '</i><i class="chev-right"></i></a>');
});
}
});
var cacheData = localStorageObjectCache_GET('browsingHistory', window.loggedIn.screen_name);
if(cacheData) {
$('#history-container').css('display','block');
$('#history-container').html('');
$.each(cacheData, function(key,obj) {
$('#history-container').append('<a class="stream-selection" data-stream-header="' + obj.dataStreamHeader + '" href="' + obj.dataStreamHref + '">' + obj.dataStreamHeader + '</i><i class="chev-right"></i></a>');
});
}
updateHistoryLocalStorage();
}
}

View File

@ -319,14 +319,13 @@ $(window).load(function() {
var selectedForUser = window.loggedIn.id;
}
localStorageObjectCache_GET('selectedLanguage',selectedForUser, function(data){
if(data) {
window.selectedLanguage = data;
}
else {
window.selectedLanguage = browserLang;
}
});
var cacheData = localStorageObjectCache_GET('selectedLanguage',selectedForUser);
if(cacheData) {
window.selectedLanguage = cacheData;
}
else {
window.selectedLanguage = browserLang;
}
// check that this language is available, otherwise use english
if(typeof window.availableLanguages[window.selectedLanguage] == 'undefined') {
@ -336,26 +335,24 @@ $(window).load(function() {
// if we already have this version of this language in localstorage, we
// use that cached version. we do this because $.ajax doesn't respect caching, it seems
localStorageObjectCache_GET('languageData',window.availableLanguages[window.selectedLanguage], function(data){
if(data) {
proceedToSetLanguageAndLogin(data);
}
else {
$.ajax({
dataType: "json",
url: window.fullUrlToThisQvitterApp + 'locale/' + window.availableLanguages[window.selectedLanguage],
error: function(data){console.log(data)},
success: function(data) {
var cacheData = localStorageObjectCache_GET('languageData',window.availableLanguages[window.selectedLanguage]);
if(cacheData) {
proceedToSetLanguageAndLogin(cacheData);
}
else {
$.ajax({
dataType: "json",
url: window.fullUrlToThisQvitterApp + 'locale/' + window.availableLanguages[window.selectedLanguage],
error: function(data){console.log(data)},
success: function(data) {
// store this response in localstorage
localStorageObjectCache_STORE('languageData',window.availableLanguages[window.selectedLanguage], data);
proceedToSetLanguageAndLogin(data);
}
});
}
});
// store this response in localstorage
localStorageObjectCache_STORE('languageData',window.availableLanguages[window.selectedLanguage], data);
proceedToSetLanguageAndLogin(data);
}
});
}
});
// proceed to set language and login
@ -497,7 +494,6 @@ function doLogin(streamToSet) {
$('#user-header').css('background-image','url(\'' + window.loggedIn.cover_photo + '\')');
}
// get all users i'm following for autosuggestion
window.following = new Array();
getFromAPI('qvitter/allfollowing/' + window.loggedIn.screen_name + '.json',function(data){
@ -514,6 +510,20 @@ function doLogin(streamToSet) {
window.following[i] = { 'id': k,'name': v[0], 'username': v[1],'avatar': avatar, 'url':v[3] };
i++;
});
cacheSyntaxHighlighting(); // do this now not to stall slow computers
// we might have cached text for the queet box
// (we need to get the mentions suggestions and cache the syntax highlighting before doing this)
var cachedQueetBoxData = localStorageObjectCache_GET('queetBoxInput','queet-box');
var cachedQueetBoxDataText = $('<div/>').html(cachedQueetBoxData).text();
if(cachedQueetBoxData) {
queetBox = $('#queet-box');
queetBox.click();
queetBox.html(cachedQueetBoxData);
setSelectionRange(queetBox[0], cachedQueetBoxDataText.length, cachedQueetBoxDataText.length);
queetBox.trigger('input');
}
}
});
@ -536,7 +546,6 @@ function doLogin(streamToSet) {
$('#top-compose').fadeIn('slow');
$('input#nickname').blur();
remove_spinner();
cacheSyntaxHighlighting(); // do this now after everything is loaded, to not stall slow computers
},true);
}
@ -2445,6 +2454,25 @@ $('body').on('keyup', 'div.queet-box-syntax', function(e) {
}
});
/* ·
·
· Store unposted queets in cache, if the user accidentally reloads the page or something
·
· · · · · · · · · · · · · */
$('body').on('keyup', 'div.queet-box-syntax', function(e) {
// remove from cache if empty, or same as default text
if($.trim($(this).text()) == ''
|| $.trim($(this).text()) == window.sL.compose
|| $.trim($(this).text()) == $.trim(decodeURIComponent($(this).attr('data-start-text')))
|| $.trim($(this).text()) == $.trim(decodeURIComponent($(this).attr('data-replies-text')))) {
localStorageObjectCache_STORE('queetBoxInput',$(this).attr('id'),false);
}
else {
localStorageObjectCache_STORE('queetBoxInput',$(this).attr('id'),$(this).html());
}
});
/* ·
·