ToolbarModel.es6.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @file
  3. * A Backbone Model for the toolbar.
  4. */
  5. (function (Backbone, Drupal) {
  6. /**
  7. * Backbone model for the toolbar.
  8. *
  9. * @constructor
  10. *
  11. * @augments Backbone.Model
  12. */
  13. Drupal.toolbar.ToolbarModel = Backbone.Model.extend(/** @lends Drupal.toolbar.ToolbarModel# */{
  14. /**
  15. * @type {object}
  16. *
  17. * @prop activeTab
  18. * @prop activeTray
  19. * @prop isOriented
  20. * @prop isFixed
  21. * @prop areSubtreesLoaded
  22. * @prop isViewportOverflowConstrained
  23. * @prop orientation
  24. * @prop locked
  25. * @prop isTrayToggleVisible
  26. * @prop height
  27. * @prop offsets
  28. */
  29. defaults: /** @lends Drupal.toolbar.ToolbarModel# */{
  30. /**
  31. * The active toolbar tab. All other tabs should be inactive under
  32. * normal circumstances. It will remain active across page loads. The
  33. * active item is stored as an ID selector e.g. '#toolbar-item--1'.
  34. *
  35. * @type {string}
  36. */
  37. activeTab: null,
  38. /**
  39. * Represents whether a tray is open or not. Stored as an ID selector e.g.
  40. * '#toolbar-item--1-tray'.
  41. *
  42. * @type {string}
  43. */
  44. activeTray: null,
  45. /**
  46. * Indicates whether the toolbar is displayed in an oriented fashion,
  47. * either horizontal or vertical.
  48. *
  49. * @type {bool}
  50. */
  51. isOriented: false,
  52. /**
  53. * Indicates whether the toolbar is positioned absolute (false) or fixed
  54. * (true).
  55. *
  56. * @type {bool}
  57. */
  58. isFixed: false,
  59. /**
  60. * Menu subtrees are loaded through an AJAX request only when the Toolbar
  61. * is set to a vertical orientation.
  62. *
  63. * @type {bool}
  64. */
  65. areSubtreesLoaded: false,
  66. /**
  67. * If the viewport overflow becomes constrained, isFixed must be true so
  68. * that elements in the trays aren't lost off-screen and impossible to
  69. * get to.
  70. *
  71. * @type {bool}
  72. */
  73. isViewportOverflowConstrained: false,
  74. /**
  75. * The orientation of the active tray.
  76. *
  77. * @type {string}
  78. */
  79. orientation: 'horizontal',
  80. /**
  81. * A tray is locked if a user toggled it to vertical. Otherwise a tray
  82. * will switch between vertical and horizontal orientation based on the
  83. * configured breakpoints. The locked state will be maintained across page
  84. * loads.
  85. *
  86. * @type {bool}
  87. */
  88. locked: false,
  89. /**
  90. * Indicates whether the tray orientation toggle is visible.
  91. *
  92. * @type {bool}
  93. */
  94. isTrayToggleVisible: true,
  95. /**
  96. * The height of the toolbar.
  97. *
  98. * @type {number}
  99. */
  100. height: null,
  101. /**
  102. * The current viewport offsets determined by {@link Drupal.displace}. The
  103. * offsets suggest how a module might position is components relative to
  104. * the viewport.
  105. *
  106. * @type {object}
  107. *
  108. * @prop {number} top
  109. * @prop {number} right
  110. * @prop {number} bottom
  111. * @prop {number} left
  112. */
  113. offsets: {
  114. top: 0,
  115. right: 0,
  116. bottom: 0,
  117. left: 0,
  118. },
  119. },
  120. /**
  121. * @inheritdoc
  122. *
  123. * @param {object} attributes
  124. * Attributes for the toolbar.
  125. * @param {object} options
  126. * Options for the toolbar.
  127. *
  128. * @return {string|undefined}
  129. * Returns an error message if validation failed.
  130. */
  131. validate(attributes, options) {
  132. // Prevent the orientation being set to horizontal if it is locked, unless
  133. // override has not been passed as an option.
  134. if (attributes.orientation === 'horizontal' && this.get('locked') && !options.override) {
  135. return Drupal.t('The toolbar cannot be set to a horizontal orientation when it is locked.');
  136. }
  137. },
  138. });
  139. }(Backbone, Drupal));