Skip to content

Commit 2475ae9

Browse files
vorobeyAndrei Vorobev
andauthored
ColorBox: remove m prefix and fix some more type and eslint errors (#33754)
Co-authored-by: Andrei Vorobev <andrei.vorobev@devexpress.com>
1 parent cf75a82 commit 2475ae9

11 files changed

Lines changed: 100 additions & 56 deletions

File tree

packages/devextreme/js/__internal/m_color.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,27 @@ const standardColorTypes = [
259259
// eslint-disable-next-line @typescript-eslint/naming-convention
260260
const _round = Math.round;
261261

262+
export interface ColorInstance {
263+
baseColor: string | undefined;
264+
r: number;
265+
g: number;
266+
b: number;
267+
a: number;
268+
hsv: { h: number; s: number; v: number };
269+
hsl: { h: number; s: number; l: number };
270+
colorIsInvalid: boolean;
271+
highlight: (step?: number) => string;
272+
darken: (step?: number) => string;
273+
alter: (step: number) => ColorInstance;
274+
blend: (blendColor: ColorInstance | string, opacity: number) => ColorInstance;
275+
toHex: () => string;
276+
getPureColor: () => ColorInstance;
277+
isValidHex: (hex: string) => boolean;
278+
isValidRGB: (r: number | undefined, g: number | undefined, b: number | undefined) => boolean;
279+
isValidAlpha: (a: number) => boolean;
280+
fromHSL: (hsl: { h: number; s: number; l: number }) => ColorInstance;
281+
}
282+
262283
function Color(value?) {
263284
this.baseColor = value;
264285
let color;
@@ -495,10 +516,10 @@ function isIntegerBetweenMinAndMax(number, min?, max?) {
495516
min = min || 0;
496517
max = max || 255;
497518

498-
if (number % 1 !== 0
519+
if (typeof number !== 'number'
520+
|| number % 1 !== 0
499521
|| number < min
500522
|| number > max
501-
|| typeof number !== 'number'
502523
|| isNaN(number)) {
503524
return false;
504525
}
@@ -580,4 +601,9 @@ Color.prototype = {
580601
},
581602
};
582603

583-
export default Color;
604+
interface ColorConstructor {
605+
prototype: ColorInstance;
606+
new(value?: string): ColorInstance;
607+
}
608+
609+
export default Color as unknown as ColorConstructor;

packages/devextreme/js/__internal/ui/color_box/m_color_box.ts renamed to packages/devextreme/js/__internal/ui/color_box/color_box.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import type { ValueChangedEvent } from '@ts/ui/editor/editor';
1010

1111
import type { PopupProperties } from '../popup/m_popup';
1212
import type Popup from '../popup/m_popup';
13-
import type { ColorViewProperties } from './m_color_view';
14-
import ColorView from './m_color_view';
13+
import type { ColorViewProperties } from './color_view';
14+
import ColorView from './color_view';
1515

1616
const COLOR_BOX_CLASS = 'dx-colorbox';
1717
const COLOR_BOX_INPUT_CLASS = `${COLOR_BOX_CLASS}-input`;
@@ -219,7 +219,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
219219
onValueChanged: ({ event, value: changedValue, previousValue }): void => {
220220
const { applyValueMode: currentValueMode } = this.option();
221221
const isInstantlyMode = currentValueMode === 'instantly';
222-
const isOldValue = colorUtils.makeRgba(changedValue) === previousValue;
222+
const isOldValue = colorUtils.makeRgba(new Color(changedValue)) === previousValue;
223223
const isChangesApplied = isInstantlyMode || this._colorViewEnterKeyPressed;
224224
const isValueCleared = this._shouldSaveEmptyValue;
225225

@@ -238,7 +238,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
238238
_enterKeyHandler(e: KeyboardEvent): boolean | undefined {
239239
const newValue = this._input().val();
240240
const { value, editAlphaChannel } = this.option();
241-
const oldValue = value && editAlphaChannel ? colorUtils.makeRgba(value) : value;
241+
const oldValue = value && editAlphaChannel ? colorUtils.makeRgba(new Color(value)) : value;
242242

243243
if (!newValue) return false;
244244

@@ -251,7 +251,8 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
251251
if (newValue !== oldValue) {
252252
this._applyColorFromInput(newValue);
253253
this._saveValueChangeEvent(e);
254-
this.option('value', editAlphaChannel ? colorUtils.makeRgba(newValue) : newValue);
254+
this.option('value', editAlphaChannel
255+
? colorUtils.makeRgba(new Color(newValue)) : newValue);
255256
}
256257

257258
if (this._colorView) {
@@ -281,7 +282,8 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
281282
super._cancelButtonHandler();
282283
}
283284

284-
// needed to be typed in widget.ts
285+
// need to be typed in widget.ts
286+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
285287
_getKeyboardListeners(): any[] {
286288
return super._getKeyboardListeners().concat([this._colorView]);
287289
}
@@ -303,7 +305,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
303305
}
304306

305307
_renderNoColorIcon(): void {
306-
if (!this._$noColorIcon || !this._$noColorIcon.length) {
308+
if (!this._$noColorIcon?.length) {
307309
this._$noColorIcon = $('<i>')
308310
.addClass(`${DX_ICON_CLASS} ${DX_ICON_COLOR_DISMISS}`)
309311
.appendTo(this._$colorResultPreview);
@@ -312,14 +314,14 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
312314

313315
_updateNoColorIndicator(): void {
314316
const { value } = this.option();
315-
const hasValue = Boolean(value);
317+
const hasValue = value !== null && value !== undefined && value.length > 0;
316318

317319
this._$colorBoxInputContainer.toggleClass(COLOR_BOX_COLOR_IS_NOT_DEFINED, !hasValue);
318320

319321
if (hasValue) {
320322
this._cleanNoColorIcon();
321323

322-
colorUtils.makeTransparentBackground(this._$colorResultPreview, value);
324+
colorUtils.makeTransparentBackground(this._$colorResultPreview, new Color(value));
323325
} else {
324326
this._$colorResultPreview.removeAttr('style');
325327
this._renderNoColorIcon();
@@ -344,7 +346,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
344346
_renderValue(): DeferredObj<unknown> {
345347
const { value, editAlphaChannel } = this.option();
346348
const shouldConvertToColor = value && editAlphaChannel;
347-
const text = shouldConvertToColor ? colorUtils.makeRgba(value) : value;
349+
const text = shouldConvertToColor ? colorUtils.makeRgba(new Color(value)) : value;
348350

349351
this.option('text', text);
350352

@@ -388,13 +390,12 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
388390
}
389391

390392
if (editAlphaChannel) {
391-
return colorUtils.makeRgba(value);
393+
return colorUtils.makeRgba(newColor);
392394
}
393395

394396
return value;
395397
}
396398

397-
// eslint-disable-next-line class-methods-use-this
398399
_shouldLogFieldTemplateDeprecationWarning(): boolean {
399400
return true;
400401
}
@@ -435,7 +436,9 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
435436
case 'applyButtonText':
436437
case 'cancelButtonText':
437438
super._optionChanged(args);
438-
this._popup && this._addPopupBottomClasses();
439+
if (this._popup) {
440+
this._addPopupBottomClasses();
441+
}
439442
break;
440443
case 'editAlphaChannel':
441444
case 'keyStep':

packages/devextreme/js/__internal/ui/color_box/m_color_view.ts renamed to packages/devextreme/js/__internal/ui/color_box/color_view.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { Guid } from '@ts/core/m_guid';
1010
import { extend } from '@ts/core/utils/m_extend';
1111
import { getHeight, getOuterHeight, getWidth } from '@ts/core/utils/m_size';
1212
import type { OptionChanged } from '@ts/core/widget/types';
13-
import type { SupportedKeys } from '@ts/core/widget/widget';
13+
import type { SupportedKeyHandler, SupportedKeys } from '@ts/core/widget/widget';
1414
import eventsEngine from '@ts/events/core/m_events_engine';
1515
import { name as clickEventName } from '@ts/events/m_click';
1616
import { isCommandKeyPressed } from '@ts/events/utils/index';
17-
import Color from '@ts/m_color';
17+
import Color, { type ColorInstance } from '@ts/m_color';
1818
import Draggable from '@ts/m_draggable';
1919
import type { EditorProperties, ValueChangedEvent } from '@ts/ui/editor/editor';
2020
import Editor from '@ts/ui/editor/editor';
@@ -85,6 +85,23 @@ export interface ColorViewProperties extends EditorProperties {
8585
target?: string | Element | dxElementWrapper | null;
8686
}
8787

88+
type EditorWithLabelType = new (
89+
element: dxElementWrapper,
90+
options?: object,
91+
) => { registerKeyHandler: (key: string, handler: SupportedKeyHandler) => void };
92+
93+
interface EditorWithLabelOptions {
94+
editorType: EditorWithLabelType;
95+
value: number | string;
96+
onValueChanged: (args: ValueChangedEvent) => void;
97+
labelText: string;
98+
labelAriaText: string;
99+
labelClass: string;
100+
min?: number;
101+
max?: number;
102+
step?: number;
103+
}
104+
88105
class ColorView extends Editor<ColorViewProperties> {
89106
_$palette!: dxElementWrapper;
90107

@@ -100,8 +117,7 @@ class ColorView extends Editor<ColorViewProperties> {
100117

101118
_updateByDrag?: boolean;
102119

103-
// need typings and correct class in m_color.ts
104-
_currentColor!: any;
120+
_currentColor!: ColorInstance;
105121

106122
_isTopColorHue?: boolean;
107123

@@ -401,19 +417,11 @@ class ColorView extends Editor<ColorViewProperties> {
401417
this._renderAlphaChannelElements();
402418
}
403419

404-
_makeTransparentBackground($el: dxElementWrapper, color: any): void {
405-
if (!(color instanceof Color)) {
406-
color = new Color(color);
407-
}
408-
420+
_makeTransparentBackground($el: dxElementWrapper, color: ColorInstance): void {
409421
$el.css('backgroundColor', this._makeRgba(color));
410422
}
411423

412-
_makeRgba(color: any): string {
413-
if (!(color instanceof Color)) {
414-
color = new Color(color);
415-
}
416-
424+
_makeRgba(color: ColorInstance): string {
417425
return `rgba(${[color.r, color.g, color.b, color.a].join(', ')})`;
418426
}
419427

@@ -438,15 +446,12 @@ class ColorView extends Editor<ColorViewProperties> {
438446
if (delta < 0) {
439447
delta = Math.abs(delta);
440448
const rows: dxElementWrapper[] = [];
441-
let i;
442-
for (i = 0; i < delta; i++) {
449+
for (let i = 0; i < delta; i += 1) {
443450
rows.push($('<div>').addClass(COLOR_VIEW_ROW_CLASS));
444451
}
445452

446453
if (renderedRowsCount) {
447-
for (i = 0; i < rows.length; i++) {
448-
$renderedRows.eq(0).after(rows[i]);
449-
}
454+
rows.forEach((row) => { $renderedRows.eq(0).after(row); });
450455
} else {
451456
this._$colorPickerContainer.append(rows);
452457
}
@@ -539,12 +544,12 @@ class ColorView extends Editor<ColorViewProperties> {
539544

540545
_calculateColorValue(paletteHandlePosition: PaletteHandlePosition): number {
541546
const value = Math.floor(paletteHandlePosition.top + this._paletteHandleHeight / 2);
542-
return 100 - Math.round(value * 100 / this._paletteHeight);
547+
return 100 - Math.round((value * 100) / this._paletteHeight);
543548
}
544549

545550
_calculateColorSaturation(paletteHandlePosition: PaletteHandlePosition): number {
546551
const saturation = Math.floor(paletteHandlePosition.left + this._paletteHandleWidth / 2);
547-
return Math.round(saturation * 100 / this._paletteWidth);
552+
return Math.round((saturation * 100) / this._paletteWidth);
548553
}
549554

550555
_updateColorFromHsv(hue: number, saturation: number, value: number): void {
@@ -603,7 +608,7 @@ class ColorView extends Editor<ColorViewProperties> {
603608
_placeHueScaleHandle(): void {
604609
const hueScaleHeight = this._hueScaleWrapperHeight;
605610
const handleHeight = this._hueScaleHandleHeight;
606-
let top = (hueScaleHeight - handleHeight) * (360 - this._currentColor.hsv.h) / 360;
611+
let top = ((hueScaleHeight - handleHeight) * (360 - this._currentColor.hsv.h)) / 360;
607612

608613
if (hueScaleHeight < top + handleHeight) {
609614
top = hueScaleHeight - handleHeight;
@@ -662,7 +667,7 @@ class ColorView extends Editor<ColorViewProperties> {
662667
this._$currentColor = $('<div>').addClass([COLOR_VIEW_COLOR_PREVIEW, COLOR_VIEW_COLOR_PREVIEW_COLOR_NEW].join(' '));
663668
this._$baseColor = $('<div>').addClass([COLOR_VIEW_COLOR_PREVIEW, COLOR_VIEW_COLOR_PREVIEW_COLOR_CURRENT].join(' '));
664669

665-
this._makeTransparentBackground(this._$baseColor, matchValue);
670+
this._makeTransparentBackground(this._$baseColor, new Color(matchValue ?? BLACK_COLOR));
666671
this._makeTransparentBackground(this._$currentColor, this._currentColor);
667672

668673
$colorsPreviewContainerInner.append([this._$baseColor, this._$currentColor]);
@@ -718,7 +723,7 @@ class ColorView extends Editor<ColorViewProperties> {
718723
];
719724
}
720725

721-
_renderEditorWithLabel(options): dxElementWrapper {
726+
_renderEditorWithLabel(options: EditorWithLabelOptions): dxElementWrapper {
722727
const $editor = $('<div>');
723728
const $label = $('<label>')
724729
.addClass(options.labelClass)
@@ -747,7 +752,7 @@ class ColorView extends Editor<ColorViewProperties> {
747752
editorOptions.step = options.step || 1;
748753
}
749754

750-
const editor = new EditorConstructor($editor, editorOptions);
755+
const editor = new (EditorConstructor)($editor, editorOptions);
751756

752757
editor.registerKeyHandler('enter', (e) => {
753758
this._fireEnterKeyPressed(e);
@@ -758,7 +763,7 @@ class ColorView extends Editor<ColorViewProperties> {
758763
return $label;
759764
}
760765

761-
hexInputOptions() {
766+
hexInputOptions(): EditorWithLabelOptions {
762767
return {
763768
editorType: TextBox,
764769
value: this._currentColor.toHex().replace('#', ''),
@@ -829,7 +834,9 @@ class ColorView extends Editor<ColorViewProperties> {
829834
onValueChanged: (args) => {
830835
let { value } = args;
831836
value = this._currentColor.isValidAlpha(value) ? value : this._currentColor.a;
832-
args.event && this._saveValueChangeEvent(args.event);
837+
if (args.event) {
838+
this._saveValueChangeEvent(args.event);
839+
}
833840
this._updateColorTransparency(value);
834841
this._placeAlphaChannelHandle();
835842
},
@@ -860,7 +867,8 @@ class ColorView extends Editor<ColorViewProperties> {
860867
onDragMove: ({ event }) => {
861868
this._updateByDrag = true;
862869
const $alphaChannelHandle = this._$alphaChannelHandle;
863-
const alphaChannelHandlePosition = locate($alphaChannelHandle).left + this._alphaChannelHandleWidth / 2;
870+
const alphaChannelHandlePosition = locate($alphaChannelHandle).left
871+
+ this._alphaChannelHandleWidth / 2;
864872
this._saveValueChangeEvent(event);
865873
this._calculateColorTransparencyByScaleWidth(alphaChannelHandlePosition);
866874
},
@@ -934,15 +942,21 @@ class ColorView extends Editor<ColorViewProperties> {
934942
}
935943

936944
_updateColor(isHex: boolean, args: ValueChangedEvent): void {
937-
let rgba;
945+
let rgba: number[] = [];
938946
let newColor = '';
939947

940948
if (isHex) {
941949
newColor = this._validateHex(`#${this._hexInput.option('value') as string}`);
942950
} else {
943951
rgba = this._validateRgb();
944952
if (this._alphaChannelInput) {
945-
rgba.push(this._alphaChannelInput.option('value'));
953+
const { value: alphaValue } = this._alphaChannelInput.option();
954+
const isValidAlpha = alphaValue !== undefined
955+
&& this._currentColor.isValidAlpha(alphaValue);
956+
957+
const valueToAdd = isValidAlpha ? alphaValue : this._currentColor.a;
958+
959+
rgba.push(valueToAdd);
946960
newColor = `rgba(${rgba.join(', ')})`;
947961
} else {
948962
newColor = `rgb(${rgba.join(', ')})`;
@@ -958,15 +972,16 @@ class ColorView extends Editor<ColorViewProperties> {
958972
}
959973

960974
_validateHex(hex: string): string {
961-
return this._currentColor.isValidHex(hex) ? hex : this._currentColor.toHex() as string;
975+
return this._currentColor.isValidHex(hex) ? hex : this._currentColor.toHex();
962976
}
963977

964978
_validateRgb(): number[] {
965979
let { value: r } = this._rgbInputs[0].option();
966980
let { value: g } = this._rgbInputs[1].option();
967981
let { value: b } = this._rgbInputs[2].option();
968982

969-
if (!this._currentColor.isValidRGB(r, g, b)) {
983+
const isInvalidRgb = !this._currentColor.isValidRGB(r, g, b);
984+
if (isInvalidRgb) {
970985
r = this._currentColor.r;
971986
g = this._currentColor.g;
972987
b = this._currentColor.b;

packages/devextreme/js/__internal/ui/html_editor/modules/m_toolbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@js/ui/select_box';
2-
import '@ts/ui/color_box/m_color_view';
2+
import '@ts/ui/color_box/color_view';
33
import '@js/ui/number_box';
44
import '@js/ui/menu';
55

packages/devextreme/js/ui/color_box.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ColorBox from '../__internal/ui/color_box/m_color_box';
1+
import ColorBox from '../__internal/ui/color_box/color_box';
22
export default ColorBox;
33

44
// STYLE colorBox
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import ColorView from '../../__internal/ui/color_box/m_color_view';
1+
import ColorView from '../../__internal/ui/color_box/color_view';
22

33
export default ColorView;

0 commit comments

Comments
 (0)