node-utils.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2018-2022 Elyra Authors
  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. function toCommonProperties(items: any) {
  17. let commonProperties: any = {
  18. current_parameters: {},
  19. parameters: [],
  20. uihints: {
  21. id: "nodeProperties",
  22. parameter_info: [],
  23. action_info: [],
  24. group_info: [
  25. {
  26. id: "nodeGroupInfo",
  27. type: "panels",
  28. group_info: [],
  29. },
  30. ],
  31. },
  32. resources: {},
  33. };
  34. for (const item of items) {
  35. switch (item.type) {
  36. case "boolean":
  37. commonProperties.current_parameters[item.id] = item.default ?? false;
  38. commonProperties.parameters.push({
  39. id: item.id,
  40. });
  41. commonProperties.uihints.parameter_info.push({
  42. control: "custom",
  43. custom_control_id: "BooleanControl",
  44. parameter_ref: item.id,
  45. label: {
  46. default: item.label,
  47. },
  48. data: {
  49. helperText: item.description,
  50. required: false,
  51. },
  52. });
  53. commonProperties.uihints.group_info[0].group_info.push({
  54. id: item.id,
  55. type: "controls",
  56. parameter_refs: [item.id],
  57. });
  58. break;
  59. case "file":
  60. commonProperties.current_parameters[item.id] = item.default ?? "";
  61. commonProperties.parameters.push({
  62. id: item.id,
  63. });
  64. commonProperties.uihints.parameter_info.push({
  65. control: "custom",
  66. custom_control_id: "StringControl",
  67. parameter_ref: item.id,
  68. label: {
  69. default: item.label,
  70. },
  71. description: item.description
  72. ? {
  73. default: item.description,
  74. placement: "on_panel",
  75. }
  76. : undefined,
  77. data: {
  78. format: "file",
  79. required: item.required ?? false,
  80. },
  81. });
  82. commonProperties.uihints.group_info[0].group_info.push({
  83. id: item.id,
  84. type: "controls",
  85. parameter_refs: [item.id],
  86. });
  87. break;
  88. case "string":
  89. commonProperties.current_parameters[item.id] = item.default ?? "";
  90. if (item.enum) {
  91. commonProperties.parameters.push({
  92. id: item.id,
  93. });
  94. commonProperties.uihints.parameter_info.push({
  95. parameter_ref: item.id,
  96. label: {
  97. default: item.label,
  98. },
  99. control: "custom",
  100. custom_control_id: "EnumControl",
  101. description: item.description
  102. ? {
  103. default: item.description,
  104. placement: "on_panel",
  105. }
  106. : undefined,
  107. data: {
  108. items: item.enum,
  109. required: item.required ?? false,
  110. },
  111. });
  112. } else {
  113. commonProperties.parameters.push({
  114. id: item.id,
  115. });
  116. commonProperties.uihints.parameter_info.push({
  117. control: "custom",
  118. custom_control_id: "StringControl",
  119. parameter_ref: item.id,
  120. label: {
  121. default: item.label,
  122. },
  123. // description: item.description
  124. // ? {
  125. // default: item.description,
  126. // placement: "on_panel",
  127. // }
  128. // : undefined,
  129. data: {
  130. placeholder: item.placeholder,
  131. format: item.format || "",
  132. required: item.required ?? false,
  133. },
  134. });
  135. }
  136. commonProperties.uihints.group_info[0].group_info.push({
  137. id: item.id,
  138. type: "controls",
  139. parameter_refs: [item.id],
  140. });
  141. break;
  142. case "string[]":
  143. commonProperties.current_parameters[item.id] = item.default ?? [];
  144. commonProperties.parameters.push({
  145. id: item.id,
  146. });
  147. commonProperties.uihints.parameter_info.push({
  148. control: "custom",
  149. custom_control_id: "StringArrayControl",
  150. parameter_ref: item.id,
  151. label: {
  152. default: item.label,
  153. },
  154. description: item.description
  155. ? {
  156. default: item.description,
  157. placement: "on_panel",
  158. }
  159. : undefined,
  160. data: {
  161. placeholder: item.placeholder,
  162. required: item.required ?? false,
  163. },
  164. });
  165. commonProperties.uihints.group_info[0].group_info.push({
  166. id: item.id,
  167. type: "controls",
  168. parameter_refs: [item.id],
  169. });
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. return commonProperties;
  176. }
  177. export function createNode({
  178. op,
  179. description,
  180. properties,
  181. label,
  182. nadeLabel,
  183. image,
  184. ...rest
  185. }: any) {
  186. return {
  187. op,
  188. description,
  189. id: "",
  190. type: "execution_node",
  191. label,
  192. inputs: [
  193. {
  194. id: "inPort",
  195. app_data: {
  196. ui_data: {
  197. cardinality: {
  198. min: 0,
  199. max: -1,
  200. },
  201. label: "Input Port",
  202. },
  203. },
  204. },
  205. ],
  206. outputs: [
  207. {
  208. id: "outPort",
  209. app_data: {
  210. ui_data: {
  211. cardinality: {
  212. min: 0,
  213. max: -1,
  214. },
  215. label: "Output Port",
  216. },
  217. },
  218. },
  219. ],
  220. parameters: {},
  221. app_data: {
  222. ...rest,
  223. properties: toCommonProperties(properties),
  224. ui_data: {
  225. label: nadeLabel,
  226. image,
  227. x_pos: 0,
  228. y_pos: 0,
  229. },
  230. },
  231. };
  232. }