contextual.toolbar.es6.js 2.2 KB

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