utils.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. const { Cu } = require('chrome');
  6. const { getMostRecentBrowserWindow } = require('sdk/window/utils');
  7. const { fromIterator } = require('sdk/util/array');
  8. const BUILTIN_SIDEBAR_MENUITEMS = exports.BUILTIN_SIDEBAR_MENUITEMS = [
  9. 'menu_socialSidebar',
  10. 'menu_historySidebar',
  11. 'menu_bookmarksSidebar'
  12. ];
  13. function isSidebarShowing(window) {
  14. window = window || getMostRecentBrowserWindow();
  15. let sidebar = window.document.getElementById('sidebar-box');
  16. return !sidebar.hidden;
  17. }
  18. exports.isSidebarShowing = isSidebarShowing;
  19. function getSidebarMenuitems(window) {
  20. window = window || getMostRecentBrowserWindow();
  21. return fromIterator(window.document.querySelectorAll('#viewSidebarMenu menuitem'));
  22. }
  23. exports.getSidebarMenuitems = getSidebarMenuitems;
  24. function getExtraSidebarMenuitems() {
  25. let menuitems = getSidebarMenuitems();
  26. return menuitems.filter(function(mi) {
  27. return BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) < 0;
  28. });
  29. }
  30. exports.getExtraSidebarMenuitems = getExtraSidebarMenuitems;
  31. function makeID(id) {
  32. return 'jetpack-sidebar-' + id;
  33. }
  34. exports.makeID = makeID;
  35. function simulateCommand(ele) {
  36. let window = ele.ownerDocument.defaultView;
  37. let { document } = window;
  38. var evt = document.createEvent('XULCommandEvent');
  39. evt.initCommandEvent('command', true, true, window,
  40. 0, false, false, false, false, null);
  41. ele.dispatchEvent(evt);
  42. }
  43. exports.simulateCommand = simulateCommand;
  44. function simulateClick(ele) {
  45. let window = ele.ownerDocument.defaultView;
  46. let { document } = window;
  47. let evt = document.createEvent('MouseEvents');
  48. evt.initMouseEvent('click', true, true, window,
  49. 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  50. ele.dispatchEvent(evt);
  51. }
  52. exports.simulateClick = simulateClick;
  53. // OSX and Windows exhibit different behaviors when 'checked' is false,
  54. // so compare against the consistent 'true'. See bug 894809.
  55. function isChecked(node) {
  56. return node.getAttribute('checked') === 'true';
  57. };
  58. exports.isChecked = isChecked;