Source: site-navigation-item-selector-web/src/main/resources/META-INF/resources/js/SelectSiteNavigationMenuItem.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 'frontend-taglib/cards_treeview/CardsTreeview.es';
  15. import 'metal';
  16. import 'metal-component';
  17. import {PortletBase} from 'frontend-js-web';
  18. import Soy from 'metal-soy';
  19. import {Config} from 'metal-state';
  20. import templates from './SelectSiteNavigationMenuItem.soy';
  21. /**
  22. * SelectSiteNavigationMenuItem
  23. *
  24. * This component shows a list of available site navigation menu item to select
  25. * and allows to filter them by searching.
  26. */
  27. class SelectSiteNavigationMenuItem extends PortletBase {
  28. /**
  29. * Filters deep nested nodes based on a filtering value
  30. *
  31. * @type {Array.<Object>} nodes
  32. * @type {String} filterVAlue
  33. * @protected
  34. */
  35. filterSiblingNodes_(nodes, filterValue) {
  36. let filteredNodes = [];
  37. nodes.forEach(node => {
  38. if (node.name.toLowerCase().indexOf(filterValue) !== -1) {
  39. filteredNodes.push(node);
  40. }
  41. if (node.children) {
  42. filteredNodes = filteredNodes.concat(
  43. this.filterSiblingNodes_(node.children, filterValue)
  44. );
  45. }
  46. });
  47. return filteredNodes;
  48. }
  49. /**
  50. * Searchs for nodes by name based on a filtering value
  51. *
  52. * @param {!Event} event
  53. * @protected
  54. */
  55. searchNodes_(event) {
  56. if (!this.originalNodes) {
  57. this.originalNodes = this.nodes;
  58. } else {
  59. this.nodes = this.originalNodes;
  60. }
  61. const filterValue = event.delegateTarget.value.toLowerCase();
  62. if (filterValue !== '') {
  63. this.viewType = 'flat';
  64. this.nodes = this.filterSiblingNodes_(this.nodes, filterValue);
  65. } else {
  66. this.viewType = 'tree';
  67. }
  68. }
  69. /**
  70. * Fires item selector save event on selected node change
  71. *
  72. * @param {!Event} event
  73. * @protected
  74. */
  75. selectedNodeChange_(event) {
  76. var node = event.newVal[0];
  77. if (node) {
  78. var data = {
  79. selectSiteNavigationMenuItemId: node.id,
  80. selectSiteNavigationMenuItemName: node.name
  81. };
  82. Liferay.Util.getOpener().Liferay.fire(this.itemSelectorSaveEvent, {
  83. data
  84. });
  85. }
  86. }
  87. }
  88. SelectSiteNavigationMenuItem.STATE = {
  89. /**
  90. * Event name to fire on node selection
  91. * @type {String}
  92. */
  93. itemSelectorSaveEvent: Config.string(),
  94. /**
  95. * List of nodes
  96. * @type {Array.<Object>}
  97. */
  98. nodes: Config.array().required(),
  99. /**
  100. * Theme images root path
  101. * @type {String}
  102. */
  103. pathThemeImages: Config.string().required(),
  104. /**
  105. * Type of view to render. Accepted values are 'tree' and 'flat'
  106. * @type {String}
  107. */
  108. viewType: Config.string().value('tree')
  109. };
  110. Soy.register(SelectSiteNavigationMenuItem, templates);
  111. export default SelectSiteNavigationMenuItem;