@pie-element/fraction-model 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/author/card-bar.d.ts +21 -0
  2. package/dist/author/card-bar.d.ts.map +1 -0
  3. package/dist/author/card-bar.js +40 -0
  4. package/dist/author/defaults.d.ts +114 -0
  5. package/dist/author/defaults.d.ts.map +1 -0
  6. package/dist/author/defaults.js +84 -0
  7. package/dist/author/index.d.ts +45 -0
  8. package/dist/author/index.d.ts.map +1 -0
  9. package/dist/author/index.js +88 -0
  10. package/dist/author/main.d.ts +26 -0
  11. package/dist/author/main.d.ts.map +1 -0
  12. package/dist/author/main.js +156 -0
  13. package/dist/author/model-options.d.ts +33 -0
  14. package/dist/author/model-options.d.ts.map +1 -0
  15. package/dist/author/model-options.js +85 -0
  16. package/dist/author/number-text-field.d.ts +16 -0
  17. package/dist/author/number-text-field.d.ts.map +1 -0
  18. package/dist/author/number-text-field.js +20 -0
  19. package/dist/controller/defaults.d.ts +22 -0
  20. package/dist/controller/defaults.d.ts.map +1 -0
  21. package/dist/controller/defaults.js +13 -0
  22. package/dist/controller/index.d.ts +24 -0
  23. package/dist/controller/index.d.ts.map +1 -0
  24. package/dist/controller/index.js +68 -0
  25. package/dist/delivery/answer-fraction.d.ts +28 -0
  26. package/dist/delivery/answer-fraction.d.ts.map +1 -0
  27. package/dist/delivery/answer-fraction.js +69 -0
  28. package/dist/delivery/fraction-model-chart.d.ts +12 -0
  29. package/dist/delivery/fraction-model-chart.d.ts.map +1 -0
  30. package/dist/delivery/fraction-model-chart.js +154 -0
  31. package/dist/delivery/index.d.ts +23 -0
  32. package/dist/delivery/index.d.ts.map +1 -0
  33. package/dist/delivery/index.js +54 -0
  34. package/dist/delivery/main.d.ts +39 -0
  35. package/dist/delivery/main.d.ts.map +1 -0
  36. package/dist/delivery/main.js +136 -0
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.iife.d.ts +9 -0
  40. package/dist/index.iife.d.ts.map +1 -0
  41. package/dist/index.iife.js +179 -0
  42. package/dist/index.js +2 -0
  43. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_DataView.js +6 -0
  44. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Hash.js +16 -0
  45. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_ListCache.js +16 -0
  46. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Map.js +6 -0
  47. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_MapCache.js +16 -0
  48. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Promise.js +6 -0
  49. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Set.js +6 -0
  50. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Stack.js +13 -0
  51. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Symbol.js +5 -0
  52. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_Uint8Array.js +5 -0
  53. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_WeakMap.js +6 -0
  54. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_arrayEach.js +7 -0
  55. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_arrayFilter.js +10 -0
  56. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_arrayLikeKeys.js +15 -0
  57. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_arrayPush.js +7 -0
  58. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_assignValue.js +10 -0
  59. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_assocIndexOf.js +8 -0
  60. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseAssign.js +8 -0
  61. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseAssignIn.js +8 -0
  62. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseAssignValue.js +12 -0
  63. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseClone.js +57 -0
  64. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseCreate.js +14 -0
  65. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseGetAllKeys.js +9 -0
  66. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseGetTag.js +10 -0
  67. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseIsArguments.js +9 -0
  68. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseIsMap.js +9 -0
  69. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseIsNative.js +11 -0
  70. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseIsSet.js +9 -0
  71. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseIsTypedArray.js +11 -0
  72. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseKeys.js +12 -0
  73. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseKeysIn.js +13 -0
  74. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseTimes.js +7 -0
  75. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_baseUnary.js +8 -0
  76. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneArrayBuffer.js +8 -0
  77. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneBuffer.js +10 -0
  78. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneDataView.js +8 -0
  79. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneRegExp.js +8 -0
  80. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneSymbol.js +8 -0
  81. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_cloneTypedArray.js +8 -0
  82. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_copyArray.js +8 -0
  83. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_copyObject.js +14 -0
  84. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_copySymbols.js +8 -0
  85. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_copySymbolsIn.js +8 -0
  86. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_coreJsData.js +5 -0
  87. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_defineProperty.js +10 -0
  88. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_freeGlobal.js +4 -0
  89. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getAllKeys.js +9 -0
  90. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getAllKeysIn.js +9 -0
  91. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getMapData.js +8 -0
  92. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getNative.js +9 -0
  93. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getPrototype.js +5 -0
  94. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getRawTag.js +14 -0
  95. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getSymbols.js +10 -0
  96. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getSymbolsIn.js +11 -0
  97. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getTag.js +23 -0
  98. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_getValue.js +6 -0
  99. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_hashClear.js +7 -0
  100. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_hashDelete.js +7 -0
  101. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_hashGet.js +13 -0
  102. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_hashHas.js +9 -0
  103. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_hashSet.js +9 -0
  104. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_initCloneArray.js +8 -0
  105. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_initCloneByTag.js +33 -0
  106. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_initCloneObject.js +9 -0
  107. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_isIndex.js +8 -0
  108. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_isKeyable.js +7 -0
  109. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_isMasked.js +11 -0
  110. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_isPrototype.js +8 -0
  111. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_listCacheClear.js +6 -0
  112. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_listCacheDelete.js +9 -0
  113. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_listCacheGet.js +8 -0
  114. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_listCacheHas.js +7 -0
  115. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_listCacheSet.js +8 -0
  116. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_mapCacheClear.js +13 -0
  117. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_mapCacheDelete.js +8 -0
  118. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_mapCacheGet.js +7 -0
  119. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_mapCacheHas.js +7 -0
  120. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_mapCacheSet.js +8 -0
  121. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_nativeCreate.js +5 -0
  122. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_nativeKeys.js +5 -0
  123. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_nativeKeysIn.js +8 -0
  124. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_nodeUtil.js +9 -0
  125. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_objectToString.js +7 -0
  126. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_overArg.js +8 -0
  127. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_root.js +5 -0
  128. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_stackClear.js +7 -0
  129. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_stackDelete.js +7 -0
  130. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_stackGet.js +6 -0
  131. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_stackHas.js +6 -0
  132. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_stackSet.js +16 -0
  133. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/_toSource.js +15 -0
  134. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/cloneDeep.js +8 -0
  135. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/eq.js +6 -0
  136. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isArguments.js +10 -0
  137. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isArray.js +4 -0
  138. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isArrayLike.js +8 -0
  139. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isBuffer.js +6 -0
  140. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isEmpty.js +21 -0
  141. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isFunction.js +11 -0
  142. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isLength.js +7 -0
  143. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isMap.js +7 -0
  144. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isObject.js +7 -0
  145. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isObjectLike.js +6 -0
  146. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isSet.js +7 -0
  147. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/isTypedArray.js +7 -0
  148. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/keys.js +9 -0
  149. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/keysIn.js +9 -0
  150. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/stubArray.js +6 -0
  151. package/dist/node_modules/.bun/lodash-es@4.17.23/node_modules/lodash-es/stubFalse.js +6 -0
  152. package/package.json +70 -0
  153. package/src/author/card-bar.tsx +67 -0
  154. package/src/author/defaults.ts +88 -0
  155. package/src/author/index.ts +187 -0
  156. package/src/author/main.tsx +243 -0
  157. package/src/author/model-options.tsx +158 -0
  158. package/src/author/number-text-field.tsx +42 -0
  159. package/src/controller/defaults.ts +23 -0
  160. package/src/controller/index.ts +207 -0
  161. package/src/delivery/answer-fraction.tsx +113 -0
  162. package/src/delivery/fraction-model-chart.tsx +261 -0
  163. package/src/delivery/index.ts +96 -0
  164. package/src/delivery/main.tsx +256 -0
  165. package/src/index.iife.ts +16 -0
  166. package/src/index.ts +1 -0
@@ -0,0 +1,261 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-elements/packages/fraction-model/src/fraction-model-chart.jsx
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React, { useState, useEffect, useRef } from 'react';
12
+ import { Bar, BarChart, Cell, LabelList, Pie, PieChart, YAxis } from 'recharts';
13
+ import { styled } from '@mui/material/styles';
14
+
15
+ const PieChartParentDiv: any = styled('div')({
16
+ display: 'grid',
17
+ gridTemplateColumns: '200px 200px 200px',
18
+ gap: '10px',
19
+ padding: '5px 0',
20
+ });
21
+
22
+ const BarChartParentDiv: any = styled('div')({
23
+ display: 'grid',
24
+ gridTemplateColumns: '200px 200px 200px',
25
+ gap: '20px',
26
+ padding: '10px 0',
27
+ });
28
+
29
+ const StyledPieChart: any = styled(PieChart)({
30
+ '& .recharts-pie-sector': {
31
+ outline: 'none !important',
32
+ '&:focus': {
33
+ outline: 'none !important',
34
+ },
35
+ },
36
+ });
37
+
38
+ const FractionModelChart = (props) => {
39
+ /*
40
+ * Function to handle click event on chart
41
+ * @param chartIndex: index of the chart
42
+ * @param sectorIndex: index of the sector
43
+ * */
44
+ const handleChartClick = (chartIndex, sectorIndex) => {
45
+ if (typeof sectorIndex === 'string') {
46
+ sectorIndex = parseInt(sectorIndex);
47
+ }
48
+ const existingIndex = clickedIndexArray.findIndex((item) => item.id === chartIndex);
49
+ let newClickedIndexArray;
50
+ if (existingIndex >= 0) {
51
+ const currentItem = clickedIndexArray[existingIndex];
52
+ if (currentItem.value === sectorIndex) {
53
+ newClickedIndexArray = clickedIndexArray.filter((_, index) => index !== existingIndex);
54
+ } else {
55
+ newClickedIndexArray = [
56
+ ...clickedIndexArray.slice(0, existingIndex),
57
+ { id: chartIndex, value: sectorIndex },
58
+ ...clickedIndexArray.slice(existingIndex + 1),
59
+ ];
60
+ }
61
+ } else {
62
+ newClickedIndexArray = [...clickedIndexArray, { id: chartIndex, value: sectorIndex }];
63
+ }
64
+ onChange(newClickedIndexArray);
65
+ setClickedIndexArray(newClickedIndexArray);
66
+ };
67
+
68
+ /*
69
+ * Function to handle mouse enter event on chart
70
+ * @param chartIndex: index of the chart
71
+ * @param sectorIndex: index of the sector
72
+ * */
73
+ const handleChartMouseEnter = (chartIndex, sectorIndex) => {
74
+ setHoveredIndex(chartIndex + '-' + sectorIndex);
75
+ };
76
+
77
+ /*
78
+ * Function to handle mouse leave event on chart
79
+ * */
80
+ const handleChartMouseLeave = () => {
81
+ setHoveredIndex(null);
82
+ };
83
+
84
+ /*
85
+ * Function to get fill color for sector
86
+ * @param hoveredIndex: hovered index
87
+ * @param selection: selected index
88
+ * */
89
+ const getSectorFill = (hoveredIndex, selection) => {
90
+ const [selectionId, selectionValue] = selection.split('-').map(Number);
91
+ // Check for a matching clicked item
92
+ const clickedItem = clickedIndexArray.find((item) => item.id === selectionId);
93
+ if (clickedItem && selectionValue <= clickedItem.value) {
94
+ return 'rgb(60, 73, 150, 0.6)';
95
+ }
96
+ // Check for a matching hovered index
97
+ if (hoveredIndex) {
98
+ const [hoveredId, hoveredValue] = hoveredIndex.split('-').map(Number);
99
+ if (hoveredId === selectionId && hoveredValue >= selectionValue) {
100
+ return 'rgb(0, 0, 0, 0.25)';
101
+ }
102
+ }
103
+ return '#FFFFFF';
104
+ };
105
+
106
+ /*
107
+ * Function to render label for pie chart
108
+ * @param props: properties of the label
109
+ * */
110
+ const renderCustomizedLabelForPie = (props) => {
111
+ const { cx, cy, midAngle, outerRadius, index } = props;
112
+ const RADIAN = Math.PI / 180;
113
+ // const radius = outerRadius * 0.5; //If you want to show label inside the pie
114
+ const radius = outerRadius * 1.13;
115
+ const x = cx + radius * Math.cos(-midAngle * RADIAN);
116
+ const y = cy + radius * Math.sin(-midAngle * RADIAN);
117
+ return (
118
+ <text x={x} y={y} fill="#000000" textAnchor={x > cx ? 'start' : 'end'} dominantBaseline="central">
119
+ {`${(index + 1).toFixed(0)}`}
120
+ </text>
121
+ );
122
+ };
123
+
124
+ /*
125
+ * Function to create and return component for bar chart
126
+ * */
127
+ const barWithBorder = () => {
128
+ return (props) => {
129
+ const { fill, x, y, width, height } = props;
130
+ return (
131
+ <g>
132
+ <rect x={x} y={y} width={width} height={height} stroke={'#000000'} fill={fill} />
133
+ </g>
134
+ );
135
+ };
136
+ };
137
+
138
+ const {
139
+ value = [],
140
+ modelType = 'bar',
141
+ noOfModels = 0,
142
+ partsPerModel = 0,
143
+ showLabel = false,
144
+ disabled = false,
145
+ onChange,
146
+ } = props;
147
+ const [hoveredIndex, setHoveredIndex] = useState(null);
148
+ const [clickedIndexArray, setClickedIndexArray] = useState(value);
149
+
150
+ /*
151
+ * Function to create and return bar fraction model
152
+ * */
153
+ const getBarFractionModel = () => {
154
+ const parentData = [];
155
+ // Generate data for bar chart
156
+ for (let i = 1; i < noOfModels + 1; i++) {
157
+ const data = [{ name: i }];
158
+ for (let j = 1; j < partsPerModel + 1; j++) {
159
+ data[0][`${j}`] = 1;
160
+ }
161
+ parentData.push(data);
162
+ }
163
+ let barItems = [];
164
+ parentData.forEach((data, chartIndex) => {
165
+ barItems.push(
166
+ <BarChart width={200} height={30 * partsPerModel} data={data} key={`bar-chart-${chartIndex + 1}`}>
167
+ <YAxis hide={true} type="number" domain={[0, partsPerModel]} />
168
+ {Object.keys(data[0]).map((key, index) => {
169
+ if (key !== 'name') {
170
+ return (
171
+ <Bar
172
+ dataKey={key}
173
+ stackId="a"
174
+ key={`bar-${chartIndex + 1}-${index + 1}`}
175
+ onClick={disabled ? null : () => handleChartClick(chartIndex + 1, key)}
176
+ onMouseEnter={disabled ? null : () => handleChartMouseEnter(chartIndex + 1, key)}
177
+ onMouseLeave={disabled ? null : () => handleChartMouseLeave(chartIndex + 1, key)}
178
+ shape={barWithBorder()}
179
+ isAnimationActive={false}
180
+ fill={getSectorFill(hoveredIndex, `${chartIndex + 1}-${key}`)}
181
+ >
182
+ {showLabel && <LabelList position="left" fill="#000000" />}
183
+ </Bar>
184
+ );
185
+ }
186
+ })}
187
+ </BarChart>,
188
+ );
189
+ });
190
+ return <BarChartParentDiv>{barItems}</BarChartParentDiv>;
191
+ };
192
+
193
+ const pieChartRef = useRef(null);
194
+
195
+ /*
196
+ * Function to create and return pie fraction model
197
+ * */
198
+ const getPieFractionModel = () => {
199
+ const parentData = [];
200
+ // Generate data for pie chart
201
+ for (let i = 1; i < noOfModels + 1; i++) {
202
+ const data = Array.from({ length: partsPerModel }, (_, index) => ({
203
+ name: `${index + 1}`,
204
+ value: 1,
205
+ }));
206
+ parentData.push(data);
207
+ }
208
+ let pieItems = [];
209
+ parentData.forEach((data, chartIndex) => {
210
+ pieItems.push(
211
+ <StyledPieChart width={200} height={200} key={`pie-chart-${chartIndex}`}>
212
+ <Pie
213
+ data={data}
214
+ key={`pie-${chartIndex + 1}`}
215
+ fill="#FFFFFF"
216
+ stroke="#000000"
217
+ dataKey="value"
218
+ startAngle={90}
219
+ endAngle={-270}
220
+ isAnimationActive={false}
221
+ labelLine={false}
222
+ label={showLabel && renderCustomizedLabelForPie}
223
+ >
224
+ {data.map((entry, sectorIndex) => (
225
+ <Cell
226
+ key={`${chartIndex + 1}-${sectorIndex + 1}`}
227
+ onClick={disabled ? null : () => handleChartClick(chartIndex + 1, sectorIndex + 1)}
228
+ onMouseEnter={disabled ? null : () => handleChartMouseEnter(chartIndex + 1, sectorIndex + 1)}
229
+ onMouseLeave={disabled ? null : () => handleChartMouseLeave(chartIndex + 1, sectorIndex + 1)}
230
+ fill={getSectorFill(hoveredIndex, `${chartIndex + 1}-${sectorIndex + 1}`)}
231
+ style={{ outline: 'none', cursor: disabled ? 'default' : 'pointer' }}
232
+ />
233
+ ))}
234
+ </Pie>
235
+ </StyledPieChart>,
236
+ );
237
+ });
238
+ return <PieChartParentDiv ref={pieChartRef}>{pieItems}</PieChartParentDiv>;
239
+ };
240
+
241
+ //Render bar or pie models as per model type
242
+ if (modelType === 'bar') {
243
+ return getBarFractionModel();
244
+ } else if (modelType === 'pie') {
245
+ //Remove the last sector line of pie chart if parts per model is 1
246
+ useEffect(() => {
247
+ if (pieChartRef?.current && partsPerModel === 1){
248
+ const paths = pieChartRef.current.querySelectorAll('path');
249
+ if (paths.length > 0) {
250
+ paths.forEach((path) => {
251
+ let d = path.getAttribute('d');
252
+ path.setAttribute('d', d.replaceAll('L 100,100', ''));
253
+ });
254
+ }
255
+ }
256
+ }, []);
257
+ return getPieFractionModel();
258
+ }
259
+ };
260
+
261
+ export default FractionModelChart;
@@ -0,0 +1,96 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-elements/packages/fraction-model/src/index.js
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React from 'react';
12
+ import { createRoot } from 'react-dom/client';
13
+ import { SessionChangedEvent } from '@pie-element/shared-player-events';
14
+ import Main from './main.js';
15
+ import { cloneDeep } from 'lodash-es';
16
+ import { renderMath } from '@pie-element/shared-math-rendering-mathjax';
17
+ import FractionModelChart from './fraction-model-chart.js';
18
+
19
+ // Export FractionModelChart for use in configure
20
+ export { FractionModelChart };
21
+
22
+ export default class FractionModel extends HTMLElement {
23
+ constructor() {
24
+ super();
25
+ this._root = null;
26
+ }
27
+
28
+ set model(m) {
29
+ this._model = m;
30
+ this._render();
31
+ }
32
+
33
+ get model() {
34
+ return this._model;
35
+ }
36
+
37
+ set session(s) {
38
+ this._session = s;
39
+ this._render();
40
+ }
41
+
42
+ get session() {
43
+ return this._session;
44
+ }
45
+
46
+ /*
47
+ * Method to check if student answered the question
48
+ * @param {session} session contains the session object
49
+ * @param {model} model contains the model object
50
+ * */
51
+ isSessionComplete(session, model) {
52
+ const answers = session && session.answers;
53
+ const configComplete = model.allowedStudentConfig ? answers.noOfModel > 0 && answers.partsPerModel > 0 : true;
54
+ const responseComplete = Array.isArray(answers.response) && answers.response.length > 0;
55
+ return configComplete && responseComplete;
56
+ }
57
+
58
+ /*
59
+ * Session change event handler
60
+ * @param {session} session contains the session object
61
+ * */
62
+ onSessionChange(session) {
63
+ this._session.answers = session && session.answers;
64
+ const complete = this.isSessionComplete(this._session, this._model);
65
+ this.dispatchEvent(new SessionChangedEvent(this.tagName.toLowerCase(), complete));
66
+ }
67
+
68
+ connectedCallback() {
69
+ this._render();
70
+ }
71
+
72
+ _render() {
73
+ if (this._model && this._session) {
74
+ let model = cloneDeep(this._model);
75
+ const el = React.createElement(Main, {
76
+ model,
77
+ session: this._session,
78
+ onSessionChange: this.onSessionChange.bind(this),
79
+ });
80
+
81
+ if (!this._root) {
82
+ this._root = createRoot(this);
83
+ }
84
+ this._root.render(el);
85
+ queueMicrotask(() => {
86
+ renderMath(this);
87
+ });
88
+ }
89
+ }
90
+
91
+ disconnectedCallback() {
92
+ if (this._root) {
93
+ this._root.unmount();
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,256 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-elements/packages/fraction-model/src/main.jsx
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React from 'react';
12
+ import PropTypes from 'prop-types';
13
+ import { styled } from '@mui/material/styles';
14
+ import AnswerFraction from './answer-fraction.js';
15
+ import CorrectAnswerToggle from '@pie-lib/correct-answer-toggle';
16
+ import FractionModelChart from './fraction-model-chart.js';
17
+ import { AlertDialog } from '@pie-lib/config-ui';
18
+ import { PreviewPrompt as PreviewPromptImport, UiLayout as UiLayoutImport } from '@pie-lib/render-ui';
19
+
20
+ function isRenderableReactInteropType(value: any) {
21
+ return (
22
+ typeof value === 'function' ||
23
+ (typeof value === 'object' && value !== null && typeof value.$$typeof === 'symbol')
24
+ );
25
+ }
26
+
27
+ function unwrapReactInteropSymbol(maybeSymbol: any, namedExport?: string) {
28
+ if (!maybeSymbol) return maybeSymbol;
29
+ if (isRenderableReactInteropType(maybeSymbol)) return maybeSymbol;
30
+ if (isRenderableReactInteropType(maybeSymbol.default)) return maybeSymbol.default;
31
+ if (namedExport && isRenderableReactInteropType(maybeSymbol[namedExport])) {
32
+ return maybeSymbol[namedExport];
33
+ }
34
+ if (namedExport && isRenderableReactInteropType(maybeSymbol[namedExport]?.default)) {
35
+ return maybeSymbol[namedExport].default;
36
+ }
37
+ return maybeSymbol;
38
+ }
39
+ const UiLayout = unwrapReactInteropSymbol(UiLayoutImport, 'UiLayout') || unwrapReactInteropSymbol(renderUi.UiLayout, 'UiLayout');
40
+ const PreviewPrompt = unwrapReactInteropSymbol(PreviewPromptImport, 'PreviewPrompt') || unwrapReactInteropSymbol(renderUi.PreviewPrompt, 'PreviewPrompt');
41
+ import * as RenderUiNamespace from '@pie-lib/render-ui';
42
+ const renderUiNamespaceAny = RenderUiNamespace as any;
43
+ const renderUiDefaultMaybe = renderUiNamespaceAny['default'];
44
+ const renderUi =
45
+ renderUiDefaultMaybe && typeof renderUiDefaultMaybe === 'object'
46
+ ? renderUiDefaultMaybe
47
+ : renderUiNamespaceAny;
48
+ import { cloneDeep } from 'lodash-es';
49
+
50
+ const ModelPreview: any = styled('div')({
51
+ padding: '16px',
52
+ });
53
+
54
+ const TitleContainer: any = styled('div')({
55
+ textAlign: 'center',
56
+ fontSize: '20px',
57
+ });
58
+
59
+ export class Main extends React.Component {
60
+ static propTypes = {
61
+ model: PropTypes.object,
62
+ session: PropTypes.object,
63
+ onSessionChange: PropTypes.func.isRequired,
64
+ };
65
+
66
+ constructor(props) {
67
+ super(props);
68
+ this.state = {
69
+ session: {
70
+ ...props.session,
71
+ answers: (props.session && props.session.answers) || this.generateAnswers(props.model),
72
+ },
73
+ showCorrect: false,
74
+ answerChangeDialog: {
75
+ open: false,
76
+ text: '',
77
+ },
78
+ };
79
+ this.callOnSessionChange();
80
+ }
81
+
82
+ /**
83
+ * Function to trigger when session value change for fraction model
84
+ */
85
+ callOnSessionChange: any = () => {
86
+ const { onSessionChange } = this.props;
87
+ if (onSessionChange) {
88
+ onSessionChange(this.state.session);
89
+ }
90
+ };
91
+
92
+ /**
93
+ * Function to generate answers for preview selection
94
+ * @param {object} model contains model object for fraction model
95
+ * @returns answer model
96
+ */
97
+ generateAnswers: any = (model) => {
98
+ const answers = {
99
+ response: [],
100
+ };
101
+ if (model.allowedStudentConfig) {
102
+ (answers.noOfModel = ''), (answers.partsPerModel = '');
103
+ }
104
+ return answers;
105
+ };
106
+
107
+ /**
108
+ * Function to trigger when answer change from preview
109
+ * @param {object} newAnswers contains updated answer model
110
+ */
111
+ onAnswerChange: any = (newAnswers) => {
112
+ let oldSession = cloneDeep(this.state.session);
113
+ let newSession = {
114
+ ...this.state.session,
115
+ answers: newAnswers,
116
+ };
117
+ if (newAnswers.response.length > 0) {
118
+ this.setState({
119
+ answerChangeDialog: {
120
+ open: true,
121
+ oldSession: oldSession,
122
+ newSession: newSession,
123
+ text: 'Changing either the Number of Models or Parts per Model will remove added answer. Are you sure you want to continue?',
124
+ },
125
+ });
126
+ } else {
127
+ this.setState(
128
+ (state) => ({
129
+ session: newSession,
130
+ }),
131
+ this.callOnSessionChange,
132
+ );
133
+ }
134
+ };
135
+
136
+ /*
137
+ * Function to toggle correct answer
138
+ * @param {boolean} show contains boolean value to show correct answer
139
+ * */
140
+ toggleShowCorrect: any = (show) => {
141
+ this.setState({ showCorrect: show });
142
+ };
143
+
144
+ /*
145
+ * Function to trigger when response change from preview
146
+ * @param {object} response contains updated response model
147
+ * */
148
+ onResponseChange: any = (response) => {
149
+ this.setState(
150
+ (state) => ({
151
+ session: {
152
+ ...state.session,
153
+ answers: {
154
+ ...state.session.answers,
155
+ response,
156
+ },
157
+ },
158
+ }),
159
+ this.callOnSessionChange,
160
+ );
161
+ };
162
+
163
+ /*
164
+ * Method to generate random key
165
+ * */
166
+ generateRandomKey: any = () => {
167
+ return Math.floor(Math.random() * 10000);
168
+ };
169
+
170
+ render() {
171
+ const { model } = this.props;
172
+ const { showCorrect, session, answerChangeDialog } = this.state;
173
+ const { prompt, title, correctness = {}, extraCSSRules, language } = model;
174
+ const showCorrectAnswerToggle = correctness.correctness && correctness.correctness !== 'correct';
175
+ const fractionModelChartKey = this.generateRandomKey();
176
+
177
+ return (
178
+ <UiLayout extraCSSRules={extraCSSRules}>
179
+ <ModelPreview>
180
+ <TitleContainer>
181
+ <PreviewPrompt className="prompt" prompt={title} tagName="h3" />
182
+ </TitleContainer>
183
+ <PreviewPrompt className="prompt" prompt={prompt} tagName="p" />
184
+
185
+ <CorrectAnswerToggle
186
+ language={language}
187
+ show={showCorrectAnswerToggle}
188
+ toggled={showCorrect}
189
+ onToggle={this.toggleShowCorrect}
190
+ />
191
+
192
+ <AnswerFraction
193
+ model={model}
194
+ showCorrect={showCorrect}
195
+ disabled={model.view}
196
+ onAnswerChange={this.onAnswerChange}
197
+ answers={session.answers}
198
+ />
199
+
200
+ <FractionModelChart
201
+ key={fractionModelChartKey}
202
+ disabled={model.view}
203
+ value={showCorrect ? model.correctResponse : session.answers.response}
204
+ modelType={model.modelTypeSelected}
205
+ noOfModels={
206
+ showCorrect
207
+ ? model.maxModelSelected
208
+ : model.allowedStudentConfig
209
+ ? session.answers.noOfModel
210
+ : model.maxModelSelected
211
+ }
212
+ partsPerModel={
213
+ showCorrect
214
+ ? model.partsPerModel
215
+ : model.allowedStudentConfig
216
+ ? session.answers.partsPerModel
217
+ : model.partsPerModel
218
+ }
219
+ showLabel={model.showGraphLabels}
220
+ onChange={this.onResponseChange}
221
+ ></FractionModelChart>
222
+
223
+ <AlertDialog
224
+ open={answerChangeDialog.open}
225
+ title="Warning"
226
+ text={answerChangeDialog.text}
227
+ onConfirm={() => {
228
+ let newSession = this.state.answerChangeDialog.newSession;
229
+ newSession.answers.response = [];
230
+ this.setState(
231
+ () => ({
232
+ session: newSession,
233
+ answerChangeDialog: { open: false },
234
+ }),
235
+ this.callOnSessionChange,
236
+ );
237
+ }}
238
+ onClose={() => {
239
+ this.setState(
240
+ (prevState) => ({
241
+ session: prevState.answerChangeDialog.oldSession,
242
+ answerChangeDialog: { open: false },
243
+ }),
244
+ this.callOnSessionChange,
245
+ );
246
+ }}
247
+ onConfirmText={'OK'}
248
+ onCloseText={'Cancel'}
249
+ />
250
+ </ModelPreview>
251
+ </UiLayout>
252
+ );
253
+ }
254
+ }
255
+
256
+ export default Main;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * IIFE entry point for fraction-model element
3
+ * This file is only used for IIFE builds and includes auto-registration
4
+ *
5
+ * @sync-generated - Auto-generated during sync from pie-elements
6
+ */
7
+
8
+ import Element from './index.js';
9
+
10
+ // Auto-register the custom element for IIFE mode
11
+ if (typeof window !== 'undefined' && !customElements.get('fraction-model-element')) {
12
+ customElements.define('fraction-model-element', Element);
13
+ }
14
+
15
+ // Export for IIFE global
16
+ export default Element;
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from './delivery/index.js';