Skip to content

Commit

Permalink
Clean up code and project organization, fix properties, implement scr…
Browse files Browse the repository at this point in the history
…ollToNow
  • Loading branch information
gismofx committed Apr 23, 2021
1 parent ae55f70 commit 81756fb
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public async Task InitCalendarDataAsync()
{
useCreationPopup = true,
useDetailPopup = true,
defaultView = TUICalendarViewName.Month
defaultView = TUICalendarViewName.Month,
taskView = false,
scheduleView = true

};

var calendarProps = new List<TUICalendarProps>();
Expand Down
24 changes: 20 additions & 4 deletions toast_ui.blazor_calendar/Models/TUICalendarOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Text.Json.Serialization;
using toast_ui.blazor_calendar.Services;
using toast_ui.blazor_calendar.Services.JsonConverters;

namespace toast_ui.blazor_calendar.Models
{
Expand All @@ -22,14 +23,16 @@ public class TUICalendarOptions
/// Show the milestone and task in weekly, daily view.
/// The default value is true. If the value is array, it can be ['milestone', 'task'].
/// </summary>
public string[] taskView { get; set; } = new[] {"milestone", "task"};
//[JsonConverter(typeof(TUITaskViewJsonConverter))]
public bool taskView { get; set; } = true;

/// <summary>
/// Show the all day and time grid in weekly, daily view.
/// The default value is false.
/// If the value is array, it can be ['allday', 'time'].
/// </summary>
public string[] scheduleView { get; set; } = new[] { "allday", "time" };
//public string[] scheduleView { get; set; } = new[] { "allday", "time" };
public bool scheduleView { get; set; } = true;

/// <summary>
/// themeConfig for custom style.
Expand Down Expand Up @@ -122,11 +125,11 @@ private bool CompareMembers(TUICalendarOptions options)
{
return false;
}
if (!taskView.SequenceEqual(options.taskView))
if (!taskView.Equals(options.taskView))
{
return false;
}
if (!scheduleView.SequenceEqual(options.scheduleView))
if (!scheduleView.Equals(options.scheduleView))
{
return false;
}
Expand Down Expand Up @@ -199,4 +202,17 @@ private TUICalendarViewName(string value)

}

public class TUITaskView
{
private TUITaskView(string[] value)
{
Value = value;
}
public string[] Value { get; set; }

public static TUITaskView MilestoneAndTask { get { return new TUITaskView(new[] { "milestone", "task" }); }}
public static TUITaskView Milestone { get { return new TUITaskView(new[] { "milestone" }); } }
public static TUITaskView Task { get { return new TUITaskView(new[] { "task" }); } }
public static TUITaskView None { get { return new TUITaskView(new[] { "" }); } }
}
}
1 change: 1 addition & 0 deletions toast_ui.blazor_calendar/Models/TUISchedule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text.Json.Serialization;
using toast_ui.blazor_calendar.Services;
using toast_ui.blazor_calendar.Services.JsonConverters;

namespace toast_ui.blazor_calendar.Models
{
Expand Down
4 changes: 4 additions & 0 deletions toast_ui.blazor_calendar/NpmJS/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@ window.TUICalendar = {
return TUICalendar.calendarRef.getDateRangeEnd();
},

scrollToNow: function () {
TUICalendar.calendarRef.scrollToNow();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface ITUICalendarInteropService
ValueTask<DateTimeOffset?> GetDateRangeStart();
ValueTask<DateTimeOffset?> GetDateRangeEnd();
ValueTask SetCalendarOptionsAsync(TUICalendarOptions calendarOptions);
void ScrollToNow();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Threading.Tasks;
using toast_ui.blazor_calendar.Models;

namespace toast_ui.blazor_calendar.Services
namespace toast_ui.blazor_calendar.Services.JsonConverters
{
class TUICalendarViewNameJsonConverter : JsonConverter<TUICalendarViewName>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace toast_ui.blazor_calendar.Services
namespace toast_ui.blazor_calendar.Services.JsonConverters
{
class TZDateJsonConverter : JsonConverter<DateTimeOffset>
{
Expand Down
152 changes: 81 additions & 71 deletions toast_ui.blazor_calendar/Services/TUICalendarInteropService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.JSInterop;
using toast_ui.blazor_calendar.Models;
using toast_ui.blazor_calendar.Services.JsonConverters;

namespace toast_ui.blazor_calendar.Services
{
Expand All @@ -15,7 +15,6 @@ namespace toast_ui.blazor_calendar.Services
/// </summary>
public class TUICalendarInteropService : ITUICalendarInteropService, IAsyncDisposable
{

private readonly IJSRuntime _JSRuntime;

//private DotNetObjectReference<TUICalendar> ObjectReference;
Expand All @@ -25,24 +24,9 @@ public TUICalendarInteropService(IJSRuntime jsRuntime)
_JSRuntime = jsRuntime;
}

/// <summary>
/// Initialize the Calendar
/// </summary>
/// <param name="objectReference"></param>
/// <returns></returns>
public async ValueTask InitCalendarAsync(DotNetObjectReference<TUICalendar> objectReference, TUICalendarOptions calendarOptions)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.initializeCalendar", objectReference, calendarOptions);
}

/// <summary>
/// Sets Calendar Options
/// </summary>
/// <param name="calendarOptions"></param>
/// <returns></returns>
public async ValueTask SetCalendarOptionsAsync(TUICalendarOptions calendarOptions)
public async ValueTask ChangeView(TUICalendarViewName viewName)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setCalendarOptions", calendarOptions);
await _JSRuntime.InvokeVoidAsync("TUICalendar.changeView", viewName.Value);
}

/// <summary>
Expand All @@ -58,27 +42,41 @@ public async ValueTask CreateSchedulesAsync(IEnumerable<TUISchedule> schedules)
}
}

/// <summary>
/// Set the calendars' properties via TUICalendarProps
/// </summary>
/// <param name="calendars"></param>
/// <returns></returns>
public async ValueTask SetCalendars(IEnumerable<TUICalendarProps> calendars)
public ValueTask DisposeAsync()
{
if (calendars is not null)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setCalendars", calendars);
}
return new ValueTask();
//throw new NotImplementedException();
}

public async ValueTask ChangeView(TUICalendarViewName viewName)
public async ValueTask<DateTimeOffset?> GetDateRangeEnd()
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.changeView", viewName.Value);
var result = await _JSRuntime.InvokeAsync<JsonElement>("TUICalendar.getDateRangeEnd");
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new TZDateJsonConverter());
return JsonSerializer.Deserialize<DateTimeOffset?>(result.ToString(), deserializeOptions);
}

public TUISchedule UpdateSchedule(TUISchedule scheduleToModify, JsonElement changedSchedule)
public async ValueTask<DateTimeOffset?> GetDateRangeStart()
{
return CombineTuiSchedule(scheduleToModify, changedSchedule);
var result = await _JSRuntime.InvokeAsync<JsonElement>("TUICalendar.getDateRangeStart");
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new TZDateJsonConverter());
return JsonSerializer.Deserialize<DateTimeOffset?>(result.ToString(), deserializeOptions);
}

public async ValueTask HideShowCalendar(string calendarId, bool hide)
{
await _JSRuntime.InvokeVoidAsync("hideShowCalendar", calendarId, hide);
}

/// <summary>
/// Initialize the Calendar
/// </summary>
/// <param name="objectReference"></param>
/// <returns></returns>
public async ValueTask InitCalendarAsync(DotNetObjectReference<TUICalendar> objectReference, TUICalendarOptions calendarOptions)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.initializeCalendar", objectReference, calendarOptions);
}

/// <summary>
Expand All @@ -94,16 +92,65 @@ public async ValueTask MoveCalendar(CalendarMove moveTo)
case CalendarMove.Next:
value = 1;
break;

case CalendarMove.Previous:
value = -1;
break;

case CalendarMove.Today:
value = 0;
break;
}
await _JSRuntime.InvokeVoidAsync("TUICalendar.moveToNextOrPreviousOrToday", value);
}

/// <summary>
/// Scroll to current time on today in daily or weekly view
/// </summary>
public void ScrollToNow()
{
_JSRuntime.InvokeVoidAsync("TUICalendar.scrollToNow");
}

/// <summary>
/// Sets Calendar Options
/// </summary>
/// <param name="calendarOptions"></param>
/// <returns></returns>
public async ValueTask SetCalendarOptionsAsync(TUICalendarOptions calendarOptions)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setCalendarOptions", calendarOptions);
}
/// <summary>
/// Set the calendars' properties via TUICalendarProps
/// </summary>
/// <param name="calendars"></param>
/// <returns></returns>
public async ValueTask SetCalendars(IEnumerable<TUICalendarProps> calendars)
{
if (calendars is not null)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setCalendars", calendars);
}
}
public async ValueTask SetDate(DateTimeOffset? dateToDisplay)
{
if (dateToDisplay is not null)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setDate", dateToDisplay);
}
}

/// <summary>
/// Call when an updated schedule has been returned from the calendar
/// </summary>
/// <param name="scheduleToModify">Current Schedule Object</param>
/// <param name="changedSchedule">The changes made to the schedule</param>
/// <returns>The changed schedule ready to further processing and/or saving</returns>
public TUISchedule UpdateSchedule(TUISchedule scheduleToModify, JsonElement changedSchedule)
{
return CombineTuiSchedule(scheduleToModify, changedSchedule);
}
private TUISchedule CombineTuiSchedule(TUISchedule schedule, JsonElement changes)
{
var c = JsonSerializer.Deserialize<TUISchedule>(changes.ToString());
Expand All @@ -125,42 +172,5 @@ private void CopyValues<T>(T target, T source)
prop.SetValue(target, value, null);
}
}

public ValueTask DisposeAsync()
{
return new ValueTask();
//throw new NotImplementedException();
}

public async ValueTask HideShowCalendar(string calendarId, bool hide)
{
await _JSRuntime.InvokeVoidAsync("hideShowCalendar", calendarId, hide);
}

public async ValueTask SetDate(DateTimeOffset? dateToDisplay)
{
if (dateToDisplay is not null)
{
await _JSRuntime.InvokeVoidAsync("TUICalendar.setDate", dateToDisplay);
}
}

public async ValueTask<DateTimeOffset?> GetDateRangeStart()
{
var result = await _JSRuntime.InvokeAsync<JsonElement>("TUICalendar.getDateRangeStart");
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new TZDateJsonConverter());
return JsonSerializer.Deserialize<DateTimeOffset?>(result.ToString(), deserializeOptions);

}

public async ValueTask<DateTimeOffset?> GetDateRangeEnd()
{
var result = await _JSRuntime.InvokeAsync<JsonElement>("TUICalendar.getDateRangeEnd");
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new TZDateJsonConverter());
return JsonSerializer.Deserialize<DateTimeOffset?>(result.ToString(), deserializeOptions);
}

}
}
}
Loading

0 comments on commit 81756fb

Please sign in to comment.