-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfifolinks.js
168 lines (137 loc) · 4.59 KB
/
fifolinks.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var highestId = 0;
$(function() {
$('#save_link').click(function() {
addLink($('#new_url').val(), $('#new_title').val());
return false;
});
if (window.openDatabase) {
db = window.openDatabase("fifolinks", "0.1");
db.transaction(function(tx) {
monthAgo = new Date((new Date()).getTime() - 2629743830).getTime();
tx.executeSql("DELETE FROM links WHERE (timestamp < ? AND read = ?)", [monthAgo, true], null, handleError);
tx.executeSql("SELECT * FROM links WHERE read = ? ORDER BY timestamp ASC", [false], loadLinks, handleError);
tx.executeSql("SELECT * FROM links WHERE read = ? ORDER BY timestamp DESC LIMIT 50", [true], loadLinks, handleError);
});
} else {
$('#sorry').show();
}
});
function loadLinks(tx, results) {
for (var i=0; i < results.rows.length; i++) {
var item = results.rows.item(i);
if (item.id >= highestId) highestId = item.id + 1;
if (item.read == "true") {
addLinkElement($('#read_links'), item);
} else {
addLinkElement($('#unread_links'), item);
}
}
}
function addLink(url,label) {
db.transaction(function(tx) {
tx.executeSql("INSERT INTO links(id,label,url,read,timestamp) VALUES(?,?,?,?,?)",
[
highestId++,
url,
label,
false,
(new Date()).getTime()
], null, handleError);
tx.executeSql("SELECT * FROM links WHERE id = ?", [highestId-1], loadLinks, handleError);
});
}
function addLinkElement(el,item) {
el.append(
'<li id="link_' + item.id + '" class="link">' +
'<a href="' + item.url + '" ' + (item.read == 'true' ? 'class="primary read">' : 'class="primary">') +
'<div class="label">' + item.label + '</div>' +
'<div class="url">' + item.url + '</div>' +
'</a>' +
'<div class="date">updated ' + (new Date(item.timestamp)).toLocaleString() + '</div>' +
'<div class="actions">' +
'<a href="#" class="swap">' + (item.read == 'true' ? 'Mark unread' : 'Mark read') + '</a>' +
'<span class="separator">|</span>' +
'<a href="#" class="delete">Delete</a>' +
'</div>' +
'<div style="clear:both;"></div>' +
'</li>'
);
el.find('#link_' + item.id + ' a.primary').click(linkClick);
el.find('#link_' + item.id + ' a.swap').click(swapClick);
el.find('#link_' + item.id + ' a.delete').click(deleteClick);
}
function isLinkRead(el) {
return $(el).parents('ul').hasClass('read');
}
function linkClick() {
var linkId = $(this).parents('li').attr('id');
var sqlId = linkId.replace('link_','');
if (!isLinkRead($(this))) {
swapLink(linkId);
markLink(sqlId,true);
}
}
function swapClick() {
var linkId = $(this).parents('li').attr('id');
var sqlId = linkId.replace('link_','');
read = isLinkRead($(this));
swapLink(linkId);
markLink(sqlId,!read);
return false;
}
function deleteClick() {
var linkId = $(this).parents('li').attr('id');
var sqlId = linkId.replace('link_','');
db.transaction(function(tx) {
tx.executeSql("DELETE FROM links WHERE id = ?",
[sqlId], null, handleError
);
});
$('#'+linkId).remove();
return false;
}
function swapLink(id) {
var newParentType = "read";
var el = $('#' + id);
if (el.parents('ul').hasClass("read")) {
newParentType = "unread";
}
// Move the link
if (newParentType == "unread") {
el.slideUp(200,function(){
$('#' +newParentType+ '_links').append(el)
cleanupReadLinks(el,newParentType);
}).slideDown(200);
} else {
el.slideUp(200,function(){
$('#' +newParentType+ '_links').prepend(el)
cleanupReadLinks(el,newParentType);
}).slideDown(200);
}
}
function cleanupReadLinks(el, newParentType) {
var link = el.find('a.primary');
link.removeClass('read');
if (newParentType == 'read') link.addClass('read');
// Change the button text
$('ul.unread a.swap').html('Mark read');
$('ul.read a.swap').html('Mark unread');
}
function markLink(id,read) {
db.transaction(function(tx) {
tx.executeSql("UPDATE links SET read = ?, timestamp = ? WHERE id = ?",
[read, (new Date()).getTime(), id],
null, handleError
);
});
}
function handleError(tx, err) {
// No such table
if (err.code == 1 && err.message == "no such table: links") {
tx.executeSql("CREATE TABLE links (id REAL UNIQUE, label TEXT, url TEXT UNIQUE, read BOOLEAN, timestamp REAL)", [], null, handleError);
} else if (err.code == 1 && err.message == "table links already exists") {
// Do nothing
} else {
$('#error').html("SQL ERROR[" +err.code+ "]: " + err.message).show();
}
}