ui.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. color : [ 64, 64, 64, 255 ],
  27. title : "Process this page with SingleFile",
  28. path : DEFAULT_ICON_PATH
  29. };
  30. var badgeStates = [], currentBarProgress = -1, currentProgress = -1, tabs = {}, badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  31. function refreshBadge(tabId) {
  32. function refreshTabBadge(tabId) {
  33. function refreshBadgeProperty(property, fn) {
  34. var badgeState;
  35. var tabData = tabs[tabId], confObject = {
  36. tabId : tabId
  37. };
  38. if (!badgeStates[tabId])
  39. badgeStates[tabId] = [];
  40. badgeState = badgeStates[tabId];
  41. if (tabData && tabData[property]) {
  42. if (badgeState[property] != tabData[property]) {
  43. badgeState[property] = tabData[property];
  44. confObject[property] = tabData[property];
  45. fn(confObject);
  46. }
  47. } else {
  48. if (badgeState[property] != badgeConfig[property]) {
  49. badgeState[property] = badgeConfig[property];
  50. confObject[property] = badgeConfig[property];
  51. fn(confObject);
  52. }
  53. }
  54. }
  55. refreshBadgeProperty("text", chrome.browserAction.setBadgeText);
  56. refreshBadgeProperty("color", chrome.browserAction.setBadgeBackgroundColor);
  57. refreshBadgeProperty("title", chrome.browserAction.setTitle);
  58. refreshBadgeProperty("path", chrome.browserAction.setIcon);
  59. }
  60. if (tabId)
  61. refreshTabBadge(tabId);
  62. else
  63. chrome.tabs.getAllInWindow(null, function(tabs) {
  64. tabs.forEach(function(tab) {
  65. refreshTabBadge(tab.id);
  66. });
  67. });
  68. }
  69. singlefile.ui.notifySavedPage = function(processed, filename) {
  70. var notificationArchiving = webkitNotifications.createNotification(DEFAULT_ICON_PATH, "SingleFile", processed ? (filename + " is saved") : ("Error: "
  71. + filename + " cannot be saved"));
  72. notificationArchiving.show();
  73. if (processed)
  74. setTimeout(function() {
  75. notificationArchiving.cancel();
  76. }, 3000);
  77. };
  78. singlefile.ui.notifyProcessInit = function(tabId) {
  79. var tabData = {
  80. id : tabId,
  81. text : "...",
  82. color : [ 2, 147, 20, 255 ],
  83. title : "Initialize process...",
  84. path : DEFAULT_ICON_PATH,
  85. processing : true
  86. };
  87. tabs[tabId] = tabData;
  88. refreshBadge(tabId);
  89. };
  90. singlefile.ui.notifyProcessStart = function(tabId, processingPagesCount) {
  91. delete tabs[tabId].text;
  92. delete tabs[tabId].title;
  93. delete tabs[tabId].path;
  94. tabs[tabId].color = [ 4, 229, 36, 255 ];
  95. badgeConfig.text = "" + processingPagesCount;
  96. refreshBadge();
  97. };
  98. singlefile.ui.notifyProcessError = function(tabId) {
  99. delete tabs[tabId].processing;
  100. tabs[tabId].color = [ 229, 4, 12, 255 ];
  101. tabs[tabId].text = "ERR";
  102. refreshBadge(tabId);
  103. };
  104. singlefile.ui.notifyProcessEnd = function(tabId, processingPagesCount) {
  105. tabs[tabId].text = "OK";
  106. delete tabs[tabId].processing;
  107. badgeConfig.text = "" + (processingPagesCount || "");
  108. if (!processingPagesCount) {
  109. currentBarProgress = -1;
  110. currentProgress = -1;
  111. delete tabs[tabId].title;
  112. badgeConfig = JSON.parse(JSON.stringify(DEFAULT_BADGE_CONFIG));
  113. }
  114. refreshBadge();
  115. };
  116. singlefile.ui.notifyProcessProgress = function(index, maxIndex) {
  117. var barProgress, progress;
  118. if (maxIndex) {
  119. progress = Math.min(100, Math.floor((index / maxIndex) * 100));
  120. if (currentProgress != progress) {
  121. currentProgress = progress;
  122. badgeConfig.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  123. barProgress = Math.floor((index / maxIndex) * 15);
  124. if (currentBarProgress != barProgress) {
  125. currentBarProgress = barProgress;
  126. badgeConfig.path = "../resources/icon_19_wait" + barProgress + ".png";
  127. }
  128. refreshBadge();
  129. }
  130. }
  131. };
  132. singlefile.ui.notifyTabRemoved = function(tabId) {
  133. delete tabs[tabId];
  134. delete badgeStates[tabId];
  135. };
  136. singlefile.ui.notifyProcessable = function(tabId, processable, reset) {
  137. if (!processable) {
  138. tabs[tabId] = {
  139. path : DEFAULT_PASSIVE_ICON_PATH,
  140. title : "SingleFile cannot process this page"
  141. };
  142. } else if (reset && tabs[tabId] && !tabs[tabId].processing)
  143. delete tabs[tabId];
  144. refreshBadge(tabId);
  145. };
  146. })();