1- import Color from '@js/color' ;
21import registerComponent from '@js/core/component_registrator' ;
32import type { dxElementWrapper } from '@js/core/renderer' ;
43import $ from '@js/core/renderer' ;
4+ import type { DeferredObj } from '@js/core/utils/deferred' ;
55import type { Properties } from '@js/ui/color_box' ;
66import type { OptionChanged } from '@ts/core/widget/types' ;
7+ import Color from '@ts/m_color' ;
78import DropDownEditor from '@ts/ui/drop_down_editor/m_drop_down_editor' ;
9+ import type { ValueChangedEvent } from '@ts/ui/editor/editor' ;
810
911import type { PopupProperties } from '../popup/m_popup' ;
1012import type Popup from '../popup/m_popup' ;
@@ -29,14 +31,21 @@ export const DX_ICON_COLOR_DISMISS = 'dx-icon-colordismiss';
2931
3032const colorEditorPrototype = ColorView . prototype ;
3133const colorUtils = {
32- makeTransparentBackground : colorEditorPrototype . _makeTransparentBackground . bind ( colorEditorPrototype ) ,
34+ makeTransparentBackground :
35+ colorEditorPrototype . _makeTransparentBackground . bind ( colorEditorPrototype ) ,
3336 makeRgba : colorEditorPrototype . _makeRgba . bind ( colorEditorPrototype ) ,
3437} ;
3538
3639export interface ColorBoxProperties extends Omit < Properties ,
3740'onClosed' | 'onOpened'
38- | 'onCopy' | 'onCut' | 'onEnterKey' | 'onFocusIn' | 'onFocusOut' | 'onInput' | 'onKeyDown' | 'onKeyUp' | 'onPaste'
39- | 'onValueChanged' | 'validationMessagePosition' | 'onContentReady' | 'onDisposing' | 'onOptionChanged' | 'onInitialized' > {
41+ | 'onCopy' | 'onCut'
42+ | 'onEnterKey' | 'onFocusIn'
43+ | 'onFocusOut' | 'onInput'
44+ | 'onKeyDown' | 'onKeyUp' | 'onPaste'
45+ | 'onValueChanged' | 'validationMessagePosition'
46+ | 'onContentReady' | 'onDisposing'
47+ | 'onOptionChanged' | 'onInitialized' > {
48+ buttonsLocation ?: string ;
4049}
4150
4251class ColorBox extends DropDownEditor < ColorBoxProperties > {
@@ -55,17 +64,19 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
5564 _$colorBoxInputContainer ! : dxElementWrapper ;
5665
5766 _supportedKeys ( ) : Record < string , ( e : KeyboardEvent ) => boolean | undefined > {
58- // @ts -expect-error ts-error
59- const arrowHandler = function ( e ) {
67+ const arrowHandler = ( e : KeyboardEvent ) : boolean => {
6068 e . stopPropagation ( ) ;
61- if ( this . option ( 'opened' ) ) {
69+ const { opened } = this . option ( ) ;
70+ if ( opened ) {
6271 e . preventDefault ( ) ;
6372 return true ;
6473 }
74+ return false ;
6575 } ;
6676
67- const upArrowHandler = function ( e ) {
68- if ( ! this . option ( 'opened' ) ) {
77+ const upArrowHandler = ( e : KeyboardEvent ) : boolean => {
78+ const { opened } = this . option ( ) ;
79+ if ( ! opened ) {
6980 e . preventDefault ( ) ;
7081 return false ;
7182 }
@@ -76,12 +87,13 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
7687 return true ;
7788 } ;
7889
79- const downArrowHandler = function ( e ) {
80- if ( ! this . option ( 'opened' ) && ! e . altKey ) {
90+ const downArrowHandler = ( e : KeyboardEvent ) : boolean => {
91+ const { opened } = this . option ( ) ;
92+ if ( ! opened && ! e . altKey ) {
8193 e . preventDefault ( ) ;
8294 return false ;
8395 }
84- if ( ! this . option ( ' opened' ) && e . altKey ) {
96+ if ( ! opened && e . altKey ) {
8597 this . _validatedOpening ( ) ;
8698 return false ;
8799 }
@@ -104,18 +116,18 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
104116 editAlphaChannel : false ,
105117 applyValueMode : 'useButtons' ,
106118 keyStep : 1 ,
107- // @ts -expect-error ts-error
119+ // @ts -expect-error fieldTemplate is deprecated --- IGNORE ---
108120 fieldTemplate : null ,
109121 buttonsLocation : 'bottom after' ,
110122 } ;
111123 }
112124
113125 _popupHidingHandler ( ) : void {
114126 super . _popupHidingHandler ( ) ;
115- const { applyValueMode } = this . option ( ) ;
127+ const { applyValueMode, value } = this . option ( ) ;
116128
117129 if ( applyValueMode === 'useButtons' ) {
118- this . _updateColorViewValue ( this . option ( ' value' ) ) ;
130+ this . _updateColorViewValue ( value ) ;
119131 }
120132 }
121133
@@ -164,8 +176,8 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
164176 this . _colorView = this . _createComponent ( $colorView , ColorView , this . _colorViewConfig ( ) ) ;
165177 }
166178
167- _applyNewColor ( value ) : void {
168- this . option ( 'value' , value ) ;
179+ _applyNewColor ( newValue : string | null | undefined ) : void {
180+ this . option ( 'value' , newValue ) ;
169181
170182 this . _updateNoColorIndicator ( ) ;
171183
@@ -184,8 +196,6 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
184196 stylingMode,
185197 } = this . option ( ) ;
186198
187- const that = this ;
188-
189199 return {
190200 value,
191201 matchValue : value ,
@@ -194,36 +204,38 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
194204 focusStateEnabled,
195205 stylingMode,
196206 target : this . _input ( ) ,
197- onEnterKeyPressed ( { event } ) {
198- that . _colorViewEnterKeyPressed = true ;
199- if ( that . _colorView . option ( 'value' ) !== that . option ( 'value' ) ) {
200- that . _saveValueChangeEvent ( event ) ;
201- that . _applyNewColor ( that . _colorView . option ( 'value' ) ) ;
202- that . close ( ) ;
207+ onEnterKeyPressed : ( e : ValueChangedEvent < KeyboardEvent > ) : void => {
208+ const { event } = e ;
209+ const { value : optionValue } = this . option ( ) ;
210+ this . _colorViewEnterKeyPressed = true ;
211+ if ( this . _colorView . option ( 'value' ) !== optionValue ) {
212+ this . _saveValueChangeEvent ( event ) ;
213+ const { value : colorViewValue } = this . _colorView . option ( ) ;
214+ this . _applyNewColor ( colorViewValue ) ;
215+ this . close ( ) ;
203216 }
204217 } ,
205218
206- onValueChanged ( { event, value, previousValue } ) {
207- // @ts -expect-error ts-error
208- const instantlyMode = that . option ( 'applyValueMode' ) === 'instantly' ;
209- const isOldValue = colorUtils . makeRgba ( value ) === previousValue ;
210- const changesApplied = instantlyMode || that . _colorViewEnterKeyPressed ;
211- const valueCleared = that . _shouldSaveEmptyValue ;
219+ onValueChanged : ( { event, value : changedValue , previousValue } ) : void => {
220+ const { applyValueMode : currentValueMode } = this . option ( ) ;
221+ const isInstantlyMode = currentValueMode === 'instantly' ;
222+ const isOldValue = colorUtils . makeRgba ( changedValue ) === previousValue ;
223+ const isChangesApplied = isInstantlyMode || this . _colorViewEnterKeyPressed ;
224+ const isValueCleared = this . _shouldSaveEmptyValue ;
212225
213- if ( isOldValue || ! changesApplied || valueCleared ) {
226+ if ( isOldValue || ! isChangesApplied || isValueCleared ) {
214227 return ;
215228 }
216229
217230 if ( event ) {
218- // @ts -expect-error ts-error
219- that . _saveValueChangeEvent ( event ) ;
231+ this . _saveValueChangeEvent ( event ) ;
220232 }
221- that . _applyNewColor ( value ) ;
233+ this . _applyNewColor ( changedValue ) ;
222234 } ,
223235 } ;
224236 }
225237
226- _enterKeyHandler ( e ) {
238+ _enterKeyHandler ( e : KeyboardEvent ) : boolean | undefined {
227239 const newValue = this . _input ( ) . val ( ) ;
228240 const { value, editAlphaChannel } = this . option ( ) ;
229241 const oldValue = value && editAlphaChannel ? colorUtils . makeRgba ( value ) : value ;
@@ -234,17 +246,16 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
234246
235247 if ( color . colorIsInvalid ) {
236248 this . _input ( ) . val ( oldValue === null ? undefined : oldValue ) ;
237- return ;
249+ return false ;
238250 }
239- // @ts -expect-error ts-error
240251 if ( newValue !== oldValue ) {
241252 this . _applyColorFromInput ( newValue ) ;
242253 this . _saveValueChangeEvent ( e ) ;
243- this . option ( 'value' , this . option ( ' editAlphaChannel' ) ? colorUtils . makeRgba ( newValue ) : newValue ) ;
254+ this . option ( 'value' , editAlphaChannel ? colorUtils . makeRgba ( newValue ) : newValue ) ;
244255 }
245256
246257 if ( this . _colorView ) {
247- const colorViewValue = this . _colorView . option ( 'value' ) ;
258+ const { value : colorViewValue } = this . _colorView . option ( ) ;
248259
249260 if ( value !== colorViewValue ) {
250261 this . _saveValueChangeEvent ( e ) ;
@@ -256,9 +267,10 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
256267 return false ;
257268 }
258269
259- _applyButtonHandler ( e ) : void {
270+ _applyButtonHandler ( e : ValueChangedEvent ) : void {
260271 this . _saveValueChangeEvent ( e . event ) ;
261- this . _applyNewColor ( this . _colorView . option ( 'value' ) ) ;
272+ const { value } = this . _colorView . option ( ) ;
273+ this . _applyNewColor ( value ) ;
262274
263275 super . _applyButtonHandler ( ) ;
264276 }
@@ -269,7 +281,8 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
269281 super . _cancelButtonHandler ( ) ;
270282 }
271283
272- _getKeyboardListeners ( ) {
284+ // needed to be typed in widget.ts
285+ _getKeyboardListeners ( ) : any [ ] {
273286 return super . _getKeyboardListeners ( ) . concat ( [ this . _colorView ] ) ;
274287 }
275288
@@ -328,7 +341,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
328341 this . _updateNoColorIndicator ( ) ;
329342 }
330343
331- _renderValue ( ) {
344+ _renderValue ( ) : DeferredObj < unknown > {
332345 const { value, editAlphaChannel } = this . option ( ) ;
333346 const shouldConvertToColor = value && editAlphaChannel ;
334347 const text = shouldConvertToColor ? colorUtils . makeRgba ( value ) : value ;
@@ -340,13 +353,12 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
340353
341354 _resetInputValue ( ) : void {
342355 const $input = this . _input ( ) ;
343- const value = this . option ( 'value' ) ;
344- // @ts -expect-error ts-error
345- $input . val ( value ) ;
356+ const { value } = this . option ( ) ;
357+ $input . val ( value === null ? undefined : value ) ;
346358 this . _updateColorViewValue ( value ) ;
347359 }
348360
349- _updateColorViewValue ( value ) : void {
361+ _updateColorViewValue ( value : string | null | undefined ) : void {
350362 if ( this . _colorView ) {
351363 this . _colorView . option ( {
352364 value,
@@ -355,7 +367,7 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
355367 }
356368 }
357369
358- _valueChangeEventHandler ( e ) : void {
370+ _valueChangeEventHandler ( e : ValueChangedEvent ) : void {
359371 let value = this . _input ( ) . val ( ) ;
360372
361373 if ( value ) {
@@ -366,13 +378,13 @@ class ColorBox extends DropDownEditor<ColorBoxProperties> {
366378 super . _valueChangeEventHandler ( e , value ) ;
367379 }
368380
369- _applyColorFromInput ( value ) {
370- const { editAlphaChannel } = this . option ( ) ;
381+ _applyColorFromInput ( value : string ) : string {
382+ const { editAlphaChannel, value : optionValue } = this . option ( ) ;
371383 const newColor = new Color ( value ) ;
372384
373385 if ( newColor . colorIsInvalid ) {
374386 this . _resetInputValue ( ) ;
375- return this . option ( 'value' ) ;
387+ return optionValue ?? '' ;
376388 }
377389
378390 if ( editAlphaChannel ) {
0 commit comments