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

121 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-06-08 08:35:43 +02:00
// import {SettingsCookie, PluginCookie, Plugins} from "data";
2019-06-09 22:30:33 +02:00
import {React, Utilities, PluginManager} from "modules";
2019-05-28 23:27:25 +02:00
import CloseButton from "../icons/close";
2019-06-08 08:35:43 +02:00
// import ReloadIcon from "../icons/reload";
2019-05-28 23:27:25 +02:00
2019-05-30 07:06:17 +02:00
export default class V2C_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 = {
2019-06-08 08:35:43 +02:00
checked: PluginManager.isEnabled(this.props.content.id),
settings: 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
2019-06-08 08:35:43 +02:00
// this.reload = this.reload.bind(this);
// this.onReload = this.onReload.bind(this);
2019-05-28 20:19:48 +02:00
}
componentDidUpdate() {
if (this.state.settings) {
if (typeof this.settingsPanel === "object") {
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-09 22:30:33 +02:00
const self = $(this.panelRef.current);
2019-06-09 04:24:05 +02:00
const container = self.parents(".scroller-2FKFPG");
2019-05-28 20:19:48 +02:00
if (!isHidden(container[0], self[0])) return;
container.animate({
scrollTop: self.offset().top - container.offset().top + container.scrollTop() - 30
}, 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() {
const self = this;
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.settings) {
2019-06-08 08:35:43 +02:00
try { self.settingsPanel = content.plugin.getSettingsPanel(); }
catch (err) { Utilities.err("Plugins", "Unable to get settings panel for " + content.name + ".", err); }
2019-05-28 20:55:07 +02:00
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-05-28 20:19:48 +02:00
self.setState({settings: false});
}},
2019-05-30 07:06:17 +02:00
React.createElement(CloseButton, null)
2019-05-28 20:19:48 +02:00
),
2019-06-09 22:30:33 +02:00
typeof self.settingsPanel === "object" && React.createElement("div", {id: `plugin-settings-${name}`, className: "plugin-settings", ref: this.panelRef}),
typeof self.settingsPanel !== "object" && React.createElement("div", {id: `plugin-settings-${name}`, className: "plugin-settings", ref: this.panelRef, dangerouslySetInnerHTML: {__html: self.settingsPanel}})
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-08 08:35:43 +02:00
//!SettingsCookie["fork-ps-5"] && React.createElement(ReloadIcon, {className: "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});
2019-06-08 08:35:43 +02:00
PluginManager.togglePlugin(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({settings: true});
}
}