Source: portlet/portlet.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 fetch from '../util/fetch.es';
  15. import objectToFormData from '../util/form/object_to_form_data.es';
  16. import getPortletId from '../util/get_portlet_id';
  17. import createPortletURL from '../util/portlet_url/create_portlet_url.es';
  18. import register from './register.es';
  19. /**
  20. * Minimizes portlet
  21. * @param {String} portletSelector Portlet container selector
  22. * @param {HTMLElement} trigger Trigger element
  23. * @param {Object} options Additional options
  24. */
  25. export function minimizePortlet(portletSelector, trigger, options) {
  26. options = {
  27. doAsUserId: themeDisplay.getDoAsUserIdEncoded(),
  28. plid: themeDisplay.getPlid(),
  29. ...options,
  30. };
  31. const portlet = document.querySelector(portletSelector);
  32. if (portlet) {
  33. const content = portlet.querySelector('.portlet-content-container');
  34. if (content) {
  35. const minimized = content.classList.contains('d-none');
  36. if (minimized) {
  37. content.classList.remove('d-none');
  38. portlet.classList.remove('portlet-minimized');
  39. }
  40. else {
  41. content.classList.add('d-none');
  42. portlet.classList.add('portlet-minimized');
  43. }
  44. if (trigger) {
  45. const title = minimized
  46. ? Liferay.Language.get('minimize')
  47. : Liferay.Language.get('restore');
  48. trigger.setAttribute('alt', title);
  49. trigger.setAttribute('title', title);
  50. const triggerText = trigger.querySelector('.taglib-text-icon');
  51. if (triggerText) {
  52. triggerText.innerHTML = title;
  53. }
  54. const icon = trigger.querySelector('i');
  55. if (icon) {
  56. icon.classList.remove('icon-minus', 'icon-resize-vertical');
  57. if (minimized) {
  58. icon.classList.add('icon-minus');
  59. icon.classList.remove('icon-resize-vertical');
  60. }
  61. else {
  62. icon.classList.add('icon-resize-vertical');
  63. icon.classList.remove('icon-minus');
  64. }
  65. }
  66. }
  67. const portletId = getPortletId(portlet.id);
  68. const formData = objectToFormData({
  69. cmd: 'minimize',
  70. doAsUserId: options.doAsUserId,
  71. p_auth: Liferay.authToken,
  72. p_l_id: options.plid,
  73. p_p_id: portletId,
  74. p_p_restore: minimized,
  75. p_v_l_s_g_id: themeDisplay.getSiteGroupId(),
  76. });
  77. fetch(themeDisplay.getPathMain() + '/portal/update_layout', {
  78. body: formData,
  79. method: 'POST',
  80. })
  81. .then((response) => {
  82. if (response.ok && minimized) {
  83. const params = {
  84. doAsUserId: options.doAsUserId,
  85. p_l_id: options.plid,
  86. p_p_boundary: false,
  87. p_p_id: portletId,
  88. p_p_isolated: true,
  89. };
  90. fetch(
  91. createPortletURL(
  92. themeDisplay.getPathMain() +
  93. '/portal/render_portlet',
  94. params
  95. )
  96. )
  97. .then((response) => response.text())
  98. .then((response) => {
  99. const range = document.createRange();
  100. range.selectNode(portlet);
  101. portlet.innerHTML = '';
  102. const fragment = range.createContextualFragment(
  103. response
  104. );
  105. portlet.appendChild(fragment);
  106. })
  107. .catch((error) => {
  108. if (process.env.NODE_ENV === 'development') {
  109. console.error(error);
  110. }
  111. });
  112. }
  113. })
  114. .catch((error) => {
  115. if (process.env.NODE_ENV === 'development') {
  116. console.error(error);
  117. }
  118. });
  119. }
  120. }
  121. }
  122. export default {
  123. register,
  124. };