Source: PortletBase.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 Component from 'metal-component';
  15. import objectToFormData from './util/form/object_to_form_data.es';
  16. function toElementHelper(elementOrSelector) {
  17. if (typeof elementOrSelector === 'string') {
  18. elementOrSelector = document.querySelector(elementOrSelector);
  19. }
  20. return elementOrSelector;
  21. }
  22. function isString(val) {
  23. return typeof val === 'string';
  24. }
  25. /**
  26. * Provides helper functions that simplify querying the DOM for elements related
  27. * to a specific portlet.
  28. *
  29. * @abstract
  30. * @extends {Component}
  31. */
  32. class PortletBase extends Component {
  33. /**
  34. * Returns a Node List containing all the matching element nodes within the
  35. * subtrees of the root object, in tree order. If there are no matching
  36. * nodes, the method returns an empty Node List.
  37. *
  38. * @param {string} selectors A list of one or more CSS relative selectors.
  39. * @param {(string|Element|Document)=} root The root node of the search. If
  40. * not specified, the element search will start in the portlet's
  41. * root node or in the document.
  42. * @return {NodeList<Element>} A list of elements matching the selectors, in
  43. * tree order.
  44. */
  45. all(selectors, root) {
  46. root = toElementHelper(root) || this.rootNode || document;
  47. return root.querySelectorAll(
  48. this.namespaceSelectors_(
  49. this.portletNamespace || this.namespace,
  50. selectors
  51. )
  52. );
  53. }
  54. /**
  55. * Performs an HTTP POST request to the given URL with the given body.
  56. *
  57. * @deprecated As of Athanasius (7.3.x), replaced by `Liferay.Util.fetch`.
  58. * @param {!string} url The URL to send the post request to.
  59. * @param {!Object|!FormData} body The request body.
  60. * @return {Promise} A promise.
  61. */
  62. fetch(url, body) {
  63. const requestBody = this.getRequestBody_(body);
  64. // eslint-disable-next-line @liferay/portal/no-global-fetch
  65. return fetch(url, {
  66. body: requestBody,
  67. credentials: 'include',
  68. method: 'POST',
  69. });
  70. }
  71. /**
  72. * Transforms the given body into a valid <code>FormData</code> element.
  73. *
  74. * @param {!FormData|!HTMLFormElement|!Object} body The original data.
  75. * @return {FormData} The transformed form data.
  76. */
  77. getRequestBody_(body) {
  78. let requestBody;
  79. if (body instanceof FormData) {
  80. requestBody = body;
  81. }
  82. else if (body instanceof HTMLFormElement) {
  83. requestBody = new FormData(body);
  84. }
  85. else if (typeof body === 'object') {
  86. requestBody = objectToFormData(this.ns(body));
  87. }
  88. else {
  89. requestBody = body;
  90. }
  91. return requestBody;
  92. }
  93. /**
  94. * Namespaces the list of selectors, appending the portlet namespace to the
  95. * selectors of type ID. Selectors of other types remain unaltered.
  96. *
  97. * @param {string} namespace The portlet's namespace.
  98. * @param {string} selectors A list of one or more CSS relative selectors.
  99. * @protected
  100. * @return {string} The namespaced ID selectors.
  101. */
  102. namespaceSelectors_(namespace, selectors) {
  103. return selectors.replace(
  104. new RegExp('(#|\\[id=(\\"|\\\'))(?!' + namespace + ')', 'g'),
  105. '$1' + namespace
  106. );
  107. }
  108. /**
  109. * Appends the portlet's namespace to the given string or object properties.
  110. *
  111. * @param {!Object|string} obj The object or string to namespace.
  112. * @return {Object|string} An object with its properties namespaced, using
  113. * the portlet namespace or a namespaced string.
  114. */
  115. ns(obj) {
  116. return Liferay.Util.ns(this.portletNamespace || this.namespace, obj);
  117. }
  118. /**
  119. * Returns the first matching Element node within the subtrees of the
  120. * root object. If there is no matching Element, the method returns null.
  121. *
  122. * @param {string} selectors A list of one or more CSS relative selectors.
  123. * @param {(string|Element|Document)=} root The root node of the search. If
  124. * not specified, the element search will start in the portlet's
  125. * root node or in the document.
  126. * @return {Element|null} A list of the first element matching the selectors
  127. * or <code>null</code>.
  128. */
  129. one(selectors, root) {
  130. root = toElementHelper(root) || this.rootNode || document;
  131. return root.querySelector(
  132. this.namespaceSelectors_(
  133. this.portletNamespace || this.namespace,
  134. selectors
  135. )
  136. );
  137. }
  138. /**
  139. * Returns the default portlet root node element. By default, this is the
  140. * element with ID <code>p_p_id{portletNamespace}</code>.
  141. *
  142. * @protected
  143. * @return {Element} The portlet's default root node element.
  144. */
  145. rootNodeValueFn_() {
  146. return document.getElementById(
  147. `p_p_id${this.portletNamespace || this.namespace}`
  148. );
  149. }
  150. }
  151. /**
  152. * State definition.
  153. *
  154. * @ignore
  155. * @static
  156. * @type {!Object}
  157. */
  158. PortletBase.STATE = {
  159. /**
  160. * Portlet's namespace.
  161. *
  162. * @deprecated As of Judson (7.1.x)
  163. * @instance
  164. * @memberof PortletBase
  165. * @type {string}
  166. */
  167. namespace: {
  168. validator: isString,
  169. },
  170. /**
  171. * Portlet's namespace.
  172. *
  173. * @instance
  174. * @memberof PortletBase
  175. * @type {string}
  176. */
  177. portletNamespace: {
  178. validator: isString,
  179. },
  180. /**
  181. * Portlet's root node element.
  182. *
  183. * @instance
  184. * @memberof PortletBase
  185. * @type {Element}
  186. */
  187. rootNode: {
  188. setter: toElementHelper,
  189. valueFn: 'rootNodeValueFn_',
  190. },
  191. };
  192. export default PortletBase;