profiles.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. (function() {
  2. $script.ready('om-state', updateMenuByState);
  3. $script.ready('om-page-info', updateMenuByPageInfo);
  4. $script.ready(['om-state', 'om-page-info'], updateMenuByStateAndPageInfo);
  5. var profileTemplate = document.getElementById('js-profile-tpl')
  6. .cloneNode(true);
  7. profileTemplate.removeAttribute('id');
  8. var iconForProfileType = {
  9. 'DirectProfile': 'glyphicon-transfer',
  10. 'SystemProfile': 'glyphicon-off',
  11. 'AutoDetectProfile': 'glyphicon-file',
  12. 'FixedProfile': 'glyphicon-globe',
  13. 'PacProfile': 'glyphicon-file',
  14. 'VirtualProfile': 'glyphicon-question-sign',
  15. 'RuleListProfile': 'glyphicon-list',
  16. 'SwitchProfile': 'glyphicon-retweet',
  17. };
  18. var orderForType = {
  19. 'FixedProfile': -2000,
  20. 'PacProfile': -1000,
  21. 'VirtualProfile': 1000,
  22. 'SwitchProfile': 2000,
  23. 'RuleListProfile': 3000,
  24. };
  25. return;
  26. function updateMenuByState() {
  27. var state = OmegaPopup.state;
  28. if (state.proxyNotControllable) {
  29. location.href = 'proxy_not_controllable.html';
  30. return;
  31. }
  32. addProfilesItems(state);
  33. $script.done('om-profile-items');
  34. updateOtherItems(state);
  35. }
  36. function compareProfile(a, b) {
  37. var diff;
  38. diff = (orderForType[a.profileType] | 0) - (orderForType[b.profileType] | 0);
  39. if (diff !== 0) {
  40. return diff;
  41. }
  42. if (a.name === b.name) {
  43. return 0;
  44. } else if (a.name < b.name) {
  45. return -1;
  46. } else {
  47. return 1;
  48. }
  49. }
  50. function updateMenuByPageInfo() {
  51. var info = OmegaPopup.pageInfo;
  52. if (info && info.errorCount > 0) {
  53. document.querySelector('.om-reqinfo').classList.remove('om-hidden');
  54. var text = OmegaTargetPopup.getMessage('popup_requestErrorCount',
  55. [info.errorCount]);
  56. document.querySelector('.om-reqinfo-text').textContent = text;
  57. }
  58. }
  59. function updateMenuByStateAndPageInfo() {
  60. var state = OmegaPopup.state;
  61. var info = OmegaPopup.pageInfo;
  62. if (state.showExternalProfile && state.externalProfile &&
  63. (!info || !info.errorCount)) {
  64. showMenuForExternalProfile(state);
  65. }
  66. if (!info || !info.url) return updateOtherItems(null);
  67. document.querySelector('.om-page-domain').textContent = info.domain;
  68. OmegaPopup.showTempRuleDropdown = showTempRuleDropdown;
  69. $script.done('om-dropdowns');
  70. }
  71. function showMenuForExternalProfile(state) {
  72. var profile = state.externalProfile;
  73. profile.name = OmegaTargetPopup.getMessage('popup_externalProfile')
  74. var profileDisp = createMenuItemForProfile(profile);
  75. var link = profileDisp.querySelector('a');
  76. link.id = 'js-external';
  77. link.addEventListener('click', function() {
  78. location.href = '../popup.html#!external';
  79. });
  80. if (state.currentProfileName === '') {
  81. profileDisp.classList.add('om-effective');
  82. }
  83. var reqInfo = document.querySelector('.om-reqinfo');
  84. reqInfo.parentElement.insertBefore(profileDisp, reqInfo);
  85. }
  86. function showTempRuleDropdown() {
  87. var tempRuleItem = document.querySelector('.om-nav-temprule');
  88. toggleDropdown(tempRuleItem, createTempRuleDropdown);
  89. document.getElementById('js-temprule').focus();
  90. }
  91. function updateOtherItems(state) {
  92. var hasValidResults = state && state.validResultProfiles &&
  93. state.validResultProfiles.length;
  94. if (!hasValidResults || !state.currentProfileCanAddRule) {
  95. document.querySelector('.om-nav-addrule').classList.add('om-hidden');
  96. document.getElementById('js-addrule').href = '#';
  97. }
  98. if (!hasValidResults) {
  99. document.querySelector('.om-nav-temprule').classList.add('om-hidden');
  100. document.getElementById('js-temprule').href = '#';
  101. }
  102. }
  103. var isValidResultProfile = {};
  104. validResultProfiles.forEach(function(name) {
  105. isValidResultProfile['+' + name] = true;
  106. });
  107. function addProfilesItems(state) {
  108. var systemProfileDisp = document.getElementById('js-system');
  109. var directProfileDisp = document.getElementById('js-direct');
  110. var currentProfileClass = 'om-active';
  111. if (state.isSystemProfile) {
  112. systemProfileDisp.parentElement.classList.add('om-active');
  113. currentProfileClass = 'om-effective';
  114. }
  115. if (state.currentProfileName === 'direct') {
  116. directProfileDisp.parentElement.classList.add(currentProfileClass);
  117. }
  118. systemProfileDisp.setAttribute('title',
  119. state.availableProfiles['+system'].desc);
  120. directProfileDisp.setAttribute('title',
  121. state.availableProfiles['+direct'].desc);
  122. var profilesEnd = document.getElementById('js-profiles-end');
  123. var profilesContainer = profilesEnd.parentElement;
  124. var profileCount = 0;
  125. var charCodeUnderscore = '_'.charCodeAt(0)
  126. var profiles = Object.keys(state.availableProfiles).map(function(key) {
  127. return state.availableProfiles[key];
  128. }).sort(compareProfile);
  129. profiles.forEach(function(profile) {
  130. if (profile.builtin) return;
  131. if (profile.name.charCodeAt(0) === charCodeUnderscore) return;
  132. profileCount++;
  133. var profileDisp = createMenuItemForProfile(profile,
  134. state.availableProfiles);
  135. var link = profileDisp.querySelector('a');
  136. link.id = 'js-profile-' + profileCount;
  137. link.addEventListener('click', function() {
  138. $script.ready('om-main', function() {
  139. OmegaPopup.applyProfile(profile.name);
  140. });
  141. });
  142. if (profile.name === state.currentProfileName) {
  143. profileDisp.classList.add(currentProfileClass);
  144. }
  145. if (profile.validResultProfiles) {
  146. profileDisp.classList.add('om-has-dropdown');
  147. link.classList.add('om-has-edit');
  148. var toggle = document.createElement('div');
  149. toggle.classList.add('om-edit-toggle');
  150. var icon = document.createElement('span');
  151. icon.setAttribute('class', 'glyphicon glyphicon-chevron-down');
  152. toggle.appendChild(icon);
  153. toggle.addEventListener('click', function(e) {
  154. e.stopPropagation();
  155. e.preventDefault();
  156. toggleDropdown(profileDisp,
  157. createDefaultProfileDropdown.bind(profileDisp, profile));
  158. });
  159. link.appendChild(toggle);
  160. }
  161. profilesContainer.insertBefore(profileDisp, profilesEnd);
  162. });
  163. }
  164. function createMenuItemForProfile(profile, profiles) {
  165. var profileDisp = profileTemplate.cloneNode(true);
  166. var text = OmegaTargetPopup.getMessage('profile_' + profile.name) ||
  167. profile.name;
  168. if (profile.defaultProfileName) {
  169. text += ' [' + profile.defaultProfileName + ']';
  170. }
  171. profileDisp.querySelector('.om-profile-name').textContent = text;
  172. var targetProfile = profile;
  173. if (profile.profileType === 'VirtualProfile') {
  174. targetProfile = profiles['+' + profile.defaultProfileName];
  175. }
  176. profileDisp.setAttribute('title',
  177. targetProfile.desc || targetProfile.name || '');
  178. var iconClass = iconForProfileType[targetProfile.profileType];
  179. var icon = profileDisp.querySelector('.glyphicon');
  180. icon.setAttribute('class', 'glyphicon ' + iconClass)
  181. icon.style.color = targetProfile.color;
  182. if (targetProfile !== profile) {
  183. icon.classList.add('om-virtual-profile-icon');
  184. }
  185. return profileDisp;
  186. }
  187. function toggleDropdown(container, createDropdown) {
  188. if (!container.classList.contains('om-dropdown-loaded')) {
  189. var dropdown = createDropdown();
  190. dropdown.classList.add('om-dropdown');
  191. container.appendChild(dropdown);
  192. container.classList.add('om-dropdown-loaded');
  193. }
  194. if (container.classList.contains('om-open')) {
  195. container.classList.remove('om-open');
  196. } else {
  197. container.classList.add('om-open');
  198. }
  199. }
  200. function createTempRuleDropdown() {
  201. var ul = document.createElement('ul');
  202. var state = OmegaPopup.state;
  203. var pageInfo = OmegaPopup.pageInfo;
  204. var profiles = state.validResultProfiles.map(function(name) {
  205. return state.availableProfiles['+' + name];
  206. }).sort(compareProfile);
  207. profiles.forEach(function(profile) {
  208. if (profile.name.indexOf('__') === 0) return;
  209. if ((profile.name === OmegaPopup.state.currentProfileName) &&
  210. (!pageInfo.tempRuleProfileName) &&
  211. (state.validResultProfiles.length > 1)
  212. ) return;
  213. var li = createMenuItemForProfile(profile, state.availableProfiles);
  214. var link = li.querySelector('a');
  215. link.addEventListener('click', function() {
  216. $script.ready('om-main', function() {
  217. OmegaPopup.addTempRule(pageInfo.domain, profile.name);
  218. });
  219. });
  220. if (profile.name === pageInfo.tempRuleProfileName) {
  221. li.classList.add('om-active');
  222. }
  223. ul.appendChild(li);
  224. });
  225. return ul;
  226. }
  227. function createDefaultProfileDropdown(profile) {
  228. var ul = document.createElement('ul');
  229. var state = OmegaPopup.state;
  230. var profiles = profile.validResultProfiles.map(function(name) {
  231. return state.availableProfiles['+' + name];
  232. }).sort(compareProfile);
  233. profiles.forEach(function(resultProfile) {
  234. if (resultProfile.name.indexOf('__') === 0) return;
  235. if ((resultProfile === profile.currentProfileName) &&
  236. (profile.validResultProfiles.length > 1)
  237. ) return;
  238. var li = createMenuItemForProfile(resultProfile, state.availableProfiles);
  239. var link = li.querySelector('a');
  240. link.addEventListener('click', function() {
  241. $script.ready('om-main', function() {
  242. OmegaPopup.setDefaultProfile(profile.name, resultProfile.name);
  243. });
  244. });
  245. if (resultProfile.name === profile.currentProfileName) {
  246. li.classList.add('om-active');
  247. }
  248. ul.appendChild(li);
  249. });
  250. return ul;
  251. }
  252. })();