Source: portal/tabs.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import toCharCode from '../util/to_char_code.es';
  15. const EVENT_SHOW_TAB = 'showTab';
  16. /**
  17. * Prepares and fires the an event that will show a tab
  18. * @param {String} namespace The portlet's namespace
  19. * @param {Array} names Names of all tabs
  20. * @param {String} id Tab id.
  21. * @param {Function} callback The callback function.
  22. */
  23. export function showTab(namespace, names, id, callback) {
  24. const namespacedId = namespace + toCharCode(id);
  25. const selectedTab = document.getElementById(namespacedId + 'TabsId');
  26. const selectedTabSection = document.getElementById(
  27. namespacedId + 'TabsSection'
  28. );
  29. if (selectedTab && selectedTabSection) {
  30. const details = {
  31. id,
  32. names,
  33. namespace,
  34. selectedTab,
  35. selectedTabSection,
  36. };
  37. if (callback && typeof callback === 'function') {
  38. callback.call(this, namespace, names, id, details);
  39. }
  40. try {
  41. Liferay.on(EVENT_SHOW_TAB, applyTabSelectionDOMChanges);
  42. Liferay.fire(EVENT_SHOW_TAB, details);
  43. }
  44. finally {
  45. Liferay.detach(EVENT_SHOW_TAB, applyTabSelectionDOMChanges);
  46. }
  47. }
  48. }
  49. /**
  50. * Applies DOM changes that represent tab selection
  51. * @param {String} namespace The portlet's namespace
  52. * @param {Array} names Names of all tabs
  53. * @param {String} id Tab id.
  54. * @param {HTMLElement} selectedTab Selected tab element
  55. * @param {HTMLElement} selectedTabSection Selected tab section element
  56. */
  57. export function applyTabSelectionDOMChanges({
  58. id,
  59. names,
  60. namespace,
  61. selectedTab,
  62. selectedTabSection,
  63. }) {
  64. const selectedTabLink = selectedTab.querySelector('a');
  65. if (selectedTab && selectedTabLink) {
  66. const activeTab = selectedTab.parentElement.querySelector('.active');
  67. if (activeTab) {
  68. activeTab.classList.remove('active');
  69. }
  70. selectedTabLink.classList.add('active');
  71. }
  72. if (selectedTabSection) {
  73. selectedTabSection.classList.remove('hide');
  74. }
  75. const tabTitle = document.getElementById(namespace + 'dropdownTitle');
  76. if (tabTitle && selectedTabLink) {
  77. tabTitle.innerHTML = selectedTabLink.textContent;
  78. }
  79. names.splice(names.indexOf(id), 1);
  80. let tabSection;
  81. for (var i = 0; i < names.length; i++) {
  82. tabSection = document.getElementById(
  83. namespace + toCharCode(names[i]) + 'TabsSection'
  84. );
  85. if (tabSection) {
  86. tabSection.classList.add('hide');
  87. }
  88. }
  89. }