-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create edit saved annotation component
- Loading branch information
Showing
10 changed files
with
185 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { html, TemplateResult, render } from "lit"; | ||
import { ref } from "lit/directives/ref.js"; | ||
import { Modal as Modal } from "bootstrap"; | ||
import { ShadowlessLitElement } from "components/shadowless_lit_element"; | ||
|
||
export declare abstract class ModalMixinInterface { | ||
modalTemplate(title: TemplateResult, body: TemplateResult, footer: TemplateResult): TemplateResult; | ||
abstract get filledModalTemplate() :TemplateResult; | ||
showModal(): void; | ||
hideModal(): void; | ||
} | ||
|
||
type Constructor<T> = abstract new (...args: any[]) => T; | ||
|
||
export function modalMixin<T extends Constructor<ShadowlessLitElement>>(superClass: T): Constructor<ModalMixinInterface> & T { | ||
abstract class ModalMixinClass extends superClass implements ModalMixinInterface { | ||
modal: Modal; | ||
|
||
private initModal(el: Element): void { | ||
if (!this.modal) { | ||
this.modal = new Modal(el); | ||
} else { | ||
this.modal.handleUpdate(); | ||
} | ||
} | ||
|
||
modalTemplate(title: TemplateResult, body: TemplateResult, footer: TemplateResult): TemplateResult { | ||
return html` | ||
<div class="modal fade" ${ref(el => this.initModal(el))} tabindex="-1" role="dialog"> | ||
<div class="modal-dialog" role="document"> | ||
<div class="modal-content"> | ||
<div class="modal-header"> | ||
<h4 class="modal-title">${title}</h4> | ||
<button type="button" class="btn-close" data-bs-dismiss="modal" @click=${() => this.hideModal()}></button> | ||
</div> | ||
<div class="modal-body"> | ||
${body} | ||
</div> | ||
<div class="modal-footer"> | ||
${footer} | ||
</div> | ||
</div> | ||
</div> | ||
</div>`; | ||
} | ||
|
||
abstract get filledModalTemplate() :TemplateResult; | ||
|
||
update(changedProperties: Map<string, any>): void { | ||
super.update(changedProperties); | ||
this.renderModal(); | ||
} | ||
|
||
private renderModal(): void { | ||
render(this.filledModalTemplate, document.getElementById("modal-container"), { host: this }); | ||
} | ||
|
||
showModal(): void { | ||
this.renderModal(); | ||
this.modal?.show(); | ||
} | ||
|
||
hideModal(): void { | ||
this.modal?.hide(); | ||
} | ||
} | ||
|
||
return ModalMixinClass as Constructor<ModalMixinInterface> & T; | ||
} |
61 changes: 61 additions & 0 deletions
61
app/assets/javascripts/components/saved_annotations/edit_saved_annotation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { customElement, property } from "lit/decorators.js"; | ||
import { html, TemplateResult } from "lit"; | ||
import { ShadowlessLitElement } from "components/shadowless_lit_element"; | ||
import { SavedAnnotation, updateSavedAnnotation } from "state/SavedAnnotations"; | ||
import "./saved_annotation_form"; | ||
import { modalMixin } from "components/modal_mixin"; | ||
|
||
@customElement("d-edit-saved-annotation") | ||
export class EditSavedAnnotation extends modalMixin(ShadowlessLitElement) { | ||
@property({ type: Object }) | ||
savedAnnotation: SavedAnnotation; | ||
|
||
@property({ state: true }) | ||
errors: string[]; | ||
|
||
async updateSavedAnnotation(): Promise<void> { | ||
try { | ||
await updateSavedAnnotation(this.savedAnnotation.id, { | ||
saved_annotation: this.savedAnnotation | ||
}); | ||
this.errors = undefined; | ||
this.hideModal(); | ||
} catch (errors) { | ||
this.errors = errors; | ||
} | ||
} | ||
|
||
get filledModalTemplate(): TemplateResult { | ||
return this.modalTemplate(html` | ||
${I18n.t("js.saved_annotation.edit.title")}</h4> | ||
`, html` | ||
${this.errors !== undefined ? html` | ||
<div class="callout callout-danger"> | ||
<h4>${I18n.t("js.saved_annotation.edit.errors", {count: this.errors.length})}</h4> | ||
<ul> | ||
${this.errors.map(error => html` | ||
<li>${error}</li>`)} | ||
</ul> | ||
</div> | ||
` : ""} | ||
<d-saved-annotation-form | ||
.savedAnnotation=${this.savedAnnotation} | ||
@change=${e => this.savedAnnotation = e.detail} | ||
></d-saved-annotation-form> | ||
`, html` | ||
<button class="btn btn-primary btn-text" @click=${() => this.updateSavedAnnotation()}> | ||
${I18n.t("js.saved_annotation.edit.save")} | ||
</button> | ||
`); | ||
} | ||
|
||
render(): TemplateResult { | ||
return html` | ||
<a class="btn btn-icon" | ||
title="${I18n.t("js.saved_annotation.edit.button_title")}" | ||
@click=${() => this.showModal()} | ||
> | ||
<i class="mdi mdi-pencil"></i> | ||
</a>`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,5 +36,6 @@ | |
</div> | ||
<% end %> | ||
</div> | ||
<div id="modal-container"></div> | ||
<%= yield %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters