BetterDiscordAddons/Plugins/BetterSearchPage/BetterSearchPage.plugin.js

201 lines
8.9 KiB
JavaScript
Raw Normal View History

2020-02-27 08:44:03 +01:00
//META{"name":"BetterSearchPage","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/BetterSearchPage","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/BetterSearchPage/BetterSearchPage.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
var BetterSearchPage = (_ => {
2020-06-08 20:07:08 +02:00
var settings = {};
2020-02-04 08:20:40 +01:00
return class BetterSearchPage {
getName () {return "BetterSearchPage";}
2019-01-17 23:48:29 +01:00
2020-05-22 18:14:25 +02:00
getVersion () {return "1.1.5";}
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getAuthor () {return "DevilBro";}
2019-01-17 23:48:29 +01:00
2020-02-04 08:20:40 +01:00
getDescription () {return "Adds some extra controls to the search results page.";}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
constructor () {
this.patchedModules = {
after: {
2020-05-22 18:14:25 +02:00
SearchResultsInner: "default"
2020-02-04 08:20:40 +01:00
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
initConstructor () {
this.defaults = {
settings: {
addFirstLast: {value:true, description:"Adds a first and last page button."},
addJumpTo: {value:true, description:"Adds a jump to input field (press enter to jump)."},
cloneToTheTop: {value:true, description:"Clones the controls to the top of the results page."}
}
};
}
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
getSettingsPanel () {
if (!window.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
let settings = BDFDB.DataUtils.get(this, "settings");
2020-03-28 07:55:39 +01:00
let settingsPanel, settingsItems = [];
2020-02-12 14:16:19 +01:00
2020-03-28 07:55:39 +01:00
for (let key in settings) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
2020-02-12 14:16:19 +01:00
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key]
}));
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, settingsItems);
2020-02-04 08:20:40 +01:00
}
2018-10-11 10:21:26 +02:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-02-04 08:20:40 +01:00
load () {}
start () {
if (!window.BDFDB) window.BDFDB = {myPlugins:{}};
if (window.BDFDB && window.BDFDB.myPlugins && typeof window.BDFDB.myPlugins == "object") window.BDFDB.myPlugins[this.getName()] = this;
let libraryScript = document.querySelector("head script#BDFDBLibraryScript");
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
libraryScript.setAttribute("id", "BDFDBLibraryScript");
libraryScript.setAttribute("type", "text/javascript");
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
libraryScript.setAttribute("date", performance.now());
libraryScript.addEventListener("load", _ => {this.initialize();});
document.head.appendChild(libraryScript);
}
else if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
this.startTimeout = setTimeout(_ => {
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2019-05-26 13:55:26 +02:00
}
2018-10-11 10:21:26 +02:00
2020-02-04 08:20:40 +01:00
initialize () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
if (this.started) return;
BDFDB.PluginUtils.init(this);
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-04 08:20:40 +01:00
}
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
2020-02-04 08:20:40 +01:00
stop () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
this.stopping = true;
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2019-10-22 11:37:23 +02:00
2020-02-04 08:20:40 +01:00
BDFDB.PluginUtils.clear(this);
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-04 08:20:40 +01:00
onSettingsClosed (e) {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2020-06-08 20:07:08 +02:00
this.forceUpdateAll();
2020-02-04 08:20:40 +01:00
}
2019-10-30 10:30:46 +01:00
}
2020-05-22 18:14:25 +02:00
processSearchResultsInner (e) {
2020-02-04 08:20:40 +01:00
if (e.instance.props.search) {
2020-06-16 17:07:08 +02:00
let [children, index] = BDFDB.ReactUtils.findParent(e.returnvalue, {name:"SearchPagination"});
2020-02-04 08:20:40 +01:00
if (index > -1) {
let currentpage = parseInt(Math.floor(e.instance.props.search.offset / BDFDB.DiscordConstants.SEARCH_PAGE_SIZE)) + 1;
let maxpage = e.instance.props.search.totalResults > 5000 ? parseInt(Math.ceil(5000 / BDFDB.DiscordConstants.SEARCH_PAGE_SIZE)) : parseInt(Math.ceil(e.instance.props.search.totalResults / BDFDB.DiscordConstants.SEARCH_PAGE_SIZE));
let doJump = page => {
page = page < 1 ? 1 : (page > maxpage ? maxpage : page);
if (page < currentpage) BDFDB.LibraryModules.SearchPageUtils.searchPreviousPage(e.instance.props.searchId, (currentpage - page) * BDFDB.DiscordConstants.SEARCH_PAGE_SIZE);
else if (page > currentpage) BDFDB.LibraryModules.SearchPageUtils.searchNextPage(e.instance.props.searchId, (page - currentpage) * BDFDB.DiscordConstants.SEARCH_PAGE_SIZE);
};
let pagination = children[index].type(children[index].props);
2020-02-06 16:15:36 +01:00
if (!pagination) return;
2020-02-04 08:20:40 +01:00
if (currentpage >= maxpage) {
pagination.props.children[2].props.className = BDFDB.DOMUtils.formatClassName(pagination.props.children[2].props.className, BDFDB.disCN.searchresultspaginationdisabled);
pagination.props.children[2].props.onClick = _ => {};
}
pagination.props.children[0] = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: "Previous",
children: pagination.props.children[0]
});
pagination.props.children[2] = BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: currentpage >= maxpage ? "Max Page is 200" : "Next",
tooltipConfig: {color: currentpage >= maxpage && BDFDB.LibraryComponents.TooltipContainer.Colors.RED},
children: pagination.props.children[2]
});
if (settings.addFirstLast) {
pagination.props.children.unshift(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
2020-07-06 10:51:40 +02:00
text: BDFDB.LanguageStrings.LibraryStrings.first,
"aria-label": BDFDB.LanguageStrings.LibraryStrings.first,
2020-02-04 08:20:40 +01:00
onClick: _ => {if (currentpage != 1) doJump(1);},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.searchresultspaginationbutton, currentpage == 1 && BDFDB.disCN.searchresultspaginationdisabled),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.searchresultspaginationicon,
nativeClass: true,
2020-07-06 10:51:40 +02:00
name: BDFDB.LibraryComponents.SvgIcon.Names.LEFT_DOUBLE_CARET
2020-02-04 08:20:40 +01:00
})
})
}));
pagination.props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
2020-07-06 10:51:40 +02:00
text: currentpage >= maxpage ? "Max Page is 200" : BDFDB.LanguageStrings.LibraryStrings.last,
2020-02-04 08:20:40 +01:00
tooltipConfig: {color: currentpage >= maxpage && BDFDB.LibraryComponents.TooltipContainer.Colors.RED},
2020-07-06 10:51:40 +02:00
"aria-label": BDFDB.LanguageStrings.LibraryStrings.last,
2020-02-04 08:20:40 +01:00
onClick: _ => {if (currentpage != maxpage) doJump(maxpage);},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.searchresultspaginationbutton, currentpage >= maxpage && BDFDB.disCN.searchresultspaginationdisabled),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.searchresultspaginationicon,
nativeClass: true,
2020-07-06 10:51:40 +02:00
name: BDFDB.LibraryComponents.SvgIcon.Names.RIGHT_DOUBLE_CARET
2020-02-04 08:20:40 +01:00
})
})
}));
}
if (settings.addJumpTo) {
pagination.props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TextInput, {
key: "BSP-pagination-jumpinput",
2019-10-28 10:52:09 +01:00
type: "number",
size: BDFDB.LibraryComponents.TextInput.Sizes.MINI,
suppress: true,
value: currentpage,
min: 1,
max: maxpage,
2020-02-04 08:20:40 +01:00
onKeyDown: (e, inputinstance) => {if (e.which == 13) doJump(inputinstance.props.value);}
}));
pagination.props.children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
2019-10-28 10:52:09 +01:00
text: BDFDB.LanguageUtils.LanguageStrings.JUMP,
"aria-label": BDFDB.LanguageUtils.LanguageStrings.JUMP,
2019-11-18 23:07:59 +01:00
onClick: (e, buttoninstance) => {
2020-02-04 08:20:40 +01:00
let jumpinput = BDFDB.ReactUtils.findOwner(buttoninstance._reactInternalFiber.return, {key:"BSP-pagination-jumpinput"});
2019-10-28 10:52:09 +01:00
if (jumpinput) doJump(jumpinput.props.value);
2020-02-04 08:20:40 +01:00
},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.disCN.searchresultspaginationbutton,
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.searchresultspaginationicon,
nativeClass: true,
style: {transform: "rotate(90deg"},
name: BDFDB.LibraryComponents.SvgIcon.Names.RIGHT_CARET
})
})
}));
}
children[index] = pagination;
if (settings.cloneToTheTop) children.unshift(pagination);
}
2019-10-28 10:52:09 +01:00
}
}
2020-06-08 20:07:08 +02:00
forceUpdateAll () {
settings = BDFDB.DataUtils.get(this, "settings");
BDFDB.ModuleUtils.forceAllUpdates(this);
}
}
2020-02-04 08:20:40 +01:00
})();