Drupal investigation

contextual.toolbar.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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);