ContentPages.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2019 Red Hat, Inc. and/or its affiliates.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import * as React from "../../common/keycloak/web_modules/react.js";
  17. import { Route, Switch } from "../../common/keycloak/web_modules/react-router-dom.js";
  18. import { NavItem, NavExpandable } from "../../common/keycloak/web_modules/@patternfly/react-core.js";
  19. import { Msg } from "./widgets/Msg.js";
  20. import { PageNotFound } from "./content/page-not-found/PageNotFound.js";
  21. import { ForbiddenPage } from "./content/forbidden-page/ForbiddenPage.js";
  22. ;
  23. export function isModulePageDef(item) {
  24. return item.modulePath !== undefined;
  25. }
  26. export function isExpansion(contentItem) {
  27. return contentItem.content !== undefined;
  28. }
  29. function groupId(group) {
  30. return 'grp-' + group;
  31. }
  32. function itemId(group, item) {
  33. return 'grp-' + group + '_itm-' + item;
  34. }
  35. function isChildOf(parent, child) {
  36. for (var item of parent.content) {
  37. if (isExpansion(item) && isChildOf(item, child)) return true;
  38. if (parent.groupId === child.groupId) return true;
  39. }
  40. return false;
  41. }
  42. function createNavItems(activePage, contentParam, groupNum) {
  43. if (typeof content === 'undefined') return React.createElement(React.Fragment, null);
  44. const links = contentParam.map(item => {
  45. const navLinkId = `nav-link-${item.id}`;
  46. if (isExpansion(item)) {
  47. return React.createElement(NavExpandable, {
  48. id: navLinkId,
  49. groupId: item.groupId,
  50. key: item.groupId,
  51. title: Msg.localize(item.label, item.labelParams),
  52. isExpanded: isChildOf(item, activePage)
  53. }, createNavItems(activePage, item.content, groupNum + 1));
  54. } else {
  55. const page = item;
  56. return React.createElement(NavItem, {
  57. id: navLinkId,
  58. groupId: item.groupId,
  59. itemId: item.itemId,
  60. key: item.itemId,
  61. to: '#/' + page.path,
  62. isActive: activePage.itemId === item.itemId,
  63. type: "button"
  64. }, Msg.localize(page.label, page.labelParams));
  65. }
  66. });
  67. return React.createElement(React.Fragment, null, links);
  68. }
  69. export function makeNavItems(activePage) {
  70. console.log({
  71. activePage
  72. });
  73. return createNavItems(activePage, content, 0);
  74. }
  75. function setIds(contentParam, groupNum) {
  76. if (typeof contentParam === 'undefined') return groupNum;
  77. let expansionGroupNum = groupNum;
  78. for (let i = 0; i < contentParam.length; i++) {
  79. const item = contentParam[i];
  80. if (isExpansion(item)) {
  81. item.itemId = itemId(groupNum, i);
  82. expansionGroupNum = expansionGroupNum + 1;
  83. item.groupId = groupId(expansionGroupNum);
  84. expansionGroupNum = setIds(item.content, expansionGroupNum);
  85. console.log('currentGroup=' + expansionGroupNum);
  86. } else {
  87. item.groupId = groupId(groupNum);
  88. item.itemId = itemId(groupNum, i);
  89. }
  90. }
  91. ;
  92. return expansionGroupNum;
  93. }
  94. export function initGroupAndItemIds() {
  95. setIds(content, 0);
  96. console.log({
  97. content
  98. });
  99. } // get rid of Expansions and put all PageDef items into a single array
  100. export function flattenContent(pageDefs) {
  101. const flat = [];
  102. for (let item of pageDefs) {
  103. if (isExpansion(item)) {
  104. flat.push(...flattenContent(item.content));
  105. } else {
  106. flat.push(item);
  107. }
  108. }
  109. return flat;
  110. }
  111. export function makeRoutes() {
  112. if (typeof content === 'undefined') return React.createElement("span", null);
  113. const pageDefs = flattenContent(content);
  114. const routes = pageDefs.map(page => {
  115. if (isModulePageDef(page)) {
  116. const node = React.createElement(page.module[page.componentName], {
  117. 'pageDef': page
  118. });
  119. return React.createElement(Route, {
  120. key: page.itemId,
  121. path: '/' + page.path,
  122. exact: true,
  123. render: () => node
  124. });
  125. } else {
  126. const pageDef = page;
  127. return React.createElement(Route, {
  128. key: page.itemId,
  129. path: '/' + page.path,
  130. exact: true,
  131. component: pageDef.component
  132. });
  133. }
  134. });
  135. return React.createElement(Switch, null, routes, React.createElement(Route, {
  136. path: "/forbidden",
  137. component: ForbiddenPage
  138. }), React.createElement(Route, {
  139. component: PageNotFound
  140. }));
  141. }
  142. //# sourceMappingURL=ContentPages.js.map