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

220 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-12-01 13:07:23 +01:00
import Component from "../components/component.js";
import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
import toastService from "../services/toast.js";
2023-08-21 04:15:53 -04:00
/**
* This is the base widget for all other widgets.
2023-11-03 01:11:47 +01:00
*
2023-08-21 04:15:53 -04:00
* For information on using widgets, see the tutorial {@tutorial widget_basics}.
*/
2020-01-15 21:36:01 +01:00
class BasicWidget extends Component {
constructor() {
super();
this.attrs = {
style: ''
};
this.classes = [];
this.children = [];
this.childPositionCounter = 10;
}
child(...components) {
if (!components) {
return this;
}
super.child(...components);
for (const component of components) {
if (component.position === undefined) {
component.position = this.childPositionCounter;
this.childPositionCounter += 10;
}
}
2023-11-03 01:11:47 +01:00
this.children.sort((a, b) => a.position - b.position);
return this;
}
id(id) {
this.attrs.id = id;
return this;
}
class(className) {
this.classes.push(className);
return this;
}
css(name, value) {
this.attrs.style += `${name}: ${value};`;
return this;
}
contentSized() {
2021-06-13 22:55:31 +02:00
this.css("contain", "none");
2021-06-05 22:07:15 +02:00
return this;
}
collapsible() {
this.css('min-height', '0');
2021-06-13 22:55:31 +02:00
this.css('min-width', '0');
return this;
}
2020-05-12 12:45:32 +02:00
filling() {
this.css('flex-grow', '1');
return this;
}
2023-08-21 04:15:53 -04:00
/**
* Accepts a string of CSS to add with the widget.
2023-11-03 01:11:47 +01:00
* @param {string} block
2023-08-21 04:15:53 -04:00
* @returns {this} for chaining
*/
cssBlock(block) {
this.cssEl = block;
return this;
}
render() {
try {
this.doRender();
} catch (e) {
this.logRenderingError(e);
}
2020-01-20 22:35:52 +01:00
2022-12-11 13:20:37 +01:00
this.$widget.attr('data-component-id', this.componentId);
this.$widget
.addClass('component')
.prop('component', this);
2020-07-14 23:29:37 +02:00
if (!this.isEnabled()) {
this.toggleInt(false);
}
if (this.cssEl) {
const css = this.cssEl.trim().startsWith('<style>') ? this.cssEl : `<style>${this.cssEl}</style>`;
this.$widget.append(css);
}
for (const key in this.attrs) {
if (key === 'style') {
if (this.attrs[key]) {
let style = this.$widget.attr('style');
style = style ? `${style}; ${this.attrs[key]}` : this.attrs[key];
this.$widget.attr(key, style);
}
}
else {
this.$widget.attr(key, this.attrs[key]);
}
}
for (const className of this.classes) {
this.$widget.addClass(className);
}
return this.$widget;
}
logRenderingError(e) {
console.log("Got issue in widget ", this);
console.error(e);
let noteId = this._noteId;
if (this._noteId) {
froca.getNote(noteId, true).then((note) => {
toastService.showPersistent({
title: t("toast.widget-error.title"),
icon: "alert",
message: t("toast.widget-error.message-custom", {
id: noteId,
title: note.title,
message: e.message
})
});
});
return;
}
toastService.showPersistent({
title: t("toast.widget-error.title"),
icon: "alert",
message: t("toast.widget-error.message-unknown", {
message: e.message
})
});
}
/**
* Indicates if the widget is enabled. Widgets are enabled by default. Generally setting this to `false` will cause the widget not to be displayed, however it will still be available on the DOM but hidden.
* @returns
*/
isEnabled() {
return true;
}
/**
2023-08-21 04:15:53 -04:00
* Method used for rendering the widget.
2023-11-03 01:11:47 +01:00
*
2023-08-21 04:15:53 -04:00
* Your class should override this method.
* The method is expected to create a this.$widget containing jQuery object
*/
doRender() {}
2020-03-06 22:17:07 +01:00
toggleInt(show) {
this.$widget.toggleClass('hidden-int', !show);
}
2022-05-30 17:45:59 +02:00
isHiddenInt() {
return this.$widget.hasClass('hidden-int');
}
2020-03-06 22:17:07 +01:00
toggleExt(show) {
this.$widget.toggleClass('hidden-ext', !show);
2020-01-14 20:27:40 +01:00
}
2022-05-30 17:45:59 +02:00
isHiddenExt() {
return this.$widget.hasClass('hidden-ext');
}
canBeShown() {
return !this.isHiddenInt() && !this.isHiddenExt();
}
isVisible() {
return this.$widget.is(":visible");
}
2020-03-16 21:16:09 +01:00
getPosition() {
return this.position;
}
2020-01-19 21:12:53 +01:00
remove() {
if (this.$widget) {
this.$widget.remove();
}
}
getClosestNtxId() {
2021-05-24 21:05:44 +02:00
if (this.$widget) {
return this.$widget.closest("[data-ntx-id]").attr("data-ntx-id");
}
else {
return null;
}
}
cleanup() {}
}
2020-05-12 12:45:32 +02:00
export default BasicWidget;