escapeAdmin.es6.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * @file
  3. * Replaces the home link in toolbar with a back to site link.
  4. */
  5. (function ($, Drupal, drupalSettings) {
  6. const pathInfo = drupalSettings.path;
  7. const escapeAdminPath = sessionStorage.getItem('escapeAdminPath');
  8. const windowLocation = window.location;
  9. // Saves the last non-administrative page in the browser to be able to link
  10. // back to it when browsing administrative pages. If there is a destination
  11. // parameter there is not need to save the current path because the page is
  12. // loaded within an existing "workflow".
  13. if (!pathInfo.currentPathIsAdmin && !/destination=/.test(windowLocation.search)) {
  14. sessionStorage.setItem('escapeAdminPath', windowLocation);
  15. }
  16. /**
  17. * Replaces the "Home" link with "Back to site" link.
  18. *
  19. * Back to site link points to the last non-administrative page the user
  20. * visited within the same browser tab.
  21. *
  22. * @type {Drupal~behavior}
  23. *
  24. * @prop {Drupal~behaviorAttach} attach
  25. * Attaches the replacement functionality to the toolbar-escape-admin element.
  26. */
  27. Drupal.behaviors.escapeAdmin = {
  28. attach() {
  29. const $toolbarEscape = $('[data-toolbar-escape-admin]').once('escapeAdmin');
  30. if ($toolbarEscape.length && pathInfo.currentPathIsAdmin) {
  31. if (escapeAdminPath !== null) {
  32. $toolbarEscape.attr('href', escapeAdminPath);
  33. }
  34. else {
  35. $toolbarEscape.text(Drupal.t('Home'));
  36. }
  37. }
  38. },
  39. };
  40. }(jQuery, Drupal, drupalSettings));