1
1
import { TranslateService } from '@ngx-translate/core' ;
2
+ import * as moment_ from 'moment-mini' ;
3
+ import { BaseOptions as FlatpickrBaseOptions } from 'flatpickr/dist/types/options' ;
4
+ import * as _flatpickr from 'flatpickr' ;
5
+ import { FlatpickrFn } from 'flatpickr/dist/types/instance' ;
6
+ const flatpickr : FlatpickrFn = _flatpickr as any ; // patch for rollup
7
+ const moment = moment_ ; // patch to fix rollup "moment has no default export" issue, document here https://github.com/rollup/rollup/issues/670
8
+
2
9
import { Constants } from './../constants' ;
3
10
import { mapFlatpickrDateFormatWithFieldType , mapMomentDateFormatWithFieldType , setDeepValue , getDescendantProperty } from './../services/utilities' ;
4
11
import {
@@ -12,8 +19,6 @@ import {
12
19
FlatpickrOption ,
13
20
GridOption ,
14
21
} from './../models/index' ;
15
- import * as moment_ from 'moment-mini' ;
16
- const moment = moment_ ; // patch to fix rollup "moment has no default export" issue, document here https://github.com/rollup/rollup/issues/670
17
22
18
23
declare function require ( name : string ) ;
19
24
require ( 'flatpickr' ) ;
@@ -28,6 +33,8 @@ declare const $: any;
28
33
export class DateEditor implements Editor {
29
34
private _$inputWithData : any ;
30
35
private _$input : any ;
36
+ private _$editorInputElm : any ;
37
+ private _originalDate : string ;
31
38
private _pickerMergedOptions : FlatpickrOption ;
32
39
33
40
/** The translate library */
@@ -109,6 +116,7 @@ export class DateEditor implements Editor {
109
116
altFormat : outputFormat ,
110
117
dateFormat : inputFormat ,
111
118
closeOnSelect : true ,
119
+ wrap : true ,
112
120
locale : ( currentLocale !== 'en' ) ? this . loadFlatpickrLocale ( currentLocale ) : 'en' ,
113
121
onChange : ( ) => this . save ( ) ,
114
122
errorHandler : ( ) => {
@@ -118,14 +126,26 @@ export class DateEditor implements Editor {
118
126
119
127
// merge options with optional user's custom options
120
128
this . _pickerMergedOptions = { ...pickerOptions , ...( this . editorOptions as FlatpickrOption ) } ;
121
- const inputCssClasses = `.editor-text.editor-${ columnId } .flatpickr ` ;
129
+ const inputCssClasses = `.editor-text.editor-${ columnId } .form-control ` ;
122
130
if ( this . _pickerMergedOptions . altInput ) {
123
- this . _pickerMergedOptions . altInputClass = 'flatpickr-alt-input editor-text' ;
131
+ this . _pickerMergedOptions . altInputClass = 'flatpickr-alt-input form-control' ;
132
+ }
133
+
134
+ this . _$editorInputElm = $ ( `<div class="flatpickr input-group"></div>` ) ;
135
+ const closeButtonElm = $ ( `<span class="input-group-btn" data-clear>
136
+ <button class="btn btn-default icon-close" type="button"></button>
137
+ </span>` ) ;
138
+ this . _$input = $ ( `<input type="text" data-input data-defaultDate="${ this . defaultDate } " class="${ inputCssClasses . replace ( / \. / g, ' ' ) } " placeholder="${ placeholder } " title="${ title } " />` ) ;
139
+ this . _$input . appendTo ( this . _$editorInputElm ) ;
140
+
141
+ // show clear date button (unless user specifically doesn't want it)
142
+ const isCloseButtonHidden = this . columnEditor && this . columnEditor . params && this . columnEditor . params . hideClearButton || false ;
143
+ if ( ! isCloseButtonHidden ) {
144
+ closeButtonElm . appendTo ( this . _$editorInputElm ) ;
124
145
}
125
146
126
- this . _$input = $ ( `<input type="text" data-defaultDate="${ this . defaultDate } " class="${ inputCssClasses . replace ( / \. / g, ' ' ) } " placeholder="${ placeholder } " title="${ title } " />` ) ;
127
- this . _$input . appendTo ( this . args . container ) ;
128
- this . flatInstance = ( this . _$input [ 0 ] && typeof this . _$input [ 0 ] . flatpickr === 'function' ) ? this . _$input [ 0 ] . flatpickr ( this . _pickerMergedOptions ) : null ;
147
+ this . _$editorInputElm . appendTo ( this . args . container ) ;
148
+ this . flatInstance = ( flatpickr && this . _$editorInputElm [ 0 ] && typeof this . _$editorInputElm [ 0 ] . flatpickr === 'function' ) ? this . _$editorInputElm [ 0 ] . flatpickr ( this . _pickerMergedOptions ) : flatpickr ( this . _$editorInputElm , this . _pickerMergedOptions as unknown as Partial < FlatpickrBaseOptions > ) ;
129
149
130
150
// when we're using an alternate input to display data, we'll consider this input as the one to do the focus later on
131
151
// else just use the top one
@@ -141,6 +161,9 @@ export class DateEditor implements Editor {
141
161
destroy ( ) {
142
162
this . hide ( ) ;
143
163
this . _$input . remove ( ) ;
164
+ if ( this . _$editorInputElm && this . _$editorInputElm . remove ) {
165
+ this . _$editorInputElm . remove ( ) ;
166
+ }
144
167
if ( this . _$inputWithData && typeof this . _$inputWithData . remove === 'function' ) {
145
168
this . _$inputWithData . remove ( ) ;
146
169
}
@@ -196,17 +219,18 @@ export class DateEditor implements Editor {
196
219
}
197
220
}
198
221
199
- isValueChanged ( ) {
222
+ isValueChanged ( ) : boolean {
200
223
const elmValue = this . _$input . val ( ) ;
201
224
const inputFormat = mapMomentDateFormatWithFieldType ( this . columnEditor . type || ( this . columnDef && this . columnDef . type ) || FieldType . dateIso ) ;
202
225
const outputTypeFormat = mapMomentDateFormatWithFieldType ( ( this . columnDef && ( this . columnDef . outputType || this . columnEditor . type || this . columnDef . type ) ) || FieldType . dateUtc ) ;
203
226
const elmDateStr = elmValue ? moment ( elmValue , inputFormat , false ) . format ( outputTypeFormat ) : '' ;
204
- const orgDateStr = this . originalDate ? moment ( this . originalDate , inputFormat , false ) . format ( outputTypeFormat ) : '' ;
227
+ const orgDateStr = this . _originalDate ? moment ( this . _originalDate , inputFormat , false ) . format ( outputTypeFormat ) : '' ;
205
228
if ( elmDateStr === 'Invalid date' || orgDateStr === 'Invalid date' ) {
206
229
return false ;
207
230
}
208
231
209
- return ( ! ( elmDateStr === '' && orgDateStr === '' ) ) && ( elmDateStr !== orgDateStr ) ;
232
+ const isChanged = ( ! ( elmDateStr === '' && orgDateStr === '' ) ) && ( elmDateStr !== orgDateStr ) ;
233
+ return isChanged ;
210
234
}
211
235
212
236
loadValue ( item : any ) {
@@ -217,10 +241,8 @@ export class DateEditor implements Editor {
217
241
const isComplexObject = fieldName && fieldName . indexOf ( '.' ) > 0 ;
218
242
const value = ( isComplexObject ) ? getDescendantProperty ( item , fieldName ) : item [ fieldName ] ;
219
243
220
- this . originalDate = value ;
244
+ this . _originalDate = value ;
221
245
this . flatInstance . setDate ( value ) ;
222
- this . show ( ) ;
223
- this . focus ( ) ;
224
246
}
225
247
}
226
248
0 commit comments