@builttocreate/engine-utils 2.6.0-beta.22 → 2.6.0-beta.23

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.
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.sortPages = exports.sortFieldPositionsByXAndYCoordinates = exports.mergeAssoicatedFieldPositionsForMobileView = exports.getPageOrder = exports.getMobileViewFromFile = exports.getDocumentFromTemplate = exports.getDefaultTemplate = exports.getDefaultJoyDocPage = exports.getDefaultJoyDocFile = exports.getDefaultJoyDoc = exports.getDefaultDocument = exports.getCleanedJoyDocPages = exports.getCleanedJoyDoc = exports.generateMobileViewPageFieldPositions = exports.generateMobileViewPage = exports.generateMobileViewFile = exports.duplicateDocumentPage = exports.duplicate = exports["default"] = exports.cleanPageOrder = void 0;
8
+ exports.sortPages = exports.sortFieldPositionsByXAndYCoordinates = exports.mergeAssoicatedFieldPositionsForMobileView = exports.getPageOrder = exports.getMobileViewFromFile = exports.getDocumentFromTemplate = exports.getDefaultTemplate = exports.getDefaultJoyDocPage = exports.getDefaultJoyDocFile = exports.getDefaultJoyDoc = exports.getDefaultDocument = exports.getCleanedJoyDocPages = exports.getCleanedJoyDoc = exports.generateMobileViewPageFieldPositions = exports.generateMobileViewPage = exports.generateMobileViewFile = exports.formatTableFieldPositions = exports.duplicateDocumentPage = exports.duplicate = exports["default"] = exports.cleanPageOrder = void 0;
9
9
 
10
10
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
11
 
@@ -805,8 +805,149 @@ var sortFieldPositionsByXAndYCoordinates = function sortFieldPositionsByXAndYCoo
805
805
  });
806
806
  return sortedFieldPositions;
807
807
  };
808
+ /**
809
+ * Formats individual table cell field positions that have already been merged with their associated field.
810
+ * This additional information is added and used for rendering the fieldPositions on the form.
811
+ *
812
+ * For instance, adds properties like columnType, calculatedCellValue, options, etc.
813
+ *
814
+ * @param {Array} fieldPositions
815
+ * @returns {Array}
816
+ */
817
+
808
818
 
809
819
  exports.sortFieldPositionsByXAndYCoordinates = sortFieldPositionsByXAndYCoordinates;
820
+
821
+ var formatTableFieldPositions = function formatTableFieldPositions(fieldPositions) {
822
+ /**
823
+ * We only want to generate the table value calculated lookup for each table field once.
824
+ * We use these lookups because multiple table fieldPositions belonging to the same table
825
+ * field will all use the same table value calculated lookup.
826
+ */
827
+ var tableFieldLookups = {};
828
+ var tableFieldColumnLookups = {};
829
+ var nextFieldPositions = [];
830
+ (0, _toConsumableArray2["default"])(fieldPositions).forEach(function (fieldPosition) {
831
+ var nextFieldPosition = _objectSpread({}, fieldPosition);
832
+
833
+ if (fieldPosition.type === _FieldTypes["default"].table) {
834
+ var field = fieldPosition.field,
835
+ column = fieldPosition.column,
836
+ rowIndex = fieldPosition.rowIndex,
837
+ tableColumns = fieldPosition.tableColumns,
838
+ value = fieldPosition.value,
839
+ rowOrder = fieldPosition.rowOrder,
840
+ tableColumnOrder = fieldPosition.tableColumnOrder;
841
+ /**
842
+ * Value will include deleted rows as well. We are removing the deleted rows
843
+ * since we want to compute the cellValue and calculatedCellValue only
844
+ * with undeleted table rows
845
+ */
846
+
847
+ var rows = (0, _tableHelper.getRows)(value);
848
+ var nextRowOrder = (0, _tableHelper.getRowOrder)(rowOrder, value);
849
+
850
+ if (!tableFieldColumnLookups[field]) {
851
+ var fieldColumnLookup = {};
852
+ (0, _tableHelper.getColumns)((0, _tableHelper.sortColumns)(tableColumns, tableColumnOrder)).forEach(function (column) {
853
+ return fieldColumnLookup[column._id] = column;
854
+ });
855
+ tableFieldColumnLookups[field] = fieldColumnLookup;
856
+ }
857
+
858
+ if (!tableFieldLookups[field]) {
859
+ var fieldTableLookup = (0, _tableHelper.generateTableLookup)(rows, (0, _tableHelper.sortColumns)(tableColumns, tableColumnOrder));
860
+ tableFieldLookups[field] = fieldTableLookup;
861
+ }
862
+ /**
863
+ * Ensure each table field has a properly formatted rowOrder
864
+ */
865
+
866
+
867
+ if (!nextFieldPosition.rowOrder) nextFieldPosition.rowOrder = nextRowOrder;
868
+ /**
869
+ * Add calculated value lookup. For instance, { A1: '', B1: '', C1: '', ... }
870
+ */
871
+
872
+ if (!nextFieldPosition.calculatedValue) nextFieldPosition.calculatedValue = tableFieldLookups[field];
873
+ /**
874
+ * Individual Table Cell vs Entire Table
875
+ *
876
+ * Only add cellValue, calculatedCellValue, columnType, and column options to
877
+ * individually mapped table cell fieldPositions. Regular table fields (entire table)
878
+ * just simply use the fieldPositions.value parameter so we don't need the calculated
879
+ * values, etc.
880
+ *
881
+ * We determine an individual table cell fieldPosition vs an entire table
882
+ * fieldPosition by checking for column and rowIndex. Individual cells have
883
+ * column and rowIndex but regular tables do not.
884
+ *
885
+ * Note:
886
+ * rowIndex + 1 is used while calculating cell value since column operands
887
+ * start from 1 instead of zero in the table lookups
888
+ * (ie: [A1:{}, B1:{}] instead of [A0:{}, B0:{}])
889
+ *
890
+ * If tableRowIndex associated with a cell doesn't have a table row associated
891
+ * with it (ie rows[rowIndex] is undefined) it means that particular row is deleted
892
+ */
893
+
894
+ if (tableFieldLookups[field] && column && typeof rowIndex === 'number') {
895
+ var _rows$targetRowIndex;
896
+
897
+ /**
898
+ * What is the difference between rowIndex and targetRowIndex?
899
+ *
900
+ * - The rowIndex on the fieldPosition is the saved index of where the rowId lives inside of the rowOrder.
901
+ * - The targetRowIndex is the actual index of the row object inside the value (array of row data) of the field.
902
+ *
903
+ * We use the fieldPosition.rowIndex to get the rowId from the rowOrder. Then we use the rowId to locate
904
+ * the assoicated row object data in the value property. The rowOrder and value (rows) do not follow the same
905
+ * order. The rowOrder is updated when users insert rows, move rows up or down, etc. it manages where rows
906
+ * are at in the list and how they should be rendered. This is why the rowIndex uses the rowOrder.
907
+ */
908
+ var targetColumn = tableFieldColumnLookups[field][column];
909
+ var targetRowId = nextRowOrder[rowIndex];
910
+ var targetRowIndex = rows.findIndex(function (row) {
911
+ return row._id === targetRowId;
912
+ });
913
+ /**
914
+ * Add proper rowId to the field position so that it can be used
915
+ * inside the element for change handlers;
916
+ */
917
+
918
+ nextFieldPosition.rowId = targetRowId;
919
+ /**
920
+ * If rows[rowIndex] is undefined, it means the row that was present
921
+ * at the particular rowIndex was deleted
922
+ */
923
+
924
+ nextFieldPosition.cellValue = ((_rows$targetRowIndex = rows[targetRowIndex]) === null || _rows$targetRowIndex === void 0 ? void 0 : _rows$targetRowIndex.cells[column]) || ''; //targetColumn will appear as undefined if a column is deleted
925
+
926
+ if (targetColumn) {
927
+ nextFieldPosition.calculatedCellValue = tableFieldLookups[field]["".concat(targetColumn.operand).concat(targetRowIndex + 1)];
928
+ nextFieldPosition.columnType = targetColumn.type;
929
+ nextFieldPosition.options = targetColumn.options;
930
+ nextFieldPosition.columnIdentifier = targetColumn.identifier;
931
+ /**
932
+ * IMPORTANT NOTE: We only add individual table cell field position into
933
+ * the fieldPosition list if the associated column is not deleted.
934
+ */
935
+
936
+ nextFieldPositions.push(nextFieldPosition);
937
+ }
938
+ } else {
939
+ //Push table fields with original display type
940
+ nextFieldPositions.push(nextFieldPosition);
941
+ }
942
+ } else {
943
+ //Push other fields
944
+ nextFieldPositions.push(nextFieldPosition);
945
+ }
946
+ });
947
+ return nextFieldPositions;
948
+ };
949
+
950
+ exports.formatTableFieldPositions = formatTableFieldPositions;
810
951
  var _default = {
811
952
  getDefaultJoyDocPage: getDefaultJoyDocPage,
812
953
  getDefaultJoyDocFile: getDefaultJoyDocFile,
@@ -824,6 +965,7 @@ var _default = {
824
965
  mergeAssoicatedFieldPositionsForMobileView: mergeAssoicatedFieldPositionsForMobileView,
825
966
  generateMobileViewFile: generateMobileViewFile,
826
967
  generateMobileViewPage: generateMobileViewPage,
827
- generateMobileViewPageFieldPositions: generateMobileViewPageFieldPositions
968
+ generateMobileViewPageFieldPositions: generateMobileViewPageFieldPositions,
969
+ formatTableFieldPositions: formatTableFieldPositions
828
970
  };
829
971
  exports["default"] = _default;
@@ -698,7 +698,9 @@ var generateTableLookup = function generateTableLookup(rows, columns) {
698
698
  if (!column.operand) return;
699
699
  var cellKey = "".concat(column.operand).concat(rowIndex + 1);
700
700
  var cellRawValue = row.cells[column._id] ? row.cells[column._id] : undefined;
701
- var cellValue = cellRawValue && typeof cellRawValue === 'string' ? cellRawValue.trim() : undefined;
701
+ var cellValue;
702
+ if (cellRawValue && typeof cellRawValue === 'string') cellValue = cellRawValue.trim();
703
+ if (cellRawValue && typeof cellRawValue === 'number') cellValue = cellRawValue.toString();
702
704
  var formulaCell = cellValue !== undefined && cellValue.charAt(0) === '=';
703
705
  var containsOperands = cellValue !== undefined && cellValue.match(/[A-Z]([0-9]{1,10})/gi);
704
706
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builttocreate/engine-utils",
3
- "version": "2.6.0-beta.22",
3
+ "version": "2.6.0-beta.23",
4
4
  "description": "Utility library for common logic shared across web and mobile",
5
5
  "main": "dist/index.js",
6
6
  "files": [