contextual.toolbar.js 2.1 KB

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