Source: ItemSelectorDialog.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 {Config} from 'metal-state';
  16. import toggleDisabled from './util/toggle_disabled';
  17. /**
  18. * Shows a dialog and handles the selected item.
  19. * @deprecated As of Cavanaugh (7.4.x), replaced by `openSelectionModal`
  20. */
  21. class ItemSelectorDialog extends Component {
  22. /**
  23. * Closes the dialog.
  24. */
  25. close() {
  26. Liferay.Util.getWindow(this.eventName).hide();
  27. }
  28. /**
  29. * Opens the dialog.
  30. */
  31. open() {
  32. this._currentItem = null;
  33. this._selectedItem = null;
  34. const eventName = this.eventName;
  35. const dialogConfig = this._getDialogConfig();
  36. const dialogEvents = {
  37. on: {
  38. click: (event) => {
  39. event.domEvent.preventDefault();
  40. },
  41. visibleChange: (event) => {
  42. if (!event.newVal) {
  43. this.selectedItem = this._selectedItem;
  44. this.emit('selectedItemChange', {
  45. selectedItem: this.selectedItem,
  46. });
  47. }
  48. this.emit('visibleChange', {visible: event.newVal});
  49. },
  50. },
  51. };
  52. Liferay.Util.selectEntity(
  53. {
  54. dialog: {...dialogConfig, ...dialogEvents},
  55. eventName,
  56. id: eventName,
  57. stack: !this.zIndex,
  58. title: this.title,
  59. uri: this.url,
  60. },
  61. this._onItemSelected.bind(this)
  62. );
  63. }
  64. _getDialogConfig() {
  65. const dialogConfig = {
  66. constrain: true,
  67. cssClass: this.dialogClasses,
  68. destroyOnHide: true,
  69. modal: true,
  70. zIndex: this.zIndex,
  71. };
  72. if (!this.singleSelect) {
  73. const dialogFooter = [
  74. {
  75. cssClass: 'btn-secondary close-modal',
  76. id: 'cancelButton',
  77. label: this.buttonCancelLabel,
  78. on: {
  79. click: () => {
  80. this.close();
  81. },
  82. },
  83. },
  84. {
  85. cssClass: 'btn-primary',
  86. disabled: true,
  87. id: 'addButton',
  88. label: this.buttonAddLabel,
  89. on: {
  90. click: () => {
  91. this._selectedItem = this._currentItem;
  92. this.close();
  93. },
  94. },
  95. },
  96. ];
  97. dialogConfig['toolbars.footer'] = dialogFooter;
  98. }
  99. return dialogConfig;
  100. }
  101. /**
  102. * Saves the current selected item in the dialog and disables the Add
  103. * button.
  104. *
  105. * @param {EventFacade} event The event.
  106. * @private
  107. */
  108. _onItemSelected(event) {
  109. const currentItem = event.data;
  110. if (this.singleSelect) {
  111. this._selectedItem = currentItem;
  112. this.close();
  113. }
  114. else {
  115. const dialog = Liferay.Util.getWindow(this.eventName);
  116. const addButton = dialog
  117. .getToolbar('footer')
  118. .get('boundingBox')
  119. .one('#addButton');
  120. toggleDisabled(addButton, !currentItem);
  121. }
  122. this._currentItem = currentItem;
  123. }
  124. }
  125. /**
  126. * State definition.
  127. *
  128. * @static
  129. * @type {!Object}
  130. */
  131. ItemSelectorDialog.STATE = {
  132. /**
  133. * Label for the Add button.
  134. *
  135. * @instance
  136. * @memberof ItemSelectorDialog
  137. * @type {String}
  138. */
  139. buttonAddLabel: Config.string().value(Liferay.Language.get('add')),
  140. /**
  141. * Label for the Cancel button.
  142. *
  143. * @instance
  144. * @memberof ItemSelectorDialog
  145. * @type {String}
  146. */
  147. buttonCancelLabel: Config.string().value(Liferay.Language.get('cancel')),
  148. /**
  149. * CSS classes to pass to the dialog.
  150. *
  151. * @instance
  152. * @memberof ItemSelectorDialog
  153. * @type {String}
  154. */
  155. dialogClasses: Config.string(),
  156. /**
  157. * Event name.
  158. *
  159. * @instance
  160. * @memberof ItemSelectorDialog
  161. * @type {String}
  162. */
  163. eventName: Config.string().required(),
  164. /**
  165. * The selected item(s) in the dialog.
  166. *
  167. * @instance
  168. * @memberof ItemSelectorDialog
  169. * @type {Object|Object[]}
  170. */
  171. selectedItem: Config.oneOfType([
  172. Config.object(),
  173. Config.arrayOf(Config.object()),
  174. ]),
  175. /**
  176. * Enables single selection of item.
  177. * @type {boolean}
  178. */
  179. singleSelect: Config.bool().value(false),
  180. /**
  181. * Dialog's title.
  182. *
  183. * @instance
  184. * @memberof ItemSelectorDialog
  185. * @type {String}
  186. */
  187. title: Config.string().value(Liferay.Language.get('select-file')),
  188. /**
  189. * URL that opens the dialog.
  190. *
  191. * @instance
  192. * @memberof ItemSelectorDialog
  193. * @type {String}
  194. */
  195. url: Config.string().required(),
  196. /**
  197. * Dialog's zIndex.
  198. *
  199. * @instance
  200. * @memberof ItemSelectorDialog
  201. * @type {Number}
  202. */
  203. zIndex: Config.number(),
  204. };
  205. export default ItemSelectorDialog;