Browse Source

add caching of shortened URIs after fetching

readme-update
Simon Grim 9 years ago
parent
commit
7d0ee60aba
  1. 35
      js/interface_common.js

35
js/interface_common.js

@ -6,6 +6,7 @@
// Post actions: submit, count characters // Post actions: submit, count characters
var twister = { var twister = {
URIs: {}, // shortened URIs are cached here after fetching
html: { html: {
detached: $('<div>'), // here elements go to detach themself detached: $('<div>'), // here elements go to detach themself
blanka: $('<a target="_blank">') // to open stuff in new tab, see routeOnClick() blanka: $('<a target="_blank">') // to open stuff in new tab, see routeOnClick()
@ -823,23 +824,19 @@ function fillElemWithTxt(elem, txt, htmlFormatMsgOpt) {
} }
function fetchShortenedURI(req) { function fetchShortenedURI(req) {
// FIXME need to check if ret for req is cached here if (twister.URIs[req]) {
applyShortenedURI(req, twister.URIs[req]);
return;
}
if (parseInt(twisterVersion) < 93500) { if (parseInt(twisterVersion) < 93500) {
console.warn('can\'t fetch URI "' + req + '": daemon is obsolete, version 0.9.35 or higher is required'); console.warn('can\'t fetch URI "' + req + '": daemon is obsolete, version 0.9.35 or higher is required');
return; return;
} }
twisterRpc('decodeshorturl', [req], twisterRpc('decodeshorturl', [req],
function gotShortURI(req, ret) { function (req, ret) {
// FIXME need to cache ret here twister.URIs[req] = ret;
var elems = $('.link-shortened[href="' + req + '"]') $.localStorage.set('twistaURIs', twister.URIs);
.attr('href', ret) applyShortenedURI(req, ret);
.removeClass('link-shortened')
.off('click mouseup')
.on('click mouseup', muteEvent)
;
for (var i = 0; i < elems.length; i++)
if (elems[i].text === req)
elems[i].text = ret;
}, req, }, req,
function (req, ret) { function (req, ret) {
console.warn('can\'t fetch URI "' + req + '": ' + ret.message); console.warn('can\'t fetch URI "' + req + '": ' + ret.message);
@ -847,6 +844,18 @@ function fetchShortenedURI(req) {
); );
} }
function applyShortenedURI(short, long) {
var elems = $('.link-shortened[href="' + short + '"]')
.attr('href', long)
.removeClass('link-shortened')
.off('click mouseup')
.on('click mouseup', muteEvent)
;
for (var i = 0; i < elems.length; i++)
if (elems[i].text === short) // there may be some other text, possibly formatted, so we check it
elems[i].text = long;
}
function routeOnClick(event) { function routeOnClick(event) {
function routeNewTab(event) { function routeNewTab(event) {
@ -2328,6 +2337,8 @@ function setTextcompleteDropdownListPos(position) {
} }
$(document).ready(function () { $(document).ready(function () {
if ($.localStorage.isSet('twistaURIs'))
twister.URIs = $.localStorage.get('twistaURIs');
twister.html.blanka.appendTo('body').hide(); twister.html.blanka.appendTo('body').hide();
twister.tmpl.followersList = extractTemplate('#template-followers-list'); twister.tmpl.followersList = extractTemplate('#template-followers-list');
twister.tmpl.followersPeer = extractTemplate('#template-followers-peer'); twister.tmpl.followersPeer = extractTemplate('#template-followers-peer');

Loading…
Cancel
Save