2019-10-24 01:06:54 +08:00
|
|
|
import Controller from "@ember/controller";
|
2021-12-09 20:47:56 +08:00
|
|
|
import { action } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { alias } from "@ember/object/computed";
|
2023-07-19 03:53:23 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
|
|
|
import { observes } from "@ember-decorators/object";
|
|
|
|
import { INPUT_DELAY } from "discourse-common/config/environment";
|
|
|
|
import { debounce } from "discourse-common/utils/decorators";
|
|
|
|
import I18n from "I18n";
|
2015-04-09 02:17:21 +08:00
|
|
|
|
2023-03-15 17:42:12 +08:00
|
|
|
export default class AdminSiteSettingsController extends Controller {
|
2023-07-19 03:53:23 +08:00
|
|
|
@service router;
|
|
|
|
|
2023-07-28 01:41:42 +08:00
|
|
|
filter = "";
|
2023-03-15 17:42:12 +08:00
|
|
|
|
|
|
|
@alias("model") allSiteSettings;
|
|
|
|
|
|
|
|
visibleSiteSettings = null;
|
|
|
|
onlyOverridden = false;
|
2013-02-22 01:58:21 +08:00
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
get maxResults() {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2023-08-25 07:47:40 +08:00
|
|
|
sortSettings(settings) {
|
|
|
|
// Sort the site settings so that fuzzy results are at the bottom
|
|
|
|
// and ordered by their gap count asc.
|
|
|
|
return settings.sort((a, b) => {
|
|
|
|
const aWeight = a.weight === undefined ? 0 : a.weight;
|
|
|
|
const bWeight = b.weight === undefined ? 0 : b.weight;
|
|
|
|
return aWeight - bWeight;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
performSearch(filter, allSiteSettings, onlyOverridden) {
|
|
|
|
let pluginFilter;
|
2013-03-02 01:45:25 +08:00
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
if (filter) {
|
|
|
|
filter = filter
|
2020-05-10 19:07:45 +08:00
|
|
|
.toLowerCase()
|
|
|
|
.split(" ")
|
|
|
|
.filter((word) => {
|
|
|
|
if (word.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (word.startsWith("plugin:")) {
|
2022-04-01 23:35:17 +08:00
|
|
|
pluginFilter = word.slice("plugin:".length).trim();
|
2020-05-10 19:07:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.join(" ")
|
|
|
|
.trim();
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
2013-02-22 03:42:48 +08:00
|
|
|
|
2015-09-30 01:34:09 +08:00
|
|
|
const all = {
|
|
|
|
nameKey: "all_results",
|
|
|
|
name: I18n.t("admin.site_settings.categories.all_results"),
|
|
|
|
siteSettings: [],
|
|
|
|
};
|
2013-11-15 01:37:41 +08:00
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
const matchesGroupedByCategory = [all];
|
2015-09-30 01:34:09 +08:00
|
|
|
const matches = [];
|
2023-05-16 16:23:05 +08:00
|
|
|
|
|
|
|
const strippedQuery = filter.replace(/[^a-z0-9]/gi, "");
|
|
|
|
let fuzzyRegex;
|
2023-08-25 07:47:40 +08:00
|
|
|
let fuzzyRegexGaps;
|
2023-05-16 16:23:05 +08:00
|
|
|
|
|
|
|
if (strippedQuery.length > 2) {
|
|
|
|
fuzzyRegex = new RegExp(strippedQuery.split("").join(".*"), "i");
|
2023-08-25 07:47:40 +08:00
|
|
|
fuzzyRegexGaps = new RegExp(strippedQuery.split("").join("(.*)"), "i");
|
2023-05-16 16:23:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
allSiteSettings.forEach((settingsCategory) => {
|
|
|
|
let fuzzyMatches = [];
|
|
|
|
|
2015-09-30 01:34:09 +08:00
|
|
|
const siteSettings = settingsCategory.siteSettings.filter((item) => {
|
2023-05-16 16:23:05 +08:00
|
|
|
if (onlyOverridden && !item.get("overridden")) {
|
2020-09-22 22:28:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pluginFilter && item.plugin !== pluginFilter) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-15 01:37:41 +08:00
|
|
|
if (filter) {
|
2018-05-16 21:37:40 +08:00
|
|
|
const setting = item.get("setting").toLowerCase();
|
2023-05-16 16:23:05 +08:00
|
|
|
let filterResult =
|
2018-05-16 21:37:40 +08:00
|
|
|
setting.includes(filter) ||
|
|
|
|
setting.replace(/_/g, " ").includes(filter) ||
|
|
|
|
item.get("description").toLowerCase().includes(filter) ||
|
2023-05-16 16:23:05 +08:00
|
|
|
(item.get("value") || "").toString().toLowerCase().includes(filter);
|
|
|
|
if (!filterResult && fuzzyRegex && fuzzyRegex.test(setting)) {
|
2023-08-22 09:04:57 +08:00
|
|
|
// Tightens up fuzzy search results a bit.
|
2023-09-14 23:52:25 +08:00
|
|
|
const fuzzySearchLimiter = 25;
|
2023-08-22 09:04:57 +08:00
|
|
|
const strippedSetting = setting.replace(/[^a-z0-9]/gi, "");
|
|
|
|
if (
|
|
|
|
strippedSetting.length <=
|
|
|
|
strippedQuery.length + fuzzySearchLimiter
|
|
|
|
) {
|
2023-08-25 07:47:40 +08:00
|
|
|
const gapResult = strippedSetting.match(fuzzyRegexGaps);
|
|
|
|
if (gapResult) {
|
|
|
|
item.weight = gapResult.filter((gap) => gap !== "").length;
|
|
|
|
}
|
2023-08-22 09:04:57 +08:00
|
|
|
fuzzyMatches.push(item);
|
|
|
|
}
|
2023-05-16 16:23:05 +08:00
|
|
|
}
|
|
|
|
return filterResult;
|
2013-11-15 01:37:41 +08:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2023-05-16 16:23:05 +08:00
|
|
|
|
|
|
|
if (fuzzyMatches.length > 0) {
|
|
|
|
siteSettings.pushObjects(fuzzyMatches);
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:34:09 +08:00
|
|
|
if (siteSettings.length > 0) {
|
|
|
|
matches.pushObjects(siteSettings);
|
2015-06-09 20:34:06 +08:00
|
|
|
matchesGroupedByCategory.pushObject({
|
|
|
|
nameKey: settingsCategory.nameKey,
|
|
|
|
name: I18n.t(
|
|
|
|
"admin.site_settings.categories." + settingsCategory.nameKey
|
|
|
|
),
|
2023-08-25 07:47:40 +08:00
|
|
|
siteSettings: this.sortSettings(siteSettings),
|
2015-09-30 01:34:09 +08:00
|
|
|
count: siteSettings.length,
|
2015-06-09 20:34:06 +08:00
|
|
|
});
|
2013-02-21 02:15:50 +08:00
|
|
|
}
|
2013-11-15 01:37:41 +08:00
|
|
|
});
|
2013-02-22 01:58:21 +08:00
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
all.siteSettings.pushObjects(matches.slice(0, this.maxResults));
|
2023-08-25 07:47:40 +08:00
|
|
|
all.siteSettings = this.sortSettings(all.siteSettings);
|
|
|
|
|
2023-05-16 16:23:05 +08:00
|
|
|
all.hasMore = matches.length > this.maxResults;
|
|
|
|
all.count = all.hasMore ? `${this.maxResults}+` : matches.length;
|
|
|
|
all.maxResults = this.maxResults;
|
|
|
|
|
|
|
|
return matchesGroupedByCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
filterContentNow(category) {
|
|
|
|
if (isEmpty(this.allSiteSettings)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(this.filter) && !this.onlyOverridden) {
|
|
|
|
this.set("visibleSiteSettings", this.allSiteSettings);
|
|
|
|
if (this.categoryNameKey === "all_results") {
|
2023-07-19 03:53:23 +08:00
|
|
|
this.router.transitionTo("adminSiteSettings");
|
2023-05-16 16:23:05 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchesGroupedByCategory = this.performSearch(
|
|
|
|
this.filter,
|
|
|
|
this.allSiteSettings,
|
|
|
|
this.onlyOverridden
|
|
|
|
);
|
2015-09-30 01:34:09 +08:00
|
|
|
|
2020-08-05 15:39:54 +08:00
|
|
|
const categoryMatches = matchesGroupedByCategory.findBy(
|
|
|
|
"nameKey",
|
|
|
|
category
|
|
|
|
);
|
2023-05-16 16:23:05 +08:00
|
|
|
|
2020-08-05 15:39:54 +08:00
|
|
|
if (!categoryMatches || categoryMatches.count === 0) {
|
|
|
|
category = "all_results";
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:12 +08:00
|
|
|
this.set("visibleSiteSettings", matchesGroupedByCategory);
|
2023-07-19 03:53:23 +08:00
|
|
|
this.router.transitionTo(
|
2018-05-16 21:37:40 +08:00
|
|
|
"adminSiteSettingsCategory",
|
|
|
|
category || "all_results"
|
|
|
|
);
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2015-07-03 00:45:17 +08:00
|
|
|
|
2020-01-17 01:56:53 +08:00
|
|
|
@observes("filter", "onlyOverridden", "model")
|
2023-03-03 19:48:58 +08:00
|
|
|
optsChanged() {
|
|
|
|
this.filterContent();
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2023-03-03 19:48:58 +08:00
|
|
|
|
2022-10-20 19:28:09 +08:00
|
|
|
@debounce(INPUT_DELAY)
|
2020-12-18 21:18:52 +08:00
|
|
|
filterContent() {
|
2022-10-20 19:28:09 +08:00
|
|
|
if (this._skipBounce) {
|
|
|
|
this.set("_skipBounce", false);
|
|
|
|
} else {
|
2023-05-16 16:23:05 +08:00
|
|
|
if (!this.isDestroyed) {
|
|
|
|
this.filterContentNow(this.categoryNameKey);
|
|
|
|
}
|
2022-10-20 19:28:09 +08:00
|
|
|
}
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2013-12-21 00:06:07 +08:00
|
|
|
|
2021-12-09 20:47:56 +08:00
|
|
|
@action
|
|
|
|
clearFilter() {
|
|
|
|
this.setProperties({ filter: "", onlyOverridden: false });
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2015-08-17 01:35:23 +08:00
|
|
|
|
2021-12-09 20:47:56 +08:00
|
|
|
@action
|
|
|
|
toggleMenu() {
|
|
|
|
const adminDetail = document.querySelector(".admin-detail");
|
|
|
|
["mobile-closed", "mobile-open"].forEach((state) => {
|
|
|
|
adminDetail.classList.toggle(state);
|
|
|
|
});
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
|
|
|
}
|