ui.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_19.png";
  23. var DEFAULT_PASSIVE_ICON_PATH = "../resources/icon_19_forbidden.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
  36. });
  37. chrome.browserAction.setBadgeBackgroundColor({
  38. tabId : tabId,
  39. color : tabsData[tabId] && tabsData[tabId].bgColor || badgeConfig.bgColor
  40. });
  41. chrome.browserAction.setTitle({
  42. tabId : tabId,
  43. title : tabsData[tabId] && tabsData[tabId].title || badgeConfig.title
  44. });
  45. chrome.browserAction.setIcon({
  46. tabId : tabId,
  47. path : tabsData[tabId] && tabsData[tabId].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_ICON_PATH,
  75. processing : true
  76. };
  77. tabsData[tabId] = tabData;
  78. refreshBadge(tabId);
  79. };
  80. singlefile.ui.notifyProcessStart = function(tabId, processingPagesCount) {
  81. delete tabsData[tabId].text;
  82. delete tabsData[tabId].title;
  83. delete tabsData[tabId].iconPath;
  84. tabsData[tabId].bgColor = [ 4, 229, 36, 255 ];
  85. badgeConfig.text = "" + processingPagesCount;
  86. refreshBadge();
  87. };
  88. singlefile.ui.notifyProcessError = function(tabId) {
  89. delete tabsData[tabId].processing;
  90. tabsData[tabId].bgColor = [ 229, 4, 12, 255 ];
  91. tabsData[tabId].text = "ERR";
  92. refreshBadge(tabId);
  93. };
  94. singlefile.ui.notifyProcessEnd = function(tabId, processingPagesCount) {
  95. tabsData[tabId].text = "OK";
  96. delete tabsData[tabId].processing;
  97. badgeConfig.text = "" + (processingPagesCount || "");
  98. if (!processingPagesCount) {
  99. currentBarProgress = -1;
  100. currentProgress = -1;
  101. delete tabsData[tabId].title;
  102. badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  103. }
  104. refreshBadge();
  105. };
  106. singlefile.ui.notifyProcessProgress = function(index, maxIndex) {
  107. var barProgress, progress;
  108. if (maxIndex) {
  109. progress = Math.min(100, Math.floor((index / maxIndex) * 100));
  110. if (currentProgress != progress) {
  111. currentProgress = progress;
  112. badgeConfig.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  113. barProgress = Math.floor((index / maxIndex) * 15);
  114. if (currentBarProgress != barProgress) {
  115. currentBarProgress = barProgress;
  116. badgeConfig.iconPath = "../resources/icon_19_wait" + barProgress + ".png";
  117. }
  118. refreshBadge();
  119. }
  120. }
  121. };
  122. singlefile.ui.notifyTabRemoved = function(tabId) {
  123. delete tabsData[tabId];
  124. };
  125. singlefile.ui.notifyProcessable = function(tabId, processable, reset) {
  126. if (!processable) {
  127. tabsData[tabId] = {
  128. iconPath : DEFAULT_PASSIVE_ICON_PATH,
  129. title : "SingleFile cannot process this page"
  130. };
  131. } else if (reset && tabsData[tabId] && !tabsData[tabId].processing)
  132. delete tabsData[tabId];
  133. refreshBadge(tabId);
  134. };
  135. })();