2019-09-08 19:08:01 +08:00
|
|
|
import StandardWidget from "./standard_widget.js";
|
2019-09-08 22:06:42 +08:00
|
|
|
import libraryLoader from "../services/library_loader.js";
|
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import dateNoteService from "../services/date_notes.js";
|
|
|
|
import treeService from "../services/tree.js";
|
2019-09-08 22:30:33 +08:00
|
|
|
import server from "../services/server.js";
|
2019-09-08 19:08:01 +08:00
|
|
|
|
|
|
|
const TPL = `
|
2019-09-08 22:06:42 +08:00
|
|
|
<div class="calendar-widget">
|
|
|
|
<div class="calendar-header">
|
2019-11-07 05:58:32 +08:00
|
|
|
<button class="calendar-btn bx bx-left-arrow-alt" data-calendar-toggle="previous"></button>
|
2019-09-08 22:06:42 +08:00
|
|
|
|
|
|
|
<div class="calendar-header__label" data-calendar-label="month">
|
|
|
|
March 2017
|
|
|
|
</div>
|
|
|
|
|
2019-11-07 05:58:32 +08:00
|
|
|
<button class="calendar-btn bx bx-right-arrow-alt" data-calendar-toggle="next"></button>
|
2019-09-08 22:06:42 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="calendar-week">
|
|
|
|
<span>Mon</span> <span>Tue</span><span>Wed</span> <span>Thu</span> <span>Fri</span> <span>Sat</span> <span>Sun</span>
|
|
|
|
</div>
|
|
|
|
<div class="calendar-body" data-calendar-area="month"></div>
|
|
|
|
</div>
|
2019-09-08 19:08:01 +08:00
|
|
|
`;
|
|
|
|
|
|
|
|
class CalendarWidget extends StandardWidget {
|
|
|
|
getWidgetTitle() { return "Calendar"; }
|
|
|
|
|
|
|
|
async isEnabled() {
|
|
|
|
return await super.isEnabled()
|
2020-01-15 04:23:32 +08:00
|
|
|
&& await this.tabContext.note.hasLabel("dateNote");
|
2019-09-08 19:08:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async doRenderBody() {
|
2019-09-08 22:06:42 +08:00
|
|
|
await libraryLoader.requireLibrary(libraryLoader.CALENDAR_WIDGET);
|
|
|
|
|
2019-09-08 19:08:01 +08:00
|
|
|
this.$body.html(TPL);
|
2020-01-15 04:23:32 +08:00
|
|
|
}
|
2019-09-08 19:08:01 +08:00
|
|
|
|
2020-01-15 04:23:32 +08:00
|
|
|
async activeTabChanged() {
|
|
|
|
this.init(this.$body, await this.tabContext.note.getLabelValue("dateNote"));
|
2019-09-08 22:06:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
init($el, activeDate) {
|
|
|
|
this.activeDate = new Date(Date.parse(activeDate));
|
2019-09-08 22:57:41 +08:00
|
|
|
this.todaysDate = new Date();
|
|
|
|
this.date = new Date(this.activeDate.getTime());
|
2019-09-08 22:06:42 +08:00
|
|
|
|
|
|
|
this.$month = $el.find('[data-calendar-area="month"]');
|
|
|
|
this.$next = $el.find('[data-calendar-toggle="next"]');
|
|
|
|
this.$previous = $el.find('[data-calendar-toggle="previous"]');
|
2019-09-08 19:08:01 +08:00
|
|
|
|
2019-11-10 00:39:48 +08:00
|
|
|
this.$next.on('click', () => {
|
2019-09-08 22:06:42 +08:00
|
|
|
this.clearCalendar();
|
|
|
|
this.date.setMonth(this.date.getMonth() + 1);
|
|
|
|
this.createMonth();
|
|
|
|
});
|
|
|
|
|
2019-11-10 00:39:48 +08:00
|
|
|
this.$previous.on('click', () => {
|
2019-09-08 22:06:42 +08:00
|
|
|
this.clearCalendar();
|
|
|
|
this.date.setMonth(this.date.getMonth() - 1);
|
|
|
|
this.createMonth();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$label = $el.find('[data-calendar-label="month"]');
|
|
|
|
|
|
|
|
this.date.setDate(1);
|
|
|
|
this.createMonth();
|
|
|
|
|
|
|
|
this.$body.on('click', '.calendar-date', async ev => {
|
|
|
|
const date = $(ev.target).closest('.calendar-date').attr('data-calendar-date');
|
|
|
|
|
|
|
|
const note = await dateNoteService.getDateNote(date);
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
treeService.activateNote(note.noteId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alert("Cannot find day note");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:30:33 +08:00
|
|
|
createDay(dateNotesForMonth, num, day) {
|
|
|
|
const $newDay = $('<a>')
|
2019-09-08 22:06:42 +08:00
|
|
|
.addClass("calendar-date")
|
|
|
|
.attr('data-calendar-date', utils.formatDateISO(this.date));
|
|
|
|
const $date = $('<span>').html(num);
|
|
|
|
|
|
|
|
// if it's the first day of the month
|
|
|
|
if (num === 1) {
|
|
|
|
if (day === 0) {
|
|
|
|
$newDay.css("marginLeft", (6 * 14.28) + '%');
|
|
|
|
} else {
|
|
|
|
$newDay.css("marginLeft", ((day - 1) * 14.28) + '%');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:30:33 +08:00
|
|
|
const dateNoteId = dateNotesForMonth[utils.formatDateISO(this.date)];
|
|
|
|
|
|
|
|
if (dateNoteId) {
|
|
|
|
$newDay.addClass('calendar-date-exists');
|
|
|
|
$newDay.attr("data-note-path", dateNoteId);
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:06:42 +08:00
|
|
|
if (this.isEqual(this.date, this.activeDate)) {
|
|
|
|
$newDay.addClass('calendar-date-active');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isEqual(this.date, this.todaysDate)) {
|
|
|
|
$newDay.addClass('calendar-date-today');
|
|
|
|
}
|
|
|
|
|
|
|
|
$newDay.append($date);
|
|
|
|
this.$month.append($newDay);
|
|
|
|
}
|
|
|
|
|
|
|
|
isEqual(a, b) {
|
|
|
|
return a.getFullYear() === b.getFullYear()
|
|
|
|
&& a.getMonth() === b.getMonth()
|
|
|
|
&& a.getDate() === b.getDate();
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:30:33 +08:00
|
|
|
async createMonth() {
|
|
|
|
const month = utils.formatDateISO(this.date).substr(0, 7);
|
|
|
|
const dateNotesForMonth = await server.get('date-notes/notes-for-month/' + month);
|
|
|
|
|
2019-09-08 22:06:42 +08:00
|
|
|
const currentMonth = this.date.getMonth();
|
|
|
|
while (this.date.getMonth() === currentMonth) {
|
|
|
|
this.createDay(
|
2019-09-08 22:30:33 +08:00
|
|
|
dateNotesForMonth,
|
2019-09-08 22:06:42 +08:00
|
|
|
this.date.getDate(),
|
|
|
|
this.date.getDay(),
|
|
|
|
this.date.getFullYear()
|
|
|
|
);
|
|
|
|
this.date.setDate(this.date.getDate() + 1);
|
|
|
|
}
|
|
|
|
// while loop trips over and day is at 30/31, bring it back
|
|
|
|
this.date.setDate(1);
|
|
|
|
this.date.setMonth(this.date.getMonth() - 1);
|
|
|
|
|
|
|
|
this.$label.html(this.monthsAsString(this.date.getMonth()) + ' ' + this.date.getFullYear());
|
|
|
|
}
|
|
|
|
|
|
|
|
monthsAsString(monthIndex) {
|
|
|
|
return [
|
|
|
|
'January',
|
|
|
|
'Febuary',
|
|
|
|
'March',
|
|
|
|
'April',
|
|
|
|
'May',
|
|
|
|
'June',
|
|
|
|
'July',
|
|
|
|
'August',
|
|
|
|
'September',
|
|
|
|
'October',
|
|
|
|
'November',
|
|
|
|
'December'
|
|
|
|
][monthIndex];
|
|
|
|
}
|
2019-09-08 19:08:01 +08:00
|
|
|
|
2019-09-08 22:06:42 +08:00
|
|
|
clearCalendar() {
|
|
|
|
this.$month.html('');
|
2019-09-08 19:08:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CalendarWidget;
|