rust rhai ui components wip
This commit is contained in:
73
examples/calendar/backend/scripts/calendars.rhai
Normal file
73
examples/calendar/backend/scripts/calendars.rhai
Normal file
@@ -0,0 +1,73 @@
|
||||
// calendars.rhai - Functions for handling calendars
|
||||
|
||||
import "utils" as utils;
|
||||
import "../models/calendar_model" as calendar_model;
|
||||
|
||||
// Get all calendars
|
||||
fn get_calendars() {
|
||||
return calendar_model::get_sample_calendars();
|
||||
}
|
||||
|
||||
// Get a specific calendar by ID
|
||||
fn get_calendar(calendar_id) {
|
||||
let calendars = get_all_calendars();
|
||||
return utils::find_by_id(calendars, calendar_id);
|
||||
}
|
||||
|
||||
// Get calendars owned by a specific user
|
||||
fn get_calendars_by_owner(user_id) {
|
||||
let calendars = get_all_calendars();
|
||||
return utils::filter_by_property(calendars, "owner_id", user_id);
|
||||
}
|
||||
|
||||
// Get calendars shared with a specific user
|
||||
fn get_calendars_shared_with(user_id) {
|
||||
let calendars = get_all_calendars();
|
||||
let shared_calendars = [];
|
||||
|
||||
for calendar in calendars {
|
||||
if utils::array_contains(calendar.shared_with, user_id) {
|
||||
shared_calendars.push(calendar);
|
||||
}
|
||||
}
|
||||
|
||||
return shared_calendars;
|
||||
}
|
||||
|
||||
// Get all calendars accessible by a user (owned + shared)
|
||||
fn get_accessible_calendars(user_id) {
|
||||
let owned = get_calendars_by_owner(user_id);
|
||||
let shared_calendars = get_calendars_shared_with(user_id);
|
||||
|
||||
// Combine the two arrays
|
||||
for calendar in shared_calendars {
|
||||
owned.push(calendar);
|
||||
}
|
||||
|
||||
return owned;
|
||||
}
|
||||
|
||||
// Create a new calendar
|
||||
fn create_calendar(name, owner_id, description, color, shared_with, visibility) {
|
||||
// Generate a simple ID (in a real app, we would use a proper ID generation method)
|
||||
let id = "cal" + (get_all_calendars().len() + 1).to_string();
|
||||
|
||||
return calendar_model::create_calendar(
|
||||
id, name, owner_id, description, color, shared_with, visibility
|
||||
);
|
||||
}
|
||||
|
||||
// Format calendar details for display
|
||||
fn format_calendar(calendar) {
|
||||
if utils::is_empty(calendar) {
|
||||
return "Calendar not found";
|
||||
}
|
||||
|
||||
let shared_with_count = calendar.shared_with.len();
|
||||
|
||||
return `${calendar.name} (${calendar.color})
|
||||
Description: ${calendar.description}
|
||||
Owner: ${calendar.owner_id}
|
||||
Visibility: ${calendar.visibility}
|
||||
Shared with: ${shared_with_count} users`;
|
||||
}
|
85
examples/calendar/backend/scripts/events.rhai
Normal file
85
examples/calendar/backend/scripts/events.rhai
Normal file
@@ -0,0 +1,85 @@
|
||||
// events.rhai - Functions for handling calendar events
|
||||
|
||||
import "utils" as utils;
|
||||
import "../models/event_model" as event_model;
|
||||
|
||||
// Get all events
|
||||
fn get_all_events() {
|
||||
return event_model::get_sample_events();
|
||||
}
|
||||
|
||||
// Get a specific event by ID
|
||||
fn get_event(event_id) {
|
||||
let events = get_all_events();
|
||||
return utils::find_by_id(events, event_id);
|
||||
}
|
||||
|
||||
// Get events for a specific calendar
|
||||
fn get_events_by_calendar(calendar_id) {
|
||||
let events = get_all_events();
|
||||
return utils::filter_by_property(events, "calendar_id", calendar_id);
|
||||
}
|
||||
|
||||
// Get events for a specific user (as attendee)
|
||||
fn get_events_by_attendee(user_id) {
|
||||
let events = get_all_events();
|
||||
let user_events = [];
|
||||
|
||||
for event in events {
|
||||
if utils::array_contains(event.attendees, user_id) {
|
||||
user_events.push(event);
|
||||
}
|
||||
}
|
||||
|
||||
return user_events;
|
||||
}
|
||||
|
||||
// Get events organized by a specific user
|
||||
fn get_events_by_organizer(user_id) {
|
||||
let events = get_all_events();
|
||||
return utils::filter_by_property(events, "organizer_id", user_id);
|
||||
}
|
||||
|
||||
// Get events for a specific date range
|
||||
fn get_events_by_date_range(start_date, end_date) {
|
||||
let events = get_all_events();
|
||||
let filtered_events = [];
|
||||
|
||||
for event in events {
|
||||
let event_start = event.start_time;
|
||||
|
||||
// Simple string comparison - in a real app, we would use proper date comparison
|
||||
if event_start >= start_date && event_start <= end_date {
|
||||
filtered_events.push(event);
|
||||
}
|
||||
}
|
||||
|
||||
return filtered_events;
|
||||
}
|
||||
|
||||
// Create a new event
|
||||
fn create_event(title, description, start_time, end_time, location, calendar_id, organizer_id, attendees, recurring, status) {
|
||||
// Generate a simple ID (in a real app, we would use a proper ID generation method)
|
||||
let id = "event" + (get_all_events().len() + 1).to_string();
|
||||
|
||||
return event_model::create_event(
|
||||
id, title, description, start_time, end_time, location,
|
||||
calendar_id, organizer_id, attendees, recurring, status
|
||||
);
|
||||
}
|
||||
|
||||
// Format event details for display
|
||||
fn format_event(event) {
|
||||
if utils::is_empty(event) {
|
||||
return "Event not found";
|
||||
}
|
||||
|
||||
let start_date = utils::format_date(event.start_time);
|
||||
let start_time = utils::format_time(event.start_time);
|
||||
let end_time = utils::format_time(event.end_time);
|
||||
|
||||
return `${event.title} (${start_date}, ${start_time} - ${end_time})
|
||||
Location: ${event.location}
|
||||
Description: ${event.description}
|
||||
Status: ${event.status}`;
|
||||
}
|
60
examples/calendar/backend/scripts/users.rhai
Normal file
60
examples/calendar/backend/scripts/users.rhai
Normal file
@@ -0,0 +1,60 @@
|
||||
// users.rhai - Functions for handling calendar users
|
||||
|
||||
import "utils" as utils;
|
||||
import "../models/user_model" as user_model;
|
||||
|
||||
// Get all users
|
||||
fn get_all_users() {
|
||||
return user_model::get_sample_users();
|
||||
}
|
||||
|
||||
// Get a specific user by ID
|
||||
fn get_user(user_id) {
|
||||
let users = get_all_users();
|
||||
return utils::find_by_id(users, user_id);
|
||||
}
|
||||
|
||||
// Get user by email
|
||||
fn get_user_by_email(email) {
|
||||
let users = get_all_users();
|
||||
|
||||
for user in users {
|
||||
if user.email == email {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
return #{}; // Return empty object if not found
|
||||
}
|
||||
|
||||
// Get users by timezone
|
||||
fn get_users_by_timezone(timezone) {
|
||||
let users = get_all_users();
|
||||
return utils::filter_by_property(users, "timezone", timezone);
|
||||
}
|
||||
|
||||
// Create a new user
|
||||
fn create_user(name, email, timezone, notification_time, default_calendar_id) {
|
||||
// Generate a simple ID (in a real app, we would use a proper ID generation method)
|
||||
let id = "user" + (get_all_users().len() + 1).to_string();
|
||||
|
||||
let preferences = user_model::create_user_preferences(
|
||||
notification_time, default_calendar_id
|
||||
);
|
||||
|
||||
return user_model::create_user(
|
||||
id, name, email, timezone, preferences
|
||||
);
|
||||
}
|
||||
|
||||
// Format user details for display
|
||||
fn format_user(user) {
|
||||
if utils::is_empty(user) {
|
||||
return "User not found";
|
||||
}
|
||||
|
||||
return `${user.name} (${user.email})
|
||||
Timezone: ${user.timezone}
|
||||
Default Calendar: ${user.preferences.default_calendar_id}
|
||||
Notification Time: ${user.preferences.notification_time} minutes before`;
|
||||
}
|
84
examples/calendar/backend/scripts/utils.rhai
Normal file
84
examples/calendar/backend/scripts/utils.rhai
Normal file
@@ -0,0 +1,84 @@
|
||||
// utils.rhai - Utility functions for the calendar backend
|
||||
|
||||
// Function to find an item by ID in an array
|
||||
fn find_by_id(array, id) {
|
||||
for item in array {
|
||||
if item.id == id {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return #{}; // Return empty object if not found
|
||||
}
|
||||
|
||||
// Function to filter array by a property value
|
||||
fn filter_by_property(array, property, value) {
|
||||
let result = [];
|
||||
|
||||
for item in array {
|
||||
if item[property] == value {
|
||||
result.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Function to check if an object is empty
|
||||
fn is_empty(obj) {
|
||||
if obj.type_of() == "object" {
|
||||
return obj.keys().len() == 0;
|
||||
}
|
||||
if obj.type_of() == "array" {
|
||||
return obj.len() == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to format date string
|
||||
fn format_date(date_str) {
|
||||
// Simple formatting - in a real app, we would use proper date formatting
|
||||
return date_str.substr(0, 10);
|
||||
}
|
||||
|
||||
// Function to format time string
|
||||
fn format_time(date_str) {
|
||||
// Simple formatting - in a real app, we would use proper time formatting
|
||||
if date_str.len() >= 16 {
|
||||
return date_str.substr(11, 5);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Function to check if an array contains a value
|
||||
fn array_contains(array, value) {
|
||||
for item in array {
|
||||
if item == value {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to get current date (simplified for demo)
|
||||
fn get_current_date() {
|
||||
return "2025-04-03";
|
||||
}
|
||||
|
||||
// Function to calculate a future date (simplified for demo)
|
||||
fn add_days_to_date(date_str, days) {
|
||||
let year = date_str.substr(0, 4).parse_int();
|
||||
let month = date_str.substr(5, 2).parse_int();
|
||||
let day = date_str.substr(8, 2).parse_int() + days;
|
||||
|
||||
// Very simplified date calculation - in a real app, we would handle month/year boundaries
|
||||
if day > 30 {
|
||||
day = day - 30;
|
||||
month += 1;
|
||||
}
|
||||
if month > 12 {
|
||||
month = 1;
|
||||
year += 1;
|
||||
}
|
||||
|
||||
return `${year}-${month.to_string().pad_left(2, '0')}-${day.to_string().pad_left(2, '0')}`;
|
||||
}
|
Reference in New Issue
Block a user