Skip to content

Commit 519ca1f

Browse files
committed
Translation feature
1 parent c82fc3a commit 519ca1f

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Material Datetime Picker
1+
# Material Datetime Picker
22

33
A Material Design date/time picker modal, built for the web. Works well with Materialize, or standalone.
44

55
[https://ripjar.github.io/material-datetime-picker/](https://ripjar.github.io/material-datetime-picker/)
66

7-
[![Status][status]](https://travis-ci.org/ripjar/material-datetime-picker)
7+
[![Status][status]](https://travis-ci.org/ripjar/material-datetime-picker)
88
[![Package][npm]](https://www.npmjs.com/package/material-datetime-picker)
99

1010
![Time][date] ![Time][time]
@@ -23,7 +23,7 @@ The picker depends on Google's Material Design icons (packaged with Materialize)
2323
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
2424
```
2525

26-
---
26+
---
2727

2828
For best results also include Google's Material Font `Roboto`;
2929

@@ -47,7 +47,7 @@ If you want to use this project as a standalone `<script>`, you can use `dist/m
4747
### Manual (recommended)
4848

4949
The picker can be instantiated and interacted with manally;
50-
50+
5151
```javascript
5252
import MaterialDateTimePicker from 'material-datetime-picker';
5353

@@ -57,15 +57,15 @@ const picker = new MaterialDateTimePicker()
5757
.on('close', () => console.log('closed'));
5858

5959
document.querySelector('.c-datepicker-btn')
60-
.on('click', () => picker.open());
60+
.on('click', () => picker.open());
6161
```
6262

6363
---
6464

6565
### As form input
6666

6767
The picker is decoupled from any single form element for simplicity. However, it should be simple to link the picker to a form input or button. For instance, given the input element `<input class="c-datepicker-input" />`, the following could be written;
68-
68+
6969
```javascript
7070
import MaterialDateTimePicker from 'material-datetime-picker';
7171

@@ -75,28 +75,28 @@ const picker = new MaterialDatePicker()
7575
input.value = val.format("DD/MM/YYYY");
7676
});
7777

78-
input.addEventListener('focus', () => picker.open());
78+
input.addEventListener('focus', () => picker.open());
7979
```
8080

8181
## Options
82-
82+
8383
All options are optional, including the `el`.
8484

8585
```javascript
8686
{
87-
// DOM Element to attach the datepicker. This element will receive
88-
// events when the data changes. If an input element, will be
87+
// DOM Element to attach the datepicker. This element will receive
88+
// events when the data changes. If an input element, will be
8989
// populated with formatted date and time chosen.
9090
// `el` must be a DOM Element object. Selectpr strings or wrappers
9191
// like a jQuery selection are not supported.
9292
el: document.querySelector('.c-datepicker-btn'),
93-
// if `el` is set, the format used to display the datetime in the input,
94-
format: 'DD/MM/YY',
93+
// if `el` is set, the format used to display the datetime in the input,
94+
format: 'DD/MM/YY',
9595
// the default value of the picker
9696
default: moment(),
9797
// the container to append the picker. If you change this, you need to make
9898
// sure your element has a z-index > 0 so that it displays in front of the scrim.
99-
container: document.body,
99+
container: document.body,
100100
// cosmetic classes that can be overriden
101101
// mostly used for styling the calendar
102102
styles: {
@@ -123,7 +123,12 @@ All options are optional, including the `el`.
123123
clockNum: 'c-datepicker__clock__num'
124124
},
125125
// date range to allow (see rome validator factories)
126-
dateValidator: null
126+
dateValidator: null
127+
// translations for buttons label
128+
okLabel: 'OK',
129+
cancelLabel: 'Cancel'
130+
// format to display the day number.
131+
dayFormat: 'Do',
127132
}
128133
```
129134

lib/js/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ const defaults = () => ({
4040
// the container to append the picker
4141
container: document.body,
4242
// allow any dates
43-
dateValidator: undefined
43+
dateValidator: undefined,
44+
// translations for buttons label
45+
okLabel: 'OK',
46+
cancelLabel: 'Cancel',
47+
// format to display the day number.
48+
dayFormat: 'Do'
4449
});
4550

4651
class DateTimePicker extends Events {
@@ -70,15 +75,17 @@ class DateTimePicker extends Events {
7075
styles: this.options.styles,
7176
time: false,
7277
dateValidator: validator,
73-
initialValue: this.value
78+
initialValue: this.options.default,
79+
weekdayFormat: this.options.default.localeData().weekdaysMin(),
80+
weekStart: this.options.default.clone().weekday(0).day()
7481
}).on('data', onData);
7582
}
7683

7784
// called to open the picker
7885
open() {
7986
const scrimEl = scrimTemplate(this.options);
8087
_appendTemplate(document.body, scrimEl);
81-
_appendTemplate(this.options.container, popupTemplate());
88+
_appendTemplate(this.options.container, popupTemplate(this.options.okLabel, this.options.cancelLabel));
8289
this.pickerEl = this.options.container.querySelector(`.${prefix}`);
8390
this.scrimEl = document.body.querySelector(`.${this.options.styles.scrim}`);
8491
this.amToggleEl = this.$('.c-datepicker__clock--am');
@@ -89,10 +96,7 @@ class DateTimePicker extends Events {
8996
// set/setDate/setTime need refactoring to have single concerns
9097
// (set: set the value; setDate/setTime rename to renderDate/renderTime
9198
// and deal with updating the view only).
92-
// For now this allows us to set the default time using the same quantize
93-
// rules as setting the date explicitly. Setting this.value meets setTime|Date's
94-
// expectation that we have a value, and `0` guarantees that we will detect
95-
this.value = moment(0);
99+
this.value = this.options.default.startOf('minute');
96100
this.setDate(this.options.default);
97101
this.setTime(this.options.default);
98102
}
@@ -311,8 +315,8 @@ class DateTimePicker extends Events {
311315
) {
312316
this.setDate(m);
313317
evts.push('change:date');
314-
}
315-
318+
}
319+
316320
if (m.hour() !== this.value.hour()
317321
|| m.minutes() !== this.value.minutes()
318322
) {
@@ -342,7 +346,7 @@ class DateTimePicker extends Events {
342346
setDate(date) {
343347
const m = moment(date);
344348
const month = m.format('MMM');
345-
const day = m.format('Do');
349+
const day = m.format(this.options.dayFormat);
346350
const dayOfWeek = m.format('dddd');
347351
const year = m.format('YYYY');
348352

lib/template/datepicker.template.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default () => `
1+
export default (okLabel, cancelLabel) => `
22
<div class="c-datepicker">
33
<a class="c-datepicker__toggle c-datepicker__toggle--right c-datepicker--show-time js-show-clock" title="show time picker">
44
</a>
@@ -69,8 +69,8 @@ export default () => `
6969
</div>
7070
</div>
7171
<div class="modal-btns">
72-
<a class="c-btn c-btn--flat js-cancel">Cancel</a>
73-
<a class="c-btn c-btn--flat js-ok">OK</a>
72+
<a class="c-btn c-btn--flat js-cancel">${cancelLabel}</a>
73+
<a class="c-btn c-btn--flat js-ok">${okLabel}</a>
7474
</div>
7575
</div>
7676
`;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"license": "MIT",
2424
"dependencies": {
25-
"moment": "2.10.6",
25+
"moment": "2.18.1",
2626
"rome": "https://github.com/jwhitfieldseed/rome.git#19f5d3031a922c29c52b9038b2832a827e5e99d6"
2727
},
2828
"devDependencies": {

0 commit comments

Comments
 (0)