Skip to content

Commit

Permalink
Merge 846a6aa into d9372ec
Browse files Browse the repository at this point in the history
  • Loading branch information
rodfersou committed Sep 25, 2018
2 parents d9372ec + 846a6aa commit ad15c76
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/brasil/gov/agenda/browser/mixin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from six.moves import range # noqa: I001
from brasil.gov.agenda.config import AGENDADIARIAFMT
from datetime import timedelta
from plone import api
from zope.component import getMultiAdapter
Expand Down Expand Up @@ -44,15 +45,15 @@ def days(self):
has_appointment = False
if day == self.date:
cssclass.append('is-selected')
if self.agenda.get(day.strftime('%Y-%m-%d'), False):
if self.agenda.get(day.strftime(AGENDADIARIAFMT), False):
has_appointment = True
cssclass.append('has-appointment')
# Weekday difference between datetime and DateTime objects
strweek = self._translate(tool.day_msgid((day.weekday() + 1) % 7))
weekdays.append({
'day': day.day,
'dayurl': day.strftime(AGENDADIARIAFMT),
'weekday': strweek[:3],
'iso': day.isoformat(),
'cssclass': ' '.join(cssclass),
'hasappointment': has_appointment,
})
Expand Down
4 changes: 2 additions & 2 deletions src/brasil/gov/agenda/browser/templates/agenda_macros.pt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
<ul class="daypicker"
tal:condition="view/days">
<tal:event repeat="day view/days">
<li tal:attributes="data-day day/iso;
<li tal:attributes="data-day day/dayurl;
class day/cssclass;">
<a tal:attributes="href string:${view/agenda/absolute_url}/${day/iso}"
<a tal:attributes="href string:${view/agenda/absolute_url}/${day/dayurl}"
tal:omit-tag="not:day/hasappointment">
<div class="daypicker-day"
tal:content="day/day" />
Expand Down
69 changes: 44 additions & 25 deletions webpack/app/js/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ let zfill = (number, size=2) => (Array(size).fill('0').join('') + number).slice(


export default class DatePicker {
constructor(container) {
constructor(container, callback) {
this.container = container;
this.callback = callback;
this.agendaURL = container.getAttribute('data-url');
this.$month = this.$('.monthpicker .month');
this.$year = this.$('.monthpicker .year');
this.$day = this.$('.daypicker')
this.$datePicker = this.$('.monthpicker input');
this.$datePicker3 = this.$('.calendar');
this.isMobile = false;
this.$currentPicker = this.$datePicker.length > 0 ? this.$datePicker : this.$datePicker3;
this.is3calendar = false;
if (this.$datePicker[0] == null) {
this.is3calendar = true;
}
this.$currentPicker = this.is3calendar ? this.$datePicker3 : this.$datePicker;
this.daysWithAppointments = []
this.initMonthPicker();
let today = new Date();
Expand All @@ -39,21 +44,27 @@ export default class DatePicker {
context: this,
global: false,
}).always(function(data) {
this.daysWithAppointments = data[3].daysWithAppointments;
if (typeof this.callback === 'function') {
this.callback(data);
}
this.daysWithAppointments = [];
if (data[3].daysWithAppointments != null) {
this.daysWithAppointments = data[3].daysWithAppointments;
}
this.updateMonthPicker();
});
}
updateMonthPicker() {
this.$currentPicker.datepicker('setDate', new Date(this.year, this.month, this.day));
if (this.$datePicker.length > 0) {
if (this.is3calendar === false) {
let monthNamesShort = this.$currentPicker.datepicker('option', 'monthNamesShort');
this.$month.html(monthNamesShort[this.month].toUpperCase());
this.$year.html(this.year);
$('.monthpicker').attr('data-month', this.month);
$('.monthpicker').attr('data-year', this.year);
this.fixCalendarTitle();
}
if (this.$datePicker3.length > 0) {
if (this.is3calendar === true) {
this.resize();
}
}
Expand Down Expand Up @@ -90,32 +101,40 @@ export default class DatePicker {
// this event is needed to get right translation
$(window).on('load', function() {
let onSelect = function(dateText, inst) {
let year = inst.selectedYear;
let month = inst.selectedMonth + 1;
let day = parseInt(inst.selectedDay);
// https://stackoverflow.com/a/19374679
window.location = `${this.agendaURL}/${year}-${zfill(month)}-${zfill(day)}`;
}.bind(this);
this.year = inst.selectedYear;
this.month = inst.selectedMonth;
this.day = parseInt(inst.selectedDay);
if (this.is3calendar === true) {
// https://stackoverflow.com/a/19374679
window.location = `${this.agendaURL}/${this.year}-${zfill(this.month + 1)}-${zfill(this.day)}`;
} else {
this.update();
}
};
let beforeShowDay = function(date) {
let day = date.toISOString().slice(0, 10);
if (this.daysWithAppointments.indexOf(day) >= 0) {
return [true, 'ui-has-appointments', ''];
}
// disable click for days without appointments
return [false, '', ''];
}.bind(this);
this.$datePicker.datepicker( {
onSelect: onSelect,
beforeShow: this.fixCalendarTitle,
beforeShowDay: beforeShowDay,
onChangeMonthYear: this.fixCalendarTitle,
});
this.$datePicker3.datepicker( {
numberOfMonths: 3,
onSelect: onSelect,
beforeShowDay: beforeShowDay,
onChangeMonthYear: this.fixCalendarTitle,
});
};
let onChangeMonthYear = function(year, month, inst) {
this.year = inst.selectedYear;
this.month = inst.selectedMonth;
this.day = parseInt(inst.selectedDay);
this.update();
};
let options = {
onSelect: onSelect.bind(this),
beforeShowDay: beforeShowDay.bind(this),
onChangeMonthYear: onChangeMonthYear.bind(this),
};
if (this.is3calendar) {
options.numberOfMonths = 3;
} else {
options.beforeShow = this.fixCalendarTitle.bind(this);
}
this.$currentPicker.datepicker(options);
this.update();
}.bind(this));
}
Expand Down
17 changes: 4 additions & 13 deletions webpack/app/js/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default class AgendaTile {
$(selector) {
return $(selector, this.tile);
}
onDateChange(agendaDiaria) {
onDateChange(data) {
this.updateDayPicker(data);
let agendaDiaria = data[3];
this.swiper.removeAllSlides();
let $slide = $('<div class="swiper-slide"></div>');
if (agendaDiaria.hasAppointment === false) {
Expand Down Expand Up @@ -103,17 +105,6 @@ export default class AgendaTile {
this.datepicker.month = date.getMonth();
this.datepicker.day = date.getDate();
this.datepicker.$currentPicker.datepicker('setDate', date);

let agendaDiariaURL = `${this.datepicker.year}-${zfill(this.datepicker.month + 1)}-${zfill(this.datepicker.day)}`;
$.ajax({
url: `${this.datepicker.agendaURL}/json/${agendaDiariaURL}`,
context: this,
global: false,
}).always(function(data) {
this.updateDayPicker(data);
this.onDateChange(data[3]);
this.datepicker.daysWithAppointments = data[3].daysWithAppointments;
this.datepicker.updateMonthPicker();
});
this.datepicker.update();
}
}

0 comments on commit ad15c76

Please sign in to comment.