Source: CategorySelector.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 Soy from 'metal-soy';
  16. import {Config} from 'metal-state';
  17. import templates from './CategorySelector.soy';
  18. /**
  19. * CategorySelector is a temporary Component wrapping the existing
  20. * AUI module liferay-commerce-frontend-asset-categories-selector
  21. */
  22. class CategorySelector extends Component {
  23. /**
  24. * Updates the calculated rule fields for `queryValues` and `categoryIdsTitles`
  25. * every time a new category entry is added or removed to the selection
  26. * @protected
  27. */
  28. onEntriesChanged_() {
  29. this.rule.categoryIdsTitles = this.categoriesSelector_.entries.values.map(
  30. (element) => element.value
  31. );
  32. this.rule.queryValues = this.categoriesSelector_.entries.keys.join(',');
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. rendered() {
  38. AUI().use('liferay-commerce-frontend-asset-categories-selector', () => {
  39. const config = {
  40. categoryIds: this.rule.queryValues || '',
  41. categoryTitles: this.rule.categoryIdsTitles || [],
  42. contentBox: this.element,
  43. eventName: this.eventName,
  44. groupIds: this.groupIds,
  45. hiddenInput: `#${this.refs.hiddenInput.getAttribute('id')}`,
  46. portletURL: this.categorySelectorURL,
  47. vocabularyIds: this.vocabularyIds,
  48. };
  49. this.categoriesSelector_ = new Liferay.AssetTaglibCategoriesSelector(
  50. config
  51. );
  52. const entries = this.categoriesSelector_.entries;
  53. entries.after('add', this.onEntriesChanged_, this);
  54. entries.after('remove', this.onEntriesChanged_, this);
  55. this.categoriesSelector_.render();
  56. this.element.parentNode.removeAttribute('tabindex');
  57. });
  58. }
  59. }
  60. CategorySelector.STATE = {
  61. /**
  62. * Portlet ID used for selecting categories.
  63. * For this component it's required because is the only way
  64. * to select categories.
  65. */
  66. categorySelectorURL: Config.string().value(''),
  67. /**
  68. * Name of the event that will be dispatched when the
  69. * category selector dialog is closed
  70. */
  71. eventName: Config.string().value(''),
  72. /**
  73. * Array of group ids (sites) where categories will be searched.
  74. * It defaults to an empty array, which is the current site.
  75. */
  76. groupIds: Config.string().value(''),
  77. /**
  78. * Id of the hidden input used to pass the selected categories
  79. */
  80. hiddenInput: Config.string().value(''),
  81. /**
  82. * Number used for avoiding conflicts between different
  83. * instances of the component/portlet.
  84. */
  85. index: Config.number().value(0),
  86. /**
  87. * String used for avoiding conflicts between different
  88. * instances of the component/portlet.
  89. */
  90. namespace: Config.string().value(''),
  91. /**
  92. * Existing information of the form.
  93. * @prop {string[]} queryValues Categories that are already selected.
  94. * This property is updated as the user selects new categories.
  95. * @prop {string[]} categoryIdsTitles Titles of the categories that are
  96. * already selected. It is kept in sync with `queryValues`
  97. */
  98. rule: Config.object().value({}),
  99. /**
  100. * Ids of the vocabularies parents of the selected categories.
  101. * Vocabularies are super groups which group a set of categories.
  102. */
  103. vocabularyIds: Config.string().value(''),
  104. };
  105. Soy.register(CategorySelector, templates);
  106. export {CategorySelector};
  107. export default CategorySelector;