You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
4.3 KiB
JavaScript
144 lines
4.3 KiB
JavaScript
import Event from './event.js'
|
|
import * as utils from './utils.js';
|
|
|
|
export default class Assignment {
|
|
constructor(obj) {
|
|
this.location = obj.location;
|
|
this.start_time = new utils.Time(obj.start_time);
|
|
this.end_time = new utils.Time(obj.end_time);
|
|
this.notes = obj.notes;
|
|
this.track = obj.track;
|
|
this.days = obj.days;
|
|
this.people_by_day = obj.people_by_day;
|
|
this.squishable = obj.squishable ?? false;
|
|
|
|
if (!this.people_by_day) {
|
|
this.people_by_day = {};
|
|
for (const day of this.days) {
|
|
this.people_by_day[day] = {staff: [], students: []};
|
|
}
|
|
} else if ('all_days' in this.people_by_day) {
|
|
const only = this.people_by_day['all_days'];
|
|
this.people_by_day = {};
|
|
for (const day of this.days) {
|
|
this.people_by_day[day] = {...only};
|
|
}
|
|
}
|
|
|
|
this.all_staff = new Set(Object.values(this.people_by_day)
|
|
.flatMap(day => day['staff']));
|
|
this.all_students = new Set(Object.values(this.people_by_day)
|
|
.flatMap(day => day['students']));
|
|
}
|
|
|
|
asJsonObject() {
|
|
return {
|
|
'location': this.location,
|
|
'start_time': this.start_time.asJsonObject(),
|
|
'end_time': this.end_time.asJsonObject(),
|
|
'notes': this.notes,
|
|
'track': this.track,
|
|
'days': this.days,
|
|
'people_by_day': this.people_by_day,
|
|
'squishable': this.squishable,
|
|
}
|
|
}
|
|
|
|
asJson() {
|
|
return JSON.stringify(this.asJsonObject(), null, 2);
|
|
}
|
|
|
|
get timeRange() {
|
|
return new utils.TimeRange(this);
|
|
}
|
|
|
|
asEvent(day, granularity, location_only) {
|
|
const people = this.people_by_day[day];
|
|
const staff = people['staff'] ?? [];
|
|
const students = people['students'] ?? [];
|
|
const num_people = staff.length + students.length;
|
|
|
|
let compact_mode = false;
|
|
if (granularity) {
|
|
const row_count = this.timeRange.duration.dividedBy(granularity);
|
|
const needed_rows = location_only ? 2 : 2 + num_people;
|
|
compact_mode = needed_rows > row_count;
|
|
}
|
|
|
|
const classes = [];
|
|
if (compact_mode) classes.push('compact');
|
|
|
|
const description = document.createElement('span');
|
|
description.classList.add('event_description');
|
|
|
|
const locationSpan = document.createElement('span');
|
|
locationSpan.innerText = this.location;
|
|
locationSpan.classList.add('location');
|
|
|
|
if (location_only) {
|
|
classes.push('location_only');
|
|
description.appendChild(locationSpan);
|
|
description.appendChild(document.createElement('br'));
|
|
} else {
|
|
if (compact_mode) locationSpan.classList.add('compact');
|
|
description.appendChild(locationSpan);
|
|
if (!compact_mode || num_people > 0) {
|
|
description.appendChild(document.createElement('br'));
|
|
}
|
|
|
|
function addNames(names, className) {
|
|
let first = true;
|
|
for (const name of names.sort()) {
|
|
if (first) {
|
|
first = false;
|
|
} else {
|
|
if (compact_mode) {
|
|
description.appendChild(document.createTextNode(',\n'));
|
|
} else {
|
|
description.appendChild(document.createElement('br'));
|
|
description.appendChild(document.createTextNode('\n'));
|
|
}
|
|
}
|
|
const nameSpan = document.createElement('span');
|
|
nameSpan.classList.add(className);
|
|
if (compact_mode) nameSpan.classList.add('compact');
|
|
nameSpan.innerText = name;
|
|
description.appendChild(nameSpan);
|
|
}
|
|
}
|
|
|
|
addNames(staff, 'staff');
|
|
|
|
description.appendChild(document.createElement('br'));
|
|
description.appendChild(document.createTextNode('\n'));
|
|
|
|
addNames(students, 'student');
|
|
|
|
if (this.notes) {
|
|
const notesSpan = document.createElement('span');
|
|
notesSpan.classList.add('notes');
|
|
notesSpan.innerText = this.notes;
|
|
|
|
if (compact_mode) {
|
|
notesSpan.classList.add('compact');
|
|
description.appendChild(document.createTextNode('\n'));
|
|
} else {
|
|
description.appendChild(document.createElement('br'));
|
|
}
|
|
|
|
description.appendChild(notesSpan);
|
|
}
|
|
}
|
|
|
|
return new Event({
|
|
'assignment': this,
|
|
'start_time': this.start_time,
|
|
'end_time': this.end_time,
|
|
'track': this.track,
|
|
'description': description,
|
|
'classes': classes,
|
|
'title': this.location,
|
|
});
|
|
}
|
|
}
|