Skip to content

Commit

Permalink
Switch to updated Portal API.
Browse files Browse the repository at this point in the history
  • Loading branch information
peroo committed Jul 18, 2012
1 parent 3443ae2 commit 29e3bc5
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 128 deletions.
137 changes: 67 additions & 70 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,49 @@
var portalURL = "http://portal.opera.com/portal/tabs/?utm_source=DesktopBrowser&utm_medium=Speeddial&utm_campaign=SDE&tab_name=Opera%20Portal";
var cell = opera.contexts.speeddial;
cell.url = "http://portal.opera.com/portal/tabs/?utm_source=DesktopBrowser&utm_medium=Speeddial&utm_campaign=SDE&tab_name=Opera%20Portal";
var prefs = widget.preferences;

function parseData(text) {
var items = JSON.parse(text);
var fragment = document.createElement('div');
return items.map(function(item) {
fragment.innerHTML = item.post_content;
item.post_content = fragment.innerText;
return item;
function error() {
opera.postError('ERROR: Something goes here');
}

function parseSources(callback, request) {
var items = JSON.parse(request.responseText);
callback(items);
}

function parseFeed(callback, request) {
callback(JSON.parse(request.responseText));
}

function getFeed(feed, callback) {
_XHR(prefs.baseURI + "boxes/" + feed.id + "?per_box=" + feed.count, parseFeed.bind(undefined, callback));
}

function getSources(callback) {
_XHR(prefs.baseURI + "all_boxes", function(request) {
var data = JSON.parse(request.responseText);
var box = data.boxes[0];
prefs.sources = JSON.stringify([{id: box.id, count: 3}]);
getFeeds();
});
}

function getSources() {
function _XHR(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', widget.preferences.baseURI + "?boxes=100&per_box=3", true);
xhr.open('GET', url, true);
xhr.onreadystatechange = function(event) {
if(xhr.readyState === 4) {
if(xhr.status === 200) {
data = parseData(xhr.responseText);
refresh();
callback(xhr);
}
else if(!widget.preferences.sources) {
document.getElementById('content').innerHTML =
"<h2>Error. Will reattempt in 5 minutes.</h2>";
else {
error();
}
}
}
xhr.timeout = 10000;
xhr.ontimeout = error;
xhr.send();
}

Expand All @@ -41,17 +58,17 @@
focused = focused.nextSibling;
}
else {
focused = focused.parentNode.firstChild;
}
focused = focused.parentNode.firstChild;
}
focused.className = 'focused';
var url = document.getElementsByClassName('focused')[0].dataset['url'];
cell.title = focused.title;
cell.url = portalURL + "&ext_url=" + encodeURIComponent(url);
cell.url = portalURL + "&ext_url=" + encodeURIComponent(url);
}

function setRefreshTimer() {
clearInterval(delayTimer);
delayTimer = setInterval(swap, parseInt(widget.preferences.delay) * 1000);
delayTimer = setInterval(swap, parseInt(prefs.delay) * 1000);
}

// Cache images to avoid image-caching bug in SDE-context
Expand All @@ -69,64 +86,46 @@
return buffer[src];
}

function refresh() {
var sources = {};
if(!widget.preferences.sources) {
sources[data[0].box_title] = 3;
}
else {
JSON.parse(widget.preferences.sources).forEach(function(source) {
sources[source.name] = source.count;
});
function getFeeds() {
if(!prefs.sources) {
return getSources(getFeeds);
}
var sources = JSON.parse(prefs.sources);

var posts = data.filter(function(post) {
if(sources[post.box_title]) {
sources[post.box_title]--;
return true;
}
return false;
var data = [];
sources.forEach(function(source) {
getFeed(source, function(result) {
data.push(result);
if(data.length == sources.length) {
refreshFeeds(data);
}
});
});
}

if(posts.length == 0) {
posts = data.slice(0,2);
}
function refreshFeeds(feedData) {
var posts = feedData.reduce(function(arr, feed) {
return arr.concat(feed);
}, []);

var div = document.createElement('div');
for(var i=0, post; post = posts[i]; i++) {
var image = imageBuffer(post.post_image);

post.post_title = hyphenate(post.post_title);

if(post.box_type == "feed") {
var article = document.createElement('article');
article.dataset.url = post.url;
article.title = post.box_title;
article.appendChild(image);
var h2 = document.createElement('h2');
h2.innerHTML = post.post_title;
article.appendChild(h2);
var p = document.createElement('p');
p.textContent = post.post_content;
article.appendChild(p);
div.appendChild(article);
} else if(post.box_type == "sports") {
var status = (post.status == "Played") ?
" (FINAL)" :
(" (" + post.datetime + ")");
var article = document.createElement('article');
article.appendChild(image);
var h2 = document.createElement('h2');
h2.textContent = post.competition_name;
article.appendChild(h2);
var p = document.createElement('p');
p.textContent = post.team_B_name + ' @ ' + post.team_A_name;
var p2 = document.createElement('p');
p2.textContent = post.fs_B + '-' + post.fs_A + status;
article.appendChild(p);
article.appendChild(p2);
div.appendChild(article);
}
var article = document.createElement('article');
article.dataset.url = post.url;
article.title = post.box_title;
article.appendChild(image);
var h2 = document.createElement('h2');
h2.innerHTML = post.post_title;
article.appendChild(h2);
var p = document.createElement('p');
p.innerHTML = post.post_content;
p.innerHTML = p.textContent;
article.appendChild(p);
div.appendChild(article);
}

var content = document.getElementById('content');
Expand All @@ -138,10 +137,8 @@
}

function reconfigure(ev) {
if (ev.storageArea != widget.preferences) return;
switch(ev.key) {
case 'delay': setRefreshTimer(); break;
case 'sources': getSources(); break;
if(ev.key == 'sources') {
getFeeds();
}
}

Expand All @@ -156,7 +153,7 @@
function init() {
setRefreshTimer();
window.addEventListener('storage', reconfigure, false);
setInterval(getSources, 300000);
setInterval(getFeeds, 300000);
getSources();
}

Expand Down
152 changes: 94 additions & 58 deletions js/options.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,114 @@
(function() {
var options = "";

function getSources() {
var agent = new XMLHttpRequest();
agent.open('GET', widget.preferences['baseURI']+"?boxes=100&per_box=1", false, '', '');
agent.send()
if (agent.status<400) {
return JSON.parse(agent.responseText);
function fetchSources(callback) {
var req = new XMLHttpRequest();
req.open('GET', widget.preferences['baseURI']+"all_boxes", true);
req.onreadystatechange = function() {
if(req.readyState == 4) {
if(req.status == 200) {
callback(req.responseText);
}
}
}
return [];
req.send();
}
function addItem(refNode, data) {
if (!data) data = {'id':"", 'count':3};
var row = document.getElementById('sources').insertBefore(document.createElement('tr'), refNode);
row.innerHTML = sourceHTML;
var options = row.getElementsByTagName('option');
for (var b = 0; b < options.length; b++) {
if (options[b].value==data['name']) {
options[b].selected = true;
break;

function saveSources() {
var sourceElements = document.querySelectorAll('#source-list li');
var sourceData = Array.prototype.map.call(sourceElements,
function(li) {
var select = li.querySelector('select');
return {
id: select.options[select.selectedIndex].value,
count: li.querySelector('input').value
};
}
}
row.getElementsByTagName('input')[0].value = data['count'];
row.getElementsByTagName('button')[0].addEventListener('click', less, false);
row.getElementsByTagName('button')[1].addEventListener('click', more, false);
);
widget.preferences.sources = JSON.stringify(sourceData);
window.close();
}
function save(ev) {
var rows = document.getElementById('sources').getElementsByTagName('tr');
var data = []
for (var a = 0; a < rows.length; a++) {
data.push({'name':rows[a].getElementsByTagName('select')[0].value, 'count':rows[a].getElementsByTagName('input')[0].value});

function getSelectedSources() {
var savedSources = widget.preferences.sources;
if(savedSources) {
return JSON.parse(savedSources);
}
else {
return [];
}
data = JSON.stringify(data);
if (widget.preferences['source'] != data) widget.preferences.setItem('sources', data);
var delay = document.getElementsByName('secondsperpost')[0].value;
if (widget.preferences['delay'] != delay) widget.preferences.setItem('delay', delay);
ev.preventDefault();
window.close();
}
function less(ev) {
var row = ev.target.parentNode.parentNode
table = row.parentNode;
table.removeChild(row);
if (!table.firstChild) addItem();
ev.preventDefault();

function createListElement(select, source) {
var li = document.createElement('li');
var countInput = document.createElement('input');
countInput.type = 'number';
countInput.step = 1;
countInput.min = 1;
countInput.max = 5;
countInput.value = source ? source.count : 3;
var span = document.createElement('span');
span.className = 'l11n';
span.textContent = _getString('posts');
var button = document.createElement('button');
button.type = 'button';
button.textContent = '✖';
button.className = 'remove-source';
button.addEventListener('click', function() {
li.parentElement.removeChild(li);
}, false);

if(source) {
Array.prototype.forEach.call(select.options, function(option, index) {
if(option.value == source.id) {
select.selectedIndex = index;
}
});
}

li.appendChild(select);
li.appendChild(countInput);
li.appendChild(span);
li.appendChild(button);

return li;
}
function more(ev) {
addItem(ev.target.parentNode.parentNode.nextSibling)
ev.preventDefault();

function addSource(select) {
document.getElementById('source-list')
.appendChild(createListElement(select.cloneNode(true)));
}

function init() {
var allSources = getSources().filter(function(source) {
return source.box_type == 'feed';
function initSourceList(selectedSources, sourceList) {
var sources = JSON.parse(sourceList).boxes;
var select = document.createElement('select');
sources.forEach(function(source) {
var option = document.createElement('option');
option.value = source.id;
option.textContent = source.name;
select.appendChild(option);
});

var sources;
if (widget.preferences.sources) {
sources = JSON.parse(widget.preferences.sources);
var ul = document.getElementById('source-list');
ul.innerHTML = '';
if(selectedSources.length > 0) {
selectedSources.forEach(function(source) {
ul.appendChild(createListElement(select.cloneNode(true), source));
});
}
else {
sources = [{name: allSources[0].box_title, count: 3}];
widget.preferences.sources = JSON.stringify(sources);
ul.appendChild(createListElement(select.cloneNode(true)));
}

for (var i = 0, source; source = allSources[i]; i++) {
options += '<option value="' + source.box_title + '">' + source.box_title + '</option>';
}
sourceHTML = '<td><select>'+options+'</select></td><td><label><span><input type="number" step="1" min="1" max="3"></span><span>' + _getString('posts') + '</span></label></td><td><button class="minus"><span>-</span></button><button class="plus"><span>+</span></button></td>';
document.getElementsByName('secondsperpost')[0].value = widget.preferences['delay'];
document.getElementsByTagName('form')[0].addEventListener('submit', save, false);
for (var a = 0; a < sources.length; a++) {
addItem(null, sources[a]);
}
document.getElementById('new-source').addEventListener('click',
addSource.bind(undefined, select), false
);
}

function init() {
var selectedSources = getSelectedSources();
fetchSources(initSourceList.bind(undefined, selectedSources));
document.getElementById('save-button')
.addEventListener('click', saveSources, false);
}
document.addEventListener('DOMContentLoaded', init, false);
}())

0 comments on commit 29e3bc5

Please sign in to comment.