contextual.toolbar.es6.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @file
  3. * Attaches behaviors for the Contextual module's edit toolbar tab.
  4. */
  5. (function ($, Drupal, Backbone) {
  6. const strings = {
  7. tabbingReleased: Drupal.t('Tabbing is no longer constrained by the Contextual module.'),
  8. tabbingConstrained: Drupal.t('Tabbing is constrained to a set of @contextualsCount and the edit mode toggle.'),
  9. pressEsc: Drupal.t('Press the esc key to exit.'),
  10. };
  11. /**
  12. * Initializes a contextual link: updates its DOM, sets up model and views.
  13. *
  14. * @param {HTMLElement} context
  15. * A contextual links DOM element as rendered by the server.
  16. */
  17. function initContextualToolbar(context) {
  18. if (!Drupal.contextual || !Drupal.contextual.collection) {
  19. return;
  20. }
  21. const contextualToolbar = Drupal.contextualToolbar;
  22. contextualToolbar.model = new contextualToolbar.StateModel({
  23. // Checks whether localStorage indicates we should start in edit mode
  24. // rather than view mode.
  25. // @see Drupal.contextualToolbar.VisualView.persist
  26. isViewing: localStorage.getItem('Drupal.contextualToolbar.isViewing') !== 'false',
  27. }, {
  28. contextualCollection: Drupal.contextual.collection,
  29. });
  30. const viewOptions = {
  31. el: $('.toolbar .toolbar-bar .contextual-toolbar-tab'),
  32. model: contextualToolbar.model,
  33. strings,
  34. };
  35. new contextualToolbar.VisualView(viewOptions);
  36. new contextualToolbar.AuralView(viewOptions);
  37. }
  38. /**
  39. * Attaches contextual's edit toolbar tab behavior.
  40. *
  41. * @type {Drupal~behavior}
  42. *
  43. * @prop {Drupal~behaviorAttach} attach
  44. * Attaches contextual toolbar behavior on a contextualToolbar-init event.
  45. */
  46. Drupal.behaviors.contextualToolbar = {
  47. attach(context) {
  48. if ($('body').once('contextualToolbar-init').length) {
  49. initContextualToolbar(context);
  50. }
  51. },
  52. };
  53. /**
  54. * Namespace for the contextual toolbar.
  55. *
  56. * @namespace
  57. */
  58. Drupal.contextualToolbar = {
  59. /**
  60. * The {@link Drupal.contextualToolbar.StateModel} instance.
  61. *
  62. * @type {?Drupal.contextualToolbar.StateModel}
  63. */
  64. model: null,
  65. };
  66. }(jQuery, Drupal, Backbone));