@cellaware/utils 8.1.1 → 8.1.3

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.
@@ -117,5 +117,12 @@ export interface DatagridCondition {
117
117
  export declare function initDatagridCondition(): DatagridCondition;
118
118
  export declare function summarizeCondition(condition: DatagridCondition, localeMessageFn: (language: string, messageId: string) => Promise<string>): Promise<string>;
119
119
  export declare function codifyCondition(condition: DatagridCondition): string;
120
+ /**
121
+ * Equivalent of `parseNumeric` in front end
122
+ *
123
+ * https://github.com/cellaware/chatwms-az-swa-ng/blob/development/src/app/utils/data.ts#L126
124
+ */
125
+ export declare function stripNumericValueFormat(value: string | number | undefined): any;
120
126
  export declare function transformDatagrid(rows: any[], datagridState: DatagridStateBase, locale: string, condition?: DatagridCondition): DatagridState;
127
+ export declare function datagridToHtml(datagridState: DatagridState): string;
121
128
  export {};
@@ -377,7 +377,7 @@ export function codifyCondition(condition) {
377
377
  *
378
378
  * https://github.com/cellaware/chatwms-az-swa-ng/blob/development/src/app/utils/data.ts#L126
379
379
  */
380
- function stripNumericValueFormat(value) {
380
+ export function stripNumericValueFormat(value) {
381
381
  if (value === undefined)
382
382
  return null;
383
383
  if (typeof value === 'number')
@@ -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><thead><tr>';
820
+ let columns = [];
821
+ for (const key in datagridState.adjRowData[0]) {
822
+ columns.push(key);
823
+ buf += `<th>${key}</th>`;
824
+ }
825
+ buf += '</tr></thead>';
826
+ // Rows:
827
+ buf += '<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 += `<tr class="${rowStyles.join(', ')}">`;
868
+ }
869
+ else {
870
+ buf += '<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 += `<td class="${colStyles[colIdx].styles.join(', ')}">${rowValue}</td>`;
877
+ }
878
+ else {
879
+ buf += `<td>${rowValue}</td>`;
880
+ }
881
+ colIdx++;
882
+ }
883
+ buf += '</tr>';
884
+ rowIdx++;
885
+ }
886
+ buf += '</tbody></table>';
887
+ return buf;
888
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "8.1.1",
3
+ "version": "8.1.3",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",