@cellaware/utils 8.1.2 → 8.1.4

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.
@@ -124,4 +124,5 @@ export declare function codifyCondition(condition: DatagridCondition): string;
124
124
  */
125
125
  export declare function stripNumericValueFormat(value: string | number | undefined): any;
126
126
  export declare function transformDatagrid(rows: any[], datagridState: DatagridStateBase, locale: string, condition?: DatagridCondition): DatagridState;
127
+ export declare function datagridToHtml(datagridState: DatagridState): string;
127
128
  export {};
@@ -806,3 +806,83 @@ export function transformDatagrid(rows, datagridState, locale, condition) {
806
806
  }
807
807
  return { ...datagridState, adjRowData: rows, chartRowData };
808
808
  }
809
+ export function datagridToHtml(datagridState) {
810
+ let buf = '';
811
+ if (datagridState.adjRowData.length === 0) {
812
+ return buf;
813
+ }
814
+ const columnFormats = datagridState.columnFormats ?? [];
815
+ if (columnFormats.length === 0) {
816
+ return buf;
817
+ }
818
+ // Header:
819
+ buf += '<table>\n\t<thead>\n\t\t<tr>';
820
+ let columns = [];
821
+ for (const key in datagridState.adjRowData[0]) {
822
+ columns.push(key);
823
+ buf += `\n\t\t\t<th>${key}</th>`;
824
+ }
825
+ buf += '\n\t\t</tr>\n\t</thead>';
826
+ // Rows:
827
+ buf += '\n\t<tbody>';
828
+ let rowIdx = 0;
829
+ for (const row of datagridState.adjRowData) {
830
+ let colIdx = 0;
831
+ const rowStyles = [];
832
+ const colStyles = [];
833
+ let cellValues = [];
834
+ // Extract cell values and row/column styles first, then write to html.
835
+ for (const key in row) {
836
+ let val = row[key];
837
+ if (val instanceof Date) {
838
+ const dteStr = val.toISOString();
839
+ val = dteStr.substring(0, dteStr.lastIndexOf('.'));
840
+ }
841
+ cellValues.push(val);
842
+ let colStyle = {
843
+ columnName: '',
844
+ styles: []
845
+ };
846
+ // Reminder: Value Formatting is performed **before** Conditional Formatting.
847
+ // https://chatwms.io/user-manual/concepts/column-formatting#conditional-formatting
848
+ const colFmt = columnFormats[colIdx];
849
+ for (const condFmt of colFmt.conditionalFormats.slice().reverse()) {
850
+ // `conditionalFormats` has already been sorted by `sequence`.
851
+ // NOTE: need to reverse the array for sequence to be correctly applied.
852
+ // NOTE: using `slice` to make shallow copy of array since `reverse` does so in place.
853
+ if (evaluateConditionalFormat(condFmt, colFmt.type, formatNumberEnabled(colFmt.valueFormat) ? stripNumericValueFormat(val) : val)) {
854
+ if (condFmt.row) {
855
+ rowStyles.push(condFmt.style);
856
+ }
857
+ else {
858
+ colStyle.styles.push(condFmt.style);
859
+ }
860
+ }
861
+ }
862
+ colStyles.push(colStyle);
863
+ colIdx++;
864
+ }
865
+ // Apply row styles.
866
+ if (rowStyles.length > 0) {
867
+ buf += `\n\t\t<tr class="${rowStyles.join(', ')}">`;
868
+ }
869
+ else {
870
+ buf += '\n\t\t<tr>';
871
+ }
872
+ // Write cell values with styles.
873
+ colIdx = 0;
874
+ for (const rowValue of cellValues) {
875
+ if (colStyles[colIdx].styles.length > 0) {
876
+ buf += `\n\t\t\t<td class="${colStyles[colIdx].styles.join(', ')}">${rowValue}</td>`;
877
+ }
878
+ else {
879
+ buf += `\n\t\t\t<td>${rowValue}</td>`;
880
+ }
881
+ colIdx++;
882
+ }
883
+ buf += '\n\t\t</tr>';
884
+ rowIdx++;
885
+ }
886
+ buf += '\n\t</tbody>\n</table>';
887
+ return buf;
888
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "8.1.2",
3
+ "version": "8.1.4",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",