implement drag&drop in calendar

This commit is contained in:
marlon-erler
2024-07-26 07:07:07 +02:00
parent 43363a4ed1
commit efe4a87b63
6 changed files with 76 additions and 14 deletions
+28 -6
View File
@@ -662,6 +662,8 @@
offset, offset,
firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek), firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek),
isCurrentMonth, isCurrentMonth,
year,
month,
days: {} days: {}
}; };
for (let i = 0; i < daysInMonth; i++) { for (let i = 0; i < daysInMonth; i++) {
@@ -1491,6 +1493,10 @@
this.boardId.value = boardId; this.boardId.value = boardId;
this.save(); this.save();
}; };
setDate = (dateISOString) => {
this.date.value = dateISOString;
this.save();
};
// view // view
open = () => { open = () => {
this.containingModel.selectTask(this); this.containingModel.selectTask(this);
@@ -1729,6 +1735,12 @@
this.selectedMonth.value = 1; this.selectedMonth.value = 1;
} }
}; };
handleDrop = (year, month, date) => {
const ISOString = CalendarModel.getISODateString(year, month, date);
const draggedObject = this.coreViewModel.draggedObject.value;
if (draggedObject instanceof TaskViewModel == false) return;
draggedObject.setDate(ISOString);
};
// load // load
loadMonthTasks = () => { loadMonthTasks = () => {
this.monthGrid.value = this.calendarModel.generateMonthGrid( this.monthGrid.value = this.calendarModel.generateMonthGrid(
@@ -2560,7 +2572,7 @@
var translations = allTranslations[language] || allTranslations.en; var translations = allTranslations[language] || allTranslations.en;
// src/View/Components/monthGrid.tsx // src/View/Components/monthGrid.tsx
function MonthGrid2(monthGrid, selectedDate) { function MonthGrid2(monthGrid, selectedDate, handleDrop) {
const dayLabels = []; const dayLabels = [];
let currentWeekday = monthGrid.firstDayOfWeek; let currentWeekday = monthGrid.firstDayOfWeek;
while (dayLabels.length < 7) { while (dayLabels.length < 7) {
@@ -2591,13 +2603,18 @@
function select() { function select() {
selectedDate.value = parseInt(date); selectedDate.value = parseInt(date);
} }
function drop() {
handleDrop(date);
}
return /* @__PURE__ */ createElement( return /* @__PURE__ */ createElement(
"button", "button",
{ {
class: "tile", class: "tile",
"on:click": select, "on:click": select,
"toggle:selected": isSelected, "toggle:selected": isSelected,
"toggle:today": isToday "toggle:today": isToday,
"on:dragover": allowDrop,
"on:drop": drop
}, },
/* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, date), /* @__PURE__ */ createElement( /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, date), /* @__PURE__ */ createElement(
"div", "div",
@@ -2730,13 +2747,18 @@
const mainContent = createProxyState( const mainContent = createProxyState(
[calendarPageViewModel.monthGrid], [calendarPageViewModel.monthGrid],
() => { () => {
if (calendarPageViewModel.monthGrid.value == void 0) { const monthGrid = calendarPageViewModel.monthGrid.value;
if (monthGrid == void 0) {
return /* @__PURE__ */ createElement("div", null); return /* @__PURE__ */ createElement("div", null);
} else { } else {
return MonthGrid2( let drop = function(date) {
calendarPageViewModel.monthGrid.value, calendarPageViewModel.handleDrop(
calendarPageViewModel.selectedDate monthGrid.year.toString(),
monthGrid.month.toString(),
date
); );
};
return MonthGrid2(monthGrid, calendarPageViewModel.selectedDate, drop);
} }
} }
); );
+11 -1
View File
@@ -75,6 +75,8 @@ export default class CalendarModel {
offset, offset,
firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek), firstDayOfWeek: parseInt(this.settingsModel.firstDayOfWeek),
isCurrentMonth, isCurrentMonth,
year,
month,
days: {}, days: {},
}; };
@@ -113,7 +115,11 @@ export default class CalendarModel {
return `${paddedYear}-${paddedMonth}`; return `${paddedYear}-${paddedMonth}`;
}; };
static getISODateString = (year: string, month: string, date: string): string => { static getISODateString = (
year: string,
month: string,
date: string
): string => {
const monthString: string = CalendarModel.getMonthString(year, month); const monthString: string = CalendarModel.getMonthString(year, month);
const paddedDate: string = date.padStart(2, "0"); const paddedDate: string = date.padStart(2, "0");
return `${monthString}-${paddedDate}`; return `${monthString}-${paddedDate}`;
@@ -129,5 +135,9 @@ export interface MonthGrid<T> {
offset: number; offset: number;
firstDayOfWeek: number; firstDayOfWeek: number;
isCurrentMonth: boolean; isCurrentMonth: boolean;
year: number;
month: number;
days: { [date: string]: T }; days: { [date: string]: T };
} }
+12 -4
View File
@@ -2,6 +2,7 @@ import "./calendarPage.css";
import * as React from "bloatless-react"; import * as React from "bloatless-react";
import CalendarModel from "../../Model/Files/calendarModel";
import CalendarPageViewModel from "../../ViewModel/Pages/calendarPageViewModel"; import CalendarPageViewModel from "../../ViewModel/Pages/calendarPageViewModel";
import { MonthGrid } from "../Components/monthGrid"; import { MonthGrid } from "../Components/monthGrid";
import { TaskSettingsModal } from "../Modals/taskSettingsModal"; import { TaskSettingsModal } from "../Modals/taskSettingsModal";
@@ -14,14 +15,21 @@ export function CalendarPage(calendarPageViewModel: CalendarPageViewModel) {
const mainContent = React.createProxyState( const mainContent = React.createProxyState(
[calendarPageViewModel.monthGrid], [calendarPageViewModel.monthGrid],
() => { () => {
if (calendarPageViewModel.monthGrid.value == undefined) { const monthGrid = calendarPageViewModel.monthGrid.value;
if (monthGrid == undefined) {
return <div></div>; return <div></div>;
} else { } else {
return MonthGrid( function drop(date: string) {
calendarPageViewModel.monthGrid.value, calendarPageViewModel.handleDrop(
calendarPageViewModel.selectedDate monthGrid!.year.toString(),
monthGrid!.month.toString(),
date
); );
} }
return MonthGrid(monthGrid, calendarPageViewModel.selectedDate, drop);
}
} }
); );
+9 -1
View File
@@ -4,10 +4,12 @@ import * as React from "bloatless-react";
import { MonthGrid } from "../../Model/Files/calendarModel"; import { MonthGrid } from "../../Model/Files/calendarModel";
import TaskViewModel from "../../ViewModel/Pages/taskViewModel"; import TaskViewModel from "../../ViewModel/Pages/taskViewModel";
import { translations } from "../translations"; import { translations } from "../translations";
import { allowDrop } from "../utility";
export function MonthGrid<T>( export function MonthGrid<T>(
monthGrid: MonthGrid<React.MapState<T>>, monthGrid: MonthGrid<React.MapState<T>>,
selectedDate: React.State<number> selectedDate: React.State<number>,
handleDrop: (date: string) => void
) { ) {
const dayLabels: HTMLElement[] = []; const dayLabels: HTMLElement[] = [];
let currentWeekday = monthGrid.firstDayOfWeek; let currentWeekday = monthGrid.firstDayOfWeek;
@@ -61,12 +63,18 @@ export function MonthGrid<T>(
selectedDate.value = parseInt(date); selectedDate.value = parseInt(date);
} }
function drop() {
handleDrop(date);
}
return ( return (
<button <button
class="tile" class="tile"
on:click={select} on:click={select}
toggle:selected={isSelected} toggle:selected={isSelected}
toggle:today={isToday} toggle:today={isToday}
on:dragover={allowDrop}
on:drop={drop}
> >
<div> <div>
<b>{date}</b> <b>{date}</b>
@@ -128,6 +128,15 @@ export default class CalendarPageViewModel extends TaskContainingPageViewModel {
} }
}; };
handleDrop = (year: string, month: string, date: string): void => {
const ISOString: string = CalendarModel.getISODateString(year, month, date);
const draggedObject: any = this.coreViewModel.draggedObject.value;
if (draggedObject instanceof TaskViewModel == false) return;
draggedObject.setDate(ISOString);
};
// load // load
loadMonthTasks = (): void => { loadMonthTasks = (): void => {
this.monthGrid.value = this.calendarModel.generateMonthGrid( this.monthGrid.value = this.calendarModel.generateMonthGrid(
+5
View File
@@ -75,6 +75,11 @@ export default class TaskViewModel {
this.save(); this.save();
}; };
setDate = (dateISOString: string): void => {
this.date.value = dateISOString;
this.save();
}
// view // view
open = (): void => { open = (): void => {
this.containingModel.selectTask(this); this.containingModel.selectTask(this);