Notes/src/public/app/widgets/collapsible_section_container.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-10-31 22:47:15 +01:00
import TabAwareWidget from "./tab_aware_widget.js";
2020-10-30 22:57:26 +01:00
const TPL = `
<div class="section-container">
<style>
2020-10-31 22:47:15 +01:00
.section-container {
margin-bottom: 10px;
}
2020-10-30 22:57:26 +01:00
.section-title-container {
display: flex;
flex-direction: row;
justify-content: center;
margin-top: 7px;
}
.section-title {
padding-right: 10px;
padding-left: 10px;
color: var(--muted-text-color);
border-bottom: 1px solid var(--main-border-color);
}
2020-10-31 22:47:15 +01:00
.section-title.active {
color: var(--main-text-color);
border-bottom: 1px solid var(--main-text-color);
}
2020-10-30 22:57:26 +01:00
.section-title:hover {
cursor: pointer;
}
.section-title:hover {
color: var(--main-text-color);
}
.section-title-empty {
flex-shrink: 1;
flex-grow: 1;
}
2020-10-31 22:47:15 +01:00
.section-body {
display: none;
border-bottom: 1px solid var(--main-border-color);
}
.section-body.active {
display: block;
}
2020-10-30 22:57:26 +01:00
</style>
<div class="section-title-container"></div>
<div class="section-body-container"></div>
</div>`;
2020-10-31 22:47:15 +01:00
export default class CollapsibleSectionContainer extends TabAwareWidget {
constructor() {
super();
this.children = [];
this.positionCounter = 10;
}
child(...components) {
if (!components) {
return this;
}
super.child(...components);
for (const component of components) {
if (!component.position) {
component.position = this.positionCounter;
this.positionCounter += 10;
}
}
this.children.sort((a, b) => a.position - b.position < 0 ? -1 : 1);
return this;
}
2020-10-30 22:57:26 +01:00
doRender() {
this.$widget = $(TPL);
this.contentSized();
this.$titleContainer = this.$widget.find('.section-title-container');
this.$bodyContainer = this.$widget.find('.section-body-container');
this.$titleContainer.append('<div class="section-title section-title-empty">');
for (const widget of this.children) {
this.$titleContainer.append(
2020-10-31 22:47:15 +01:00
$('<div class="section-title section-title-real">')
.attr('data-section-component-id', widget.componentId)
2020-10-30 22:57:26 +01:00
.append(widget.renderTitle())
);
this.$titleContainer.append('<div class="section-title section-title-empty">');
this.$bodyContainer.append(
$('<div class="section-body">')
2020-10-31 22:47:15 +01:00
.attr('data-section-component-id', widget.componentId)
2020-10-30 22:57:26 +01:00
.append(widget.render())
);
}
2020-10-31 22:47:15 +01:00
this.$titleContainer.on('click', '.section-title-real', e => {
const $sectionTitle = $(e.target).closest('.section-title-real');
const activate = !$sectionTitle.hasClass("active");
this.$titleContainer.find('.section-title-real').removeClass("active");
this.$bodyContainer.find('.section-body').removeClass("active");
if (activate) {
this.$titleContainer.find(`.section-title-real[data-section-component-id="${$sectionTitle.attr('data-section-component-id')}"]`).addClass("active");
this.$bodyContainer.find(`.section-body[data-section-component-id="${$sectionTitle.attr('data-section-component-id')}"]`).addClass("active");
}
});
}
async refreshWithNote(note) {
this.$titleContainer.find('.section-title-real:first').trigger('click');
2020-10-30 22:57:26 +01:00
}
}