datepicker.js 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  1. /**
  2. * @license
  3. * Copyright Google LLC 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. import { Injectable, NgModule, ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, forwardRef, Inject, Input, Optional, Output, ViewChild, ViewEncapsulation, ElementRef, NgZone, InjectionToken, ViewContainerRef, Directive, Attribute, ContentChild, ɵɵdefineInjectable } from '@angular/core';
  9. import { Subject, merge, Subscription, of } from 'rxjs';
  10. import { take, filter } from 'rxjs/operators';
  11. import { DOWN_ARROW, END, ENTER, HOME, LEFT_ARROW, PAGE_DOWN, PAGE_UP, RIGHT_ARROW, UP_ARROW, SPACE, ESCAPE } from '@angular/cdk/keycodes';
  12. import { DateAdapter, MAT_DATE_FORMATS, mixinColor } from '@angular/material/core';
  13. import { Directionality } from '@angular/cdk/bidi';
  14. import { ComponentPortal, PortalModule } from '@angular/cdk/portal';
  15. import { animate, state, style, transition, trigger } from '@angular/animations';
  16. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  17. import { Overlay, OverlayConfig, OverlayModule } from '@angular/cdk/overlay';
  18. import { DOCUMENT, CommonModule } from '@angular/common';
  19. import { MatDialog, MatDialogModule } from '@angular/material/dialog';
  20. import { NG_VALIDATORS, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
  21. import { MatFormField } from '@angular/material/form-field';
  22. import { MAT_INPUT_VALUE_ACCESSOR } from '@angular/material/input';
  23. import { MatButtonModule } from '@angular/material/button';
  24. import { A11yModule } from '@angular/cdk/a11y';
  25. /**
  26. * @fileoverview added by tsickle
  27. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  28. */
  29. /**
  30. * \@docs-private
  31. * @param {?} provider
  32. * @return {?}
  33. */
  34. function createMissingDateImplError(provider) {
  35. return Error(`MatDatepicker: No provider found for ${provider}. You must import one of the following ` +
  36. `modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a ` +
  37. `custom implementation.`);
  38. }
  39. /**
  40. * @fileoverview added by tsickle
  41. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  42. */
  43. /**
  44. * Datepicker data that requires internationalization.
  45. */
  46. class MatDatepickerIntl {
  47. constructor() {
  48. /**
  49. * Stream that emits whenever the labels here are changed. Use this to notify
  50. * components if the labels have changed after initialization.
  51. */
  52. this.changes = new Subject();
  53. /**
  54. * A label for the calendar popup (used by screen readers).
  55. */
  56. this.calendarLabel = 'Calendar';
  57. /**
  58. * A label for the button used to open the calendar popup (used by screen readers).
  59. */
  60. this.openCalendarLabel = 'Open calendar';
  61. /**
  62. * A label for the previous month button (used by screen readers).
  63. */
  64. this.prevMonthLabel = 'Previous month';
  65. /**
  66. * A label for the next month button (used by screen readers).
  67. */
  68. this.nextMonthLabel = 'Next month';
  69. /**
  70. * A label for the previous year button (used by screen readers).
  71. */
  72. this.prevYearLabel = 'Previous year';
  73. /**
  74. * A label for the next year button (used by screen readers).
  75. */
  76. this.nextYearLabel = 'Next year';
  77. /**
  78. * A label for the previous multi-year button (used by screen readers).
  79. */
  80. this.prevMultiYearLabel = 'Previous 20 years';
  81. /**
  82. * A label for the next multi-year button (used by screen readers).
  83. */
  84. this.nextMultiYearLabel = 'Next 20 years';
  85. /**
  86. * A label for the 'switch to month view' button (used by screen readers).
  87. */
  88. this.switchToMonthViewLabel = 'Choose date';
  89. /**
  90. * A label for the 'switch to year view' button (used by screen readers).
  91. */
  92. this.switchToMultiYearViewLabel = 'Choose month and year';
  93. }
  94. }
  95. MatDatepickerIntl.decorators = [
  96. { type: Injectable, args: [{ providedIn: 'root' },] },
  97. ];
  98. /** @nocollapse */ MatDatepickerIntl.ngInjectableDef = ɵɵdefineInjectable({ factory: function MatDatepickerIntl_Factory() { return new MatDatepickerIntl(); }, token: MatDatepickerIntl, providedIn: "root" });
  99. /**
  100. * @fileoverview added by tsickle
  101. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  102. */
  103. /**
  104. * An internal class that represents the data corresponding to a single calendar cell.
  105. * \@docs-private
  106. */
  107. class MatCalendarCell {
  108. /**
  109. * @param {?} value
  110. * @param {?} displayValue
  111. * @param {?} ariaLabel
  112. * @param {?} enabled
  113. * @param {?=} cssClasses
  114. */
  115. constructor(value, displayValue, ariaLabel, enabled, cssClasses) {
  116. this.value = value;
  117. this.displayValue = displayValue;
  118. this.ariaLabel = ariaLabel;
  119. this.enabled = enabled;
  120. this.cssClasses = cssClasses;
  121. }
  122. }
  123. /**
  124. * An internal component used to display calendar data in a table.
  125. * \@docs-private
  126. */
  127. class MatCalendarBody {
  128. /**
  129. * @param {?} _elementRef
  130. * @param {?} _ngZone
  131. */
  132. constructor(_elementRef, _ngZone) {
  133. this._elementRef = _elementRef;
  134. this._ngZone = _ngZone;
  135. /**
  136. * The number of columns in the table.
  137. */
  138. this.numCols = 7;
  139. /**
  140. * The cell number of the active cell in the table.
  141. */
  142. this.activeCell = 0;
  143. /**
  144. * The aspect ratio (width / height) to use for the cells in the table. This aspect ratio will be
  145. * maintained even as the table resizes.
  146. */
  147. this.cellAspectRatio = 1;
  148. /**
  149. * Emits when a new value is selected.
  150. */
  151. this.selectedValueChange = new EventEmitter();
  152. }
  153. /**
  154. * @param {?} cell
  155. * @return {?}
  156. */
  157. _cellClicked(cell) {
  158. if (cell.enabled) {
  159. this.selectedValueChange.emit(cell.value);
  160. }
  161. }
  162. /**
  163. * @param {?} changes
  164. * @return {?}
  165. */
  166. ngOnChanges(changes) {
  167. /** @type {?} */
  168. const columnChanges = changes['numCols'];
  169. const { rows, numCols } = this;
  170. if (changes['rows'] || columnChanges) {
  171. this._firstRowOffset = rows && rows.length && rows[0].length ? numCols - rows[0].length : 0;
  172. }
  173. if (changes['cellAspectRatio'] || columnChanges || !this._cellPadding) {
  174. this._cellPadding = `${50 * this.cellAspectRatio / numCols}%`;
  175. }
  176. if (columnChanges || !this._cellWidth) {
  177. this._cellWidth = `${100 / numCols}%`;
  178. }
  179. }
  180. /**
  181. * @param {?} rowIndex
  182. * @param {?} colIndex
  183. * @return {?}
  184. */
  185. _isActiveCell(rowIndex, colIndex) {
  186. /** @type {?} */
  187. let cellNumber = rowIndex * this.numCols + colIndex;
  188. // Account for the fact that the first row may not have as many cells.
  189. if (rowIndex) {
  190. cellNumber -= this._firstRowOffset;
  191. }
  192. return cellNumber == this.activeCell;
  193. }
  194. /**
  195. * Focuses the active cell after the microtask queue is empty.
  196. * @return {?}
  197. */
  198. _focusActiveCell() {
  199. this._ngZone.runOutsideAngular((/**
  200. * @return {?}
  201. */
  202. () => {
  203. this._ngZone.onStable.asObservable().pipe(take(1)).subscribe((/**
  204. * @return {?}
  205. */
  206. () => {
  207. /** @type {?} */
  208. const activeCell = this._elementRef.nativeElement.querySelector('.mat-calendar-body-active');
  209. if (activeCell) {
  210. activeCell.focus();
  211. }
  212. }));
  213. }));
  214. }
  215. }
  216. MatCalendarBody.decorators = [
  217. { type: Component, args: [{selector: '[mat-calendar-body]',
  218. template: "<tr *ngIf=\"_firstRowOffset < labelMinRequiredCells\" aria-hidden=\"true\"><td class=\"mat-calendar-body-label\" [attr.colspan]=\"numCols\" [style.paddingTop]=\"_cellPadding\" [style.paddingBottom]=\"_cellPadding\">{{label}}</td></tr><tr *ngFor=\"let row of rows; let rowIndex = index\" role=\"row\"><td *ngIf=\"rowIndex === 0 && _firstRowOffset\" aria-hidden=\"true\" class=\"mat-calendar-body-label\" [attr.colspan]=\"_firstRowOffset\" [style.paddingTop]=\"_cellPadding\" [style.paddingBottom]=\"_cellPadding\">{{_firstRowOffset >= labelMinRequiredCells ? label : ''}}</td><td *ngFor=\"let item of row; let colIndex = index\" role=\"gridcell\" class=\"mat-calendar-body-cell\" [ngClass]=\"item.cssClasses\" [tabindex]=\"_isActiveCell(rowIndex, colIndex) ? 0 : -1\" [class.mat-calendar-body-disabled]=\"!item.enabled\" [class.mat-calendar-body-active]=\"_isActiveCell(rowIndex, colIndex)\" [attr.aria-label]=\"item.ariaLabel\" [attr.aria-disabled]=\"!item.enabled || null\" [attr.aria-selected]=\"selectedValue === item.value\" (click)=\"_cellClicked(item)\" [style.width]=\"_cellWidth\" [style.paddingTop]=\"_cellPadding\" role=\"button\" [style.paddingBottom]=\"_cellPadding\"><div class=\"mat-calendar-body-cell-content\" [class.mat-calendar-body-selected]=\"selectedValue === item.value\" [class.mat-calendar-body-today]=\"todayValue === item.value\">{{item.displayValue}}</div></td></tr>",
  219. styles: [".mat-calendar-body{min-width:224px}.mat-calendar-body-label{height:0;line-height:0;text-align:left;padding-left:4.71429%;padding-right:4.71429%}.mat-calendar-body-cell{position:relative;height:0;line-height:0;text-align:center;outline:0;cursor:pointer}.mat-calendar-body-disabled{cursor:default}.mat-calendar-body-cell-content{position:absolute;top:5%;left:5%;display:flex;align-items:center;justify-content:center;box-sizing:border-box;width:90%;height:90%;line-height:1;border-width:1px;border-style:solid;border-radius:999px}@media (-ms-high-contrast:active){.mat-calendar-body-cell-content{border:none}}@media (-ms-high-contrast:active){.mat-calendar-body-selected,.mat-datepicker-popup:not(:empty){outline:solid 1px}.mat-calendar-body-today{outline:dotted 1px}.cdk-keyboard-focused .mat-calendar-body-active>.mat-calendar-body-cell-content:not(.mat-calendar-body-selected),.cdk-program-focused .mat-calendar-body-active>.mat-calendar-body-cell-content:not(.mat-calendar-body-selected){outline:dotted 2px}}[dir=rtl] .mat-calendar-body-label{text-align:right}"],
  220. host: {
  221. 'class': 'mat-calendar-body',
  222. 'role': 'grid',
  223. 'aria-readonly': 'true'
  224. },
  225. exportAs: 'matCalendarBody',
  226. encapsulation: ViewEncapsulation.None,
  227. changeDetection: ChangeDetectionStrategy.OnPush,
  228. },] },
  229. ];
  230. /** @nocollapse */
  231. MatCalendarBody.ctorParameters = () => [
  232. { type: ElementRef },
  233. { type: NgZone }
  234. ];
  235. MatCalendarBody.propDecorators = {
  236. label: [{ type: Input }],
  237. rows: [{ type: Input }],
  238. todayValue: [{ type: Input }],
  239. selectedValue: [{ type: Input }],
  240. labelMinRequiredCells: [{ type: Input }],
  241. numCols: [{ type: Input }],
  242. activeCell: [{ type: Input }],
  243. cellAspectRatio: [{ type: Input }],
  244. selectedValueChange: [{ type: Output }]
  245. };
  246. /**
  247. * @fileoverview added by tsickle
  248. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  249. */
  250. /** @type {?} */
  251. const DAYS_PER_WEEK = 7;
  252. /**
  253. * An internal component used to display a single month in the datepicker.
  254. * \@docs-private
  255. * @template D
  256. */
  257. class MatMonthView {
  258. /**
  259. * @param {?} _changeDetectorRef
  260. * @param {?} _dateFormats
  261. * @param {?} _dateAdapter
  262. * @param {?=} _dir
  263. */
  264. constructor(_changeDetectorRef, _dateFormats, _dateAdapter, _dir) {
  265. this._changeDetectorRef = _changeDetectorRef;
  266. this._dateFormats = _dateFormats;
  267. this._dateAdapter = _dateAdapter;
  268. this._dir = _dir;
  269. /**
  270. * Emits when a new date is selected.
  271. */
  272. this.selectedChange = new EventEmitter();
  273. /**
  274. * Emits when any date is selected.
  275. */
  276. this._userSelection = new EventEmitter();
  277. /**
  278. * Emits when any date is activated.
  279. */
  280. this.activeDateChange = new EventEmitter();
  281. if (!this._dateAdapter) {
  282. throw createMissingDateImplError('DateAdapter');
  283. }
  284. if (!this._dateFormats) {
  285. throw createMissingDateImplError('MAT_DATE_FORMATS');
  286. }
  287. this._activeDate = this._dateAdapter.today();
  288. }
  289. /**
  290. * The date to display in this month view (everything other than the month and year is ignored).
  291. * @return {?}
  292. */
  293. get activeDate() { return this._activeDate; }
  294. /**
  295. * @param {?} value
  296. * @return {?}
  297. */
  298. set activeDate(value) {
  299. /** @type {?} */
  300. const oldActiveDate = this._activeDate;
  301. /** @type {?} */
  302. const validDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value)) || this._dateAdapter.today();
  303. this._activeDate = this._dateAdapter.clampDate(validDate, this.minDate, this.maxDate);
  304. if (!this._hasSameMonthAndYear(oldActiveDate, this._activeDate)) {
  305. this._init();
  306. }
  307. }
  308. /**
  309. * The currently selected date.
  310. * @return {?}
  311. */
  312. get selected() { return this._selected; }
  313. /**
  314. * @param {?} value
  315. * @return {?}
  316. */
  317. set selected(value) {
  318. this._selected = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  319. this._selectedDate = this._getDateInCurrentMonth(this._selected);
  320. }
  321. /**
  322. * The minimum selectable date.
  323. * @return {?}
  324. */
  325. get minDate() { return this._minDate; }
  326. /**
  327. * @param {?} value
  328. * @return {?}
  329. */
  330. set minDate(value) {
  331. this._minDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  332. }
  333. /**
  334. * The maximum selectable date.
  335. * @return {?}
  336. */
  337. get maxDate() { return this._maxDate; }
  338. /**
  339. * @param {?} value
  340. * @return {?}
  341. */
  342. set maxDate(value) {
  343. this._maxDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  344. }
  345. /**
  346. * @return {?}
  347. */
  348. ngAfterContentInit() {
  349. this._init();
  350. }
  351. /**
  352. * Handles when a new date is selected.
  353. * @param {?} date
  354. * @return {?}
  355. */
  356. _dateSelected(date) {
  357. if (this._selectedDate != date) {
  358. /** @type {?} */
  359. const selectedYear = this._dateAdapter.getYear(this.activeDate);
  360. /** @type {?} */
  361. const selectedMonth = this._dateAdapter.getMonth(this.activeDate);
  362. /** @type {?} */
  363. const selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date);
  364. this.selectedChange.emit(selectedDate);
  365. }
  366. this._userSelection.emit();
  367. }
  368. /**
  369. * Handles keydown events on the calendar body when calendar is in month view.
  370. * @param {?} event
  371. * @return {?}
  372. */
  373. _handleCalendarBodyKeydown(event) {
  374. // TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
  375. // disabled ones from being selected. This may not be ideal, we should look into whether
  376. // navigation should skip over disabled dates, and if so, how to implement that efficiently.
  377. // TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
  378. // disabled ones from being selected. This may not be ideal, we should look into whether
  379. // navigation should skip over disabled dates, and if so, how to implement that efficiently.
  380. /** @type {?} */
  381. const oldActiveDate = this._activeDate;
  382. /** @type {?} */
  383. const isRtl = this._isRtl();
  384. switch (event.keyCode) {
  385. case LEFT_ARROW:
  386. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, isRtl ? 1 : -1);
  387. break;
  388. case RIGHT_ARROW:
  389. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, isRtl ? -1 : 1);
  390. break;
  391. case UP_ARROW:
  392. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, -7);
  393. break;
  394. case DOWN_ARROW:
  395. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, 7);
  396. break;
  397. case HOME:
  398. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, 1 - this._dateAdapter.getDate(this._activeDate));
  399. break;
  400. case END:
  401. this.activeDate = this._dateAdapter.addCalendarDays(this._activeDate, (this._dateAdapter.getNumDaysInMonth(this._activeDate) -
  402. this._dateAdapter.getDate(this._activeDate)));
  403. break;
  404. case PAGE_UP:
  405. this.activeDate = event.altKey ?
  406. this._dateAdapter.addCalendarYears(this._activeDate, -1) :
  407. this._dateAdapter.addCalendarMonths(this._activeDate, -1);
  408. break;
  409. case PAGE_DOWN:
  410. this.activeDate = event.altKey ?
  411. this._dateAdapter.addCalendarYears(this._activeDate, 1) :
  412. this._dateAdapter.addCalendarMonths(this._activeDate, 1);
  413. break;
  414. case ENTER:
  415. case SPACE:
  416. if (!this.dateFilter || this.dateFilter(this._activeDate)) {
  417. this._dateSelected(this._dateAdapter.getDate(this._activeDate));
  418. this._userSelection.emit();
  419. // Prevent unexpected default actions such as form submission.
  420. event.preventDefault();
  421. }
  422. return;
  423. default:
  424. // Don't prevent default or focus active cell on keys that we don't explicitly handle.
  425. return;
  426. }
  427. if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
  428. this.activeDateChange.emit(this.activeDate);
  429. }
  430. this._focusActiveCell();
  431. // Prevent unexpected default actions such as form submission.
  432. event.preventDefault();
  433. }
  434. /**
  435. * Initializes this month view.
  436. * @return {?}
  437. */
  438. _init() {
  439. this._selectedDate = this._getDateInCurrentMonth(this.selected);
  440. this._todayDate = this._getDateInCurrentMonth(this._dateAdapter.today());
  441. this._monthLabel =
  442. this._dateAdapter.getMonthNames('short')[this._dateAdapter.getMonth(this.activeDate)]
  443. .toLocaleUpperCase();
  444. /** @type {?} */
  445. let firstOfMonth = this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), this._dateAdapter.getMonth(this.activeDate), 1);
  446. this._firstWeekOffset =
  447. (DAYS_PER_WEEK + this._dateAdapter.getDayOfWeek(firstOfMonth) -
  448. this._dateAdapter.getFirstDayOfWeek()) % DAYS_PER_WEEK;
  449. this._initWeekdays();
  450. this._createWeekCells();
  451. this._changeDetectorRef.markForCheck();
  452. }
  453. /**
  454. * Focuses the active cell after the microtask queue is empty.
  455. * @return {?}
  456. */
  457. _focusActiveCell() {
  458. this._matCalendarBody._focusActiveCell();
  459. }
  460. /**
  461. * Initializes the weekdays.
  462. * @private
  463. * @return {?}
  464. */
  465. _initWeekdays() {
  466. /** @type {?} */
  467. const firstDayOfWeek = this._dateAdapter.getFirstDayOfWeek();
  468. /** @type {?} */
  469. const narrowWeekdays = this._dateAdapter.getDayOfWeekNames('narrow');
  470. /** @type {?} */
  471. const longWeekdays = this._dateAdapter.getDayOfWeekNames('long');
  472. // Rotate the labels for days of the week based on the configured first day of the week.
  473. /** @type {?} */
  474. let weekdays = longWeekdays.map((/**
  475. * @param {?} long
  476. * @param {?} i
  477. * @return {?}
  478. */
  479. (long, i) => {
  480. return { long, narrow: narrowWeekdays[i] };
  481. }));
  482. this._weekdays = weekdays.slice(firstDayOfWeek).concat(weekdays.slice(0, firstDayOfWeek));
  483. }
  484. /**
  485. * Creates MatCalendarCells for the dates in this month.
  486. * @private
  487. * @return {?}
  488. */
  489. _createWeekCells() {
  490. /** @type {?} */
  491. const daysInMonth = this._dateAdapter.getNumDaysInMonth(this.activeDate);
  492. /** @type {?} */
  493. const dateNames = this._dateAdapter.getDateNames();
  494. this._weeks = [[]];
  495. for (let i = 0, cell = this._firstWeekOffset; i < daysInMonth; i++, cell++) {
  496. if (cell == DAYS_PER_WEEK) {
  497. this._weeks.push([]);
  498. cell = 0;
  499. }
  500. /** @type {?} */
  501. const date = this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), this._dateAdapter.getMonth(this.activeDate), i + 1);
  502. /** @type {?} */
  503. const enabled = this._shouldEnableDate(date);
  504. /** @type {?} */
  505. const ariaLabel = this._dateAdapter.format(date, this._dateFormats.display.dateA11yLabel);
  506. /** @type {?} */
  507. const cellClasses = this.dateClass ? this.dateClass(date) : undefined;
  508. this._weeks[this._weeks.length - 1]
  509. .push(new MatCalendarCell(i + 1, dateNames[i], ariaLabel, enabled, cellClasses));
  510. }
  511. }
  512. /**
  513. * Date filter for the month
  514. * @private
  515. * @param {?} date
  516. * @return {?}
  517. */
  518. _shouldEnableDate(date) {
  519. return !!date &&
  520. (!this.dateFilter || this.dateFilter(date)) &&
  521. (!this.minDate || this._dateAdapter.compareDate(date, this.minDate) >= 0) &&
  522. (!this.maxDate || this._dateAdapter.compareDate(date, this.maxDate) <= 0);
  523. }
  524. /**
  525. * Gets the date in this month that the given Date falls on.
  526. * Returns null if the given Date is in another month.
  527. * @private
  528. * @param {?} date
  529. * @return {?}
  530. */
  531. _getDateInCurrentMonth(date) {
  532. return date && this._hasSameMonthAndYear(date, this.activeDate) ?
  533. this._dateAdapter.getDate(date) : null;
  534. }
  535. /**
  536. * Checks whether the 2 dates are non-null and fall within the same month of the same year.
  537. * @private
  538. * @param {?} d1
  539. * @param {?} d2
  540. * @return {?}
  541. */
  542. _hasSameMonthAndYear(d1, d2) {
  543. return !!(d1 && d2 && this._dateAdapter.getMonth(d1) == this._dateAdapter.getMonth(d2) &&
  544. this._dateAdapter.getYear(d1) == this._dateAdapter.getYear(d2));
  545. }
  546. /**
  547. * @private
  548. * @param {?} obj The object to check.
  549. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  550. */
  551. _getValidDateOrNull(obj) {
  552. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  553. }
  554. /**
  555. * Determines whether the user has the RTL layout direction.
  556. * @private
  557. * @return {?}
  558. */
  559. _isRtl() {
  560. return this._dir && this._dir.value === 'rtl';
  561. }
  562. }
  563. MatMonthView.decorators = [
  564. { type: Component, args: [{selector: 'mat-month-view',
  565. template: "<table class=\"mat-calendar-table\" role=\"presentation\"><thead class=\"mat-calendar-table-header\"><tr><th *ngFor=\"let day of _weekdays\" [attr.aria-label]=\"day.long\">{{day.narrow}}</th></tr><tr><th class=\"mat-calendar-table-header-divider\" colspan=\"7\" aria-hidden=\"true\"></th></tr></thead><tbody mat-calendar-body [label]=\"_monthLabel\" [rows]=\"_weeks\" [todayValue]=\"_todayDate\" [selectedValue]=\"_selectedDate\" [labelMinRequiredCells]=\"3\" [activeCell]=\"_dateAdapter.getDate(activeDate) - 1\" (selectedValueChange)=\"_dateSelected($event)\" (keydown)=\"_handleCalendarBodyKeydown($event)\"></tbody></table>",
  566. exportAs: 'matMonthView',
  567. encapsulation: ViewEncapsulation.None,
  568. changeDetection: ChangeDetectionStrategy.OnPush
  569. },] },
  570. ];
  571. /** @nocollapse */
  572. MatMonthView.ctorParameters = () => [
  573. { type: ChangeDetectorRef },
  574. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_FORMATS,] }] },
  575. { type: DateAdapter, decorators: [{ type: Optional }] },
  576. { type: Directionality, decorators: [{ type: Optional }] }
  577. ];
  578. MatMonthView.propDecorators = {
  579. activeDate: [{ type: Input }],
  580. selected: [{ type: Input }],
  581. minDate: [{ type: Input }],
  582. maxDate: [{ type: Input }],
  583. dateFilter: [{ type: Input }],
  584. dateClass: [{ type: Input }],
  585. selectedChange: [{ type: Output }],
  586. _userSelection: [{ type: Output }],
  587. activeDateChange: [{ type: Output }],
  588. _matCalendarBody: [{ type: ViewChild, args: [MatCalendarBody, { static: false },] }]
  589. };
  590. /**
  591. * @fileoverview added by tsickle
  592. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  593. */
  594. /** @type {?} */
  595. const yearsPerPage = 24;
  596. /** @type {?} */
  597. const yearsPerRow = 4;
  598. /**
  599. * An internal component used to display a year selector in the datepicker.
  600. * \@docs-private
  601. * @template D
  602. */
  603. class MatMultiYearView {
  604. /**
  605. * @param {?} _changeDetectorRef
  606. * @param {?} _dateAdapter
  607. * @param {?=} _dir
  608. */
  609. constructor(_changeDetectorRef, _dateAdapter, _dir) {
  610. this._changeDetectorRef = _changeDetectorRef;
  611. this._dateAdapter = _dateAdapter;
  612. this._dir = _dir;
  613. /**
  614. * Emits when a new year is selected.
  615. */
  616. this.selectedChange = new EventEmitter();
  617. /**
  618. * Emits the selected year. This doesn't imply a change on the selected date
  619. */
  620. this.yearSelected = new EventEmitter();
  621. /**
  622. * Emits when any date is activated.
  623. */
  624. this.activeDateChange = new EventEmitter();
  625. if (!this._dateAdapter) {
  626. throw createMissingDateImplError('DateAdapter');
  627. }
  628. this._activeDate = this._dateAdapter.today();
  629. }
  630. /**
  631. * The date to display in this multi-year view (everything other than the year is ignored).
  632. * @return {?}
  633. */
  634. get activeDate() { return this._activeDate; }
  635. /**
  636. * @param {?} value
  637. * @return {?}
  638. */
  639. set activeDate(value) {
  640. /** @type {?} */
  641. let oldActiveDate = this._activeDate;
  642. /** @type {?} */
  643. const validDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value)) || this._dateAdapter.today();
  644. this._activeDate = this._dateAdapter.clampDate(validDate, this.minDate, this.maxDate);
  645. if (!isSameMultiYearView(this._dateAdapter, oldActiveDate, this._activeDate, this.minDate, this.maxDate)) {
  646. this._init();
  647. }
  648. }
  649. /**
  650. * The currently selected date.
  651. * @return {?}
  652. */
  653. get selected() { return this._selected; }
  654. /**
  655. * @param {?} value
  656. * @return {?}
  657. */
  658. set selected(value) {
  659. this._selected = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  660. this._selectedYear = this._selected && this._dateAdapter.getYear(this._selected);
  661. }
  662. /**
  663. * The minimum selectable date.
  664. * @return {?}
  665. */
  666. get minDate() { return this._minDate; }
  667. /**
  668. * @param {?} value
  669. * @return {?}
  670. */
  671. set minDate(value) {
  672. this._minDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  673. }
  674. /**
  675. * The maximum selectable date.
  676. * @return {?}
  677. */
  678. get maxDate() { return this._maxDate; }
  679. /**
  680. * @param {?} value
  681. * @return {?}
  682. */
  683. set maxDate(value) {
  684. this._maxDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  685. }
  686. /**
  687. * @return {?}
  688. */
  689. ngAfterContentInit() {
  690. this._init();
  691. }
  692. /**
  693. * Initializes this multi-year view.
  694. * @return {?}
  695. */
  696. _init() {
  697. this._todayYear = this._dateAdapter.getYear(this._dateAdapter.today());
  698. // We want a range years such that we maximize the number of
  699. // enabled dates visible at once. This prevents issues where the minimum year
  700. // is the last item of a page OR the maximum year is the first item of a page.
  701. // The offset from the active year to the "slot" for the starting year is the
  702. // *actual* first rendered year in the multi-year view.
  703. /** @type {?} */
  704. const activeYear = this._dateAdapter.getYear(this._activeDate);
  705. /** @type {?} */
  706. const minYearOfPage = activeYear - getActiveOffset(this._dateAdapter, this.activeDate, this.minDate, this.maxDate);
  707. this._years = [];
  708. for (let i = 0, row = []; i < yearsPerPage; i++) {
  709. row.push(minYearOfPage + i);
  710. if (row.length == yearsPerRow) {
  711. this._years.push(row.map((/**
  712. * @param {?} year
  713. * @return {?}
  714. */
  715. year => this._createCellForYear(year))));
  716. row = [];
  717. }
  718. }
  719. this._changeDetectorRef.markForCheck();
  720. }
  721. /**
  722. * Handles when a new year is selected.
  723. * @param {?} year
  724. * @return {?}
  725. */
  726. _yearSelected(year) {
  727. this.yearSelected.emit(this._dateAdapter.createDate(year, 0, 1));
  728. /** @type {?} */
  729. let month = this._dateAdapter.getMonth(this.activeDate);
  730. /** @type {?} */
  731. let daysInMonth = this._dateAdapter.getNumDaysInMonth(this._dateAdapter.createDate(year, month, 1));
  732. this.selectedChange.emit(this._dateAdapter.createDate(year, month, Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth)));
  733. }
  734. /**
  735. * Handles keydown events on the calendar body when calendar is in multi-year view.
  736. * @param {?} event
  737. * @return {?}
  738. */
  739. _handleCalendarBodyKeydown(event) {
  740. /** @type {?} */
  741. const oldActiveDate = this._activeDate;
  742. /** @type {?} */
  743. const isRtl = this._isRtl();
  744. switch (event.keyCode) {
  745. case LEFT_ARROW:
  746. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, isRtl ? 1 : -1);
  747. break;
  748. case RIGHT_ARROW:
  749. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, isRtl ? -1 : 1);
  750. break;
  751. case UP_ARROW:
  752. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, -yearsPerRow);
  753. break;
  754. case DOWN_ARROW:
  755. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, yearsPerRow);
  756. break;
  757. case HOME:
  758. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, -getActiveOffset(this._dateAdapter, this.activeDate, this.minDate, this.maxDate));
  759. break;
  760. case END:
  761. this.activeDate = this._dateAdapter.addCalendarYears(this._activeDate, yearsPerPage - getActiveOffset(this._dateAdapter, this.activeDate, this.minDate, this.maxDate) - 1);
  762. break;
  763. case PAGE_UP:
  764. this.activeDate =
  765. this._dateAdapter.addCalendarYears(this._activeDate, event.altKey ? -yearsPerPage * 10 : -yearsPerPage);
  766. break;
  767. case PAGE_DOWN:
  768. this.activeDate =
  769. this._dateAdapter.addCalendarYears(this._activeDate, event.altKey ? yearsPerPage * 10 : yearsPerPage);
  770. break;
  771. case ENTER:
  772. case SPACE:
  773. this._yearSelected(this._dateAdapter.getYear(this._activeDate));
  774. break;
  775. default:
  776. // Don't prevent default or focus active cell on keys that we don't explicitly handle.
  777. return;
  778. }
  779. if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
  780. this.activeDateChange.emit(this.activeDate);
  781. }
  782. this._focusActiveCell();
  783. // Prevent unexpected default actions such as form submission.
  784. event.preventDefault();
  785. }
  786. /**
  787. * @return {?}
  788. */
  789. _getActiveCell() {
  790. return getActiveOffset(this._dateAdapter, this.activeDate, this.minDate, this.maxDate);
  791. }
  792. /**
  793. * Focuses the active cell after the microtask queue is empty.
  794. * @return {?}
  795. */
  796. _focusActiveCell() {
  797. this._matCalendarBody._focusActiveCell();
  798. }
  799. /**
  800. * Creates an MatCalendarCell for the given year.
  801. * @private
  802. * @param {?} year
  803. * @return {?}
  804. */
  805. _createCellForYear(year) {
  806. /** @type {?} */
  807. let yearName = this._dateAdapter.getYearName(this._dateAdapter.createDate(year, 0, 1));
  808. return new MatCalendarCell(year, yearName, yearName, this._shouldEnableYear(year));
  809. }
  810. /**
  811. * Whether the given year is enabled.
  812. * @private
  813. * @param {?} year
  814. * @return {?}
  815. */
  816. _shouldEnableYear(year) {
  817. // disable if the year is greater than maxDate lower than minDate
  818. if (year === undefined || year === null ||
  819. (this.maxDate && year > this._dateAdapter.getYear(this.maxDate)) ||
  820. (this.minDate && year < this._dateAdapter.getYear(this.minDate))) {
  821. return false;
  822. }
  823. // enable if it reaches here and there's no filter defined
  824. if (!this.dateFilter) {
  825. return true;
  826. }
  827. /** @type {?} */
  828. const firstOfYear = this._dateAdapter.createDate(year, 0, 1);
  829. // If any date in the year is enabled count the year as enabled.
  830. for (let date = firstOfYear; this._dateAdapter.getYear(date) == year; date = this._dateAdapter.addCalendarDays(date, 1)) {
  831. if (this.dateFilter(date)) {
  832. return true;
  833. }
  834. }
  835. return false;
  836. }
  837. /**
  838. * @private
  839. * @param {?} obj The object to check.
  840. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  841. */
  842. _getValidDateOrNull(obj) {
  843. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  844. }
  845. /**
  846. * Determines whether the user has the RTL layout direction.
  847. * @private
  848. * @return {?}
  849. */
  850. _isRtl() {
  851. return this._dir && this._dir.value === 'rtl';
  852. }
  853. }
  854. MatMultiYearView.decorators = [
  855. { type: Component, args: [{selector: 'mat-multi-year-view',
  856. template: "<table class=\"mat-calendar-table\" role=\"presentation\"><thead class=\"mat-calendar-table-header\"><tr><th class=\"mat-calendar-table-header-divider\" colspan=\"4\"></th></tr></thead><tbody mat-calendar-body [rows]=\"_years\" [todayValue]=\"_todayYear\" [selectedValue]=\"_selectedYear\" [numCols]=\"4\" [cellAspectRatio]=\"4 / 7\" [activeCell]=\"_getActiveCell()\" (selectedValueChange)=\"_yearSelected($event)\" (keydown)=\"_handleCalendarBodyKeydown($event)\"></tbody></table>",
  857. exportAs: 'matMultiYearView',
  858. encapsulation: ViewEncapsulation.None,
  859. changeDetection: ChangeDetectionStrategy.OnPush
  860. },] },
  861. ];
  862. /** @nocollapse */
  863. MatMultiYearView.ctorParameters = () => [
  864. { type: ChangeDetectorRef },
  865. { type: DateAdapter, decorators: [{ type: Optional }] },
  866. { type: Directionality, decorators: [{ type: Optional }] }
  867. ];
  868. MatMultiYearView.propDecorators = {
  869. activeDate: [{ type: Input }],
  870. selected: [{ type: Input }],
  871. minDate: [{ type: Input }],
  872. maxDate: [{ type: Input }],
  873. dateFilter: [{ type: Input }],
  874. selectedChange: [{ type: Output }],
  875. yearSelected: [{ type: Output }],
  876. activeDateChange: [{ type: Output }],
  877. _matCalendarBody: [{ type: ViewChild, args: [MatCalendarBody, { static: false },] }]
  878. };
  879. /**
  880. * @template D
  881. * @param {?} dateAdapter
  882. * @param {?} date1
  883. * @param {?} date2
  884. * @param {?} minDate
  885. * @param {?} maxDate
  886. * @return {?}
  887. */
  888. function isSameMultiYearView(dateAdapter, date1, date2, minDate, maxDate) {
  889. /** @type {?} */
  890. const year1 = dateAdapter.getYear(date1);
  891. /** @type {?} */
  892. const year2 = dateAdapter.getYear(date2);
  893. /** @type {?} */
  894. const startingYear = getStartingYear(dateAdapter, minDate, maxDate);
  895. return Math.floor((year1 - startingYear) / yearsPerPage) ===
  896. Math.floor((year2 - startingYear) / yearsPerPage);
  897. }
  898. /**
  899. * When the multi-year view is first opened, the active year will be in view.
  900. * So we compute how many years are between the active year and the *slot* where our
  901. * "startingYear" will render when paged into view.
  902. * @template D
  903. * @param {?} dateAdapter
  904. * @param {?} activeDate
  905. * @param {?} minDate
  906. * @param {?} maxDate
  907. * @return {?}
  908. */
  909. function getActiveOffset(dateAdapter, activeDate, minDate, maxDate) {
  910. /** @type {?} */
  911. const activeYear = dateAdapter.getYear(activeDate);
  912. return euclideanModulo((activeYear - getStartingYear(dateAdapter, minDate, maxDate)), yearsPerPage);
  913. }
  914. /**
  915. * We pick a "starting" year such that either the maximum year would be at the end
  916. * or the minimum year would be at the beginning of a page.
  917. * @template D
  918. * @param {?} dateAdapter
  919. * @param {?} minDate
  920. * @param {?} maxDate
  921. * @return {?}
  922. */
  923. function getStartingYear(dateAdapter, minDate, maxDate) {
  924. /** @type {?} */
  925. let startingYear = 0;
  926. if (maxDate) {
  927. /** @type {?} */
  928. const maxYear = dateAdapter.getYear(maxDate);
  929. startingYear = maxYear - yearsPerPage + 1;
  930. }
  931. else if (minDate) {
  932. startingYear = dateAdapter.getYear(minDate);
  933. }
  934. return startingYear;
  935. }
  936. /**
  937. * Gets remainder that is non-negative, even if first number is negative
  938. * @param {?} a
  939. * @param {?} b
  940. * @return {?}
  941. */
  942. function euclideanModulo(a, b) {
  943. return (a % b + b) % b;
  944. }
  945. /**
  946. * @fileoverview added by tsickle
  947. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  948. */
  949. /**
  950. * An internal component used to display a single year in the datepicker.
  951. * \@docs-private
  952. * @template D
  953. */
  954. class MatYearView {
  955. /**
  956. * @param {?} _changeDetectorRef
  957. * @param {?} _dateFormats
  958. * @param {?} _dateAdapter
  959. * @param {?=} _dir
  960. */
  961. constructor(_changeDetectorRef, _dateFormats, _dateAdapter, _dir) {
  962. this._changeDetectorRef = _changeDetectorRef;
  963. this._dateFormats = _dateFormats;
  964. this._dateAdapter = _dateAdapter;
  965. this._dir = _dir;
  966. /**
  967. * Emits when a new month is selected.
  968. */
  969. this.selectedChange = new EventEmitter();
  970. /**
  971. * Emits the selected month. This doesn't imply a change on the selected date
  972. */
  973. this.monthSelected = new EventEmitter();
  974. /**
  975. * Emits when any date is activated.
  976. */
  977. this.activeDateChange = new EventEmitter();
  978. if (!this._dateAdapter) {
  979. throw createMissingDateImplError('DateAdapter');
  980. }
  981. if (!this._dateFormats) {
  982. throw createMissingDateImplError('MAT_DATE_FORMATS');
  983. }
  984. this._activeDate = this._dateAdapter.today();
  985. }
  986. /**
  987. * The date to display in this year view (everything other than the year is ignored).
  988. * @return {?}
  989. */
  990. get activeDate() { return this._activeDate; }
  991. /**
  992. * @param {?} value
  993. * @return {?}
  994. */
  995. set activeDate(value) {
  996. /** @type {?} */
  997. let oldActiveDate = this._activeDate;
  998. /** @type {?} */
  999. const validDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value)) || this._dateAdapter.today();
  1000. this._activeDate = this._dateAdapter.clampDate(validDate, this.minDate, this.maxDate);
  1001. if (this._dateAdapter.getYear(oldActiveDate) !== this._dateAdapter.getYear(this._activeDate)) {
  1002. this._init();
  1003. }
  1004. }
  1005. /**
  1006. * The currently selected date.
  1007. * @return {?}
  1008. */
  1009. get selected() { return this._selected; }
  1010. /**
  1011. * @param {?} value
  1012. * @return {?}
  1013. */
  1014. set selected(value) {
  1015. this._selected = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1016. this._selectedMonth = this._getMonthInCurrentYear(this._selected);
  1017. }
  1018. /**
  1019. * The minimum selectable date.
  1020. * @return {?}
  1021. */
  1022. get minDate() { return this._minDate; }
  1023. /**
  1024. * @param {?} value
  1025. * @return {?}
  1026. */
  1027. set minDate(value) {
  1028. this._minDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1029. }
  1030. /**
  1031. * The maximum selectable date.
  1032. * @return {?}
  1033. */
  1034. get maxDate() { return this._maxDate; }
  1035. /**
  1036. * @param {?} value
  1037. * @return {?}
  1038. */
  1039. set maxDate(value) {
  1040. this._maxDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1041. }
  1042. /**
  1043. * @return {?}
  1044. */
  1045. ngAfterContentInit() {
  1046. this._init();
  1047. }
  1048. /**
  1049. * Handles when a new month is selected.
  1050. * @param {?} month
  1051. * @return {?}
  1052. */
  1053. _monthSelected(month) {
  1054. /** @type {?} */
  1055. const normalizedDate = this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, 1);
  1056. this.monthSelected.emit(normalizedDate);
  1057. /** @type {?} */
  1058. const daysInMonth = this._dateAdapter.getNumDaysInMonth(normalizedDate);
  1059. this.selectedChange.emit(this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth)));
  1060. }
  1061. /**
  1062. * Handles keydown events on the calendar body when calendar is in year view.
  1063. * @param {?} event
  1064. * @return {?}
  1065. */
  1066. _handleCalendarBodyKeydown(event) {
  1067. // TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
  1068. // disabled ones from being selected. This may not be ideal, we should look into whether
  1069. // navigation should skip over disabled dates, and if so, how to implement that efficiently.
  1070. // TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
  1071. // disabled ones from being selected. This may not be ideal, we should look into whether
  1072. // navigation should skip over disabled dates, and if so, how to implement that efficiently.
  1073. /** @type {?} */
  1074. const oldActiveDate = this._activeDate;
  1075. /** @type {?} */
  1076. const isRtl = this._isRtl();
  1077. switch (event.keyCode) {
  1078. case LEFT_ARROW:
  1079. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, isRtl ? 1 : -1);
  1080. break;
  1081. case RIGHT_ARROW:
  1082. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, isRtl ? -1 : 1);
  1083. break;
  1084. case UP_ARROW:
  1085. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, -4);
  1086. break;
  1087. case DOWN_ARROW:
  1088. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, 4);
  1089. break;
  1090. case HOME:
  1091. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, -this._dateAdapter.getMonth(this._activeDate));
  1092. break;
  1093. case END:
  1094. this.activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, 11 - this._dateAdapter.getMonth(this._activeDate));
  1095. break;
  1096. case PAGE_UP:
  1097. this.activeDate =
  1098. this._dateAdapter.addCalendarYears(this._activeDate, event.altKey ? -10 : -1);
  1099. break;
  1100. case PAGE_DOWN:
  1101. this.activeDate =
  1102. this._dateAdapter.addCalendarYears(this._activeDate, event.altKey ? 10 : 1);
  1103. break;
  1104. case ENTER:
  1105. case SPACE:
  1106. this._monthSelected(this._dateAdapter.getMonth(this._activeDate));
  1107. break;
  1108. default:
  1109. // Don't prevent default or focus active cell on keys that we don't explicitly handle.
  1110. return;
  1111. }
  1112. if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
  1113. this.activeDateChange.emit(this.activeDate);
  1114. }
  1115. this._focusActiveCell();
  1116. // Prevent unexpected default actions such as form submission.
  1117. event.preventDefault();
  1118. }
  1119. /**
  1120. * Initializes this year view.
  1121. * @return {?}
  1122. */
  1123. _init() {
  1124. this._selectedMonth = this._getMonthInCurrentYear(this.selected);
  1125. this._todayMonth = this._getMonthInCurrentYear(this._dateAdapter.today());
  1126. this._yearLabel = this._dateAdapter.getYearName(this.activeDate);
  1127. /** @type {?} */
  1128. let monthNames = this._dateAdapter.getMonthNames('short');
  1129. // First row of months only contains 5 elements so we can fit the year label on the same row.
  1130. this._months = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]].map((/**
  1131. * @param {?} row
  1132. * @return {?}
  1133. */
  1134. row => row.map((/**
  1135. * @param {?} month
  1136. * @return {?}
  1137. */
  1138. month => this._createCellForMonth(month, monthNames[month])))));
  1139. this._changeDetectorRef.markForCheck();
  1140. }
  1141. /**
  1142. * Focuses the active cell after the microtask queue is empty.
  1143. * @return {?}
  1144. */
  1145. _focusActiveCell() {
  1146. this._matCalendarBody._focusActiveCell();
  1147. }
  1148. /**
  1149. * Gets the month in this year that the given Date falls on.
  1150. * Returns null if the given Date is in another year.
  1151. * @private
  1152. * @param {?} date
  1153. * @return {?}
  1154. */
  1155. _getMonthInCurrentYear(date) {
  1156. return date && this._dateAdapter.getYear(date) == this._dateAdapter.getYear(this.activeDate) ?
  1157. this._dateAdapter.getMonth(date) : null;
  1158. }
  1159. /**
  1160. * Creates an MatCalendarCell for the given month.
  1161. * @private
  1162. * @param {?} month
  1163. * @param {?} monthName
  1164. * @return {?}
  1165. */
  1166. _createCellForMonth(month, monthName) {
  1167. /** @type {?} */
  1168. let ariaLabel = this._dateAdapter.format(this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, 1), this._dateFormats.display.monthYearA11yLabel);
  1169. return new MatCalendarCell(month, monthName.toLocaleUpperCase(), ariaLabel, this._shouldEnableMonth(month));
  1170. }
  1171. /**
  1172. * Whether the given month is enabled.
  1173. * @private
  1174. * @param {?} month
  1175. * @return {?}
  1176. */
  1177. _shouldEnableMonth(month) {
  1178. /** @type {?} */
  1179. const activeYear = this._dateAdapter.getYear(this.activeDate);
  1180. if (month === undefined || month === null ||
  1181. this._isYearAndMonthAfterMaxDate(activeYear, month) ||
  1182. this._isYearAndMonthBeforeMinDate(activeYear, month)) {
  1183. return false;
  1184. }
  1185. if (!this.dateFilter) {
  1186. return true;
  1187. }
  1188. /** @type {?} */
  1189. const firstOfMonth = this._dateAdapter.createDate(activeYear, month, 1);
  1190. // If any date in the month is enabled count the month as enabled.
  1191. for (let date = firstOfMonth; this._dateAdapter.getMonth(date) == month; date = this._dateAdapter.addCalendarDays(date, 1)) {
  1192. if (this.dateFilter(date)) {
  1193. return true;
  1194. }
  1195. }
  1196. return false;
  1197. }
  1198. /**
  1199. * Tests whether the combination month/year is after this.maxDate, considering
  1200. * just the month and year of this.maxDate
  1201. * @private
  1202. * @param {?} year
  1203. * @param {?} month
  1204. * @return {?}
  1205. */
  1206. _isYearAndMonthAfterMaxDate(year, month) {
  1207. if (this.maxDate) {
  1208. /** @type {?} */
  1209. const maxYear = this._dateAdapter.getYear(this.maxDate);
  1210. /** @type {?} */
  1211. const maxMonth = this._dateAdapter.getMonth(this.maxDate);
  1212. return year > maxYear || (year === maxYear && month > maxMonth);
  1213. }
  1214. return false;
  1215. }
  1216. /**
  1217. * Tests whether the combination month/year is before this.minDate, considering
  1218. * just the month and year of this.minDate
  1219. * @private
  1220. * @param {?} year
  1221. * @param {?} month
  1222. * @return {?}
  1223. */
  1224. _isYearAndMonthBeforeMinDate(year, month) {
  1225. if (this.minDate) {
  1226. /** @type {?} */
  1227. const minYear = this._dateAdapter.getYear(this.minDate);
  1228. /** @type {?} */
  1229. const minMonth = this._dateAdapter.getMonth(this.minDate);
  1230. return year < minYear || (year === minYear && month < minMonth);
  1231. }
  1232. return false;
  1233. }
  1234. /**
  1235. * @private
  1236. * @param {?} obj The object to check.
  1237. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  1238. */
  1239. _getValidDateOrNull(obj) {
  1240. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  1241. }
  1242. /**
  1243. * Determines whether the user has the RTL layout direction.
  1244. * @private
  1245. * @return {?}
  1246. */
  1247. _isRtl() {
  1248. return this._dir && this._dir.value === 'rtl';
  1249. }
  1250. }
  1251. MatYearView.decorators = [
  1252. { type: Component, args: [{selector: 'mat-year-view',
  1253. template: "<table class=\"mat-calendar-table\" role=\"presentation\"><thead class=\"mat-calendar-table-header\"><tr><th class=\"mat-calendar-table-header-divider\" colspan=\"4\"></th></tr></thead><tbody mat-calendar-body [label]=\"_yearLabel\" [rows]=\"_months\" [todayValue]=\"_todayMonth\" [selectedValue]=\"_selectedMonth\" [labelMinRequiredCells]=\"2\" [numCols]=\"4\" [cellAspectRatio]=\"4 / 7\" [activeCell]=\"_dateAdapter.getMonth(activeDate)\" (selectedValueChange)=\"_monthSelected($event)\" (keydown)=\"_handleCalendarBodyKeydown($event)\"></tbody></table>",
  1254. exportAs: 'matYearView',
  1255. encapsulation: ViewEncapsulation.None,
  1256. changeDetection: ChangeDetectionStrategy.OnPush
  1257. },] },
  1258. ];
  1259. /** @nocollapse */
  1260. MatYearView.ctorParameters = () => [
  1261. { type: ChangeDetectorRef },
  1262. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_FORMATS,] }] },
  1263. { type: DateAdapter, decorators: [{ type: Optional }] },
  1264. { type: Directionality, decorators: [{ type: Optional }] }
  1265. ];
  1266. MatYearView.propDecorators = {
  1267. activeDate: [{ type: Input }],
  1268. selected: [{ type: Input }],
  1269. minDate: [{ type: Input }],
  1270. maxDate: [{ type: Input }],
  1271. dateFilter: [{ type: Input }],
  1272. selectedChange: [{ type: Output }],
  1273. monthSelected: [{ type: Output }],
  1274. activeDateChange: [{ type: Output }],
  1275. _matCalendarBody: [{ type: ViewChild, args: [MatCalendarBody, { static: false },] }]
  1276. };
  1277. /**
  1278. * @fileoverview added by tsickle
  1279. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1280. */
  1281. /**
  1282. * Default header for MatCalendar
  1283. * @template D
  1284. */
  1285. class MatCalendarHeader {
  1286. /**
  1287. * @param {?} _intl
  1288. * @param {?} calendar
  1289. * @param {?} _dateAdapter
  1290. * @param {?} _dateFormats
  1291. * @param {?} changeDetectorRef
  1292. */
  1293. constructor(_intl, calendar, _dateAdapter, _dateFormats, changeDetectorRef) {
  1294. this._intl = _intl;
  1295. this.calendar = calendar;
  1296. this._dateAdapter = _dateAdapter;
  1297. this._dateFormats = _dateFormats;
  1298. this.calendar.stateChanges.subscribe((/**
  1299. * @return {?}
  1300. */
  1301. () => changeDetectorRef.markForCheck()));
  1302. }
  1303. /**
  1304. * The label for the current calendar view.
  1305. * @return {?}
  1306. */
  1307. get periodButtonText() {
  1308. if (this.calendar.currentView == 'month') {
  1309. return this._dateAdapter
  1310. .format(this.calendar.activeDate, this._dateFormats.display.monthYearLabel)
  1311. .toLocaleUpperCase();
  1312. }
  1313. if (this.calendar.currentView == 'year') {
  1314. return this._dateAdapter.getYearName(this.calendar.activeDate);
  1315. }
  1316. // The offset from the active year to the "slot" for the starting year is the
  1317. // *actual* first rendered year in the multi-year view, and the last year is
  1318. // just yearsPerPage - 1 away.
  1319. /** @type {?} */
  1320. const activeYear = this._dateAdapter.getYear(this.calendar.activeDate);
  1321. /** @type {?} */
  1322. const minYearOfPage = activeYear - getActiveOffset(this._dateAdapter, this.calendar.activeDate, this.calendar.minDate, this.calendar.maxDate);
  1323. /** @type {?} */
  1324. const maxYearOfPage = minYearOfPage + yearsPerPage - 1;
  1325. return `${minYearOfPage} \u2013 ${maxYearOfPage}`;
  1326. }
  1327. /**
  1328. * @return {?}
  1329. */
  1330. get periodButtonLabel() {
  1331. return this.calendar.currentView == 'month' ?
  1332. this._intl.switchToMultiYearViewLabel : this._intl.switchToMonthViewLabel;
  1333. }
  1334. /**
  1335. * The label for the previous button.
  1336. * @return {?}
  1337. */
  1338. get prevButtonLabel() {
  1339. return {
  1340. 'month': this._intl.prevMonthLabel,
  1341. 'year': this._intl.prevYearLabel,
  1342. 'multi-year': this._intl.prevMultiYearLabel
  1343. }[this.calendar.currentView];
  1344. }
  1345. /**
  1346. * The label for the next button.
  1347. * @return {?}
  1348. */
  1349. get nextButtonLabel() {
  1350. return {
  1351. 'month': this._intl.nextMonthLabel,
  1352. 'year': this._intl.nextYearLabel,
  1353. 'multi-year': this._intl.nextMultiYearLabel
  1354. }[this.calendar.currentView];
  1355. }
  1356. /**
  1357. * Handles user clicks on the period label.
  1358. * @return {?}
  1359. */
  1360. currentPeriodClicked() {
  1361. this.calendar.currentView = this.calendar.currentView == 'month' ? 'multi-year' : 'month';
  1362. }
  1363. /**
  1364. * Handles user clicks on the previous button.
  1365. * @return {?}
  1366. */
  1367. previousClicked() {
  1368. this.calendar.activeDate = this.calendar.currentView == 'month' ?
  1369. this._dateAdapter.addCalendarMonths(this.calendar.activeDate, -1) :
  1370. this._dateAdapter.addCalendarYears(this.calendar.activeDate, this.calendar.currentView == 'year' ? -1 : -yearsPerPage);
  1371. }
  1372. /**
  1373. * Handles user clicks on the next button.
  1374. * @return {?}
  1375. */
  1376. nextClicked() {
  1377. this.calendar.activeDate = this.calendar.currentView == 'month' ?
  1378. this._dateAdapter.addCalendarMonths(this.calendar.activeDate, 1) :
  1379. this._dateAdapter.addCalendarYears(this.calendar.activeDate, this.calendar.currentView == 'year' ? 1 : yearsPerPage);
  1380. }
  1381. /**
  1382. * Whether the previous period button is enabled.
  1383. * @return {?}
  1384. */
  1385. previousEnabled() {
  1386. if (!this.calendar.minDate) {
  1387. return true;
  1388. }
  1389. return !this.calendar.minDate ||
  1390. !this._isSameView(this.calendar.activeDate, this.calendar.minDate);
  1391. }
  1392. /**
  1393. * Whether the next period button is enabled.
  1394. * @return {?}
  1395. */
  1396. nextEnabled() {
  1397. return !this.calendar.maxDate ||
  1398. !this._isSameView(this.calendar.activeDate, this.calendar.maxDate);
  1399. }
  1400. /**
  1401. * Whether the two dates represent the same view in the current view mode (month or year).
  1402. * @private
  1403. * @param {?} date1
  1404. * @param {?} date2
  1405. * @return {?}
  1406. */
  1407. _isSameView(date1, date2) {
  1408. if (this.calendar.currentView == 'month') {
  1409. return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2) &&
  1410. this._dateAdapter.getMonth(date1) == this._dateAdapter.getMonth(date2);
  1411. }
  1412. if (this.calendar.currentView == 'year') {
  1413. return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2);
  1414. }
  1415. // Otherwise we are in 'multi-year' view.
  1416. return isSameMultiYearView(this._dateAdapter, date1, date2, this.calendar.minDate, this.calendar.maxDate);
  1417. }
  1418. }
  1419. MatCalendarHeader.decorators = [
  1420. { type: Component, args: [{selector: 'mat-calendar-header',
  1421. template: "<div class=\"mat-calendar-header\"><div class=\"mat-calendar-controls\"><button mat-button type=\"button\" class=\"mat-calendar-period-button\" (click)=\"currentPeriodClicked()\" [attr.aria-label]=\"periodButtonLabel\" cdkAriaLive=\"polite\">{{periodButtonText}}<div class=\"mat-calendar-arrow\" [class.mat-calendar-invert]=\"calendar.currentView != 'month'\"></div></button><div class=\"mat-calendar-spacer\"></div><ng-content></ng-content><button mat-icon-button type=\"button\" class=\"mat-calendar-previous-button\" [disabled]=\"!previousEnabled()\" (click)=\"previousClicked()\" [attr.aria-label]=\"prevButtonLabel\"></button> <button mat-icon-button type=\"button\" class=\"mat-calendar-next-button\" [disabled]=\"!nextEnabled()\" (click)=\"nextClicked()\" [attr.aria-label]=\"nextButtonLabel\"></button></div></div>",
  1422. exportAs: 'matCalendarHeader',
  1423. encapsulation: ViewEncapsulation.None,
  1424. changeDetection: ChangeDetectionStrategy.OnPush,
  1425. },] },
  1426. ];
  1427. /** @nocollapse */
  1428. MatCalendarHeader.ctorParameters = () => [
  1429. { type: MatDatepickerIntl },
  1430. { type: MatCalendar, decorators: [{ type: Inject, args: [forwardRef((/**
  1431. * @return {?}
  1432. */
  1433. () => MatCalendar)),] }] },
  1434. { type: DateAdapter, decorators: [{ type: Optional }] },
  1435. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_FORMATS,] }] },
  1436. { type: ChangeDetectorRef }
  1437. ];
  1438. /**
  1439. * A calendar that is used as part of the datepicker.
  1440. * \@docs-private
  1441. * @template D
  1442. */
  1443. class MatCalendar {
  1444. /**
  1445. * @param {?} _intl
  1446. * @param {?} _dateAdapter
  1447. * @param {?} _dateFormats
  1448. * @param {?} _changeDetectorRef
  1449. */
  1450. constructor(_intl, _dateAdapter, _dateFormats, _changeDetectorRef) {
  1451. this._dateAdapter = _dateAdapter;
  1452. this._dateFormats = _dateFormats;
  1453. this._changeDetectorRef = _changeDetectorRef;
  1454. /**
  1455. * Used for scheduling that focus should be moved to the active cell on the next tick.
  1456. * We need to schedule it, rather than do it immediately, because we have to wait
  1457. * for Angular to re-evaluate the view children.
  1458. */
  1459. this._moveFocusOnNextTick = false;
  1460. /**
  1461. * Whether the calendar should be started in month or year view.
  1462. */
  1463. this.startView = 'month';
  1464. /**
  1465. * Emits when the currently selected date changes.
  1466. */
  1467. this.selectedChange = new EventEmitter();
  1468. /**
  1469. * Emits the year chosen in multiyear view.
  1470. * This doesn't imply a change on the selected date.
  1471. */
  1472. this.yearSelected = new EventEmitter();
  1473. /**
  1474. * Emits the month chosen in year view.
  1475. * This doesn't imply a change on the selected date.
  1476. */
  1477. this.monthSelected = new EventEmitter();
  1478. /**
  1479. * Emits when any date is selected.
  1480. */
  1481. this._userSelection = new EventEmitter();
  1482. /**
  1483. * Emits whenever there is a state change that the header may need to respond to.
  1484. */
  1485. this.stateChanges = new Subject();
  1486. if (!this._dateAdapter) {
  1487. throw createMissingDateImplError('DateAdapter');
  1488. }
  1489. if (!this._dateFormats) {
  1490. throw createMissingDateImplError('MAT_DATE_FORMATS');
  1491. }
  1492. this._intlChanges = _intl.changes.subscribe((/**
  1493. * @return {?}
  1494. */
  1495. () => {
  1496. _changeDetectorRef.markForCheck();
  1497. this.stateChanges.next();
  1498. }));
  1499. }
  1500. /**
  1501. * A date representing the period (month or year) to start the calendar in.
  1502. * @return {?}
  1503. */
  1504. get startAt() { return this._startAt; }
  1505. /**
  1506. * @param {?} value
  1507. * @return {?}
  1508. */
  1509. set startAt(value) {
  1510. this._startAt = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1511. }
  1512. /**
  1513. * The currently selected date.
  1514. * @return {?}
  1515. */
  1516. get selected() { return this._selected; }
  1517. /**
  1518. * @param {?} value
  1519. * @return {?}
  1520. */
  1521. set selected(value) {
  1522. this._selected = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1523. }
  1524. /**
  1525. * The minimum selectable date.
  1526. * @return {?}
  1527. */
  1528. get minDate() { return this._minDate; }
  1529. /**
  1530. * @param {?} value
  1531. * @return {?}
  1532. */
  1533. set minDate(value) {
  1534. this._minDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1535. }
  1536. /**
  1537. * The maximum selectable date.
  1538. * @return {?}
  1539. */
  1540. get maxDate() { return this._maxDate; }
  1541. /**
  1542. * @param {?} value
  1543. * @return {?}
  1544. */
  1545. set maxDate(value) {
  1546. this._maxDate = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1547. }
  1548. /**
  1549. * The current active date. This determines which time period is shown and which date is
  1550. * highlighted when using keyboard navigation.
  1551. * @return {?}
  1552. */
  1553. get activeDate() { return this._clampedActiveDate; }
  1554. /**
  1555. * @param {?} value
  1556. * @return {?}
  1557. */
  1558. set activeDate(value) {
  1559. this._clampedActiveDate = this._dateAdapter.clampDate(value, this.minDate, this.maxDate);
  1560. this.stateChanges.next();
  1561. this._changeDetectorRef.markForCheck();
  1562. }
  1563. /**
  1564. * Whether the calendar is in month view.
  1565. * @return {?}
  1566. */
  1567. get currentView() { return this._currentView; }
  1568. /**
  1569. * @param {?} value
  1570. * @return {?}
  1571. */
  1572. set currentView(value) {
  1573. this._currentView = value;
  1574. this._moveFocusOnNextTick = true;
  1575. this._changeDetectorRef.markForCheck();
  1576. }
  1577. /**
  1578. * @return {?}
  1579. */
  1580. ngAfterContentInit() {
  1581. this._calendarHeaderPortal = new ComponentPortal(this.headerComponent || MatCalendarHeader);
  1582. this.activeDate = this.startAt || this._dateAdapter.today();
  1583. // Assign to the private property since we don't want to move focus on init.
  1584. this._currentView = this.startView;
  1585. }
  1586. /**
  1587. * @return {?}
  1588. */
  1589. ngAfterViewChecked() {
  1590. if (this._moveFocusOnNextTick) {
  1591. this._moveFocusOnNextTick = false;
  1592. this.focusActiveCell();
  1593. }
  1594. }
  1595. /**
  1596. * @return {?}
  1597. */
  1598. ngOnDestroy() {
  1599. this._intlChanges.unsubscribe();
  1600. this.stateChanges.complete();
  1601. }
  1602. /**
  1603. * @param {?} changes
  1604. * @return {?}
  1605. */
  1606. ngOnChanges(changes) {
  1607. /** @type {?} */
  1608. const change = changes['minDate'] || changes['maxDate'] || changes['dateFilter'];
  1609. if (change && !change.firstChange) {
  1610. /** @type {?} */
  1611. const view = this._getCurrentViewComponent();
  1612. if (view) {
  1613. // We need to `detectChanges` manually here, because the `minDate`, `maxDate` etc. are
  1614. // passed down to the view via data bindings which won't be up-to-date when we call `_init`.
  1615. this._changeDetectorRef.detectChanges();
  1616. view._init();
  1617. }
  1618. }
  1619. this.stateChanges.next();
  1620. }
  1621. /**
  1622. * @return {?}
  1623. */
  1624. focusActiveCell() {
  1625. this._getCurrentViewComponent()._focusActiveCell();
  1626. }
  1627. /**
  1628. * Updates today's date after an update of the active date
  1629. * @return {?}
  1630. */
  1631. updateTodaysDate() {
  1632. /** @type {?} */
  1633. let view = this.currentView == 'month' ? this.monthView :
  1634. (this.currentView == 'year' ? this.yearView : this.multiYearView);
  1635. view.ngAfterContentInit();
  1636. }
  1637. /**
  1638. * Handles date selection in the month view.
  1639. * @param {?} date
  1640. * @return {?}
  1641. */
  1642. _dateSelected(date) {
  1643. if (!this._dateAdapter.sameDate(date, this.selected)) {
  1644. this.selectedChange.emit(date);
  1645. }
  1646. }
  1647. /**
  1648. * Handles year selection in the multiyear view.
  1649. * @param {?} normalizedYear
  1650. * @return {?}
  1651. */
  1652. _yearSelectedInMultiYearView(normalizedYear) {
  1653. this.yearSelected.emit(normalizedYear);
  1654. }
  1655. /**
  1656. * Handles month selection in the year view.
  1657. * @param {?} normalizedMonth
  1658. * @return {?}
  1659. */
  1660. _monthSelectedInYearView(normalizedMonth) {
  1661. this.monthSelected.emit(normalizedMonth);
  1662. }
  1663. /**
  1664. * @return {?}
  1665. */
  1666. _userSelected() {
  1667. this._userSelection.emit();
  1668. }
  1669. /**
  1670. * Handles year/month selection in the multi-year/year views.
  1671. * @param {?} date
  1672. * @param {?} view
  1673. * @return {?}
  1674. */
  1675. _goToDateInView(date, view) {
  1676. this.activeDate = date;
  1677. this.currentView = view;
  1678. }
  1679. /**
  1680. * @private
  1681. * @param {?} obj The object to check.
  1682. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  1683. */
  1684. _getValidDateOrNull(obj) {
  1685. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  1686. }
  1687. /**
  1688. * Returns the component instance that corresponds to the current calendar view.
  1689. * @private
  1690. * @return {?}
  1691. */
  1692. _getCurrentViewComponent() {
  1693. return this.monthView || this.yearView || this.multiYearView;
  1694. }
  1695. }
  1696. MatCalendar.decorators = [
  1697. { type: Component, args: [{selector: 'mat-calendar',
  1698. template: "<ng-template [cdkPortalOutlet]=\"_calendarHeaderPortal\"></ng-template><div class=\"mat-calendar-content\" [ngSwitch]=\"currentView\" cdkMonitorSubtreeFocus tabindex=\"-1\"><mat-month-view *ngSwitchCase=\"'month'\" [(activeDate)]=\"activeDate\" [selected]=\"selected\" [dateFilter]=\"dateFilter\" [maxDate]=\"maxDate\" [minDate]=\"minDate\" [dateClass]=\"dateClass\" (selectedChange)=\"_dateSelected($event)\" (_userSelection)=\"_userSelected()\"></mat-month-view><mat-year-view *ngSwitchCase=\"'year'\" [(activeDate)]=\"activeDate\" [selected]=\"selected\" [dateFilter]=\"dateFilter\" [maxDate]=\"maxDate\" [minDate]=\"minDate\" (monthSelected)=\"_monthSelectedInYearView($event)\" (selectedChange)=\"_goToDateInView($event, 'month')\"></mat-year-view><mat-multi-year-view *ngSwitchCase=\"'multi-year'\" [(activeDate)]=\"activeDate\" [selected]=\"selected\" [dateFilter]=\"dateFilter\" [maxDate]=\"maxDate\" [minDate]=\"minDate\" (yearSelected)=\"_yearSelectedInMultiYearView($event)\" (selectedChange)=\"_goToDateInView($event, 'year')\"></mat-multi-year-view></div>",
  1699. styles: [".mat-calendar{display:block}.mat-calendar-header{padding:8px 8px 0 8px}.mat-calendar-content{padding:0 8px 8px 8px;outline:0}.mat-calendar-controls{display:flex;margin:5% calc(33% / 7 - 16px)}.mat-calendar-spacer{flex:1 1 auto}.mat-calendar-period-button{min-width:0}.mat-calendar-arrow{display:inline-block;width:0;height:0;border-left:5px solid transparent;border-right:5px solid transparent;border-top-width:5px;border-top-style:solid;margin:0 0 0 5px;vertical-align:middle}.mat-calendar-arrow.mat-calendar-invert{transform:rotate(180deg)}[dir=rtl] .mat-calendar-arrow{margin:0 5px 0 0}.mat-calendar-next-button,.mat-calendar-previous-button{position:relative}.mat-calendar-next-button::after,.mat-calendar-previous-button::after{top:0;left:0;right:0;bottom:0;position:absolute;content:'';margin:15.5px;border:0 solid currentColor;border-top-width:2px}[dir=rtl] .mat-calendar-next-button,[dir=rtl] .mat-calendar-previous-button{transform:rotate(180deg)}.mat-calendar-previous-button::after{border-left-width:2px;transform:translateX(2px) rotate(-45deg)}.mat-calendar-next-button::after{border-right-width:2px;transform:translateX(-2px) rotate(45deg)}.mat-calendar-table{border-spacing:0;border-collapse:collapse;width:100%}.mat-calendar-table-header th{text-align:center;padding:0 0 8px 0}.mat-calendar-table-header-divider{position:relative;height:1px}.mat-calendar-table-header-divider::after{content:'';position:absolute;top:0;left:-8px;right:-8px;height:1px}"],
  1700. host: {
  1701. 'class': 'mat-calendar',
  1702. },
  1703. exportAs: 'matCalendar',
  1704. encapsulation: ViewEncapsulation.None,
  1705. changeDetection: ChangeDetectionStrategy.OnPush,
  1706. },] },
  1707. ];
  1708. /** @nocollapse */
  1709. MatCalendar.ctorParameters = () => [
  1710. { type: MatDatepickerIntl },
  1711. { type: DateAdapter, decorators: [{ type: Optional }] },
  1712. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_FORMATS,] }] },
  1713. { type: ChangeDetectorRef }
  1714. ];
  1715. MatCalendar.propDecorators = {
  1716. headerComponent: [{ type: Input }],
  1717. startAt: [{ type: Input }],
  1718. startView: [{ type: Input }],
  1719. selected: [{ type: Input }],
  1720. minDate: [{ type: Input }],
  1721. maxDate: [{ type: Input }],
  1722. dateFilter: [{ type: Input }],
  1723. dateClass: [{ type: Input }],
  1724. selectedChange: [{ type: Output }],
  1725. yearSelected: [{ type: Output }],
  1726. monthSelected: [{ type: Output }],
  1727. _userSelection: [{ type: Output }],
  1728. monthView: [{ type: ViewChild, args: [MatMonthView, { static: false },] }],
  1729. yearView: [{ type: ViewChild, args: [MatYearView, { static: false },] }],
  1730. multiYearView: [{ type: ViewChild, args: [MatMultiYearView, { static: false },] }]
  1731. };
  1732. /**
  1733. * @fileoverview added by tsickle
  1734. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1735. */
  1736. /**
  1737. * Animations used by the Material datepicker.
  1738. * \@docs-private
  1739. * @type {?}
  1740. */
  1741. const matDatepickerAnimations = {
  1742. /**
  1743. * Transforms the height of the datepicker's calendar.
  1744. */
  1745. transformPanel: trigger('transformPanel', [
  1746. state('void', style({
  1747. opacity: 0,
  1748. transform: 'scale(1, 0.8)'
  1749. })),
  1750. transition('void => enter', animate('120ms cubic-bezier(0, 0, 0.2, 1)', style({
  1751. opacity: 1,
  1752. transform: 'scale(1, 1)'
  1753. }))),
  1754. transition('* => void', animate('100ms linear', style({ opacity: 0 })))
  1755. ]),
  1756. /**
  1757. * Fades in the content of the calendar.
  1758. */
  1759. fadeInCalendar: trigger('fadeInCalendar', [
  1760. state('void', style({ opacity: 0 })),
  1761. state('enter', style({ opacity: 1 })),
  1762. // TODO(crisbeto): this animation should be removed since it isn't quite on spec, but we
  1763. // need to keep it until #12440 gets in, otherwise the exit animation will look glitchy.
  1764. transition('void => *', animate('120ms 100ms cubic-bezier(0.55, 0, 0.55, 0.2)'))
  1765. ])
  1766. };
  1767. /**
  1768. * @fileoverview added by tsickle
  1769. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1770. */
  1771. /**
  1772. * Used to generate a unique ID for each datepicker instance.
  1773. * @type {?}
  1774. */
  1775. let datepickerUid = 0;
  1776. /**
  1777. * Injection token that determines the scroll handling while the calendar is open.
  1778. * @type {?}
  1779. */
  1780. const MAT_DATEPICKER_SCROLL_STRATEGY = new InjectionToken('mat-datepicker-scroll-strategy');
  1781. /**
  1782. * \@docs-private
  1783. * @param {?} overlay
  1784. * @return {?}
  1785. */
  1786. function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(overlay) {
  1787. return (/**
  1788. * @return {?}
  1789. */
  1790. () => overlay.scrollStrategies.reposition());
  1791. }
  1792. /**
  1793. * \@docs-private
  1794. * @type {?}
  1795. */
  1796. const MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER = {
  1797. provide: MAT_DATEPICKER_SCROLL_STRATEGY,
  1798. deps: [Overlay],
  1799. useFactory: MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY,
  1800. };
  1801. // Boilerplate for applying mixins to MatDatepickerContent.
  1802. /**
  1803. * \@docs-private
  1804. */
  1805. class MatDatepickerContentBase {
  1806. /**
  1807. * @param {?} _elementRef
  1808. */
  1809. constructor(_elementRef) {
  1810. this._elementRef = _elementRef;
  1811. }
  1812. }
  1813. /** @type {?} */
  1814. const _MatDatepickerContentMixinBase = mixinColor(MatDatepickerContentBase);
  1815. /**
  1816. * Component used as the content for the datepicker dialog and popup. We use this instead of using
  1817. * MatCalendar directly as the content so we can control the initial focus. This also gives us a
  1818. * place to put additional features of the popup that are not part of the calendar itself in the
  1819. * future. (e.g. confirmation buttons).
  1820. * \@docs-private
  1821. * @template D
  1822. */
  1823. class MatDatepickerContent extends _MatDatepickerContentMixinBase {
  1824. /**
  1825. * @param {?} elementRef
  1826. */
  1827. constructor(elementRef) {
  1828. super(elementRef);
  1829. }
  1830. /**
  1831. * @return {?}
  1832. */
  1833. ngAfterViewInit() {
  1834. this._calendar.focusActiveCell();
  1835. }
  1836. }
  1837. MatDatepickerContent.decorators = [
  1838. { type: Component, args: [{selector: 'mat-datepicker-content',
  1839. template: "<mat-calendar cdkTrapFocus [id]=\"datepicker.id\" [ngClass]=\"datepicker.panelClass\" [startAt]=\"datepicker.startAt\" [startView]=\"datepicker.startView\" [minDate]=\"datepicker._minDate\" [maxDate]=\"datepicker._maxDate\" [dateFilter]=\"datepicker._dateFilter\" [headerComponent]=\"datepicker.calendarHeaderComponent\" [selected]=\"datepicker._selected\" [dateClass]=\"datepicker.dateClass\" [@fadeInCalendar]=\"'enter'\" (selectedChange)=\"datepicker.select($event)\" (yearSelected)=\"datepicker._selectYear($event)\" (monthSelected)=\"datepicker._selectMonth($event)\" (_userSelection)=\"datepicker.close()\"></mat-calendar>",
  1840. styles: [".mat-datepicker-content{display:block;border-radius:4px}.mat-datepicker-content .mat-calendar{width:296px;height:354px}.mat-datepicker-content-touch{display:block;max-height:80vh;overflow:auto;margin:-24px}.mat-datepicker-content-touch .mat-calendar{min-width:250px;min-height:312px;max-width:750px;max-height:788px}@media all and (orientation:landscape){.mat-datepicker-content-touch .mat-calendar{width:64vh;height:80vh}}@media all and (orientation:portrait){.mat-datepicker-content-touch .mat-calendar{width:80vw;height:100vw}}"],
  1841. host: {
  1842. 'class': 'mat-datepicker-content',
  1843. '[@transformPanel]': '"enter"',
  1844. '[class.mat-datepicker-content-touch]': 'datepicker.touchUi',
  1845. },
  1846. animations: [
  1847. matDatepickerAnimations.transformPanel,
  1848. matDatepickerAnimations.fadeInCalendar,
  1849. ],
  1850. exportAs: 'matDatepickerContent',
  1851. encapsulation: ViewEncapsulation.None,
  1852. changeDetection: ChangeDetectionStrategy.OnPush,
  1853. inputs: ['color'],
  1854. },] },
  1855. ];
  1856. /** @nocollapse */
  1857. MatDatepickerContent.ctorParameters = () => [
  1858. { type: ElementRef }
  1859. ];
  1860. MatDatepickerContent.propDecorators = {
  1861. _calendar: [{ type: ViewChild, args: [MatCalendar, { static: false },] }]
  1862. };
  1863. // TODO(mmalerba): We use a component instead of a directive here so the user can use implicit
  1864. // template reference variables (e.g. #d vs #d="matDatepicker"). We can change this to a directive
  1865. // if angular adds support for `exportAs: '$implicit'` on directives.
  1866. /**
  1867. * Component responsible for managing the datepicker popup/dialog.
  1868. * @template D
  1869. */
  1870. class MatDatepicker {
  1871. /**
  1872. * @param {?} _dialog
  1873. * @param {?} _overlay
  1874. * @param {?} _ngZone
  1875. * @param {?} _viewContainerRef
  1876. * @param {?} scrollStrategy
  1877. * @param {?} _dateAdapter
  1878. * @param {?} _dir
  1879. * @param {?} _document
  1880. */
  1881. constructor(_dialog, _overlay, _ngZone, _viewContainerRef, scrollStrategy, _dateAdapter, _dir, _document) {
  1882. this._dialog = _dialog;
  1883. this._overlay = _overlay;
  1884. this._ngZone = _ngZone;
  1885. this._viewContainerRef = _viewContainerRef;
  1886. this._dateAdapter = _dateAdapter;
  1887. this._dir = _dir;
  1888. this._document = _document;
  1889. /**
  1890. * The view that the calendar should start in.
  1891. */
  1892. this.startView = 'month';
  1893. this._touchUi = false;
  1894. /**
  1895. * Emits selected year in multiyear view.
  1896. * This doesn't imply a change on the selected date.
  1897. */
  1898. this.yearSelected = new EventEmitter();
  1899. /**
  1900. * Emits selected month in year view.
  1901. * This doesn't imply a change on the selected date.
  1902. */
  1903. this.monthSelected = new EventEmitter();
  1904. /**
  1905. * Emits when the datepicker has been opened.
  1906. */
  1907. this.openedStream = new EventEmitter();
  1908. /**
  1909. * Emits when the datepicker has been closed.
  1910. */
  1911. this.closedStream = new EventEmitter();
  1912. this._opened = false;
  1913. /**
  1914. * The id for the datepicker calendar.
  1915. */
  1916. this.id = `mat-datepicker-${datepickerUid++}`;
  1917. this._validSelected = null;
  1918. /**
  1919. * The element that was focused before the datepicker was opened.
  1920. */
  1921. this._focusedElementBeforeOpen = null;
  1922. /**
  1923. * Subscription to value changes in the associated input element.
  1924. */
  1925. this._inputSubscription = Subscription.EMPTY;
  1926. /**
  1927. * Emits when the datepicker is disabled.
  1928. */
  1929. this._disabledChange = new Subject();
  1930. /**
  1931. * Emits new selected date when selected date changes.
  1932. */
  1933. this._selectedChanged = new Subject();
  1934. if (!this._dateAdapter) {
  1935. throw createMissingDateImplError('DateAdapter');
  1936. }
  1937. this._scrollStrategy = scrollStrategy;
  1938. }
  1939. /**
  1940. * The date to open the calendar to initially.
  1941. * @return {?}
  1942. */
  1943. get startAt() {
  1944. // If an explicit startAt is set we start there, otherwise we start at whatever the currently
  1945. // selected value is.
  1946. return this._startAt || (this._datepickerInput ? this._datepickerInput.value : null);
  1947. }
  1948. /**
  1949. * @param {?} value
  1950. * @return {?}
  1951. */
  1952. set startAt(value) {
  1953. this._startAt = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  1954. }
  1955. /**
  1956. * Color palette to use on the datepicker's calendar.
  1957. * @return {?}
  1958. */
  1959. get color() {
  1960. return this._color ||
  1961. (this._datepickerInput ? this._datepickerInput._getThemePalette() : undefined);
  1962. }
  1963. /**
  1964. * @param {?} value
  1965. * @return {?}
  1966. */
  1967. set color(value) {
  1968. this._color = value;
  1969. }
  1970. /**
  1971. * Whether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather
  1972. * than a popup and elements have more padding to allow for bigger touch targets.
  1973. * @return {?}
  1974. */
  1975. get touchUi() { return this._touchUi; }
  1976. /**
  1977. * @param {?} value
  1978. * @return {?}
  1979. */
  1980. set touchUi(value) {
  1981. this._touchUi = coerceBooleanProperty(value);
  1982. }
  1983. /**
  1984. * Whether the datepicker pop-up should be disabled.
  1985. * @return {?}
  1986. */
  1987. get disabled() {
  1988. return this._disabled === undefined && this._datepickerInput ?
  1989. this._datepickerInput.disabled : !!this._disabled;
  1990. }
  1991. /**
  1992. * @param {?} value
  1993. * @return {?}
  1994. */
  1995. set disabled(value) {
  1996. /** @type {?} */
  1997. const newValue = coerceBooleanProperty(value);
  1998. if (newValue !== this._disabled) {
  1999. this._disabled = newValue;
  2000. this._disabledChange.next(newValue);
  2001. }
  2002. }
  2003. /**
  2004. * Whether the calendar is open.
  2005. * @return {?}
  2006. */
  2007. get opened() { return this._opened; }
  2008. /**
  2009. * @param {?} value
  2010. * @return {?}
  2011. */
  2012. set opened(value) { value ? this.open() : this.close(); }
  2013. /**
  2014. * The currently selected date.
  2015. * @return {?}
  2016. */
  2017. get _selected() { return this._validSelected; }
  2018. /**
  2019. * @param {?} value
  2020. * @return {?}
  2021. */
  2022. set _selected(value) { this._validSelected = value; }
  2023. /**
  2024. * The minimum selectable date.
  2025. * @return {?}
  2026. */
  2027. get _minDate() {
  2028. return this._datepickerInput && this._datepickerInput.min;
  2029. }
  2030. /**
  2031. * The maximum selectable date.
  2032. * @return {?}
  2033. */
  2034. get _maxDate() {
  2035. return this._datepickerInput && this._datepickerInput.max;
  2036. }
  2037. /**
  2038. * @return {?}
  2039. */
  2040. get _dateFilter() {
  2041. return this._datepickerInput && this._datepickerInput._dateFilter;
  2042. }
  2043. /**
  2044. * @return {?}
  2045. */
  2046. ngOnDestroy() {
  2047. this.close();
  2048. this._inputSubscription.unsubscribe();
  2049. this._disabledChange.complete();
  2050. if (this._popupRef) {
  2051. this._popupRef.dispose();
  2052. this._popupComponentRef = null;
  2053. }
  2054. }
  2055. /**
  2056. * Selects the given date
  2057. * @param {?} date
  2058. * @return {?}
  2059. */
  2060. select(date) {
  2061. /** @type {?} */
  2062. let oldValue = this._selected;
  2063. this._selected = date;
  2064. if (!this._dateAdapter.sameDate(oldValue, this._selected)) {
  2065. this._selectedChanged.next(date);
  2066. }
  2067. }
  2068. /**
  2069. * Emits the selected year in multiyear view
  2070. * @param {?} normalizedYear
  2071. * @return {?}
  2072. */
  2073. _selectYear(normalizedYear) {
  2074. this.yearSelected.emit(normalizedYear);
  2075. }
  2076. /**
  2077. * Emits selected month in year view
  2078. * @param {?} normalizedMonth
  2079. * @return {?}
  2080. */
  2081. _selectMonth(normalizedMonth) {
  2082. this.monthSelected.emit(normalizedMonth);
  2083. }
  2084. /**
  2085. * Register an input with this datepicker.
  2086. * @param {?} input The datepicker input to register with this datepicker.
  2087. * @return {?}
  2088. */
  2089. _registerInput(input) {
  2090. if (this._datepickerInput) {
  2091. throw Error('A MatDatepicker can only be associated with a single input.');
  2092. }
  2093. this._datepickerInput = input;
  2094. this._inputSubscription =
  2095. this._datepickerInput._valueChange.subscribe((/**
  2096. * @param {?} value
  2097. * @return {?}
  2098. */
  2099. (value) => this._selected = value));
  2100. }
  2101. /**
  2102. * Open the calendar.
  2103. * @return {?}
  2104. */
  2105. open() {
  2106. if (this._opened || this.disabled) {
  2107. return;
  2108. }
  2109. if (!this._datepickerInput) {
  2110. throw Error('Attempted to open an MatDatepicker with no associated input.');
  2111. }
  2112. if (this._document) {
  2113. this._focusedElementBeforeOpen = this._document.activeElement;
  2114. }
  2115. this.touchUi ? this._openAsDialog() : this._openAsPopup();
  2116. this._opened = true;
  2117. this.openedStream.emit();
  2118. }
  2119. /**
  2120. * Close the calendar.
  2121. * @return {?}
  2122. */
  2123. close() {
  2124. if (!this._opened) {
  2125. return;
  2126. }
  2127. if (this._popupRef && this._popupRef.hasAttached()) {
  2128. this._popupRef.detach();
  2129. }
  2130. if (this._dialogRef) {
  2131. this._dialogRef.close();
  2132. this._dialogRef = null;
  2133. }
  2134. if (this._calendarPortal && this._calendarPortal.isAttached) {
  2135. this._calendarPortal.detach();
  2136. }
  2137. /** @type {?} */
  2138. const completeClose = (/**
  2139. * @return {?}
  2140. */
  2141. () => {
  2142. // The `_opened` could've been reset already if
  2143. // we got two events in quick succession.
  2144. if (this._opened) {
  2145. this._opened = false;
  2146. this.closedStream.emit();
  2147. this._focusedElementBeforeOpen = null;
  2148. }
  2149. });
  2150. if (this._focusedElementBeforeOpen &&
  2151. typeof this._focusedElementBeforeOpen.focus === 'function') {
  2152. // Because IE moves focus asynchronously, we can't count on it being restored before we've
  2153. // marked the datepicker as closed. If the event fires out of sequence and the element that
  2154. // we're refocusing opens the datepicker on focus, the user could be stuck with not being
  2155. // able to close the calendar at all. We work around it by making the logic, that marks
  2156. // the datepicker as closed, async as well.
  2157. this._focusedElementBeforeOpen.focus();
  2158. setTimeout(completeClose);
  2159. }
  2160. else {
  2161. completeClose();
  2162. }
  2163. }
  2164. /**
  2165. * Open the calendar as a dialog.
  2166. * @private
  2167. * @return {?}
  2168. */
  2169. _openAsDialog() {
  2170. // Usually this would be handled by `open` which ensures that we can only have one overlay
  2171. // open at a time, however since we reset the variables in async handlers some overlays
  2172. // may slip through if the user opens and closes multiple times in quick succession (e.g.
  2173. // by holding down the enter key).
  2174. if (this._dialogRef) {
  2175. this._dialogRef.close();
  2176. }
  2177. this._dialogRef = this._dialog.open(MatDatepickerContent, {
  2178. direction: this._dir ? this._dir.value : 'ltr',
  2179. viewContainerRef: this._viewContainerRef,
  2180. panelClass: 'mat-datepicker-dialog',
  2181. });
  2182. this._dialogRef.afterClosed().subscribe((/**
  2183. * @return {?}
  2184. */
  2185. () => this.close()));
  2186. this._dialogRef.componentInstance.datepicker = this;
  2187. this._setColor();
  2188. }
  2189. /**
  2190. * Open the calendar as a popup.
  2191. * @private
  2192. * @return {?}
  2193. */
  2194. _openAsPopup() {
  2195. if (!this._calendarPortal) {
  2196. this._calendarPortal = new ComponentPortal(MatDatepickerContent, this._viewContainerRef);
  2197. }
  2198. if (!this._popupRef) {
  2199. this._createPopup();
  2200. }
  2201. if (!this._popupRef.hasAttached()) {
  2202. this._popupComponentRef = this._popupRef.attach(this._calendarPortal);
  2203. this._popupComponentRef.instance.datepicker = this;
  2204. this._setColor();
  2205. // Update the position once the calendar has rendered.
  2206. this._ngZone.onStable.asObservable().pipe(take(1)).subscribe((/**
  2207. * @return {?}
  2208. */
  2209. () => {
  2210. this._popupRef.updatePosition();
  2211. }));
  2212. }
  2213. }
  2214. /**
  2215. * Create the popup.
  2216. * @private
  2217. * @return {?}
  2218. */
  2219. _createPopup() {
  2220. /** @type {?} */
  2221. const overlayConfig = new OverlayConfig({
  2222. positionStrategy: this._createPopupPositionStrategy(),
  2223. hasBackdrop: true,
  2224. backdropClass: 'mat-overlay-transparent-backdrop',
  2225. direction: this._dir,
  2226. scrollStrategy: this._scrollStrategy(),
  2227. panelClass: 'mat-datepicker-popup',
  2228. });
  2229. this._popupRef = this._overlay.create(overlayConfig);
  2230. this._popupRef.overlayElement.setAttribute('role', 'dialog');
  2231. merge(this._popupRef.backdropClick(), this._popupRef.detachments(), this._popupRef.keydownEvents().pipe(filter((/**
  2232. * @param {?} event
  2233. * @return {?}
  2234. */
  2235. event => {
  2236. // Closing on alt + up is only valid when there's an input associated with the datepicker.
  2237. return event.keyCode === ESCAPE ||
  2238. (this._datepickerInput && event.altKey && event.keyCode === UP_ARROW);
  2239. })))).subscribe((/**
  2240. * @param {?} event
  2241. * @return {?}
  2242. */
  2243. event => {
  2244. if (event) {
  2245. event.preventDefault();
  2246. }
  2247. this.close();
  2248. }));
  2249. }
  2250. /**
  2251. * Create the popup PositionStrategy.
  2252. * @private
  2253. * @return {?}
  2254. */
  2255. _createPopupPositionStrategy() {
  2256. return this._overlay.position()
  2257. .flexibleConnectedTo(this._datepickerInput.getConnectedOverlayOrigin())
  2258. .withTransformOriginOn('.mat-datepicker-content')
  2259. .withFlexibleDimensions(false)
  2260. .withViewportMargin(8)
  2261. .withLockedPosition()
  2262. .withPositions([
  2263. {
  2264. originX: 'start',
  2265. originY: 'bottom',
  2266. overlayX: 'start',
  2267. overlayY: 'top'
  2268. },
  2269. {
  2270. originX: 'start',
  2271. originY: 'top',
  2272. overlayX: 'start',
  2273. overlayY: 'bottom'
  2274. },
  2275. {
  2276. originX: 'end',
  2277. originY: 'bottom',
  2278. overlayX: 'end',
  2279. overlayY: 'top'
  2280. },
  2281. {
  2282. originX: 'end',
  2283. originY: 'top',
  2284. overlayX: 'end',
  2285. overlayY: 'bottom'
  2286. }
  2287. ]);
  2288. }
  2289. /**
  2290. * @private
  2291. * @param {?} obj The object to check.
  2292. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  2293. */
  2294. _getValidDateOrNull(obj) {
  2295. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  2296. }
  2297. /**
  2298. * Passes the current theme color along to the calendar overlay.
  2299. * @private
  2300. * @return {?}
  2301. */
  2302. _setColor() {
  2303. /** @type {?} */
  2304. const color = this.color;
  2305. if (this._popupComponentRef) {
  2306. this._popupComponentRef.instance.color = color;
  2307. }
  2308. if (this._dialogRef) {
  2309. this._dialogRef.componentInstance.color = color;
  2310. }
  2311. }
  2312. }
  2313. MatDatepicker.decorators = [
  2314. { type: Component, args: [{selector: 'mat-datepicker',
  2315. template: '',
  2316. exportAs: 'matDatepicker',
  2317. changeDetection: ChangeDetectionStrategy.OnPush,
  2318. encapsulation: ViewEncapsulation.None,
  2319. },] },
  2320. ];
  2321. /** @nocollapse */
  2322. MatDatepicker.ctorParameters = () => [
  2323. { type: MatDialog },
  2324. { type: Overlay },
  2325. { type: NgZone },
  2326. { type: ViewContainerRef },
  2327. { type: undefined, decorators: [{ type: Inject, args: [MAT_DATEPICKER_SCROLL_STRATEGY,] }] },
  2328. { type: DateAdapter, decorators: [{ type: Optional }] },
  2329. { type: Directionality, decorators: [{ type: Optional }] },
  2330. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [DOCUMENT,] }] }
  2331. ];
  2332. MatDatepicker.propDecorators = {
  2333. calendarHeaderComponent: [{ type: Input }],
  2334. startAt: [{ type: Input }],
  2335. startView: [{ type: Input }],
  2336. color: [{ type: Input }],
  2337. touchUi: [{ type: Input }],
  2338. disabled: [{ type: Input }],
  2339. yearSelected: [{ type: Output }],
  2340. monthSelected: [{ type: Output }],
  2341. panelClass: [{ type: Input }],
  2342. dateClass: [{ type: Input }],
  2343. openedStream: [{ type: Output, args: ['opened',] }],
  2344. closedStream: [{ type: Output, args: ['closed',] }],
  2345. opened: [{ type: Input }]
  2346. };
  2347. /**
  2348. * @fileoverview added by tsickle
  2349. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  2350. */
  2351. /**
  2352. * \@docs-private
  2353. * @type {?}
  2354. */
  2355. const MAT_DATEPICKER_VALUE_ACCESSOR = {
  2356. provide: NG_VALUE_ACCESSOR,
  2357. useExisting: forwardRef((/**
  2358. * @return {?}
  2359. */
  2360. () => MatDatepickerInput)),
  2361. multi: true
  2362. };
  2363. /**
  2364. * \@docs-private
  2365. * @type {?}
  2366. */
  2367. const MAT_DATEPICKER_VALIDATORS = {
  2368. provide: NG_VALIDATORS,
  2369. useExisting: forwardRef((/**
  2370. * @return {?}
  2371. */
  2372. () => MatDatepickerInput)),
  2373. multi: true
  2374. };
  2375. /**
  2376. * An event used for datepicker input and change events. We don't always have access to a native
  2377. * input or change event because the event may have been triggered by the user clicking on the
  2378. * calendar popup. For consistency, we always use MatDatepickerInputEvent instead.
  2379. * @template D
  2380. */
  2381. class MatDatepickerInputEvent {
  2382. /**
  2383. * @param {?} target
  2384. * @param {?} targetElement
  2385. */
  2386. constructor(target, targetElement) {
  2387. this.target = target;
  2388. this.targetElement = targetElement;
  2389. this.value = this.target.value;
  2390. }
  2391. }
  2392. /**
  2393. * Directive used to connect an input to a MatDatepicker.
  2394. * @template D
  2395. */
  2396. class MatDatepickerInput {
  2397. /**
  2398. * @param {?} _elementRef
  2399. * @param {?} _dateAdapter
  2400. * @param {?} _dateFormats
  2401. * @param {?} _formField
  2402. */
  2403. constructor(_elementRef, _dateAdapter, _dateFormats, _formField) {
  2404. this._elementRef = _elementRef;
  2405. this._dateAdapter = _dateAdapter;
  2406. this._dateFormats = _dateFormats;
  2407. this._formField = _formField;
  2408. /**
  2409. * Emits when a `change` event is fired on this `<input>`.
  2410. */
  2411. this.dateChange = new EventEmitter();
  2412. /**
  2413. * Emits when an `input` event is fired on this `<input>`.
  2414. */
  2415. this.dateInput = new EventEmitter();
  2416. /**
  2417. * Emits when the value changes (either due to user input or programmatic change).
  2418. */
  2419. this._valueChange = new EventEmitter();
  2420. /**
  2421. * Emits when the disabled state has changed
  2422. */
  2423. this._disabledChange = new EventEmitter();
  2424. this._onTouched = (/**
  2425. * @return {?}
  2426. */
  2427. () => { });
  2428. this._cvaOnChange = (/**
  2429. * @return {?}
  2430. */
  2431. () => { });
  2432. this._validatorOnChange = (/**
  2433. * @return {?}
  2434. */
  2435. () => { });
  2436. this._datepickerSubscription = Subscription.EMPTY;
  2437. this._localeSubscription = Subscription.EMPTY;
  2438. /**
  2439. * The form control validator for whether the input parses.
  2440. */
  2441. this._parseValidator = (/**
  2442. * @return {?}
  2443. */
  2444. () => {
  2445. return this._lastValueValid ?
  2446. null : { 'matDatepickerParse': { 'text': this._elementRef.nativeElement.value } };
  2447. });
  2448. /**
  2449. * The form control validator for the min date.
  2450. */
  2451. this._minValidator = (/**
  2452. * @param {?} control
  2453. * @return {?}
  2454. */
  2455. (control) => {
  2456. /** @type {?} */
  2457. const controlValue = this._getValidDateOrNull(this._dateAdapter.deserialize(control.value));
  2458. return (!this.min || !controlValue ||
  2459. this._dateAdapter.compareDate(this.min, controlValue) <= 0) ?
  2460. null : { 'matDatepickerMin': { 'min': this.min, 'actual': controlValue } };
  2461. });
  2462. /**
  2463. * The form control validator for the max date.
  2464. */
  2465. this._maxValidator = (/**
  2466. * @param {?} control
  2467. * @return {?}
  2468. */
  2469. (control) => {
  2470. /** @type {?} */
  2471. const controlValue = this._getValidDateOrNull(this._dateAdapter.deserialize(control.value));
  2472. return (!this.max || !controlValue ||
  2473. this._dateAdapter.compareDate(this.max, controlValue) >= 0) ?
  2474. null : { 'matDatepickerMax': { 'max': this.max, 'actual': controlValue } };
  2475. });
  2476. /**
  2477. * The form control validator for the date filter.
  2478. */
  2479. this._filterValidator = (/**
  2480. * @param {?} control
  2481. * @return {?}
  2482. */
  2483. (control) => {
  2484. /** @type {?} */
  2485. const controlValue = this._getValidDateOrNull(this._dateAdapter.deserialize(control.value));
  2486. return !this._dateFilter || !controlValue || this._dateFilter(controlValue) ?
  2487. null : { 'matDatepickerFilter': true };
  2488. });
  2489. /**
  2490. * The combined form control validator for this input.
  2491. */
  2492. this._validator = Validators.compose([this._parseValidator, this._minValidator, this._maxValidator, this._filterValidator]);
  2493. /**
  2494. * Whether the last value set on the input was valid.
  2495. */
  2496. this._lastValueValid = false;
  2497. if (!this._dateAdapter) {
  2498. throw createMissingDateImplError('DateAdapter');
  2499. }
  2500. if (!this._dateFormats) {
  2501. throw createMissingDateImplError('MAT_DATE_FORMATS');
  2502. }
  2503. // Update the displayed date when the locale changes.
  2504. this._localeSubscription = _dateAdapter.localeChanges.subscribe((/**
  2505. * @return {?}
  2506. */
  2507. () => {
  2508. this.value = this.value;
  2509. }));
  2510. }
  2511. /**
  2512. * The datepicker that this input is associated with.
  2513. * @param {?} value
  2514. * @return {?}
  2515. */
  2516. set matDatepicker(value) {
  2517. if (!value) {
  2518. return;
  2519. }
  2520. this._datepicker = value;
  2521. this._datepicker._registerInput(this);
  2522. this._datepickerSubscription.unsubscribe();
  2523. this._datepickerSubscription = this._datepicker._selectedChanged.subscribe((/**
  2524. * @param {?} selected
  2525. * @return {?}
  2526. */
  2527. (selected) => {
  2528. this.value = selected;
  2529. this._cvaOnChange(selected);
  2530. this._onTouched();
  2531. this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
  2532. this.dateChange.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
  2533. }));
  2534. }
  2535. /**
  2536. * Function that can be used to filter out dates within the datepicker.
  2537. * @param {?} value
  2538. * @return {?}
  2539. */
  2540. set matDatepickerFilter(value) {
  2541. this._dateFilter = value;
  2542. this._validatorOnChange();
  2543. }
  2544. /**
  2545. * The value of the input.
  2546. * @return {?}
  2547. */
  2548. get value() { return this._value; }
  2549. /**
  2550. * @param {?} value
  2551. * @return {?}
  2552. */
  2553. set value(value) {
  2554. value = this._dateAdapter.deserialize(value);
  2555. this._lastValueValid = !value || this._dateAdapter.isValid(value);
  2556. value = this._getValidDateOrNull(value);
  2557. /** @type {?} */
  2558. const oldDate = this.value;
  2559. this._value = value;
  2560. this._formatValue(value);
  2561. if (!this._dateAdapter.sameDate(oldDate, value)) {
  2562. this._valueChange.emit(value);
  2563. }
  2564. }
  2565. /**
  2566. * The minimum valid date.
  2567. * @return {?}
  2568. */
  2569. get min() { return this._min; }
  2570. /**
  2571. * @param {?} value
  2572. * @return {?}
  2573. */
  2574. set min(value) {
  2575. this._min = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  2576. this._validatorOnChange();
  2577. }
  2578. /**
  2579. * The maximum valid date.
  2580. * @return {?}
  2581. */
  2582. get max() { return this._max; }
  2583. /**
  2584. * @param {?} value
  2585. * @return {?}
  2586. */
  2587. set max(value) {
  2588. this._max = this._getValidDateOrNull(this._dateAdapter.deserialize(value));
  2589. this._validatorOnChange();
  2590. }
  2591. /**
  2592. * Whether the datepicker-input is disabled.
  2593. * @return {?}
  2594. */
  2595. get disabled() { return !!this._disabled; }
  2596. /**
  2597. * @param {?} value
  2598. * @return {?}
  2599. */
  2600. set disabled(value) {
  2601. /** @type {?} */
  2602. const newValue = coerceBooleanProperty(value);
  2603. /** @type {?} */
  2604. const element = this._elementRef.nativeElement;
  2605. if (this._disabled !== newValue) {
  2606. this._disabled = newValue;
  2607. this._disabledChange.emit(newValue);
  2608. }
  2609. // We need to null check the `blur` method, because it's undefined during SSR.
  2610. if (newValue && element.blur) {
  2611. // Normally, native input elements automatically blur if they turn disabled. This behavior
  2612. // is problematic, because it would mean that it triggers another change detection cycle,
  2613. // which then causes a changed after checked error if the input element was focused before.
  2614. element.blur();
  2615. }
  2616. }
  2617. /**
  2618. * @return {?}
  2619. */
  2620. ngOnDestroy() {
  2621. this._datepickerSubscription.unsubscribe();
  2622. this._localeSubscription.unsubscribe();
  2623. this._valueChange.complete();
  2624. this._disabledChange.complete();
  2625. }
  2626. /**
  2627. * \@docs-private
  2628. * @param {?} fn
  2629. * @return {?}
  2630. */
  2631. registerOnValidatorChange(fn) {
  2632. this._validatorOnChange = fn;
  2633. }
  2634. /**
  2635. * \@docs-private
  2636. * @param {?} c
  2637. * @return {?}
  2638. */
  2639. validate(c) {
  2640. return this._validator ? this._validator(c) : null;
  2641. }
  2642. /**
  2643. * @deprecated
  2644. * \@breaking-change 8.0.0 Use `getConnectedOverlayOrigin` instead
  2645. * @return {?}
  2646. */
  2647. getPopupConnectionElementRef() {
  2648. return this.getConnectedOverlayOrigin();
  2649. }
  2650. /**
  2651. * Gets the element that the datepicker popup should be connected to.
  2652. * @return {?} The element to connect the popup to.
  2653. */
  2654. getConnectedOverlayOrigin() {
  2655. return this._formField ? this._formField.getConnectedOverlayOrigin() : this._elementRef;
  2656. }
  2657. // Implemented as part of ControlValueAccessor.
  2658. /**
  2659. * @param {?} value
  2660. * @return {?}
  2661. */
  2662. writeValue(value) {
  2663. this.value = value;
  2664. }
  2665. // Implemented as part of ControlValueAccessor.
  2666. /**
  2667. * @param {?} fn
  2668. * @return {?}
  2669. */
  2670. registerOnChange(fn) {
  2671. this._cvaOnChange = fn;
  2672. }
  2673. // Implemented as part of ControlValueAccessor.
  2674. /**
  2675. * @param {?} fn
  2676. * @return {?}
  2677. */
  2678. registerOnTouched(fn) {
  2679. this._onTouched = fn;
  2680. }
  2681. // Implemented as part of ControlValueAccessor.
  2682. /**
  2683. * @param {?} isDisabled
  2684. * @return {?}
  2685. */
  2686. setDisabledState(isDisabled) {
  2687. this.disabled = isDisabled;
  2688. }
  2689. /**
  2690. * @param {?} event
  2691. * @return {?}
  2692. */
  2693. _onKeydown(event) {
  2694. /** @type {?} */
  2695. const isAltDownArrow = event.altKey && event.keyCode === DOWN_ARROW;
  2696. if (this._datepicker && isAltDownArrow && !this._elementRef.nativeElement.readOnly) {
  2697. this._datepicker.open();
  2698. event.preventDefault();
  2699. }
  2700. }
  2701. /**
  2702. * @param {?} value
  2703. * @return {?}
  2704. */
  2705. _onInput(value) {
  2706. /** @type {?} */
  2707. let date = this._dateAdapter.parse(value, this._dateFormats.parse.dateInput);
  2708. this._lastValueValid = !date || this._dateAdapter.isValid(date);
  2709. date = this._getValidDateOrNull(date);
  2710. if (!this._dateAdapter.sameDate(date, this._value)) {
  2711. this._value = date;
  2712. this._cvaOnChange(date);
  2713. this._valueChange.emit(date);
  2714. this.dateInput.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
  2715. }
  2716. else {
  2717. this._validatorOnChange();
  2718. }
  2719. }
  2720. /**
  2721. * @return {?}
  2722. */
  2723. _onChange() {
  2724. this.dateChange.emit(new MatDatepickerInputEvent(this, this._elementRef.nativeElement));
  2725. }
  2726. /**
  2727. * Returns the palette used by the input's form field, if any.
  2728. * @return {?}
  2729. */
  2730. _getThemePalette() {
  2731. return this._formField ? this._formField.color : undefined;
  2732. }
  2733. /**
  2734. * Handles blur events on the input.
  2735. * @return {?}
  2736. */
  2737. _onBlur() {
  2738. // Reformat the input only if we have a valid value.
  2739. if (this.value) {
  2740. this._formatValue(this.value);
  2741. }
  2742. this._onTouched();
  2743. }
  2744. /**
  2745. * Formats a value and sets it on the input element.
  2746. * @private
  2747. * @param {?} value
  2748. * @return {?}
  2749. */
  2750. _formatValue(value) {
  2751. this._elementRef.nativeElement.value =
  2752. value ? this._dateAdapter.format(value, this._dateFormats.display.dateInput) : '';
  2753. }
  2754. /**
  2755. * @private
  2756. * @param {?} obj The object to check.
  2757. * @return {?} The given object if it is both a date instance and valid, otherwise null.
  2758. */
  2759. _getValidDateOrNull(obj) {
  2760. return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
  2761. }
  2762. }
  2763. MatDatepickerInput.decorators = [
  2764. { type: Directive, args: [{
  2765. selector: 'input[matDatepicker]',
  2766. providers: [
  2767. MAT_DATEPICKER_VALUE_ACCESSOR,
  2768. MAT_DATEPICKER_VALIDATORS,
  2769. { provide: MAT_INPUT_VALUE_ACCESSOR, useExisting: MatDatepickerInput },
  2770. ],
  2771. host: {
  2772. '[attr.aria-haspopup]': '_datepicker ? "dialog" : null',
  2773. '[attr.aria-owns]': '(_datepicker?.opened && _datepicker.id) || null',
  2774. '[attr.min]': 'min ? _dateAdapter.toIso8601(min) : null',
  2775. '[attr.max]': 'max ? _dateAdapter.toIso8601(max) : null',
  2776. '[disabled]': 'disabled',
  2777. '(input)': '_onInput($event.target.value)',
  2778. '(change)': '_onChange()',
  2779. '(blur)': '_onBlur()',
  2780. '(keydown)': '_onKeydown($event)',
  2781. },
  2782. exportAs: 'matDatepickerInput',
  2783. },] },
  2784. ];
  2785. /** @nocollapse */
  2786. MatDatepickerInput.ctorParameters = () => [
  2787. { type: ElementRef },
  2788. { type: DateAdapter, decorators: [{ type: Optional }] },
  2789. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_DATE_FORMATS,] }] },
  2790. { type: MatFormField, decorators: [{ type: Optional }] }
  2791. ];
  2792. MatDatepickerInput.propDecorators = {
  2793. matDatepicker: [{ type: Input }],
  2794. matDatepickerFilter: [{ type: Input }],
  2795. value: [{ type: Input }],
  2796. min: [{ type: Input }],
  2797. max: [{ type: Input }],
  2798. disabled: [{ type: Input }],
  2799. dateChange: [{ type: Output }],
  2800. dateInput: [{ type: Output }]
  2801. };
  2802. /**
  2803. * @fileoverview added by tsickle
  2804. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  2805. */
  2806. /**
  2807. * Can be used to override the icon of a `matDatepickerToggle`.
  2808. */
  2809. class MatDatepickerToggleIcon {
  2810. }
  2811. MatDatepickerToggleIcon.decorators = [
  2812. { type: Directive, args: [{
  2813. selector: '[matDatepickerToggleIcon]'
  2814. },] },
  2815. ];
  2816. /**
  2817. * @template D
  2818. */
  2819. class MatDatepickerToggle {
  2820. /**
  2821. * @param {?} _intl
  2822. * @param {?} _changeDetectorRef
  2823. * @param {?} defaultTabIndex
  2824. */
  2825. constructor(_intl, _changeDetectorRef, defaultTabIndex) {
  2826. this._intl = _intl;
  2827. this._changeDetectorRef = _changeDetectorRef;
  2828. this._stateChanges = Subscription.EMPTY;
  2829. /** @type {?} */
  2830. const parsedTabIndex = Number(defaultTabIndex);
  2831. this.tabIndex = (parsedTabIndex || parsedTabIndex === 0) ? parsedTabIndex : null;
  2832. }
  2833. /**
  2834. * Whether the toggle button is disabled.
  2835. * @return {?}
  2836. */
  2837. get disabled() {
  2838. if (this._disabled === undefined && this.datepicker) {
  2839. return this.datepicker.disabled;
  2840. }
  2841. return !!this._disabled;
  2842. }
  2843. /**
  2844. * @param {?} value
  2845. * @return {?}
  2846. */
  2847. set disabled(value) {
  2848. this._disabled = coerceBooleanProperty(value);
  2849. }
  2850. /**
  2851. * @param {?} changes
  2852. * @return {?}
  2853. */
  2854. ngOnChanges(changes) {
  2855. if (changes['datepicker']) {
  2856. this._watchStateChanges();
  2857. }
  2858. }
  2859. /**
  2860. * @return {?}
  2861. */
  2862. ngOnDestroy() {
  2863. this._stateChanges.unsubscribe();
  2864. }
  2865. /**
  2866. * @return {?}
  2867. */
  2868. ngAfterContentInit() {
  2869. this._watchStateChanges();
  2870. }
  2871. /**
  2872. * @param {?} event
  2873. * @return {?}
  2874. */
  2875. _open(event) {
  2876. if (this.datepicker && !this.disabled) {
  2877. this.datepicker.open();
  2878. event.stopPropagation();
  2879. }
  2880. }
  2881. /**
  2882. * @private
  2883. * @return {?}
  2884. */
  2885. _watchStateChanges() {
  2886. /** @type {?} */
  2887. const datepickerDisabled = this.datepicker ? this.datepicker._disabledChange : of();
  2888. /** @type {?} */
  2889. const inputDisabled = this.datepicker && this.datepicker._datepickerInput ?
  2890. this.datepicker._datepickerInput._disabledChange : of();
  2891. /** @type {?} */
  2892. const datepickerToggled = this.datepicker ?
  2893. merge(this.datepicker.openedStream, this.datepicker.closedStream) :
  2894. of();
  2895. this._stateChanges.unsubscribe();
  2896. this._stateChanges = merge(this._intl.changes, datepickerDisabled, inputDisabled, datepickerToggled).subscribe((/**
  2897. * @return {?}
  2898. */
  2899. () => this._changeDetectorRef.markForCheck()));
  2900. }
  2901. }
  2902. MatDatepickerToggle.decorators = [
  2903. { type: Component, args: [{selector: 'mat-datepicker-toggle',
  2904. template: "<button #button mat-icon-button type=\"button\" [attr.aria-haspopup]=\"datepicker ? 'dialog' : null\" [attr.aria-label]=\"_intl.openCalendarLabel\" [attr.tabindex]=\"disabled ? -1 : tabIndex\" [disabled]=\"disabled\" [disableRipple]=\"disableRipple\" (click)=\"_open($event)\"><svg *ngIf=\"!_customIcon\" class=\"mat-datepicker-toggle-default-icon\" viewBox=\"0 0 24 24\" width=\"24px\" height=\"24px\" fill=\"currentColor\" focusable=\"false\"><path d=\"M19 3h-1V1h-2v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V8h14v11zM7 10h5v5H7z\"/></svg><ng-content select=\"[matDatepickerToggleIcon]\"></ng-content></button>",
  2905. styles: [".mat-form-field-appearance-legacy .mat-form-field-prefix .mat-datepicker-toggle-default-icon,.mat-form-field-appearance-legacy .mat-form-field-suffix .mat-datepicker-toggle-default-icon{width:1em}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-datepicker-toggle-default-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-datepicker-toggle-default-icon{display:block;width:1.5em;height:1.5em}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button .mat-datepicker-toggle-default-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button .mat-datepicker-toggle-default-icon{margin:auto}"],
  2906. host: {
  2907. 'class': 'mat-datepicker-toggle',
  2908. // Always set the tabindex to -1 so that it doesn't overlap with any custom tabindex the
  2909. // consumer may have provided, while still being able to receive focus.
  2910. '[attr.tabindex]': '-1',
  2911. '[class.mat-datepicker-toggle-active]': 'datepicker && datepicker.opened',
  2912. '[class.mat-accent]': 'datepicker && datepicker.color === "accent"',
  2913. '[class.mat-warn]': 'datepicker && datepicker.color === "warn"',
  2914. '(focus)': '_button.focus()',
  2915. },
  2916. exportAs: 'matDatepickerToggle',
  2917. encapsulation: ViewEncapsulation.None,
  2918. changeDetection: ChangeDetectionStrategy.OnPush,
  2919. },] },
  2920. ];
  2921. /** @nocollapse */
  2922. MatDatepickerToggle.ctorParameters = () => [
  2923. { type: MatDatepickerIntl },
  2924. { type: ChangeDetectorRef },
  2925. { type: String, decorators: [{ type: Attribute, args: ['tabindex',] }] }
  2926. ];
  2927. MatDatepickerToggle.propDecorators = {
  2928. datepicker: [{ type: Input, args: ['for',] }],
  2929. tabIndex: [{ type: Input }],
  2930. disabled: [{ type: Input }],
  2931. disableRipple: [{ type: Input }],
  2932. _customIcon: [{ type: ContentChild, args: [MatDatepickerToggleIcon, { static: false },] }],
  2933. _button: [{ type: ViewChild, args: ['button', { static: false },] }]
  2934. };
  2935. /**
  2936. * @fileoverview added by tsickle
  2937. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  2938. */
  2939. class MatDatepickerModule {
  2940. }
  2941. MatDatepickerModule.decorators = [
  2942. { type: NgModule, args: [{
  2943. imports: [
  2944. CommonModule,
  2945. MatButtonModule,
  2946. MatDialogModule,
  2947. OverlayModule,
  2948. A11yModule,
  2949. PortalModule,
  2950. ],
  2951. exports: [
  2952. MatCalendar,
  2953. MatCalendarBody,
  2954. MatDatepicker,
  2955. MatDatepickerContent,
  2956. MatDatepickerInput,
  2957. MatDatepickerToggle,
  2958. MatDatepickerToggleIcon,
  2959. MatMonthView,
  2960. MatYearView,
  2961. MatMultiYearView,
  2962. MatCalendarHeader,
  2963. ],
  2964. declarations: [
  2965. MatCalendar,
  2966. MatCalendarBody,
  2967. MatDatepicker,
  2968. MatDatepickerContent,
  2969. MatDatepickerInput,
  2970. MatDatepickerToggle,
  2971. MatDatepickerToggleIcon,
  2972. MatMonthView,
  2973. MatYearView,
  2974. MatMultiYearView,
  2975. MatCalendarHeader,
  2976. ],
  2977. providers: [
  2978. MatDatepickerIntl,
  2979. MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER,
  2980. ],
  2981. entryComponents: [
  2982. MatDatepickerContent,
  2983. MatCalendarHeader,
  2984. ]
  2985. },] },
  2986. ];
  2987. /**
  2988. * @fileoverview added by tsickle
  2989. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  2990. */
  2991. /**
  2992. * @fileoverview added by tsickle
  2993. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  2994. */
  2995. export { MatMultiYearView, yearsPerPage, yearsPerRow, MatDatepickerModule, MatCalendarHeader, MatCalendar, MatCalendarCell, MatCalendarBody, MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY, MAT_DATEPICKER_SCROLL_STRATEGY, MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER, MatDatepickerContent, MatDatepicker, matDatepickerAnimations, MAT_DATEPICKER_VALUE_ACCESSOR, MAT_DATEPICKER_VALIDATORS, MatDatepickerInputEvent, MatDatepickerInput, MatDatepickerIntl, MatDatepickerToggleIcon, MatDatepickerToggle, MatMonthView, MatYearView };
  2996. //# sourceMappingURL=datepicker.js.map