undo.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifdef 0
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #endif
  6. /**
  7. * Dialog allowing to undo the removal of single site or to completely restore
  8. * the grid's original state.
  9. */
  10. var gUndoDialog = {
  11. /**
  12. * The undo dialog's timeout in miliseconds.
  13. */
  14. HIDE_TIMEOUT_MS: 15000,
  15. /**
  16. * Contains undo information.
  17. */
  18. _undoData: null,
  19. /**
  20. * Initializes the undo dialog.
  21. */
  22. init: function() {
  23. this._undoContainer = document.getElementById("newtab-undo-container");
  24. this._undoContainer.addEventListener("click", this, false);
  25. this._undoButton = document.getElementById("newtab-undo-button");
  26. this._undoCloseButton = document.getElementById("newtab-undo-close-button");
  27. this._undoRestoreButton = document.getElementById("newtab-undo-restore-button");
  28. },
  29. /**
  30. * Shows the undo dialog.
  31. * @param aSite The site that just got removed.
  32. */
  33. show: function(aSite) {
  34. if (this._undoData)
  35. clearTimeout(this._undoData.timeout);
  36. this._undoData = {
  37. index: aSite.cell.index,
  38. wasPinned: aSite.isPinned(),
  39. blockedLink: aSite.link,
  40. timeout: setTimeout(this.hide.bind(this), this.HIDE_TIMEOUT_MS)
  41. };
  42. this._undoContainer.removeAttribute("undo-disabled");
  43. this._undoButton.removeAttribute("tabindex");
  44. this._undoCloseButton.removeAttribute("tabindex");
  45. this._undoRestoreButton.removeAttribute("tabindex");
  46. },
  47. /**
  48. * Hides the undo dialog.
  49. */
  50. hide: function() {
  51. if (!this._undoData)
  52. return;
  53. clearTimeout(this._undoData.timeout);
  54. this._undoData = null;
  55. this._undoContainer.setAttribute("undo-disabled", "true");
  56. this._undoButton.setAttribute("tabindex", "-1");
  57. this._undoCloseButton.setAttribute("tabindex", "-1");
  58. this._undoRestoreButton.setAttribute("tabindex", "-1");
  59. },
  60. /**
  61. * The undo dialog event handler.
  62. * @param aEvent The event to handle.
  63. */
  64. handleEvent: function(aEvent) {
  65. switch (aEvent.target.id) {
  66. case "newtab-undo-button":
  67. this._undo();
  68. break;
  69. case "newtab-undo-restore-button":
  70. this._undoAll();
  71. break;
  72. case "newtab-undo-close-button":
  73. this.hide();
  74. break;
  75. }
  76. },
  77. /**
  78. * Undo the last blocked site.
  79. */
  80. _undo: function() {
  81. if (!this._undoData)
  82. return;
  83. let {index, wasPinned, blockedLink} = this._undoData;
  84. gBlockedLinks.unblock(blockedLink);
  85. if (wasPinned) {
  86. gPinnedLinks.pin(blockedLink, index);
  87. }
  88. gUpdater.updateGrid();
  89. this.hide();
  90. },
  91. /**
  92. * Undo all blocked sites.
  93. */
  94. _undoAll: function() {
  95. NewTabUtils.undoAll(function() {
  96. gUpdater.updateGrid();
  97. this.hide();
  98. }.bind(this));
  99. }
  100. };
  101. gUndoDialog.init();