BetterDiscordAddons/Plugins/BetterSearchPage/BetterSearchPage.plugin.js

201 lines
9.0 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-09-04 10:51:39 +02:00
getSettingsPanel (collapseStates = {}) {
2020-02-04 08:20:40 +01:00
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-08-21 16:17:47 +02:00
load () {
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) BDFDB.PluginUtils.load(this);
}
2020-02-04 08:20:40 +01:00
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) {
2020-07-06 10:54:09 +02:00
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));
2020-02-04 08:20:40 +01:00
let doJump = page => {
2020-07-06 10:54:09 +02:00
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);
2020-02-04 08:20:40 +01:00
};
let pagination = children[index].type(children[index].props);
2020-02-06 16:15:36 +01:00
if (!pagination) return;
2020-07-06 10:54:09 +02:00
if (currentPage >= maxPage) {
2020-02-04 08:20:40 +01:00
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, {
2020-07-06 10:54:09 +02:00
text: currentPage >= maxPage ? "Max Page is 200" : "Next",
tooltipConfig: {color: currentPage >= maxPage && BDFDB.LibraryComponents.TooltipContainer.Colors.RED},
2020-02-04 08:20:40 +01:00
children: pagination.props.children[2]
});
if (settings.addFirstLast) {
pagination.props.children.unshift(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
2020-07-06 10:56:08 +02:00
text: BDFDB.LanguageUtils.LibraryStrings.first,
"aria-label": BDFDB.LanguageUtils.LibraryStrings.first,
2020-07-06 10:54:09 +02:00
onClick: _ => {if (currentPage != 1) doJump(1);},
2020-02-04 08:20:40 +01:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
2020-07-06 11:00:10 +02:00
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.searchresultspaginationbutton, currentPage == 1 && BDFDB.disCN.searchresultspaginationdisabled, BDFDB.disCN.focusable),
2020-02-04 08:20:40 +01:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.searchresultspaginationicon,
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:56:08 +02:00
text: currentPage >= maxPage ? "Max Page is 200" : BDFDB.LanguageUtils.LibraryStrings.last,
2020-07-06 10:54:09 +02:00
tooltipConfig: {color: currentPage >= maxPage && BDFDB.LibraryComponents.TooltipContainer.Colors.RED},
2020-07-06 10:56:08 +02:00
"aria-label": BDFDB.LanguageUtils.LibraryStrings.last,
2020-07-06 10:54:09 +02:00
onClick: _ => {if (currentPage != maxPage) doJump(maxPage);},
2020-02-04 08:20:40 +01:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
2020-07-06 11:00:10 +02:00
className: BDFDB.DOMUtils.formatClassName(BDFDB.disCN.searchresultspaginationbutton, currentPage >= maxPage && BDFDB.disCN.searchresultspaginationdisabled, BDFDB.disCN.focusable),
2020-02-04 08:20:40 +01:00
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.searchresultspaginationicon,
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,
2020-07-06 10:54:09 +02:00
value: currentPage,
2019-10-28 10:52:09 +01:00
min: 1,
2020-07-06 10:54:09 +02:00
max: maxPage,
2020-07-06 12:25:41 +02:00
onKeyDown: (event, instance) => {if (event.which == 13) doJump(instance.props.value);}
2020-02-04 08:20:40 +01:00
}));
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,
2020-07-06 12:25:41 +02:00
onClick: (event, instance) => {
let jumpInput = BDFDB.ReactUtils.findOwner(instance._reactInternalFiber.return, {key:"BSP-pagination-jumpinput"});
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,
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");
2020-09-11 19:31:36 +02:00
BDFDB.PatchUtils.forceAllUpdates(this);
2020-06-08 20:07:08 +02:00
}
}
2020-07-26 16:39:51 +02:00
})();
module.exports = BetterSearchPage;