Skip to content

Commit 229edc3

Browse files
mmalerbajosephperrott
authored andcommitted
feat(datepicker): Add Moment.js adapter (angular#6860)
* create moment adapter * add Moment to karma.conf.js * use MAT_DATE_LOCALE * tests are now running * add in tests * WIP: demo * add synthetic default imports for moment * add locale switching to demo * fix moment import issues with tests * fix aot * address comments * fix closure issue * add additional explanation of rollup issue * remove moment adapter demo since it is preventing sync into g3 (will add back as a docs example in future PR). * fix bad rebase
1 parent bd56ba1 commit 229edc3

22 files changed

+678
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"magic-string": "^0.21.3",
102102
"minimatch": "^3.0.4",
103103
"minimist": "^1.2.0",
104+
"moment": "^2.18.1",
104105
"node-sass": "^4.5.3",
105106
"protractor": "^5.1.2",
106107
"request": "^2.81.0",

scripts/closure-compiler/build-devapp-bundle.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ set -e -o pipefail
88
# Go to the project root directory
99
cd $(dirname $0)/../..
1010

11-
# Build a release of material and of the CDK package.
11+
# Build a release of material, material-moment-adapter, and cdk packages.
1212
$(npm bin)/gulp material:build-release:clean
1313
$(npm bin)/gulp cdk:build-release
14+
$(npm bin)/gulp material-moment-adapter:build-release
1415

1516
# Build demo-app with ES2015 modules. Closure compiler is then able to parse imports.
1617
$(npm bin)/gulp :build:devapp:assets :build:devapp:scss
@@ -38,6 +39,7 @@ OPTS=(
3839
"--js_module_root=dist/packages"
3940
"--js_module_root=dist/releases/material"
4041
"--js_module_root=dist/releases/cdk"
42+
"--js_module_root=dist/releases/material-moment-adapter"
4143
"--js_module_root=node_modules/@angular/core"
4244
"--js_module_root=node_modules/@angular/common"
4345
"--js_module_root=node_modules/@angular/compiler"
@@ -49,6 +51,7 @@ OPTS=(
4951
"--js_module_root=node_modules/@angular/platform-browser-dynamic"
5052
"--js_module_root=node_modules/@angular/animations"
5153
"--js_module_root=node_modules/@angular/animations/browser"
54+
"--js_module_root=node_modules/moment"
5255

5356
# Flags to simplify debugging.
5457
"--formatting=PRETTY_PRINT"
@@ -57,6 +60,7 @@ OPTS=(
5760
# Include the Material and CDK FESM bundles
5861
dist/releases/material/@angular/material.js
5962
dist/releases/cdk/@angular/cdk.js
63+
dist/releases/material-moment-adapter/@angular/material-moment-adapter.js
6064

6165
# Include all Angular FESM bundles.
6266
node_modules/@angular/core/@angular/core.js
@@ -71,8 +75,9 @@ OPTS=(
7175
node_modules/@angular/animations/@angular/animations.js
7276
node_modules/@angular/animations/@angular/animations/browser.js
7377

74-
# Include other dependencies like Zone.js and RxJS
78+
# Include other dependencies like Zone.js, Moment.js, and RxJS
7579
node_modules/zone.js/dist/zone.js
80+
node_modules/moment/moment.js
7681
$rxjsSourceFiles
7782

7883
# Include all files from the demo-app package.

src/demo-app/system-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ System.config({
99
map: {
1010
'rxjs': 'node:rxjs',
1111
'main': 'main.js',
12+
'moment': 'node:moment/min/moment-with-locales.min.js',
1213

1314
// Angular specific mappings.
1415
'@angular/core': 'node:@angular/core/bundles/core.umd.js',
@@ -26,6 +27,7 @@ System.config({
2627
'node:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
2728

2829
'@angular/material': 'dist/bundles/material.umd.js',
30+
'@angular/material-moment-adapter': 'dist/bundles/material-moment-adapter.umd.js',
2931
'@angular/cdk': 'dist/bundles/cdk.umd.js',
3032
'@angular/cdk/a11y': 'dist/bundles/cdk-a11y.umd.js',
3133
'@angular/cdk/bidi': 'dist/bundles/cdk-bidi.umd.js',

src/demo-app/tsconfig-aot.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"outDir": ".",
1212
"paths": {
1313
"@angular/material": ["./material"],
14-
"@angular/cdk/*": ["./cdk/*"]
14+
"@angular/cdk/*": ["./cdk/*"],
15+
"@angular/material-moment-adapter": ["./material-moment-adapter"]
1516
}
1617
},
1718
"files": [

src/demo-app/tsconfig-build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"baseUrl": ".",
2323
"paths": {
2424
"@angular/material": ["../../dist/packages/material/public_api"],
25-
"@angular/cdk/*": ["../../dist/packages/cdk/*"]
25+
"@angular/cdk/*": ["../../dist/packages/cdk/*"],
26+
"@angular/material-moment-adapter": ["../../dist/packages/material-moment-adapter"]
2627
}
2728
},
2829
"files": [

src/lib/core/datetime/date-adapter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88

99
import {InjectionToken, LOCALE_ID} from '@angular/core';
10+
import {Observable} from 'rxjs/Observable';
11+
import {Subject} from 'rxjs/Subject';
12+
1013

1114
/** InjectionToken for datepicker that can be used to override default locale code. */
1215
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE');
@@ -19,6 +22,10 @@ export abstract class DateAdapter<D> {
1922
/** The locale to use for all dates. */
2023
protected locale: any;
2124

25+
/** A stream that emits when the locale changes. */
26+
get localeChanges(): Observable<void> { return this._localeChanges; }
27+
protected _localeChanges= new Subject<void>();
28+
2229
/**
2330
* Gets the year component of the given date.
2431
* @param date The date to extract the year from.
@@ -184,6 +191,7 @@ export abstract class DateAdapter<D> {
184191
*/
185192
setLocale(locale: any) {
186193
this.locale = locale;
194+
this._localeChanges.next();
187195
}
188196

189197
/**

src/lib/datepicker/datepicker-input.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
182182

183183
private _datepickerSubscription = Subscription.EMPTY;
184184

185+
private _localeSubscription = Subscription.EMPTY;
186+
185187
/** The form control validator for whether the input parses. */
186188
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
187189
return this._lastValueValid ?
@@ -228,6 +230,11 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
228230
if (!this._dateFormats) {
229231
throw createMissingDateImplError('MD_DATE_FORMATS');
230232
}
233+
234+
// Update the displayed date when the locale changes.
235+
this._localeSubscription = _dateAdapter.localeChanges.subscribe(() => {
236+
this.value = this.value;
237+
});
231238
}
232239

233240
ngAfterContentInit() {
@@ -245,6 +252,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
245252

246253
ngOnDestroy() {
247254
this._datepickerSubscription.unsubscribe();
255+
this._localeSubscription.unsubscribe();
248256
this._valueChange.complete();
249257
this._disabledChange.complete();
250258
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {NgModule} from '@angular/core';
10+
import {
11+
DateAdapter,
12+
MAT_DATE_LOCALE,
13+
MAT_DATE_LOCALE_PROVIDER,
14+
MD_DATE_FORMATS
15+
} from '@angular/material';
16+
import {MomentDateAdapter} from './moment-date-adapter';
17+
import {MD_MOMENT_DATE_FORMATS} from './moment-date-formats';
18+
19+
export * from './moment-date-adapter';
20+
export * from './moment-date-formats';
21+
22+
23+
@NgModule({
24+
providers: [
25+
MAT_DATE_LOCALE_PROVIDER,
26+
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]}
27+
],
28+
})
29+
export class MomentDateModule {}
30+
31+
32+
@NgModule({
33+
imports: [MomentDateModule],
34+
providers: [{provide: MD_DATE_FORMATS, useValue: MD_MOMENT_DATE_FORMATS}],
35+
})
36+
export class MdMomentDateModule {}

0 commit comments

Comments
 (0)