]> git.wh0rd.org Git - chrome-ext/tabs-backup.git/blob - popup.js
migrate from localStorage to chrome.storage.local
[chrome-ext/tabs-backup.git] / popup.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Add event listeners once the DOM has fully loaded by listening for the
6 // `DOMContentLoaded` event on the document, and adding your listeners to
7 // specific elements when it triggers.
8 document.addEventListener('DOMContentLoaded', function () {
9         //document.querySelector('button').addEventListener('click', testf);
10         document.getElementById("menuItem_backupNow").addEventListener('click', menu_backupNow);
11         //document.getElementById("menuItem_restoreNow").addEventListener('click', menu_restoreNow);
12         document.getElementById("menuItem_options").addEventListener('click', menu_ShowOptions);
13         document.getElementById("menuItem_showOlderBackups").addEventListener('click', menu_ShowOlderBackups);
14         document.getElementById("menuItem_showAdvancedView").addEventListener('click', menu_ShowAdvancedView);
15
16
17         initBackupsList (false /*showAll*/);
18
19         /*$(function() {
20         $( "#dialog-confirm" ).dialog({
21             resizable: false,
22             height:140,
23             modal: true,
24             buttons: {
25                 "Delete all items": function() {
26                     $( this ).dialog( "close" );
27                 },
28                 Cancel: function() {
29                     $( this ).dialog( "close" );
30                 }
31             }
32         });
33     });*/
34 });
35
36 function menu_ShowOptions () {
37         chrome.runtime.openOptionsPage();
38 }
39
40 function menu_ShowAdvancedView() {
41         chrome.tabs.create({url:chrome.extension.getURL("advanced.html")});
42 }
43
44 function menu_ShowOlderBackups () {
45         // save current scrollbar position
46         //var scrollPosition = $(document).scrollTop();
47
48         // Re-initialize backups list
49         //initBackupsList(true /*showAll*/, function () {
50                 // Update the scrollbar position to the saved one
51         //      $(document).scrollTop(scrollPosition);
52         //});
53
54
55
56         var oldestVisibleBackupItem = $(".backupItem:last");
57         var oldestVisibleBackupItemId = oldestVisibleBackupItem.attr('id');
58         // the id is in the form 'div_' + backupName
59         var oldestVisibleBackupName = oldestVisibleBackupItemId.substring(4);
60
61
62         var backupsDiv = document.getElementById ('backupsDiv');
63
64         chrome.storage.local.get(null, function(items) {
65                 var backupsList = [];
66                 if(items.backups_list) {
67                         backupsList = items.backups_list;
68                 }
69
70                 var shouldInsert = false;
71
72                 for (var i = backupsList.length-1; i >= 0; i--) {
73                         var backupName = backupsList[i];
74                         var backupObj = items[backupName];
75
76                         if (!backupObj) {
77                                 continue;
78                         }
79
80                         if (backupObj.isAutomatic === undefined) {
81                                 backupObj.isAutomatic = true;
82                         }
83
84                         if (oldestVisibleBackupName == backupName) {
85                                 // found last visible item, start inserting
86                                 shouldInsert = true;
87                         } else {
88
89                                 if (shouldInsert) {
90                                         insertBackupItem(backupName, backupObj, false /*insertAtBeginning*/, false /*doAnimation*/);
91                                 }
92                         }
93                 }
94
95                 // Hide the "show all" link
96                 $("#showOlderBackupsDiv").hide();
97         });
98
99 }
100
101 function insertBackupItem (backupName, backupObj, insertAtBeginning, doAnimation) {
102         var backupsDiv = document.getElementById ('backupsDiv');
103
104         var restoreButtonId = 'restoreSelectedBackup_' + backupName;
105         var deleteButtonId = 'deleteSelectedBackup_' + backupName;
106         var divId = 'div_' + backupName;
107
108         var elem = document.createElement("div");
109         if (doAnimation) {
110                 // start with hidden element (only if we are doing the animation later)
111                 elem.style.cssText = 'display: none';
112         }
113
114         elem.id = divId;
115         elem.className = 'backupItem';
116         elem.innerHTML = '<div class="backupItemWrapper">' +
117                                         '<div class="backupItemContent">' +
118                                          '<div class="backupItemTitle">' + backupName + "</div>" +
119                                          '<div class="backupItemDetails">' +
120                                                 'Nr. Windows:<span class="backupItemDetailsNr">' + backupObj.windows.length + '</span><br />' +
121                                                 'Nr. Tabs:<span class="backupItemDetailsNr">' + backupObj.totNumTabs + '</span>' +
122                                          '</div>' +
123
124                                          '<div class="backupItemToolbar">' +
125                                                 '<a id="' + restoreButtonId + '"><img src="icon_48.png" title="Open Windows & Tabs" style="border: 0; width: 24px; height: 24px" /></a>' +
126                                                 '<a id="' + deleteButtonId + '"><img src="trash_48.png" title="Delete Backup" style="border: 0; width: 22px; height: 22px" /></a>' +
127                                          '</div>' +
128
129                                          '<div class="backupItemFooter">' +
130                                                 (backupObj.isAutomatic ? '<span class="backupItemFooterAutoBackup">AUTO BACKUP</span>' :
131                                                                                                  '<span class="backupItemFooterManualBackup">MANUAL BACKUP</span>') +
132                                          '</div>' +
133                                          '</div>' +
134                                          '</div>';
135
136         //elem.innerHTML += "# Windows: " +
137         //backupsDiv.appendChild(elem);
138
139         var restoreFuncHandler = (function(backupName) {
140                 return function(event) {
141                         bootbox.confirm("Open Windows & Tabs of backup '" + backupName + "'?", function(confirmed) {
142                                 if (confirmed) {
143                                         chrome.runtime.getBackgroundPage((bg) => bg.restoreNow(backupName));
144                                 }
145                         });
146
147                         /*if (!confirm("Open Windows & Tabs of backup '" + backupName + "'?")) {
148                                 return;
149                         }*/
150
151
152                 };
153         })(backupName);
154
155         var deleteFuncHandler = function() {
156
157
158                         bootbox.confirm("Delete backup '" + backupName + "'?", function(confirmed) {
159                                 if (confirmed) {
160                                         chrome.runtime.getBackgroundPage((bg) => {
161                                                 bg.deleteBackup(backupName, () => updateStorageInfo());
162                                         });
163
164                                         //if (elem.parentNode) {
165                                         //  elem.parentNode.removeChild(elem);
166                                         //
167                                         removeBackupItemDiv (backupName);
168                                 }
169                         });
170
171
172                 };
173
174         if (insertAtBeginning && backupsDiv.childNodes.length > 0) {
175                 // some items already exist
176                 var firstNode = backupsDiv.childNodes[0];
177                 backupsDiv.insertBefore(elem, firstNode);
178         } else {
179                 backupsDiv.appendChild(elem);
180         }
181
182
183         document.getElementById(restoreButtonId).addEventListener('click', restoreFuncHandler);
184         document.getElementById(deleteButtonId).addEventListener('click', deleteFuncHandler);
185
186         if (doAnimation) {
187                 var divIdJQ = jq(divId);
188                 $(divIdJQ).slideDown(1000);
189         }
190         /*obj.animate({ height: 1, opacity: 1 }, {
191             duration: 1000,
192             complete: function(){obj.css('display', 'block');}
193         });*/
194
195         //obj.fadeIn(2000);
196         //obj.slideDown();
197
198         //$(divId).display = 'none';
199         //$(divId).slideUp();
200         //$(divId).fadeOut(1000);
201         /*var bkp = $("backupsDiv");
202         bkp.remove();
203         var div = $(divId);
204         var a = 0;*/
205         /*setTimeout( function() {
206                 var obj = $("#" + divId);
207                 obj.fadeIn();
208
209         }, 1000 );*/
210 }
211
212 function jq(myid) {
213    return '#' + myid.replace(/(:|\.| )/g,'\\$1');
214  }
215
216 function removeBackupItemDiv (backupName) {
217         var divId = 'div_' + backupName;
218         var divIdClean = jq(divId);
219         var obj = $(divIdClean);
220         //obj.fadeOut();
221         //obj.slideUp();
222         obj.animate({ height: 0, opacity: 0 }, {
223             duration: 1000,
224             complete: function(){obj.remove();}
225         });
226
227         //var backupItemDiv = document.getElementById (divId);
228         //if (backupItemDiv.parentNode) {
229          // backupItemDiv.parentNode.removeChild(backupItemDiv);
230         //}
231 }
232
233 function initBackupsList(showAll, callback) {
234         var backupsDiv = document.getElementById ('backupsDiv');
235         //var node = backupsDiv.childNodes[0];
236         backupsDiv.innerHTML = '';
237         //backupsDiv.style = 'display: none';
238         //$("#backupsDiv").html("");
239         /*while (backupsDiv.hasChildNodes()) {
240                 backupsDiv.removeChild(backupsDiv.lastChild);
241         }*/
242
243         $("#showOlderBackupsDiv").hide();
244
245         if (!showAll) {
246                 $("#backupsDiv").hide();
247         }
248
249         chrome.storage.local.get(null, function(items) {
250                 var backupsList = [];
251                 if(items.backups_list) {
252                         backupsList = items.backups_list;
253                 }
254
255                 var numInsertedItems = 0;
256                 for (var i = backupsList.length-1; i >= 0; i--) {
257                 //for (var i = 0; i < backupsList.length; i++) {
258                         var backupName = backupsList[i];
259                         var backupObj = items[backupName];
260
261                         if (!backupObj) {
262                                 continue;
263                         }
264
265                         if (backupObj.isAutomatic === undefined) {
266                                 backupObj.isAutomatic = true;
267                         }
268
269                         if (!showAll) {
270                                 if (numInsertedItems >= 10) {
271                                         $("#showOlderBackupsDiv").show();
272                                         break;
273                                 }
274                         }
275
276                         insertBackupItem(backupName, backupObj, false /*insertAtBeginning*/, false /*doAnimation*/);
277
278                         numInsertedItems++;
279                 }
280
281                 if (!showAll) {
282                         $("#backupsDiv").slideDown();
283                 }
284
285                 if (callback) {
286                         callback();
287                 }
288
289         });
290
291         updateStorageInfo();
292
293 }
294
295 function updateStorageInfo() {
296         chrome.storage.local.getBytesInUse(null, function(bytesInUse) {
297                 var storageText;
298                 if (bytesInUse < 1024) {
299                         storageText = bytesInUse.toFixed(2) + " bytes";
300                 } else if (bytesInUse < 1024 * 1024) {
301                         storageText = (bytesInUse / 1024).toFixed(2)  + " KiB";
302                 } else {
303                         storageText = (bytesInUse / (1024 * 1024)).toFixed(2) + " MiB";
304                 }
305
306                 var storageSpan = document.getElementById("storageSpan");
307                 storageSpan.innerHTML = storageText;
308         });
309 }
310
311 var lastTimeBackupNowClicked = 0;
312
313 function menu_backupNow() {
314         // Ignore clicks if less than 1 second has passed since last click (avoids rapid useless backups)
315         if (lastTimeBackupNowClicked != 0) {
316                 var diffTime = Math.abs(new Date().getTime() - lastTimeBackupNowClicked);
317                 if (diffTime < 1000) {
318                         return;
319                 }
320         }
321
322         lastTimeBackupNowClicked = new Date().getTime();
323
324         chrome.runtime.getBackgroundPage((bg) => bg.backupNowManual(function(success, backupName, backupObj) {
325                 if (success) {
326                         //updateBackupsList();
327                         insertBackupItem (backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/);
328                         updateStorageInfo();
329
330                         //bootbox.alert("Backup successfully created!");
331                 } else {
332                         alert('An error occured while creating the backup..');
333                 }
334         }));
335
336 }
337
338 function menu_restoreNow() {
339         chrome.runtime.getBackgroundPage((bg) => bg.restoreNow('full_backup'));
340 }