ui.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // TODO : getAllInWindow is deprecated
  61. if (tabId)
  62. refreshTabBadge(tabId);
  63. else
  64. chrome.tabs.getAllInWindow(null, function(tabs) {
  65. tabs.forEach(function(tab) {
  66. refreshTabBadge(tab.id);
  67. });
  68. });
  69. }
  70. singlefile.ui.notifyProcessInit = function(tabId) {
  71. var tabData = {
  72. id : tabId,
  73. text : "...",
  74. color : [ 2, 147, 20, 255 ],
  75. title : "Initialize process...",
  76. path : DEFAULT_ICON_PATH,
  77. processing : true
  78. };
  79. tabs[tabId] = tabData;
  80. refreshBadge(tabId);
  81. };
  82. singlefile.ui.notifyProcessStart = function(tabId, processingPagesCount) {
  83. delete tabs[tabId].text;
  84. delete tabs[tabId].title;
  85. delete tabs[tabId].path;
  86. tabs[tabId].color = [ 4, 229, 36, 255 ];
  87. badgeConfig.text = "" + processingPagesCount;
  88. refreshBadge();
  89. };
  90. singlefile.ui.notifyProcessError = function(tabId) {
  91. delete tabs[tabId].processing;
  92. tabs[tabId].color = [ 229, 4, 12, 255 ];
  93. tabs[tabId].text = "ERR";
  94. refreshBadge(tabId);
  95. };
  96. singlefile.ui.notifyProcessEnd = function(tabId, processingPagesCount, displayNotification, displayBanner, url, title) {
  97. var params = encodeURIComponent(url) + "&" + encodeURIComponent(title);
  98. if (displayNotification)
  99. webkitNotifications.createHTMLNotification("/pages/notification.html?" + params).show();
  100. if (displayBanner)
  101. chrome.tabs.sendRequest(tabId, {
  102. displayBanner : true,
  103. url : chrome.extension.getURL("/pages/banner.html") + "?" + params
  104. });
  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. })();