ui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2011 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. (function() {
  21. singlefile.ui = {};
  22. var DEFAULT_ICON_PATH = "../resources/icon_48.png";
  23. var DEFAULT_PASSIVE_ICON_PATH = "../resources/icon_48_passive.png";
  24. var DEFAULT_BADGE_CONFIG = {
  25. text : "",
  26. bgColor : [ 64, 64, 64, 255 ],
  27. title : "Process this page with SingleFile",
  28. iconPath : DEFAULT_ICON_PATH
  29. };
  30. var currentBarProgress = -1, currentProgress = -1, tabsData = {}, badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  31. function refreshBadge(tabId) {
  32. function refreshTabBadge(tabId) {
  33. chrome.browserAction.setBadgeText({
  34. tabId : tabId,
  35. text : tabsData[tabId] ? tabsData[tabId].text || badgeConfig.text : badgeConfig.text
  36. });
  37. chrome.browserAction.setBadgeBackgroundColor({
  38. tabId : tabId,
  39. color : tabsData[tabId] ? tabsData[tabId].bgColor || badgeConfig.bgColor : badgeConfig.bgColor
  40. });
  41. chrome.browserAction.setTitle({
  42. tabId : tabId,
  43. title : tabsData[tabId] ? tabsData[tabId].title || badgeConfig.title : badgeConfig.title
  44. });
  45. chrome.browserAction.setIcon({
  46. tabId : tabId,
  47. path : tabsData[tabId] ? tabsData[tabId].iconPath || badgeConfig.iconPath : badgeConfig.iconPath
  48. });
  49. }
  50. if (tabId)
  51. refreshTabBadge(tabId);
  52. else
  53. chrome.tabs.getAllInWindow(null, function(tabs) {
  54. tabs.forEach(function(tab) {
  55. refreshTabBadge(tab.id);
  56. });
  57. });
  58. }
  59. singlefile.ui.notifySavedPage = function(processed, filename) {
  60. var notificationArchiving = webkitNotifications.createNotification(DEFAULT_ICON_PATH, "SingleFile", processed ? (filename + " is saved") : ("Error: "
  61. + filename + " cannot be saved"));
  62. notificationArchiving.show();
  63. if (processed)
  64. setTimeout(function() {
  65. notificationArchiving.cancel();
  66. }, 3000);
  67. };
  68. singlefile.ui.notifyProcessInit = function(tabId) {
  69. var tabData = {
  70. id : tabId,
  71. text : "...",
  72. bgColor : [ 2, 147, 20, 255 ],
  73. title : "Initialize process...",
  74. iconPath : DEFAULT_PASSIVE_ICON_PATH
  75. };
  76. tabsData[tabId] = tabData;
  77. refreshBadge(tabId);
  78. };
  79. singlefile.ui.notifyProcessStart = function(tabId, processingPagesCount) {
  80. delete tabsData[tabId].text;
  81. delete tabsData[tabId].title;
  82. delete tabsData[tabId].iconPath;
  83. tabsData[tabId].bgColor = [ 4, 229, 36, 255 ];
  84. badgeConfig.text = "" + processingPagesCount;
  85. refreshBadge();
  86. };
  87. singlefile.ui.notifyProcessError = function(tabId) {
  88. tabsData[tabId].bgColor = [ 229, 4, 12, 255 ];
  89. tabsData[tabId].text = "ERR";
  90. refreshBadge(tabId);
  91. };
  92. singlefile.ui.notifyProcessEnd = function(tabId, processingPagesCount) {
  93. tabsData[tabId].text = "OK";
  94. badgeConfig.text = "" + (processingPagesCount || "");
  95. if (!processingPagesCount) {
  96. currentBarProgress = -1;
  97. currentProgress = -1;
  98. delete tabsData[tabId].title;
  99. badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  100. }
  101. refreshBadge();
  102. };
  103. singlefile.ui.notifyProcessProgress = function(index, maxIndex) {
  104. var barProgress, progress;
  105. if (maxIndex) {
  106. progress = Math.min(100, Math.floor((index / maxIndex) * 100));
  107. if (currentProgress != progress) {
  108. currentProgress = progress;
  109. badgeConfig.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  110. barProgress = Math.floor((index / maxIndex) * 9);
  111. if (currentBarProgress != barProgress) {
  112. currentBarProgress = barProgress;
  113. badgeConfig.iconPath = "../resources/icon_48_wait" + barProgress + ".png";
  114. }
  115. refreshBadge();
  116. }
  117. }
  118. };
  119. singlefile.ui.notifyTabRemoved = function(tabId) {
  120. delete tabsData[tabId];
  121. };
  122. singlefile.ui.notifyProcessable = function(tabId, processable, reset) {
  123. if (!processable) {
  124. tabsData[tabId] = {
  125. iconPath : DEFAULT_PASSIVE_ICON_PATH,
  126. title : "SingleFile cannot process this page"
  127. };
  128. refreshBadge(tabId);
  129. } else if (reset)
  130. delete tabsData[tabId];
  131. };
  132. })();