ui.js 5.0 KB

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