/
behaviour.js
193 lines (163 loc) · 5.14 KB
/
behaviour.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var overdrive={};
/*
* Download the latest bookmarks from delicious
*/
overdrive.sync=function(count) {
var url="http://feeds.delicious.com/v1/json/"+overdrive.storage.settings['username']+"?raw&count="+count;
$.getJSON(url+"&callback=?", function(bookmarks){
$.jGrowl("Fetching data from delicious.com.");
$.each(bookmarks, function(){
var bmark=new Bookmark(this.u, this.d, this.t, this.dt,
//asynchronous callback parameter
function (obj) {
window.console.log(obj);
obj.append();
});
overdrive.storage.insertBookmark(bmark);
});
var date=new Date();
overdrive.storage.setSetting("lastUpdate", date);
overdrive.storage.setSetting("fullSync", true);
});
};
/*
* Starts the the download of all bookmarks through the tag feed
*/
overdrive.startBookmarkTimeout=function () {
if (overdrive.storage.settings["tagsDownloaded"]===true || overdrive.storage.settings["tagsDownloaded"]==="True") {
overdrive.storage.getNextTag(function (tag) {
overdrive.remote.getBookmarksFromTag(tag);
});
};
if (overdrive.storage.settings["bookmarksComplete"]!==true){
window.setTimeout(function () {
overdrive.startBookmarkTimeout();
},2000);
}
};
/*
* Click event callbacks
*/
overdrive.ui={};
overdrive.ui.bottomTabs=["username"];
overdrive.ui.reset=function () {
var list=["bookmarks","tags","settings"];
for (i=0; i<list.length; i++){
overdrive.storage.deleteTable(list[i]);
}
window.setTimeout(function () {
try{
window.location.reload();
}
catch(err){
alert(err);
}
}, 1000);
return false;
};
overdrive.ui.showFeedback=function () {
UserVoice.Popin.show();
return false;
};
$(document).ready(function(){
$('#mainSearch').focus();
try{
overdrive.storage = new LocalStorage();
overdrive.remote=new RemoteStorage();
}
catch(err){
// Error message for browsers that don't support HTML5 offline storage
$.get("error.html", function(data){
$("#errorConsole").empty().append(data);
});
$("#errorConsole").modal();
}
// callbacks to this will execute after all the settings have been fetched from the db
overdrive.storage.getSettings(function (argument) {
// if the user hasn't entered the username yet
if (overdrive.storage.settings["username"]===undefined){
$.get("dataentry.html", function(data){
$("#errorConsole").empty().append(data);
// create the special case for sqlForms, their elements will not be submitted but stored in the db
$(":submit").click(function (){
var form=$(this).parent();
form.children().each(function (){
if ($(this).attr("type")!="submit" && $(this).hasClass("sql")){
var key=$(this).attr("name");
var value=$(this).attr("value");
overdrive.storage.setSetting(key, value);
}
});
$.modal.close();
$("#errorConsole").empty();
window.location.reload();
});
});
$("#errorConsole").modal();
}
// intialise the user interface with variable from the db, uses a XHTML tag with the id of the settings in
// question
var uiFields=overdrive.ui.bottomTabs;
for (i=0; i<uiFields.length; i++)
{
$("#"+uiFields[i]).text(overdrive.storage.settings[uiFields[i]]);
}
// get the bookmarks and start the download timer
window.setTimeout(function (argument) {
// if the tags have not been downloaded yet
if (overdrive.storage.settings["tagsDownloaded"]!==true && overdrive.storage.settings["username"]!==undefined)
{
$.jGrowl("Downloading tags from delicious.com");
overdrive.remote.getAllTags(
//process callback function
function (tag, bookmarks) {
overdrive.storage.insertTag(tag, bookmarks);
},
// success callback
function () {
overdrive.storage.setSetting("tagsDownloaded", true);
window.setTimeout(function (argument) {
overdrive.startBookmarkTimeout();
}, 500);
}
);
}
}, 500);
window.setTimeout(function (argument) {
if (overdrive.storage.settings["tagsDownloaded"]===true && overdrive.storage.settings["tagsDownloaded"]==="True") {
overdrive.startBookmarkTimeout();
};
},1500);
//wait for 500ms for the DB to be initialised, then start syncing with delicious.com
if (!overdrive.storage.settings["fullSync"] && overdrive.storage.settings["username"]!=undefined)
{
overdrive.sync(100);
}
var mins=13;//minutes in between syncs
var lastUpdate=new Date(overdrive.storage.settings["lastUpdate"]+(60000*mins));
var now=new Date();
if (lastUpdate<now && overdrive.storage.settings["username"]!=undefined)
{
overdrive.sync(20); //get the last 20 bookmarks
}
});
overdrive.storage.getAllBookmarks();
$(".searchBox").keyup(function (e) {
if ($(this).attr("value")===""){
overdrive.storage.getAllBookmarks();
}
else{
overdrive.storage.searchBookmarks($(this).attr("value"));
}
});
// for when the user returns to an unfinished bookmark download
window.setTimeout(function function_name (argument) {
overdrive.startBookmarkTimeout();
}, 2500);
// catchall for click events with class="function"
$(".function").click(function () {
var fn=$(this).attr("href");
overdrive.ui[fn]();
return false;
});
});