summaryrefslogtreecommitdiff
path: root/mobile/mobile.js
blob: 57908bc0843ca37508c7f7283ecaf83b28d23e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var backend = "backend.php";

function toggleMarked(id, elem) {

	var toggled = false;

	if (elem.getAttribute("toggled") == "true") {
		toggled = 1;
	} else {
		toggled = 0;
	}

	var query = "?op=toggleMarked&id=" + id + "&mark=" + toggled;

	new Ajax.Request(backend, {
		parameters: query,
		onComplete: function (transport) {
			//
		} });
}

function togglePublished(id, elem) {

	var toggled = false;

	if (elem.getAttribute("toggled") == "true") {
		toggled = 1;
	} else {
		toggled = 0;
	}

	var query = "?op=togglePublished&id=" + id + "&mark=" + toggled;

	new Ajax.Request(backend, {
		parameters: query,
		onComplete: function (transport) {
			//
		} });

}

function setPref(elem) {
	var toggled = false;
	var id = elem.id;

	if (elem.getAttribute("toggled") == "true") {
		toggled = 1;
	} else {
		toggled = 0;
	}

	var query = "?op=setPref&id=" + id + "&to=" + toggled;

	new Ajax.Request(backend, {
		parameters: query,
		onComplete: function (transport) {
			//
		} });

}

// Go directly to another item in the same feed
function goToSibling(article_id, feed_id, link, step) {
    var links = linksInFeed(feed_id);
    for (var i=0 ; i<links.length ; i++) {
        var re = new RegExp(".*article\\.php\\?id="+article_id+"&.*");
        if (!re.test(links[i].href)) continue;
        // here, we've found the current article
        var index = i + step;
        if (index < 0) {
            markAsRead(feed_id);
            iui.showPage($("feed-"+feed_id), true);
            return false;
        }
        if (index >= links.length) {
            showRestOfFeed(feed_id);
            return false;
        }
        console.log(links[index]);
        var match = links[index].href.match(/.*article\.php\?(.*)/);
        var qs = match[1];
        var backwards = false;
        if (step < 0) backwards = true;
        link.setAttribute("selected", "progress");
        function unselect() { link.removeAttribute("selected"); }
        iui.showPageByHref("article.php?"+qs, null, null, null, unselect, backwards);
        return false;
    }
    return false;
}
function goPrev(article_id, feed_id, link) {
    return goToSibling(article_id, feed_id, link, -1);
}
function goNext(article_id, feed_id, link) {
    return goToSibling(article_id, feed_id, link, 1);
}

// Get all the links in the feed. The all_links variable includes the "get more article" link
function linksInFeed(feed_id, all_links) {
    var feed_content = $("feed-"+feed_id);
    var links_raw = feed_content.getElementsByTagName("a");
    if (all_links) return links_raw;
    var links = [];
    // filter the array to remove the "get more articles" link
    // and the "search" link (which is always first)
    for (var i=1 ; i<links_raw.length ; i++) {
        if (links_raw[i].href.match(/.*article\.php\?id=.*/)) {
            links.push(links_raw[i]);
        }
    }
    return links;
}

// Adds the "read" class to all read links in the feed
function markAsRead(feed_id) {
    var links = linksInFeed(feed_id);
    for (var j=0 ; j<links.length ; j++) {
        var match = links[j].href.match(/.*article\.php\?id=(\d+)&.*/);
        if ($("article-"+match[1])) {
            links[j].className = "read";
        }
    }
}

// Go the the articles list and expand the "get more articles" link
function showRestOfFeed(feed_id) {
    var links_raw = linksInFeed(feed_id, true);
    var lastlink = links_raw[links_raw.length - 1];
    if (lastlink.target == "_replace") {
        // It's a "get more articles" link
        iui.showPage($("feed-"+feed_id), true);
        // Mark old items a "read"
        markAsRead(feed_id);
        // Simulate click on the "get more articles" link
        lastlink.setAttribute("selected", "progress");
        function unselect() { lastlink.removeAttribute("selected"); }
        setTimeout(window.scrollTo, 0, 0, 1000);
        iui.showPageByHref(lastlink.href, null, null, lastlink, unselect);
    } else {
        iui.showPage($("home"), true);
    }
}