bookmarkableTabset.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* eslint-disable
  2. max-len,
  3. no-undef,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. define(['base'], function(App) {
  15. App.directive('bookmarkableTabset', ($location, _) => ({
  16. restrict: 'A',
  17. require: 'tabset',
  18. link(scope, el, attrs, tabset) {
  19. const _makeActive = function(hash) {
  20. if (hash != null && hash !== '') {
  21. const matchingTab = _.find(
  22. tabset.tabs,
  23. tab => tab.bookmarkableTabId === hash
  24. )
  25. if (matchingTab != null) {
  26. matchingTab.select()
  27. return el.children()[0].scrollIntoView({ behavior: 'smooth' })
  28. }
  29. }
  30. }
  31. return scope.$applyAsync(function() {
  32. // for page load
  33. const hash = $location.hash()
  34. _makeActive(hash)
  35. // for links within page to a tab
  36. // this needs to be within applyAsync because there could be a link
  37. // within a tab to another tab
  38. const linksToTabs = document.querySelectorAll('.link-to-tab')
  39. const _clickLinkToTab = event =>
  40. _makeActive(event.currentTarget.getAttribute('href').replace('#', ''))
  41. if (linksToTabs) {
  42. return Array.from(linksToTabs).map(link =>
  43. link.addEventListener('click', _clickLinkToTab)
  44. )
  45. }
  46. })
  47. }
  48. }))
  49. return App.directive('bookmarkableTab', $location => ({
  50. restrict: 'A',
  51. require: 'tab',
  52. link(scope, el, attrs, tab) {
  53. const tabScope = el.isolateScope()
  54. const tabId = attrs.bookmarkableTab
  55. if (tabScope != null && tabId != null && tabId !== '') {
  56. tabScope.bookmarkableTabId = tabId
  57. return tabScope.$watch('active', function(isActive, wasActive) {
  58. if (isActive && !wasActive && $location.hash() !== tabId) {
  59. return $location.hash(tabId)
  60. }
  61. })
  62. }
  63. }
  64. }))
  65. })