Skip to content
This repository was archived by the owner on Mar 2, 2018. It is now read-only.

Commit 10e9edf

Browse files
authored
Merge pull request #128 from SDV-Plurimedia/master
ADD directive output to know when the component close
2 parents ae80fad + ae5e3f2 commit 10e9edf

File tree

3 files changed

+70
-51
lines changed

3 files changed

+70
-51
lines changed

README.md

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ AngularJS 2 DateTime Picker
3838
import { BrowserModule } from '@angular/platform-browser';
3939
import { AppComponent } from './app.component';
4040
import { Ng2DatetimePickerModule } from 'ng2-datetime-picker';
41-
41+
4242
@NgModule({
4343
imports: [BrowserModule, FormsModule, Ng2DatetimePickerModule],
4444
declarations: [AppComponent],
@@ -48,22 +48,22 @@ AngularJS 2 DateTime Picker
4848

4949
## Use it in your code
5050

51-
51+
5252
<input
5353
[(ngModel)]="myDate"
54-
ng2-datetime-picker
54+
ng2-datetime-picker
5555
date-only="true" />
5656

5757
<form [formGroup]="myForm">
58-
<input
58+
<input
5959
required
60-
[(ngModel)]='myVar'
61-
formControlName="date"
60+
[(ngModel)]='myVar'
61+
formControlName="date"
6262
ng2-datetime-picker
6363
date-only="true"/>
6464
</form>
65-
66-
<input
65+
66+
<input
6767
[(ngModel)]="date2" ng2-datetime-picker
6868
date-format="DD-MM-YYYY hh:mm"
6969
year="2014"
@@ -72,53 +72,53 @@ AngularJS 2 DateTime Picker
7272
hour="23"
7373
minute='59'
7474
close-on-select="false" />
75-
75+
7676
For full example, please check `test` directory to see the example of;
7777

7878
- `app.module.ts`
7979
- and `app.component.ts`.
8080

8181
## Override default style
8282

83-
The default style is written in `src/ng2-datetime-picker.component.ts`.
83+
The default style is written in `src/ng2-datetime-picker.component.ts`.
8484
This can be overwritten by giving a more detailed css selector.
85-
86-
e.g.,
85+
86+
e.g.,
8787

8888
#my-div .ng2-datetime-picker {
8989
background-color: blue;
9090
}
91-
91+
9292
## Override built-in date parser and date formatter
9393

9494
The default date parser and formatter can only handle 'YYYY-MM-DD HH:MM' format
95-
if you are not using [momentjs](http://momentjs.com/). If you use momentjs, you
95+
if you are not using [momentjs](http://momentjs.com/). If you use momentjs, you
9696
can use momentjs dateformat by adding the following in your html.
9797

9898
<script src="moment-with-locales.min.js"></script>
99-
99+
100100
If you are using moment and want to pass in a string date value in one format but display it in a different format
101101
you can use both date-format and parse-format:
102-
103-
<input
104-
[(ngModel)]="date"
102+
103+
<input
104+
[(ngModel)]="date"
105105
ng2-datetime-picker
106106
date-format="MM/DD/YYYY HH:mm"
107107
parse-format="YYYY-MM-DD HH:mm:ss" />
108108

109-
If you want to have your own date format without using momentjs,
109+
If you want to have your own date format without using momentjs,
110110
please override `Ng2DateTime.parser` and `Ng2DateTime.formatDate` function.
111-
For example,
111+
For example,
112112

113113
import { Ng2DatetimePickerModule, Ng2Datetime } from 'ng2-datetime-picker';
114114

115115
// Override Date object formatter
116-
Ng2Datetime.formatDate = (date: Date) : string => {
117-
..... my own function that returns a string ....
116+
Ng2Datetime.formatDate = (date: Date) : string => {
117+
..... my own function that returns a string ....
118118
};
119119

120120
// Override Date object parser
121-
Ng2Datetime.parseDate = (str: any): Date => {
121+
Ng2Datetime.parseDate = (str: any): Date => {
122122
.... my own function that returns a date ...
123123
} ;
124124

@@ -131,7 +131,7 @@ For example,
131131

132132
In addition, you can override other static variables of `Ng2Datetime` class. The following
133133
is the list of variables that you can override.
134-
134+
135135
* **days**: default: 1,2,....31
136136
* **daysOfWeek**: default: Sunday, Monday, .....
137137
* **weekends**: default: 0,6
@@ -144,7 +144,7 @@ is the list of variables that you can override.
144144

145145
This module is only improved and maintained by contributors like you;
146146

147-
As a contributor, it's NOT required to be skilled in Javascript nor Angular2.
147+
As a contributor, it's NOT required to be skilled in Javascript nor Angular2.
148148
It’s required to be open-minded and interested in helping others.
149149
You can contribute to the following;
150150

@@ -158,13 +158,13 @@ In result of your active contribution, you will be listed as a core contributor
158158
on https://ng2-ui.github.io, and a member of ng2-ui too.
159159

160160
If you are interested in becoming a contributor and/or a member of ng-ui,
161-
please send me email to `allenhwkim AT gmail.com` with your github id.
161+
please send me email to `allenhwkim AT gmail.com` with your github id.
162162

163-
## attributes
163+
## Attributes
164164
All options are optional except value
165165

166166
* **date-only**, true or false, default is false
167-
* **time-only**, true or false, default is false
167+
* **time-only**, true or false, default is false
168168
* **close-on-select**, true or false. indicates to close ng2-datetime-picker when select a date. default: true
169169
* **date-format**, momentjs date format, e.g. YYYY-MM-DD hh:mm:ss.
170170
You need to include `moment` js in your html to use date-format.
@@ -179,7 +179,18 @@ please send me email to `allenhwkim AT gmail.com` with your github id.
179179
* **min-hour** number, mininum selectable hour
180180
* **max-hour** number, maximum selectable hour
181181
* **disabled-dates** Array of Date, dates not selectable
182-
182+
183+
## Outputs of directive
184+
185+
* **ngModelChange**, triggered when the input value as changed (contains new input value)
186+
* **valueChanged**, triggered when a date modification is done (contains new date value)
187+
* **popupClosed**, triggered when the component is closed (contains a boolean true)
188+
189+
## Outputs of component
190+
191+
* **selected$**, triggered when a date modification is done (contains new date value)
192+
* **closing$**, triggered when the component is closed (contains a boolean true)
193+
183194
## For Developers
184195

185196
### To start
@@ -188,7 +199,7 @@ please send me email to `allenhwkim AT gmail.com` with your github id.
188199
$ cd ng2-datetime-picker
189200
$ npm install
190201
$ npm start
191-
202+
192203
### List of available npm tasks
193204

194205
* `npm run` : List all available tasks

app/directive-test.component.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ moment['locale']('en-ca'); //e.g. fr-ca
1010
var templateStr = `
1111
<div id="my-div">
1212
<h1>Ng2 DateTime Picker Test</h1>
13-
13+
1414
<fieldset id="test1"><legend><h2>Open from a button</h2></legend>
1515
<ng2-utils-1>
1616
<input [(ngModel)]="date1" />
@@ -22,11 +22,11 @@ var templateStr = `
2222
date1: {{date1}}
2323
<pre>{{templateStr | htmlCode:'ng2-utils-1'}}</pre>
2424
</fieldset>
25-
25+
2626
<fieldset id="test2"><legend><h2>min date, max date, disabled dates</h2></legend>
2727
<ng2-utils-2>
2828
<input
29-
[(ngModel)]="date2"
29+
[(ngModel)]="date2"
3030
ng2-datetime-picker
3131
[disabled-dates]="date2DisabledDates"
3232
[min-date]="date2MinDate"
@@ -37,37 +37,38 @@ var templateStr = `
3737
<a href="javascript:void(0)" (click)="myDate=defaultValue">set myDate</a><br/>
3838
<pre>{{templateStr | htmlCode:'ng2-utils-2'}}</pre>
3939
</fieldset>
40-
40+
4141
<fieldset id="test3"><legend><h2>time only</h2></legend>
4242
<ng2-utils-4>
4343
<input [(ngModel)]="date3"
44-
ng2-datetime-picker
44+
ng2-datetime-picker
4545
date-format="DD-MM-YYYY hh:mm"
4646
time-only="true"
4747
minute-step="5"
48+
(popupClosed)="doOnClose($event)"
4849
close-on-select="false" />
4950
</ng2-utils-4>
5051
<pre>{{templateStr | htmlCode:'ng2-utils-4'}}</pre>
5152
</fieldset>
52-
53+
5354
<fieldset id="test4"><legend><h2>with timezone</h2></legend>
5455
<ng2-utils-6>
55-
<input
56-
[(ngModel)]="date4"
56+
<input
57+
[(ngModel)]="date4"
5758
ng2-datetime-picker
5859
[date-format]="date4TimezoneFormat" />
5960
dateWithTimezone: {{dateWithTimezone}}
6061
<br/>
6162
</ng2-utils-6>
6263
<pre>{{templateStr | htmlCode:'ng2-utils-6'}}</pre>
6364
</fieldset>
64-
65+
6566
<fieldset id="test5"><legend><h2>Reactive form</h2></legend>
6667
<ng2-utils-3>
6768
<form [formGroup]="myForm">
68-
<input
69+
<input
6970
required
70-
formControlName="date"
71+
formControlName="date"
7172
ng2-datetime-picker
7273
close-on-select="false"/>
7374
</form>
@@ -76,7 +77,7 @@ var templateStr = `
7677
<br/>myForm.dirty: {{myForm.dirty}}
7778
<br/>myForm.controls.date.dirty: {{myForm.controls.date.dirty}}
7879
<br/>
79-
<a href="javascript:void()"
80+
<a href="javascript:void()"
8081
(click)="myForm.controls.date.patchValue('2015-06-30')">
8182
2015-06-30
8283
</a>
@@ -91,7 +92,7 @@ var templateStr = `
9192
</ng2-utils-3>
9293
<pre>{{templateStr | htmlCode:'ng2-utils-3'}}</pre>
9394
</fieldset>
94-
95+
9596
</div>
9697
`;
9798

@@ -136,4 +137,8 @@ export class DirectiveTestComponent {
136137
//moment.tz.setDefault('US/Central'); // Set the default timezone that moment will use
137138
}
138139

140+
private doOnClose(event:boolean){
141+
alert(" Do something on close ");
142+
}
143+
139144
}

src/ng2-datetime-picker.directive.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export class Ng2DatetimePickerDirective implements OnInit, OnChanges {
4848
@Input('ngModel') ngModel: any;
4949
@Output('ngModelChange') ngModelChange = new EventEmitter();
5050
@Output('valueChanged') valueChanged = new EventEmitter();
51+
@Output('popupClosed') popupClosed = new EventEmitter();
5152

5253
private el: HTMLInputElement; /* input element */
5354
private ng2DatetimePickerEl: HTMLElement; /* dropdown element */
@@ -233,7 +234,7 @@ export class Ng2DatetimePickerDirective implements OnInit, OnChanges {
233234
component.closing$.subscribe(() => {
234235
this.closeOnSelect !== "false" && this.hideDatetimePicker();
235236
});
236-
237+
237238
//Hack not to fire tab keyup event
238239
this.justShown = true;
239240
setTimeout(() => this.justShown = false, 100);
@@ -246,19 +247,21 @@ export class Ng2DatetimePickerDirective implements OnInit, OnChanges {
246247

247248
hideDatetimePicker = (event?):void => {
248249
if (this.componentRef) {
249-
if ( /* invoked by clicking on somewhere in document */
250-
event &&
250+
if (
251+
/* invoked by clicking on somewhere in document */
252+
(event &&
251253
event.type === 'click' &&
252254
event.target !== this.el &&
253-
!this.elementIn(event.target, this.ng2DatetimePickerEl)
255+
!this.elementIn(event.target, this.ng2DatetimePickerEl))
256+
|| 
257+
/* invoked by function call */
258+
!event
254259
) {
255260
this.componentRef.destroy();
256261
this.componentRef = undefined;
257-
} else if (!event) { /* invoked by function call */
258-
this.componentRef.destroy();
259-
this.componentRef = undefined;
262+
this.popupClosed.emit(true);
260263
}
261-
event && event.stopPropagation();
264+
event && event.stopPropagation();
262265
}
263266
};
264267

0 commit comments

Comments
 (0)