Source: AutoField.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 './CategorySelector.es';
  15. import './TagSelector.es';
  16. import Component from 'metal-component';
  17. import Soy from 'metal-soy';
  18. import templates from './AutoField.soy';
  19. const DEFAULT_RULE = {
  20. queryContains: true,
  21. type: 'assetTags',
  22. };
  23. /**
  24. * AutoField
  25. *
  26. */
  27. class AutoField extends Component {
  28. /**
  29. * @inheritDoc
  30. */
  31. created() {
  32. this.on('rulesChanged', this.onRulesChanged_);
  33. }
  34. /**
  35. * Adds a new rule of type Tags (by default) to the current list of rules.
  36. * @protected
  37. */
  38. addRule_() {
  39. this.rules = this.rules.concat(DEFAULT_RULE);
  40. }
  41. /**
  42. * Updates a given rule when the user changes the type of selection (from Tags
  43. * to Categories) that wants to apply to it
  44. * @param {Event} event
  45. * @protected
  46. */
  47. changeSelector_(event) {
  48. const itemIndex = event.delegateTarget.getAttribute('data-item-index');
  49. const rules = this.rules;
  50. rules[itemIndex] = {
  51. queryAndOperator: 'all',
  52. queryContains: true,
  53. type: event.target.value,
  54. };
  55. this.rules = rules;
  56. }
  57. /**
  58. * Deletes a rule from the current list. This change can not be undone. Changes
  59. * made to the deleted rule will be lost.
  60. * @param {Event} event
  61. * @protected
  62. */
  63. deleteRule_(event) {
  64. const itemIndex = event.delegateTarget.getAttribute('data-rule-id');
  65. const list = this.rules;
  66. list.splice(itemIndex, 1);
  67. }
  68. /**
  69. * Updates the queryLogicIndexes whenever the list of rules change. The value
  70. * of this property is as follow:
  71. * - rules: [{}, {}, {}]
  72. * - queryLogicIndexes: "0,1,2";
  73. */
  74. onRulesChanged_() {
  75. this.queryLogicIndexes = Object.keys(this.rules).toString();
  76. }
  77. }
  78. AutoField.STATE = {
  79. /**
  80. * Array of group (sites) ids where the information is going
  81. * to be fetched. This parementer is passed by to the child
  82. * components being rendered as rules.
  83. */
  84. groupIds: {
  85. value: [],
  86. },
  87. /**
  88. * List of indices (rules) that must be sent to the server.
  89. * @see onRulesChanged_ method por more information.
  90. */
  91. queryLogicIndexes: {
  92. value: '0',
  93. },
  94. /**
  95. * Array of rules being rendered as children. Each rule
  96. * represents a step on the filtering process, being either
  97. * a TagSelector or a CategorySelector.
  98. * @type {array}
  99. */
  100. rules: {
  101. value: [DEFAULT_RULE],
  102. },
  103. };
  104. Soy.register(AutoField, templates);
  105. export {AutoField};
  106. export default AutoField;