-
Notifications
You must be signed in to change notification settings - Fork 14
/
popup.js
166 lines (151 loc) · 5.47 KB
/
popup.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
function saveListId(interactive) {
var url = "https://www.googleapis.com/tasks/v1/users/@me/lists";
chrome.runtime.getBackgroundPage(function(backgroundPage){
backgroundPage.authenticatedXhr('GET', url, null, interactive, function(error, status, response){
if (error != undefined) {
console.log("No list returned");
return;
}
resp = JSON.parse(response);
var lists = resp.items;
for (i in lists) {
console.log("Lista: " + lists[i].title);
if (lists[i].title == "PhoneToDesktop") {
localStorage.setItem('list_id', lists[i].id);
}
}
console.log('lista salva no localStorage: '+localStorage.getItem('list_id'));
if (localStorage.getItem('list_id') == null) {
alertNoList();
}
});
});
}
function reset_configuration() {
localStorage.removeItem('list_id');
console.log("reset_configuration");
console.log("list_id: "+localStorage.getItem("list_id"));
loadTasks();
}
function handle_list_id_updated(e) {
if (e.key == 'list_id') {
loadTasks();
}
}
function loadTasks() {
$("#actionbar_tab a[href='#tab_wait']").tab('show');
var list_id = localStorage.getItem('list_id');
if (list_id == null){
chrome.runtime.getBackgroundPage(function(backgroundPage) {
backgroundPage.addEventListener("storage", handle_list_id_updated, false);
});
saveListId(true);
return;
} else {
chrome.runtime.getBackgroundPage(function(backgroundPage) {
backgroundPage.removeEventListener("storage", handle_list_id_updated, false);
});
var url = "https://www.googleapis.com/tasks/v1/lists/"+list_id+"/tasks";
var callback = function(error, status, resp) {
if (status == 200) {
resp = JSON.parse(resp);
listTasks(resp.items);
} else {
alertNoList();
}
}
chrome.runtime.getBackgroundPage(function(backgroundPage){
backgroundPage.authenticatedXhr('GET', url, null, false, callback);
});
}
}
function delete_item(event){
var parent = $(this).parent().parent();
var list_id = localStorage.getItem('list_id');
var task_id = parent.attr("id");
var url = "https://www.googleapis.com/tasks/v1/lists/"+list_id+"/tasks/"+task_id;
var callback = function(error, status, resp){
if (status != 204) {
parent.slideDown(300, function(){
parent.addClass('min_height');
});
} else {
parent.remove();
}
};
parent.removeClass('min_height');
parent.slideUp(300, function(){
chrome.runtime.getBackgroundPage(function(backgroundPage){
backgroundPage.authenticatedXhr('DELETE', url, null, false, callback);
});
});
}
function alertNoList() {
$("#task_list").empty();
linear_layout = $("<div class='linear_layout min_height'>");
message_authorize = $("<p>");
message_authorize.text(chrome.i18n.getMessage("needAuthorizeApp"));
message_authorize.linkify({
target: "_blank"
});
message_reset = $("<p>");
message_reset.text(chrome.i18n.getMessage("needResetConf"));
button_reset = $("<a class='btn btn-default'>");
button_reset.text(chrome.i18n.getMessage("reset_configuration"));
button_reset.click(reset_configuration);
message_reset.append(button_reset);
linear_layout.append(message_authorize);
linear_layout.append(message_reset);
linear_layout.appendTo($("#task_list"));
$("#actionbar_tab a[href='#tab_list']").tab('show');
}
function listTasks(tasks) {
$("#task_list").empty();
var autolinker = new Autolinker();
for (j in tasks) {
if (tasks[j].title=="") {
continue;
}
var item = $("<div class='linear_layout min_height'>");
item.attr("id", tasks[j].id);
var div_btns = $("<div class='btns'>");
div_btns.appendTo(item);
var div_text = $("<div class='task_text'>");
var task_title = autolinker.link(tasks[j].title);
div_text.html(task_title).appendTo(item);
var btn_del = $("<a class='btn btn-default'>");
btn_del.append("<img src='images/delete.png' />");
btn_del.click(delete_item);
btn_del.appendTo(div_btns);
item.appendTo($("#task_list"));
}
$("#actionbar_tab a[href='#tab_list']").tab('show');
}
function prepareHTMLTexts(){
$("a[href='#tab_list']").text(chrome.i18n.getMessage("tab_list"));
$("a[href='#tab_about']").text(chrome.i18n.getMessage("tab_about"));
$("#btn_reset").text(chrome.i18n.getMessage("reset_configuration"));
var autolinker = new Autolinker();
var message1_text = autolinker.link(chrome.i18n.getMessage("about_message1"));
var message2_text = autolinker.link(chrome.i18n.getMessage("about_message2"));
$("<p>")
.html(message1_text)
.appendTo($("#about_message"));
$("<p>")
.html(message2_text)
.appendTo($("#about_message"));
}
$(document).ready(function(){
$("#actionbar_tab a").click(function(e){
e.preventDefault();
$(this).tab('show');
});
$("#btn_refresh").click(function(e){
loadTasks();
});
$("#btn_reset").click(function(e){
reset_configuration();
});
prepareHTMLTexts();
loadTasks();
});