BetterDiscordApp-rauenzi/src/ui/settings/plugincard.js

129 lines
6.0 KiB
JavaScript
Raw Normal View History

2019-06-08 08:35:43 +02:00
// import {SettingsCookie, PluginCookie, Plugins} from "data";
import {React, Utilities, Settings} from "modules";
2019-05-28 23:27:25 +02:00
import CloseButton from "../icons/close";
import ReloadIcon from "../icons/reload";
2019-05-28 23:27:25 +02:00
2019-06-10 05:40:35 +02:00
export default class PluginCard extends React.Component {
2019-05-28 20:19:48 +02:00
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.showSettings = this.showSettings.bind(this);
2019-05-28 20:19:48 +02:00
this.state = {
checked: this.props.enabled,//PluginManager.isEnabled(this.props.content.id),
settingsOpen: false
2019-05-28 20:19:48 +02:00
};
2019-06-08 08:35:43 +02:00
this.hasSettings = typeof this.props.content.plugin.getSettingsPanel === "function";
this.settingsPanel = "";
2019-06-09 22:30:33 +02:00
this.panelRef = React.createRef();
2019-05-28 20:19:48 +02:00
this.reload = this.reload.bind(this);
2019-06-08 08:35:43 +02:00
// this.onReload = this.onReload.bind(this);
2019-05-28 20:19:48 +02:00
}
reload() {
if (!this.props.reload) return;
this.props.content = this.props.reload(this.props.content.id);
this.forceUpdate();
}
2019-05-28 20:19:48 +02:00
componentDidUpdate() {
if (this.state.settingsOpen) {
if (this.settingsPanel instanceof Node) {
2019-06-09 22:30:33 +02:00
this.panelRef.current.appendChild(this.settingsPanel);
2019-05-28 20:19:48 +02:00
}
2019-05-28 20:55:07 +02:00
2019-06-08 08:35:43 +02:00
// if (!SettingsCookie["fork-ps-3"]) return;
const isHidden = (container, element) => {
2019-05-28 20:19:48 +02:00
const cTop = container.scrollTop;
const cBottom = cTop + container.clientHeight;
2019-05-28 20:19:48 +02:00
const eTop = element.offsetTop;
const eBottom = eTop + element.clientHeight;
2019-06-09 04:24:05 +02:00
2019-05-28 20:19:48 +02:00
return (eTop < cTop || eBottom > cBottom);
};
2019-05-28 20:55:07 +02:00
2019-06-19 05:09:49 +02:00
const panel = $(this.panelRef.current);
const container = panel.parents(".scroller-2FKFPG");
if (!isHidden(container[0], panel[0])) return;
2019-05-28 20:19:48 +02:00
container.animate({
2019-06-19 05:09:49 +02:00
scrollTop: panel.offset().top - container.offset().top + container.scrollTop() - 30
2019-05-28 20:19:48 +02:00
}, 300);
}
}
2019-05-28 20:55:07 +02:00
getString(value) {
return typeof value == "string" ? value : value.toString();
}
2019-05-28 20:19:48 +02:00
render() {
2019-06-08 08:35:43 +02:00
const {content} = this.props;
const name = this.getString(content.name);
const author = this.getString(content.author);
const description = this.getString(content.description);
const version = this.getString(content.version);
const website = content.website;
const source = content.source;
2019-05-28 20:19:48 +02:00
if (this.state.settingsOpen) {
2019-06-19 05:09:49 +02:00
try { this.settingsPanel = content.plugin.getSettingsPanel(); }
2019-06-08 08:35:43 +02:00
catch (err) { Utilities.err("Plugins", "Unable to get settings panel for " + content.name + ".", err); }
2019-05-28 20:55:07 +02:00
const props = {id: `plugin-settings-${name}`, className: "plugin-settings", ref: this.panelRef};
if (typeof(this.settingsPanel) == "string") props.dangerouslySetInnerHTML = this.settingsPanel;
2019-05-30 07:06:17 +02:00
return React.createElement("li", {className: "settings-open ui-switch-item"},
React.createElement("div", {style: {"float": "right", "cursor": "pointer"}, onClick: () => {
2019-06-09 22:30:33 +02:00
this.panelRef.current.innerHTML = "";
2019-06-19 05:09:49 +02:00
this.setState({settingsOpen: false});
2019-05-28 20:19:48 +02:00
}},
2019-05-30 07:06:17 +02:00
React.createElement(CloseButton, null)
2019-05-28 20:19:48 +02:00
),
React.createElement("div", props, this.settingsPanel instanceof React.Component ? this.settingsPanel : null),
);
2019-05-28 20:19:48 +02:00
}
2019-05-30 07:06:17 +02:00
return React.createElement("li", {"data-name": name, "data-version": version, "className": "settings-closed ui-switch-item"},
React.createElement("div", {className: "bda-header"},
React.createElement("span", {className: "bda-header-title"},
React.createElement("span", {className: "bda-name"}, name),
2019-05-28 20:19:48 +02:00
" v",
2019-05-30 07:06:17 +02:00
React.createElement("span", {className: "bda-version"}, version),
2019-05-28 20:19:48 +02:00
" by ",
2019-05-30 07:06:17 +02:00
React.createElement("span", {className: "bda-author"}, author)
2019-05-28 20:19:48 +02:00
),
2019-05-30 07:06:17 +02:00
React.createElement("div", {className: "bda-controls"},
2019-06-15 04:11:19 +02:00
!Settings.get("settings", "content", "autoReload") && React.createElement(ReloadIcon, {className: "bd-reload bd-reload-card", onClick: this.reload}),
2019-05-30 07:06:17 +02:00
React.createElement("label", {className: "ui-switch-wrapper ui-flex-child", style: {flex: "0 0 auto"}},
React.createElement("input", {checked: this.state.checked, onChange: this.onChange, className: "ui-switch-checkbox", type: "checkbox"}),
React.createElement("div", {className: this.state.checked ? "ui-switch checked" : "ui-switch"})
2019-05-28 20:19:48 +02:00
)
)
),
2019-05-30 07:06:17 +02:00
React.createElement("div", {className: "bda-description-wrap scroller-wrap fade"},
React.createElement("div", {className: "bda-description scroller"}, description)
2019-05-28 20:19:48 +02:00
),
2019-05-30 07:06:17 +02:00
(website || source || this.hasSettings) && React.createElement("div", {className: "bda-footer"},
React.createElement("span", {className: "bda-links"},
website && React.createElement("a", {className: "bda-link bda-link-website", href: website, target: "_blank"}, "Website"),
2019-05-28 20:19:48 +02:00
website && source && " | ",
2019-05-30 07:06:17 +02:00
source && React.createElement("a", {className: "bda-link bda-link-source", href: source, target: "_blank"}, "Source")
2019-05-28 20:19:48 +02:00
),
2019-05-30 07:06:17 +02:00
this.hasSettings && React.createElement("button", {onClick: this.showSettings, className: "bda-settings-button", disabled: !this.state.checked}, "Settings")
2019-05-28 20:19:48 +02:00
)
);
}
onChange() {
this.setState({checked: !this.state.checked});
// PluginManager.togglePlugin(this.props.content.id);
this.props.onChange && this.props.onChange(this.props.content.id);
2019-05-28 20:19:48 +02:00
}
2019-05-28 20:55:07 +02:00
showSettings() {
2019-05-28 20:19:48 +02:00
if (!this.hasSettings) return;
this.setState({settingsOpen: true});
2019-05-28 20:19:48 +02:00
}
}