Skip to content

Commit

Permalink
:)
Browse files Browse the repository at this point in the history
  • Loading branch information
mono424 committed Sep 15, 2023
1 parent 738087a commit f11c393
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
9 changes: 7 additions & 2 deletions web/assets/init-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ document.addEventListener("alpine:init", () => {
const [changeSetExpression, fieldName = null] = expression.split(".");
const changeSet = evaluate(changeSetExpression);

if (!changeSet) {
return;
}

const onChangeSetUpdateHandler = (data) => {
const value = fieldName != null ? data[fieldName] : data;
if (modifiers.includes("text")) {
Expand Down Expand Up @@ -217,10 +221,11 @@ document.addEventListener("alpine:init", () => {
* x-on-change-set-update.init="$el.innerText = friendlySectionTimestamp(sectionEditChangeSet.get())">
* </div>
*/
Alpine.directive("on-change-set-update", (el, { expression, modifiers }, { evaluateLater, cleanup }) => {
Alpine.directive("on-change-set-update", (el, { expression, modifiers }, { evaluate, evaluateLater, cleanup }) => {
const onUpdate = evaluateLater(expression);

const onChangeSetUpdateHandler = (e) => {
console.log(el, "trigger");
const isDirty = e.detail.changeSet.isDirty();

if (modifiers.includes("clean") && isDirty) {
Expand All @@ -234,7 +239,7 @@ document.addEventListener("alpine:init", () => {
el.addEventListener(nativeEventName, onChangeSetUpdateHandler);

if (modifiers.includes("init")) {
onUpdate();
evaluate(expression);
}

cleanup(() => {
Expand Down
7 changes: 4 additions & 3 deletions web/template/partial/course/manage/edit-video-sections.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<template x-if="lectureData.videoSections.length > 0">
<div class="">
<div class="grid gap-2">
<template x-for="section in lectureData.videoSections" :key="sectionKey(section)">
<template x-for="section in lectureData.videoSections" :key="getSectionKey(section)">
<div x-data="{ sectionEditChangeSet: new admin.ChangeSet(section), editMode: false }" class="w-full border dark:border-gray-600 rounded"
x-change-set-listen="sectionEditChangeSet"
x-on-change-set-update.clean="updateSection(sectionEditChangeSet.get())"
Expand All @@ -83,14 +83,15 @@
<div class="p-1 flex items-center justify-start">
<div class="text-sky-800 bg-sky-200 text-xs dark:text-indigo-200 dark:bg-indigo-800 p-1 ml-1 rounded"
x-change-set-listen="sectionEditChangeSet"
x-on-change-set-update.init="$el.innerText = friendlySectionTimestamp(sectionEditChangeSet.get())"></div>
x-on-change-set-update.init="$el.innerText = friendlySectionTimestamp(sectionEditChangeSet.get())"
></div>
<div x-change-set-listen.text="sectionEditChangeSet.description" class="text-xs font-semibold text-3 ml-2 flex-grow"></div>
<div class="flex items-center py-1 pl-2 border-l dark:border-gray-600">
<button class="text-5 py-1 px-3 rounded text-3 hover:bg-gray-200 dark:hover:bg-gray-600" @click="editMode = true">
<i class="fa fa-edit"></i>
</button>
<button class="text-5 py-1 px-3 rounded text-3 hover:bg-gray-200 dark:hover:bg-gray-600"
@click="deleteSection(section.id)">
@click="deleteSection(section)">
<i class="fa fa-trash"></i>
</button>
</div>
Expand Down
9 changes: 8 additions & 1 deletion web/ts/api/admin-lecture-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export type VideoSection = {
startSeconds: number;

//Pseudo Fields
key: string;
key?: string;
};

export type VideoSectionDelta = {
Expand All @@ -106,6 +106,13 @@ export function videoSectionHasChanged(a: VideoSection, b: VideoSection) {
);
}

export function videoSectionGenKey(section: VideoSection): string {
if (section.id != null) {
return `sid_${section.id}`;
}
return `sts_${(new Date()).getTime()}`;
}

export function videoSectionListDelta(oldSections: VideoSection[], newSections: VideoSection[]) : VideoSectionDelta {
const sectionsToAdd = [];
const sectionsToUpdate = [];
Expand Down
2 changes: 1 addition & 1 deletion web/ts/data-store/admin-lecture-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AdminLectureListProvider extends StreamableMapProvider<number, Lect
return result.map((s) => {
s.hasAttachments = (s.files || []).some((f) => f.fileType === FileType.attachment);

s.videoSections = s.videoSections ?? [];
s.videoSections = (s.videoSections ?? []).sort(videoSectionSort);

s.startDate = new Date(s.start);
s.startDateFormatted = s.startDate.toLocaleDateString("en-US", dateFormatOptions);
Expand Down
25 changes: 15 additions & 10 deletions web/ts/edit-course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,29 +215,34 @@ export function lectureEditor(lecture: Lecture): AlpineComponent {
return videoSectionFriendlyTimestamp(section);
},

sectionKey(section: VideoSection): string {
if (section.id != null) {
return `sid_${section.id}`;
}
return `sts_${videoSectionTimestamp(section)}`;
getSectionKey(section: VideoSection): string {
return section.id ? `sid_${section.id}` : section.key;
},

genPseudoSectionKey(): string {
return `sts_${(new Date()).getTime()}`;
},

addSection(section: VideoSection) {
this.changeSet.patch("videoSections", [...this.lectureData.videoSections, section].sort(videoSectionSort));
this.changeSet.patch("videoSections", [...this.lectureData.videoSections, {
...section,
key: this.genPseudoSectionKey(),
}].sort(videoSectionSort));
},

updateSection(section: VideoSection) {
const sectionKey = this.sectionKey(section);
const sectionKey = this.getSectionKey(section);
this.changeSet.patch("videoSections", [
...this.lectureData.videoSections.filter((s) => sectionKey !== this.sectionKey(s)),
...this.lectureData.videoSections.filter((s) => sectionKey !== this.getSectionKey(s)),
section
].sort(videoSectionSort));
},

deleteSection(id: number) {
deleteSection(section) {
const sectionKey = this.getSectionKey(section);
this.changeSet.patch(
"videoSections",
this.lectureData.videoSections.filter((s) => s.id !== id),
this.lectureData.videoSections.filter((s) => sectionKey !== this.getSectionKey(s)),
);
},

Expand Down

0 comments on commit f11c393

Please sign in to comment.