Skip to content

Commit 32f4a0c

Browse files
authored
refactor: move common editor code to BaseEditorClass (#2595)
* refactor: move common editor code to BaseEditorClass
1 parent db293cc commit 32f4a0c

9 files changed

Lines changed: 159 additions & 621 deletions

File tree

packages/common/src/editors/autocompleterEditor.ts

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import { BindingEventService } from '@slickgrid-universal/binding';
21
import { classNameToList, createDomElement, isObject, isPrimitiveValue, setDeepValue, toKebabCase } from '@slickgrid-universal/utils';
32
import autocompleter from 'autocompleter';
43
import type { AutocompleteItem, AutocompleteResult, AutocompleteSettings } from 'autocompleter';
54
import { addAutocompleteLoadingByOverridingFetch } from '../commonEditorFilter/commonEditorFilterUtils.js';
6-
import { applyHtmlToElement, SlickEventData, type SlickGrid } from '../core/index.js';
5+
import { applyHtmlToElement } from '../core/index.js';
76
import { textValidator } from '../editorValidators/textValidator.js';
87
import type {
98
AutocompleterOption,
109
AutocompleteSearchItem,
1110
CollectionCustomStructure,
1211
CollectionOverrideArgs,
13-
Column,
14-
ColumnEditor,
1512
CompositeEditorOption,
1613
Editor,
1714
EditorArguments,
1815
EditorValidationResult,
19-
EditorValidator,
20-
GridOption,
2116
Locale,
2217
ValidateOption,
2318
} from '../interfaces/index.js';
2419
import type { TranslaterService } from '../services/translater.service.js';
2520
import { findOrDefault, getDescendantProperty } from '../services/utilities.js';
2621
import { Constants } from './../constants.js';
22+
import { BaseEditorClass } from './baseEditorClass.js';
2723

2824
// minimum length of chars to type before starting to start querying
2925
const MIN_LENGTH = 3;
@@ -32,9 +28,8 @@ const MIN_LENGTH = 3;
3228
* An example of a 'detached' editor.
3329
* KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
3430
*/
35-
export class AutocompleterEditor<T extends AutocompleteItem = any> implements Editor {
31+
export class AutocompleterEditor<T extends AutocompleteItem = any> extends BaseEditorClass implements Editor {
3632
protected _autocompleterOptions!: Partial<AutocompleterOption<T>>;
37-
protected _bindEventService: BindingEventService;
3833
protected _currentValue: any;
3934
protected _defaultTextValue!: string;
4035
protected _originalValue: any;
@@ -56,12 +51,6 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
5651
/** The translate library */
5752
protected _translater?: TranslaterService;
5853

59-
/** is the Editor disabled? */
60-
disabled = false;
61-
62-
/** SlickGrid Grid object */
63-
grid: SlickGrid;
64-
6554
/** The property name for labels in the collection */
6655
labelName!: string;
6756

@@ -80,15 +69,13 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
8069
finalCollection: T[] = [];
8170

8271
constructor(protected readonly args: EditorArguments) {
83-
this.grid = args.grid;
84-
this._bindEventService = new BindingEventService();
72+
super(args);
8573
if (this.gridOptions?.translater) {
8674
this._translater = this.gridOptions.translater;
8775
}
8876

8977
// get locales provided by user in forRoot or else use default English locales via the Constants
9078
this._locales = this.gridOptions?.locales || Constants.locales;
91-
9279
this.init();
9380
}
9481

@@ -112,16 +99,6 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
11299
return this._elementCollection;
113100
}
114101

115-
/** Get Column Definition object */
116-
get columnDef(): Column {
117-
return this.args.column;
118-
}
119-
120-
/** Get Column Editor object */
121-
get columnEditor(): ColumnEditor {
122-
return this.columnDef?.editor || ({} as ColumnEditor);
123-
}
124-
125102
/** Getter for the Custom Structure if exist */
126103
get customStructure(): CollectionCustomStructure | undefined {
127104
let customStructure = this.columnEditor?.customStructure;
@@ -144,25 +121,11 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
144121
return { ...this.gridOptions.defaultEditorOptions?.autocompleter, ...this.columnEditor?.options };
145122
}
146123

147-
/** Getter for the Grid Options pulled through the Grid Object */
148-
get gridOptions(): GridOption {
149-
return this.grid?.getOptions() ?? {};
150-
}
151-
152124
/** Kraaden AutoComplete instance */
153125
get instance(): AutocompleteResult | undefined {
154126
return this._instance;
155127
}
156128

157-
get hasAutoCommitEdit(): boolean {
158-
return this.gridOptions.autoCommitEdit ?? false;
159-
}
160-
161-
/** Get the Validator function, can be passed in Editor property or Column Definition */
162-
get validator(): EditorValidator | undefined {
163-
return this.columnEditor?.validator ?? this.columnDef?.validator;
164-
}
165-
166129
init(): void {
167130
this.labelName = this.customStructure?.label ?? 'label';
168131
this.valueName = this.customStructure?.value ?? 'value';
@@ -441,17 +404,7 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
441404

442405
/** when it's a Composite Editor, we'll check if the Editor is editable (by checking onBeforeEditCell) and if not Editable we'll disable the Editor */
443406
protected applyInputUsabilityState(): void {
444-
const activeCell = this.grid.getActiveCell();
445-
const isCellEditable = this.grid.onBeforeEditCell
446-
.notify({
447-
...activeCell,
448-
item: this.dataContext,
449-
column: this.args.column,
450-
grid: this.grid,
451-
target: 'composite',
452-
compositeEditorOptions: this.args.compositeEditorOptions,
453-
})
454-
.getReturnValue();
407+
const isCellEditable = super.checkInputUsabilityState();
455408
this.disable(isCellEditable === false);
456409
}
457410

@@ -461,11 +414,6 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
461414
triggeredBy: 'user' | 'system' = 'user',
462415
isCalledByClearValue = false
463416
): void {
464-
const activeCell = this.grid.getActiveCell();
465-
const column = this.args.column;
466-
const columnId = this.columnDef?.id ?? '';
467-
const item = this.dataContext;
468-
const grid = this.grid;
469417
const newValue = this.serializeValue();
470418

471419
// when valid, we'll also apply the new value to the dataContext item object
@@ -474,25 +422,7 @@ export class AutocompleterEditor<T extends AutocompleteItem = any> implements Ed
474422
}
475423
this.applyValue(compositeEditorOptions.formValues, newValue);
476424

477-
const isExcludeDisabledFieldFormValues = this.gridOptions?.compositeEditorOptions?.excludeDisabledFieldFormValues ?? false;
478-
if (
479-
isCalledByClearValue ||
480-
(this.disabled && isExcludeDisabledFieldFormValues && compositeEditorOptions.formValues.hasOwnProperty(columnId))
481-
) {
482-
delete compositeEditorOptions.formValues[columnId]; // when the input is disabled we won't include it in the form result object
483-
}
484-
grid.onCompositeEditorChange.notify(
485-
{
486-
...activeCell,
487-
item,
488-
grid,
489-
column,
490-
formValues: compositeEditorOptions.formValues,
491-
editors: compositeEditorOptions.editors,
492-
triggeredBy,
493-
},
494-
new SlickEventData(event)
495-
);
425+
super.handleChangeOnCompositeEditor(event, compositeEditorOptions, triggeredBy, isCalledByClearValue);
496426
}
497427

498428
// this function should be protected but for unit tests purposes we'll make it public until a better solution is found
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { BindingEventService } from '@slickgrid-universal/binding';
2+
import { SlickEventData, type SlickGrid } from '../core/index.js';
3+
import type { Column, ColumnEditor, CompositeEditorOption, EditorArguments, EditorValidator, GridOption } from './../interfaces/index.js';
4+
5+
/*
6+
* An example of a 'detached' editor.
7+
* KeyDown events are also handled to provide handling for Tab, Shift-Tab, Esc and Ctrl-Enter.
8+
*/
9+
export class BaseEditorClass {
10+
protected _bindEventService: BindingEventService;
11+
/** is the Editor disabled? */
12+
disabled = false;
13+
14+
/** SlickGrid Grid object */
15+
protected grid: SlickGrid;
16+
17+
/** Grid options */
18+
protected gridOptions: GridOption;
19+
20+
constructor(protected readonly args: EditorArguments) {
21+
this.grid = args.grid;
22+
this.gridOptions = (this.grid.getOptions() || {}) as GridOption;
23+
this._bindEventService = new BindingEventService();
24+
}
25+
26+
/** Get Column Definition object */
27+
get columnDef(): Column {
28+
return this.args.column;
29+
}
30+
31+
/** Get Column Editor object */
32+
get columnEditor(): ColumnEditor {
33+
return this.columnDef?.editor || ({} as ColumnEditor);
34+
}
35+
36+
/** Getter for the item data context object */
37+
get dataContext(): any {
38+
return this.args.item;
39+
}
40+
41+
get hasAutoCommitEdit(): boolean {
42+
return this.gridOptions.autoCommitEdit ?? false;
43+
}
44+
45+
/** Get the Validator function, can be passed in Editor property or Column Definition */
46+
get validator(): EditorValidator | undefined {
47+
return this.columnEditor?.validator ?? this.columnDef?.validator;
48+
}
49+
50+
// --
51+
// protected functions
52+
// ------------------
53+
54+
/** when it's a Composite Editor, we'll check if the Editor is editable (by checking onBeforeEditCell) and if not Editable we'll disable the Editor */
55+
protected checkInputUsabilityState(): boolean {
56+
const activeCell = this.grid.getActiveCell();
57+
const isCellEditable = this.grid.onBeforeEditCell
58+
.notify({
59+
...activeCell,
60+
item: this.dataContext,
61+
column: this.args.column,
62+
grid: this.grid,
63+
target: 'composite',
64+
compositeEditorOptions: this.args.compositeEditorOptions,
65+
})
66+
.getReturnValue();
67+
return isCellEditable;
68+
}
69+
70+
protected handleChangeOnCompositeEditor(
71+
event: Event | null,
72+
compositeEditorOptions: CompositeEditorOption,
73+
triggeredBy: 'user' | 'system' = 'user',
74+
isCalledByClearValue = false
75+
): void {
76+
const activeCell = this.grid.getActiveCell();
77+
const column = this.args.column;
78+
const columnId = this.columnDef?.id ?? '';
79+
const item = this.dataContext;
80+
const grid = this.grid;
81+
82+
const isExcludeDisabledFieldFormValues = this.gridOptions?.compositeEditorOptions?.excludeDisabledFieldFormValues ?? false;
83+
if (
84+
isCalledByClearValue ||
85+
(this.disabled && isExcludeDisabledFieldFormValues && compositeEditorOptions.formValues.hasOwnProperty(columnId))
86+
) {
87+
delete compositeEditorOptions.formValues[columnId]; // when the input is disabled we won't include it in the form result object
88+
}
89+
grid.onCompositeEditorChange.notify(
90+
{
91+
...activeCell,
92+
item,
93+
grid,
94+
column,
95+
formValues: compositeEditorOptions.formValues,
96+
editors: compositeEditorOptions.editors,
97+
triggeredBy,
98+
},
99+
new SlickEventData(event)
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)