cells.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * This class manages a cell's DOM node (not the actually cell content, a site).
  8. * It's mostly read-only, i.e. all manipulation of both position and content
  9. * aren't handled here.
  10. */
  11. function Cell(aGrid, aNode) {
  12. this._grid = aGrid;
  13. this._node = aNode;
  14. this._node._newtabCell = this;
  15. // Register drag-and-drop event handlers.
  16. ["dragenter", "dragover", "dragexit", "drop"].forEach(function(aType) {
  17. this._node.addEventListener(aType, this, false);
  18. }, this);
  19. }
  20. Cell.prototype = {
  21. /**
  22. * The grid.
  23. */
  24. _grid: null,
  25. /**
  26. * The cell's DOM node.
  27. */
  28. get node() { return this._node; },
  29. /**
  30. * The cell's offset in the grid.
  31. */
  32. get index() {
  33. let index = this._grid.cells.indexOf(this);
  34. // Cache this value, overwrite the getter.
  35. Object.defineProperty(this, "index", {value: index, enumerable: true});
  36. return index;
  37. },
  38. /**
  39. * The previous cell in the grid.
  40. */
  41. get previousSibling() {
  42. let prev = this.node.previousElementSibling;
  43. prev = prev && prev._newtabCell;
  44. // Cache this value, overwrite the getter.
  45. Object.defineProperty(this, "previousSibling", {value: prev, enumerable: true});
  46. return prev;
  47. },
  48. /**
  49. * The next cell in the grid.
  50. */
  51. get nextSibling() {
  52. let next = this.node.nextElementSibling;
  53. next = next && next._newtabCell;
  54. // Cache this value, overwrite the getter.
  55. Object.defineProperty(this, "nextSibling", {value: next, enumerable: true});
  56. return next;
  57. },
  58. /**
  59. * The site contained in the cell, if any.
  60. */
  61. get site() {
  62. let firstChild = this.node.firstElementChild;
  63. return firstChild && firstChild._newtabSite;
  64. },
  65. /**
  66. * Checks whether the cell contains a pinned site.
  67. * @return Whether the cell contains a pinned site.
  68. */
  69. containsPinnedSite: function() {
  70. let site = this.site;
  71. return site && site.isPinned();
  72. },
  73. /**
  74. * Checks whether the cell contains a site (is empty).
  75. * @return Whether the cell is empty.
  76. */
  77. isEmpty: function() {
  78. return !this.site;
  79. },
  80. /**
  81. * Handles all cell events.
  82. */
  83. handleEvent: function(aEvent) {
  84. // We're not responding to external drag/drop events
  85. // when our parent window is in private browsing mode.
  86. if (inPrivateBrowsingMode() && !gDrag.draggedSite)
  87. return;
  88. if (aEvent.type != "dragexit" && !gDrag.isValid(aEvent))
  89. return;
  90. switch (aEvent.type) {
  91. case "dragenter":
  92. aEvent.preventDefault();
  93. gDrop.enter(this, aEvent);
  94. break;
  95. case "dragover":
  96. aEvent.preventDefault();
  97. break;
  98. case "dragexit":
  99. gDrop.exit(this, aEvent);
  100. break;
  101. case "drop":
  102. aEvent.preventDefault();
  103. gDrop.drop(this, aEvent);
  104. break;
  105. }
  106. }
  107. };