admin/tool/usertours/amd/src/managesteps.js

  1. /**
  2. * Step management code.
  3. *
  4. * @module tool_usertours/managesteps
  5. * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
  6. */
  7. import {prefetchStrings} from 'core/prefetch';
  8. import {getString} from 'core/str';
  9. import {confirm as confirmModal} from 'core/notification';
  10. /**
  11. * Handle step management actions.
  12. *
  13. * @param {Event} e
  14. * @private
  15. */
  16. const removeStepHandler = e => {
  17. const deleteButton = e.target.closest('[data-action="delete"]');
  18. if (deleteButton) {
  19. e.preventDefault();
  20. removeStepFromLink(deleteButton.href);
  21. }
  22. };
  23. /**
  24. * Handle removal of a step with confirmation.
  25. *
  26. * @param {string} targetUrl
  27. * @private
  28. */
  29. const removeStepFromLink = targetUrl => {
  30. confirmModal(
  31. getString('confirmstepremovaltitle', 'tool_usertours'),
  32. getString('confirmstepremovalquestion', 'tool_usertours'),
  33. getString('yes', 'core'),
  34. getString('no', 'core'),
  35. () => {
  36. window.location = targetUrl;
  37. }
  38. );
  39. };
  40. /**
  41. * Set up the step management handlers.
  42. */
  43. export const setup = () => {
  44. prefetchStrings('tool_usertours', [
  45. 'confirmstepremovaltitle',
  46. 'confirmstepremovalquestion',
  47. ]);
  48. prefetchStrings('core', [
  49. 'yes',
  50. 'no',
  51. ]);
  52. document.querySelector('body').addEventListener('click', removeStepHandler);
  53. };