Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose minutes #119

Merged
merged 1 commit into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions lib/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,19 @@ class DateTimePicker extends Events {
this.$('.js-ok')
.addEventListener('click', () => this.clickSubmit(), false);

this.$('.c-datepicker__clock__hours').addEventListener('mouseleave', e => this.mouseOutClock(e), false);
this.$(`.${this.options.styles.clockNum}`).forEach((el) => {
el.addEventListener('click', e => this.clickClock(e), false);
el.addEventListener('mouseenter', e => this.mouseInClock(e), false);
this.$('.js-date-hours').addEventListener('click', e => this.showHourClock(e), false);
this.$('.js-date-minutes').addEventListener('click', e => this.showMinuteClock(e), false);

this.$('.js-clock-hours').addEventListener('mouseleave', e => this.mouseOutHourClock(e), false);
this.$(`.js-clock-hours .${this.options.styles.clockNum}`).forEach((el) => {
el.addEventListener('click', e => this.clickClickHour(e), false);
el.addEventListener('mouseenter', e => this.mouseInHourClock(e), false);
});

this.$('.js-clock-minutes').addEventListener('mouseleave', e => this.mouseOutMinuteClock(e), false);
this.$(`.js-clock-minutes .${this.options.styles.clockNum}`).forEach(el => {
el.addEventListener('click', (e) => this.clickClockMinute(e), false);
el.addEventListener('mouseenter', e => this.mouseInMinuteClock(e), false);
});

this.$('.c-datepicker__clock--am')
Expand Down Expand Up @@ -157,7 +166,7 @@ class DateTimePicker extends Events {
return this;
}

clickClock(e) {
clickClickHour(e) {
const newValue = moment(this.value);
let number = parseInt(e.currentTarget.getAttribute('data-number'), 10);
if (number === 0 && this.meridiem === 'pm') {
Expand All @@ -171,6 +180,15 @@ class DateTimePicker extends Events {
return this;
}

clickClockMinute(e) {
const newValue = moment(this.value);
let number = parseInt(e.currentTarget.getAttribute('data-number'), 10);

newValue.minute(number);
this.set(newValue);
return this;
}

onChangeDate(dateString) {
const newValue = moment(this.value);
const [year, month, date] = dateString.split('-');
Expand All @@ -180,16 +198,32 @@ class DateTimePicker extends Events {
return this;
}

mouseInClock() {
const active = this.$(`.c-datepicker__clock__hours .${this.options.styles.clockNum}--active`);
mouseInHourClock() {
const active = this.$(`.js-clock-hours .${this.options.styles.clockNum}--active`);

if (active) {
active.classList.add('hide-hand');
}
}

mouseInMinuteClock() {
const active = this.$(`.js-clock-minutes .${this.options.styles.clockNum}--active`);

if (active) {
active.classList.add('hide-hand');
}
}

mouseOutClock() {
const hideHand = this.$(`.c-datepicker__clock__hours .${this.options.styles.clockNum}--active.hide-hand`);
mouseOutHourClock() {
const hideHand = this.$(`.js-clock-hours .${this.options.styles.clockNum}--active.hide-hand`);

if (hideHand) {
hideHand.classList.remove('hide-hand');
}
}

mouseOutMinuteClock() {
const hideHand = this.$(`.js-clock-minutes .${this.options.styles.clockNum}--active.hide-hand`);

if (hideHand) {
hideHand.classList.remove('hide-hand');
Expand All @@ -216,6 +250,20 @@ class DateTimePicker extends Events {
return this;
}

showHourClock() {
this.$('.js-clock-hours').classList.add('active');
this.$('.js-clock-minutes').classList.remove('active');
this.$('.js-date-hours').classList.add('active');
this.$('.js-date-minutes').classList.remove('active');
}

showMinuteClock() {
this.$('.js-clock-hours').classList.remove('active');
this.$('.js-clock-minutes').classList.add('active');
this.$('.js-date-hours').classList.remove('active');
this.$('.js-date-minutes').classList.add('active');
}

clickShowCalendar() {
this.$('.js-show-calendar').classList.add('is-selected');
this.$('.js-show-clock').classList.remove('is-selected');
Expand Down Expand Up @@ -290,18 +338,34 @@ class DateTimePicker extends Events {
// also update the hands of the clock
setTime(time) {
const m = moment(time);
const minuteAsInt = Math.ceil(parseInt(m.format('mm'), 10) / 5) * 5;
m.minutes(minuteAsInt);

const hour = m.format('HH');
const minutes = m.format('mm');
const hourAsInt = parseInt(hour, 10) % 12;
const oldActiveHours = this.$(`.c-datepicker__clock__hours .${this.options.styles.clockNum}--active`);

const oldActiveHours = this.$(`.js-clock-hours .${this.options.styles.clockNum}--active`);
const oldActiveMinutes = this.$(`.js-clock-minutes .${this.options.styles.clockNum}--active`);

this.$('.js-date-hours').innerText = hour;
this.$('.js-date-minutes').innerText = minutes;

if (oldActiveHours) {
oldActiveHours.classList.remove(`${this.options.styles.clockNum}--active`);
}
this.$(`.c-datepicker__clock__hours .${this.options.styles.clockNum}[data-number="${hourAsInt}"]`)

if (oldActiveMinutes) {
oldActiveMinutes.classList.remove(`${this.options.styles.clockNum}--active`);
}

this.$(`.js-clock-hours .${this.options.styles.clockNum}[data-number="${hourAsInt}"]`)
.classList.add(`${this.options.styles.clockNum}--active`);
this.$(`.js-clock-minutes .${this.options.styles.clockNum}[data-number="${minuteAsInt}"]`)
.classList.add(`${this.options.styles.clockNum}--active`);

this.value.hours(m.hours());
this.value.minutes(m.minutes());
this.meridiem = this.value.format('a');

if (this.meridiem === 'pm') {
Expand Down
42 changes: 30 additions & 12 deletions lib/scss/material-datetime-picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ $primary: #00bcd4;
}

.c-datepicker--show-time.is-selected ~ .c-datepicker__header .c-datepicker__header-date__time {
opacity: 1
opacity: 1;

.c-datepicker__header-date__hours, .c-datepicker__header-date__minutes {
cursor: pointer;
opacity: .6;
&.active {
opacity: 1;
}
}
}

.c-datepicker--show-calendar.is-selected {
Expand Down Expand Up @@ -451,27 +459,37 @@ $primary: #00bcd4;
}

.c-datepicker__clock__minutes {
visibility: hidden;
transform: translateY(30px) scale(0.6);
opacity: 0;
display: none;
height: 200px;
margin: -69px 0 0 0;
width: 200px;
display: none;
&.active {
display: block;
}
}

.c-datepicker__clock__hours {
height: 200px;
margin: -69px 0 0 0;
width: 200px;
display: none;

&.active {
display: block;
}
}

.c-datepicker__mask {
&:after {
content: ' ';
width: 164px;
height: 71px;
width: 156px;
height: 70px;
display: block;
position: absolute;
top: 33px;
top: 32px;
left: 0;
margin-left: -16px;
margin-left: -13px;
}
&:before {
content: ' ';
Expand All @@ -480,14 +498,14 @@ $primary: #00bcd4;
display: block;
position: absolute;
top: 6px;
left: 29px;
left: 28px;
margin-top: -18px;
}
width: 133px;
width: 127px;
height: 132px;
position: absolute;
top: 123px;
left: 35px;
top: 122px;
left: 37px;
z-index: 99;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/template/datepicker.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default () => `
<span class="c-datepicker__header-date__month js-date-month"></span>
<span class="c-datepicker__header-date__day js-date-day"></span>
<span class="c-datepicker__header-date__time js-date-time">
<span class="c-datepicker__header-date__hours js-date-hours">09</span>:<span class="c-datepicker__header-date__minutes js-date-minutes">00</span>
<span class="c-datepicker__header-date__hours js-date-hours active">09</span>:<span class="c-datepicker__header-date__minutes js-date-minutes">00</span>
</span>
</div>
</div>
Expand All @@ -33,7 +33,7 @@ export default () => `
</label>
</div>
<div class="c-datepicker__mask"></div>
<div class="c-datepicker__clock__hours">
<div class="c-datepicker__clock__hours js-clock-hours active">
<div class="c-datepicker__clock__num" data-number="3">3</div>
<div class="c-datepicker__clock__num" data-number="4">4</div>
<div class="c-datepicker__clock__num" data-number="5">5</div>
Expand All @@ -50,7 +50,7 @@ export default () => `
<div class="c-datepicker__hour-hand"></div>
</div>
</div>
<div class="c-datepicker__clock__minutes">
<div class="c-datepicker__clock__minutes js-clock-minutes">
<div class="c-datepicker__clock__num" data-number="15">15</div>
<div class="c-datepicker__clock__num" data-number="20">20</div>
<div class="c-datepicker__clock__num" data-number="25">25</div>
Expand Down