forked from kotarf/Smart-Bookmark-Sorter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
261 lines (224 loc) · 7.28 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
$(function() {
var background_page = chrome.extension.getBackgroundPage();
$( document ).tooltip();
$( "#tabs" ).tabs({ heightStyle: "content" });
// Get api key from local storage
var key = background_page.SmartBookmarkSorter.getApiKey();
if (key === null || key === undefined) {
$('#tabs').tabs('disable', 1); // disable second tab
$('#tabs').tabs('disable', 2); // disable third tab
$('#tabs').tabs('disable', 3); // disable fourth tab
}
else {
$('#tabs').tabs('disable', 0); // disable first tab
$( "#tabs" ).tabs( "select", 2);
}
$( "#dialog_confirm_privacy" ).dialog({
resizable: false,
height:300,
width:400,
modal: true,
autoOpen: false,
show: "blind",
hide: "explode",
buttons: {
"Accept AlchemyAPI's Privacy Policy": function() {
// Save the state and the key
background_page.SmartBookmarkSorter.setApiKey(key);
$('#tabs').tabs('disable', 0); // disable first tab
// Unlock and go to the next tab
$( "#tabs" ).tabs( "enable", 1 );
$( "#tabs" ).tabs( "select", 1 );
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$( "#autocomplete_apikey" ).autocomplete({
source: []
});
$( "#button_key").button().click(function() {
key = $("#autocomplete_apikey").val();
// Test the API key to see if it is valid
background_page.SmartBookmarkSorter.alchemyKeyTest( key,
function() {
// Bring up a confirmation box noting privacy
$( "#dialog_confirm_privacy" ).dialog( "open" );
},
function() {
// Display an unhappy dialog box with the link to register
$( "#dialog_apikey" ).dialog( "open" );
}, undefined, undefined, this
);
});
$( "#dialog_apikey" ).dialog({
autoOpen: false,
show: "blind",
hide: "explode"
});
$( "#button_backup").button().click(function() {
chrome.tabs.create({
url: "chrome://bookmarks/#1",
active: true
});
$("#button_continue").button("enable");
$( "#tabs" ).tabs( "enable", 2 );
$( "#tabs" ).tabs( "enable", 3 );
$.totalStorage("bookmarksorter_backup", true);
});
$( "#button_continue").button().click(function() {
$( "#tabs" ).tabs( "select", 2 );
});
if ($.totalStorage("bookmarksorter_backup") === null) {
$("#button_continue").button("disable");
}
$( "#spinner_archivedays" ).spinner({
min: 0,
stop: function( event, ui ) {
// Set the archive days
var value = $( "#spinner_archivedays" ).spinner( "value");
background_page.SmartBookmarkSorter.setOldBookmarkDays(value);
}
});
var oldBookmarkDays = background_page.SmartBookmarkSorter.getOldBookmarkDays();
$( "#spinner_archivedays" ).spinner( "value", oldBookmarkDays);
$( "#progressbar_sorting" ).progressbar({
value: 0
});
$( "#button_sample").button().click(function() {
// Check if a sort is in progress
if(!background_page.SmartBookmarkSorter.getIsOnManualSorting()) {
// Sort a sample of bookmarks
background_page.SmartBookmarkSorter.sortSample();
}
});
$( "#button_sort").button().click(function() {
// Open a dialog box
$( "#dialog_confirm_sort" ).dialog( "open" );
});
$( "#dialog_confirm_sort" ).dialog({
resizable: false,
height:140,
modal: true,
autoOpen: false,
show: "blind",
hide: "explode",
buttons: {
"Sort all bookmarks": function() {
// Check if a sort is in progress
if(!background_page.SmartBookmarkSorter.getIsOnManualSorting()) {
// Sort all bookmarks
background_page.SmartBookmarkSorter.sortAllBookmarks();
}
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$( "#dialog_error_sort" ).dialog({
resizable: false,
height:140,
modal: true,
autoOpen: false,
});
// Get autosort settings
$ ("#button_oncreate").button().click(function() {
var isChecked = $( "#button_oncreate" ).is(':checked');
if(isChecked) {
// Set the on create flag to true
background_page.SmartBookmarkSorter.setAutoOnCreate(true);
}
else {
// Set the on create flag to false
background_page.SmartBookmarkSorter.setAutoOnCreate(false);
}
});
$ ("#button_interval").button().click(function() {
var isChecked = $( "#button_interval" ).is(':checked');
if(isChecked) {
// Set the on interval sort flag to true
background_page.SmartBookmarkSorter.setAutoInterval(true);
}
else {
// Set the on interval sort flag to false
background_page.SmartBookmarkSorter.setAutoInterval(false);
}
});
$ ("#button_prioritize").button().click(function() {
var isChecked = $( "#button_prioritize" ).is(':checked');
if(isChecked) {
// Set the on create flag to true
background_page.SmartBookmarkSorter.setAutoPrioritize(true);
}
else {
// Set the on create flag to false
background_page.SmartBookmarkSorter.setAutoPrioritize(false);
}
});
// Restore states for autosort buttons
var isOnCreate = background_page.SmartBookmarkSorter.getAutoOnCreate(),
isOnInterval = background_page.SmartBookmarkSorter.getAutoInterval(),
isPrioritize = background_page.SmartBookmarkSorter.getAutoPrioritize(),
isAutoSort = background_page.SmartBookmarkSorter.getAutoOn();
if(isOnCreate) {
$("#button_oncreate").attr("checked","checked");
$("#button_oncreate").button("refresh");
}
if(isOnInterval) {
$("#button_interval").attr("checked","checked");
$("#button_interval").button("refresh");
}
if(isPrioritize) {
$("#button_prioritize").attr("checked","checked");
$("#button_prioritize").button("refresh");
}
$ ("#button_autosort").button().click(function() {
var isChecked = $( "#button_autosort" ).is(':checked');
if(isChecked) {
// Enable automatic sort
background_page.SmartBookmarkSorter.enableAutomaticSort();
background_page.SmartBookmarkSorter.setAutoOn(true);
}
else {
// Disable automatic sort
background_page.SmartBookmarkSorter.disableAutomaticSort();
background_page.SmartBookmarkSorter.setAutoOn(false);
}
});
if(isAutoSort) {
$("#button_autosort").attr("checked","checked");
$("#button_autosort").button("refresh");
}
// Add listeners for error messages and progress messages
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
var messageSplit = message.split(",");
var messageCode = messageSplit[0];
if(messageCode === background_page.SmartBookmarkSorter.config.dailyLimitError) {
// Display an error dialog
$( "#dialog_error_sort" ).dialog( "open" );
}
else if(messageCode === background_page.SmartBookmarkSorter.config.sortBeginMsg) {
var numSorts = parseInt(messageSplit[1]);
console.log("NUMSORTS = ", numSorts);
$( "#progressbar_sorting" ).progressbar( "option", "value", 0);
$( "#progressbar_sorting" ).progressbar( "option", "max", numSorts );
}
else if(messageCode === background_page.SmartBookmarkSorter.config.sortSuccessfulMsg) {
var indexSort = parseInt(messageSplit[1]);
// getter
var value = $( "#progressbar_sorting").progressbar( "option", "value" );
// setter
$( "#progressbar_sorting" ).progressbar( "option", "value", indexSort );
}
else if(messageCode === background_page.SmartBookmarkSorter.config.sortCompleteMsg) {
// getter
var value = $( "#progressbar_sorting").progressbar( "option", "value" );
// setter
$( "#progressbar_sorting" ).progressbar( "option", "value", value + 1 );
}
});
});