Skip to content

Commit

Permalink
refactor: code reuse of getCellValueFromQueryFieldGetter
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Sep 14, 2021
1 parent 5bda236 commit 139e24a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
13 changes: 4 additions & 9 deletions packages/common/src/formatters/treeExportFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Constants } from '../constants';
import { Formatter } from './../interfaces/index';
import { addWhiteSpaces, getDescendantProperty, sanitizeHtmlToText, } from '../services/utilities';
import { addWhiteSpaces, getCellValueFromQueryFieldGetter, sanitizeHtmlToText, } from '../services/utilities';
import { parseFormatterWhenExist } from './formatterUtilities';

/** Formatter that must be use with a Tree Data column */
Expand All @@ -17,14 +17,9 @@ export const treeExportFormatter: Formatter = (row, cell, value, columnDef, data
const groupExpandedSymbol = gridOptions?.excelExportOptions?.groupExpandedSymbol ?? '⮟';
let outputValue = value;

if (typeof columnDef.queryFieldNameGetterFn === 'function') {
const fieldName = columnDef.queryFieldNameGetterFn(dataContext);
if (fieldName?.indexOf('.') >= 0) {
outputValue = getDescendantProperty(dataContext, fieldName);
} else {
outputValue = dataContext.hasOwnProperty(fieldName) ? dataContext[fieldName] : value;
}
}
// when a queryFieldNameGetterFn is defined, then get the value from that getter callback function
outputValue = getCellValueFromQueryFieldGetter(columnDef, dataContext, value);

if (outputValue === null || outputValue === undefined || dataContext === undefined) {
return '';
}
Expand Down
13 changes: 4 additions & 9 deletions packages/common/src/formatters/treeFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Constants } from '../constants';
import { Formatter } from './../interfaces/index';
import { parseFormatterWhenExist } from './formatterUtilities';
import { getDescendantProperty, sanitizeTextByAvailableSanitizer } from '../services/utilities';
import { getCellValueFromQueryFieldGetter, sanitizeTextByAvailableSanitizer } from '../services/utilities';

/** Formatter that must be use with a Tree Data column */
export const treeFormatter: Formatter = (row, cell, value, columnDef, dataContext, grid) => {
Expand All @@ -13,14 +13,9 @@ export const treeFormatter: Formatter = (row, cell, value, columnDef, dataContex
const treeLevelPropName = treeDataOptions?.levelPropName ?? Constants.treeDataProperties.TREE_LEVEL_PROP;
let outputValue = value;

if (typeof columnDef.queryFieldNameGetterFn === 'function') {
const fieldName = columnDef.queryFieldNameGetterFn(dataContext);
if (fieldName?.indexOf('.') >= 0) {
outputValue = getDescendantProperty(dataContext, fieldName);
} else {
outputValue = dataContext.hasOwnProperty(fieldName) ? dataContext[fieldName] : value;
}
}
// when a queryFieldNameGetterFn is defined, then get the value from that getter callback function
outputValue = getCellValueFromQueryFieldGetter(columnDef, dataContext, value);

if (outputValue === null || outputValue === undefined || dataContext === undefined) {
return '';
}
Expand Down
29 changes: 3 additions & 26 deletions packages/common/src/plugins/contextMenu.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
MenuOptionItem,
SlickEventHandler,
} from '../interfaces/index';
import { getDescendantProperty, getTranslationPrefix, } from '../services/index';
import { getCellValueFromQueryFieldGetter, getTranslationPrefix, } from '../services/index';
import { exportWithFormatterWhenDefined } from '../formatters/formatterUtilities';
import { ExtensionUtility } from '../extensions/extensionUtility';
import { PubSubService } from '../services/pubSub.service';
Expand Down Expand Up @@ -184,7 +184,7 @@ export class ContextMenuPlugin extends MenuFromCellBaseClass<ContextMenu> {
const columnDef = args?.column as Column;
const dataContext = args?.dataContext;
if (typeof columnDef.queryFieldNameGetterFn === 'function') {
const cellValue = this.getCellValueFromQueryFieldGetter(columnDef, dataContext);
const cellValue = getCellValueFromQueryFieldGetter(columnDef, dataContext, '');
if (cellValue !== '' && cellValue !== undefined) {
return true;
}
Expand Down Expand Up @@ -397,7 +397,7 @@ export class ContextMenuPlugin extends MenuFromCellBaseClass<ContextMenu> {
let textToCopy = exportWithFormatterWhenDefined(row, cell, columnDef, dataContext, grid, exportOptions);

if (typeof columnDef.queryFieldNameGetterFn === 'function') {
textToCopy = this.getCellValueFromQueryFieldGetter(columnDef, dataContext);
textToCopy = getCellValueFromQueryFieldGetter(columnDef, dataContext, '');
}

// create fake <textarea> (positioned outside of the screen) to copy into clipboard & delete it from the DOM once we're done
Expand All @@ -420,29 +420,6 @@ export class ContextMenuPlugin extends MenuFromCellBaseClass<ContextMenu> {
}
}

/**
* When a queryFieldNameGetterFn is defined, then get the value from that getter callback function
* @param columnDef
* @param dataContext
* @return cellValue
*/
protected getCellValueFromQueryFieldGetter(columnDef: Column, dataContext: any): string {
let cellValue = '';

if (typeof columnDef.queryFieldNameGetterFn === 'function') {
const queryFieldName = columnDef.queryFieldNameGetterFn(dataContext);

// get the cell value from the item or when it's a dot notation then exploded the item and get the final value
if (queryFieldName?.indexOf('.') >= 0) {
cellValue = getDescendantProperty(dataContext, queryFieldName);
} else {
cellValue = dataContext[queryFieldName];
}
}

return cellValue;
}

/** sort all menu items by their position order when defined */
protected sortMenuItems() {
const contextMenu = this.sharedService?.gridOptions?.contextMenu;
Expand Down
24 changes: 23 additions & 1 deletion packages/common/src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const moment = (moment_ as any)['default'] || moment_; // patch to fix rollup "m

import { Constants } from '../constants';
import { FieldType, OperatorString, OperatorType } from '../enums/index';
import { EventSubscription, GridOption, } from '../interfaces/index';
import { Column, EventSubscription, GridOption, } from '../interfaces/index';
import { Observable, RxJsFacade, Subject, Subscription } from './rxjsFacade';

/**
Expand Down Expand Up @@ -461,6 +461,28 @@ export function formatNumber(input: number | string, minDecimal?: number, maxDec
}
}

/**
* When a queryFieldNameGetterFn is defined, then get the value from that getter callback function
* @param {Column} columnDef
* @param {Object} dataContext
* @param {String} defaultValue - optional value to use if value isn't found in data context
* @return outputValue
*/
export function getCellValueFromQueryFieldGetter(columnDef: Column, dataContext: any, defaultValue: any): string {
if (typeof columnDef.queryFieldNameGetterFn === 'function') {
const queryFieldName = columnDef.queryFieldNameGetterFn(dataContext);

// get the cell value from the item or when it's a dot notation then exploded the item and get the final value
if (queryFieldName?.indexOf('.') >= 0) {
defaultValue = getDescendantProperty(dataContext, queryFieldName);
} else {
defaultValue = dataContext.hasOwnProperty(queryFieldName) ? dataContext[queryFieldName] : defaultValue;
}
}

return defaultValue;
}

/**
* From a dot (.) notation path, find and return a property within an object given a path
* @param object - object input
Expand Down

0 comments on commit 139e24a

Please sign in to comment.