-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
164 lines (149 loc) · 5.06 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
function reset_configuration() {
localStorage.removeItem('list_id');
browser.runtime.getBackgroundPage((page) => {
page.getAccessToken()
.then(page.getTaskLists)
.then(page.getListIDFromLists)
.then(loadTasks)
});
}
function handle_list_id_updated(e) {
if (e.key == 'list_id') {
loadTasks();
}
}
/**
* Selects a material pane by adding the class "is-active" to it
*/
function selectPane(pane_id) {
var layout = document.querySelector('.mdl-layout').MaterialLayout;
var panes = document.querySelectorAll(".mdl-layout__tab-panel");
var pane = document.querySelector("#" + pane_id);
layout.resetPanelState_(panes);
pane.classList.add("is-active");
if (pane.onSelected) {
pane.onSelected();
}
}
/**
* Adds a event listener to the click action in a link, to make it select the
* corresponding pane by selectPane
*/
function setupDrawer(link) {
link.addEventListener('click', function (e) {
if (link.getAttribute('href').charAt(0) === '#') {
e.preventDefault();
var href = link.href.split("#")[1];
selectPane(href);
document.querySelector('.mdl-layout').MaterialLayout.toggleDrawer();
}
});
}
/**
* Users setupDrawer to make the material drawer menu work
*/
function initDrawer() {
var links = document.querySelectorAll(".mdl-navigation__link");
for (var i = 0; i < links.length; i++) {
setupDrawer(links[i]);
}
}
function loadTasks() {
selectPane("pane_wait");
browser.runtime.getBackgroundPage((page) => {
var list_id = page.getListID();
if (list_id == null) {
alertNoList()
return
}
page.getAccessToken()
.then((token) => {
page.fetchTasks(token, list_id)
.then((response) => {
listTasks(response.items);
})
})
}
);
}
function delete_item(event) {
browser.runtime.getBackgroundPage((page) => {
page.getAccessToken().then((accessToken) => {
var parent = $(this).parent().parent();
var list_id = page.getListID();
var task_id = parent.attr("id");
parent.slideUp(300, function () {
page.deleteTask(accessToken, list_id, task_id)
.then((response) => {
if (response.status != 204) {
parent.slideDown(300, function () {
parent.addClass('min_height');
});
} else {
parent.remove();
}
});
parent.removeClass('min_height');
});
});
});
}
function alertNoList() {
// called when we don't have list_id in Store.
// so we have not authorized and not fetched and stored list_id in localStore
// $("#tab_wait").empty();
$("#task_list").empty();
selectPane("pane_nolist");
}
function listTasks(tasks) {
var task_list = $("#task_list");
task_list.empty();
var autolinker = new Autolinker();
for (j in tasks) {
if (tasks[j].title == "") {
continue;
}
var item = $("<li class='mdl-list__item mdl-list__item--two-line'>");
item.attr("id", tasks[j].id);
var primary_content = $("<span class='mdl-list__item-primary-content'>");
var item_text = $("<span class='mdl-list__item-sub-title'>");
item_text.html(autolinker.link(tasks[j].title));
primary_content.append(item_text);
item.append(primary_content);
var secondary_content = $("<span class='mdl-list__item-secondary-content'>");
var btn_del = $("<a class='mdl-list__item-secondary-action' href='#'>");
btn_del.append($("<i class='material-icons'>delete</i>"));
btn_del.click(delete_item);
btn_del.appendTo(secondary_content);
secondary_content.appendTo(item);
item.appendTo(task_list);
}
selectPane("pane_list");
}
function prepareHTMLTexts() {
$("#nav_list").text(browser.i18n.getMessage("tab_list"));
$("#nav_about").text(browser.i18n.getMessage("tab_about"));
$("#btn_reset").text(browser.i18n.getMessage("reset_configuration"));
var autolinker = new Autolinker();
var message1_text = autolinker.link(browser.i18n.getMessage("about_message1"));
var message2_text = autolinker.link(browser.i18n.getMessage("about_message2"));
$("#about_message1").html(message1_text);
$("#about_message2").html(message2_text);
$("#need_authorize_message").html(autolinker.link(browser.i18n.getMessage("needAuthorizeApp")));
$("#btn_reset_config").text(browser.i18n.getMessage("reset_configuration"));
}
$(document).ready(function () {
initDrawer();
selectPane("pane_wait");
prepareHTMLTexts();
$("#btn_refresh").click(function (e) {
loadTasks();
});
$("#btn_reset").click(function (e) {
reset_configuration();
});
$("#btn_reset_config").click(function (e) {
reset_configuration();
});
loadTasks();
});