StateModel.es6.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file
  3. * A Backbone Model for the state of a contextual link's trigger, list & region.
  4. */
  5. (function(Drupal, Backbone) {
  6. /**
  7. * Models the state of a contextual link's trigger, list & region.
  8. *
  9. * @constructor
  10. *
  11. * @augments Backbone.Model
  12. */
  13. Drupal.contextual.StateModel = Backbone.Model.extend(
  14. /** @lends Drupal.contextual.StateModel# */ {
  15. /**
  16. * @type {object}
  17. *
  18. * @prop {string} title
  19. * @prop {bool} regionIsHovered
  20. * @prop {bool} hasFocus
  21. * @prop {bool} isOpen
  22. * @prop {bool} isLocked
  23. */
  24. defaults: /** @lends Drupal.contextual.StateModel# */ {
  25. /**
  26. * The title of the entity to which these contextual links apply.
  27. *
  28. * @type {string}
  29. */
  30. title: '',
  31. /**
  32. * Represents if the contextual region is being hovered.
  33. *
  34. * @type {bool}
  35. */
  36. regionIsHovered: false,
  37. /**
  38. * Represents if the contextual trigger or options have focus.
  39. *
  40. * @type {bool}
  41. */
  42. hasFocus: false,
  43. /**
  44. * Represents if the contextual options for an entity are available to
  45. * be selected (i.e. whether the list of options is visible).
  46. *
  47. * @type {bool}
  48. */
  49. isOpen: false,
  50. /**
  51. * When the model is locked, the trigger remains active.
  52. *
  53. * @type {bool}
  54. */
  55. isLocked: false,
  56. },
  57. /**
  58. * Opens or closes the contextual link.
  59. *
  60. * If it is opened, then also give focus.
  61. *
  62. * @return {Drupal.contextual.StateModel}
  63. * The current contextual state model.
  64. */
  65. toggleOpen() {
  66. const newIsOpen = !this.get('isOpen');
  67. this.set('isOpen', newIsOpen);
  68. if (newIsOpen) {
  69. this.focus();
  70. }
  71. return this;
  72. },
  73. /**
  74. * Closes this contextual link.
  75. *
  76. * Does not call blur() because we want to allow a contextual link to have
  77. * focus, yet be closed for example when hovering.
  78. *
  79. * @return {Drupal.contextual.StateModel}
  80. * The current contextual state model.
  81. */
  82. close() {
  83. this.set('isOpen', false);
  84. return this;
  85. },
  86. /**
  87. * Gives focus to this contextual link.
  88. *
  89. * Also closes + removes focus from every other contextual link.
  90. *
  91. * @return {Drupal.contextual.StateModel}
  92. * The current contextual state model.
  93. */
  94. focus() {
  95. this.set('hasFocus', true);
  96. const cid = this.cid;
  97. this.collection.each(model => {
  98. if (model.cid !== cid) {
  99. model.close().blur();
  100. }
  101. });
  102. return this;
  103. },
  104. /**
  105. * Removes focus from this contextual link, unless it is open.
  106. *
  107. * @return {Drupal.contextual.StateModel}
  108. * The current contextual state model.
  109. */
  110. blur() {
  111. if (!this.get('isOpen')) {
  112. this.set('hasFocus', false);
  113. }
  114. return this;
  115. },
  116. },
  117. );
  118. })(Drupal, Backbone);