Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import DiscourseRecommendedTheme from "@discourse/lint-configs/eslint-theme";

export default [...DiscourseRecommendedTheme];
export default [...DiscourseRecommendedTheme, {
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: {
_oaReport: 'readonly',
},
}
}];
36 changes: 15 additions & 21 deletions javascripts/discourse/lib/navigation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import $ from "jquery";
import { onNodeInserted } from "./utils.js";

export function reportNavigationClick() {
// 类别下拉点击
onNodeInserted(
".navigation-container .category-drop.is-expanded .select-kit-collection",
(node) => {
window
.$(node)
$(node)
.children()
.on("click", (ev) =>
window._oaReport("click", {
Expand All @@ -22,8 +22,7 @@ export function reportNavigationClick() {
onNodeInserted(
".navigation-container .tag-drop.is-expanded .select-kit-collection",
(node) => {
window
.$(node)
$(node)
.children()
.on("click", (ev) =>
window._oaReport("click", {
Expand All @@ -39,8 +38,7 @@ export function reportNavigationClick() {
onNodeInserted(
".navigation-container .solved-status-filter.is-expanded .select-kit-collection",
(node) => {
window
.$(node)
$(node)
.children()
.on("click", (ev) =>
window._oaReport("click", {
Expand All @@ -53,19 +51,15 @@ export function reportNavigationClick() {
}
);

onNodeInserted(
"#navigation-bar",
(node) => {
window
.$(node)
.children()
.on("click", (ev) =>
window._oaReport("click", {
target: ev.currentTarget.textContent.trim(),
module: "navigation",
$url: location.href,
})
);
}
);
onNodeInserted("#navigation-bar", (node) => {
$(node)
.children()
.on("click", (ev) =>
window._oaReport("click", {
target: ev.currentTarget.textContent.trim(),
module: "navigation",
$url: location.href,
})
);
});
}
142 changes: 76 additions & 66 deletions javascripts/discourse/lib/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import $ from "jquery";
import { debounce, onNodeInserted } from "./utils.js";

let searchKey = "";

function onClickSearchInput() {
window._oaReport("click", {
type: "search-input",
Expand Down Expand Up @@ -39,33 +42,80 @@ function onClickSuggestion(ev) {
type: "search-suggestion",
module: "search",
target: ev.currentTarget.textContent.trim(),
searchContent: window.$(".search-input-wrapper input").get(0).value,
searchContent: searchKey,
detail: ev.currentTarget.href,
$url: location.href,
});
}

function onClickSearchResultTopic(ev) {
const current$ = window.$(ev.currentTarget);
const current$ = $(ev.currentTarget);
window._oaReport("click", {
type: "search-result",
module: "search",
target: current$.find(".first-line").text().trim(),
searchContent: window.$(".search-input-wrapper input").get(0).value,
detail: {
path: ev.currentTarget.href,
categories: current$.find(".badge-category__name").text().trim(),
tags: current$.find(".discourse-tags").text().trim(),
},
searchContent: searchKey,
path: ev.currentTarget.href,
categories: current$.find(".badge-category__name").text().trim(),
tags: current$.find(".discourse-tags").text().trim(),
$url: location.href,
});
}

function onAIClick(ev) {
if (ev.currentTarget.disabled) {
return;
}
window._oaReport("click", {
type: "ai-toggle",
module: "search",
searchContent: decodeURIComponent(location.search.match(/\bq=([^&]+)/)[1]),
detail: ev.currentTarget.getAttribute("aria-checked"),
$url: location.href,
});
}

// 搜索结果页帖子的点击
function onClickSearchResultPageTopic(ev) {
let link = ev.target;
if (link === ev.currentTarget) {
return;
}
while (!link.classList.contains("search-link")) {
link = link.parentElement;
if (link === ev.currentTarget || !link) {
return;
}
}

let root = link;
while (!root.classList.contains("fps-result")) {
root = root.parentElement;
if (root === ev.currentTarget || !root) {
return;
}
}
const rank = [...root.parentElement.children].indexOf(root) + 1;

const target$ = $(root);
window._oaReport("click", {
type: "search-result",
module: "search",
target: $(link).text().trim(),
searchContent: decodeURIComponent(location.search.match(/\bq=([^&]+)/)[1]),
rank,
path: $(link).attr("href"),
categories: target$.find(".badge-category__name").text().trim(),
tags: target$.find(".discourse-tags").text().trim(),
$url: location.href,
});
}

export default function reportSearch() {
onNodeInserted(".search-input-wrapper input", (node) => {
window.$(node).on("focus", onClickSearchInput);
window.$(node).on("input", debounce(onInputSearchInput, 300));
window.$(node).on("keydown", (ev) => {
$(node).on("focus", onClickSearchInput);
$(node).on("input", debounce(onInputSearchInput, 300));
$(node).on("keydown", (ev) => {
if (ev.key === "Enter") {
window._oaReport("input", {
type: "search",
Expand All @@ -79,13 +129,11 @@ export default function reportSearch() {

// 历史记录点击
onNodeInserted(".search-menu-panel .search-menu-recent", (node) => {
window
.$(node)
$(node)
.children(".search-menu-assistant-item")
.on("click", onClickSearchHistory);
// 清除历史记录
window
.$(node)
$(node)
.find(".clear-recent-searches")
.on("click", onClearSearchHistoryClick);
});
Expand All @@ -94,72 +142,34 @@ export default function reportSearch() {
onNodeInserted(
".search-menu-panel .results div[class^=search-result]",
(node) => {
searchKey = $(".search-input-wrapper input").get(0).value;
if (node.classList.contains("search-result-topic")) {
// 话题结果点击
window
.$(node)
.find(".list .item a")
.on("click", onClickSearchResultTopic);
$(node).find(".list .item a").on("click", onClickSearchResultTopic);
} else {
// 联想结果点击
window.$(node).find(".list .item a").on("click", onClickSuggestion);
$(node).find(".list .item a").on("click", onClickSuggestion);
}
}
);

window.addEventListener("afterRouteChange", ({ detail }) => {
if (detail.to === "/search") {
// 监听是否显示AI搜索结果的点击
onNodeInserted('.search-advanced .semantic-search__results-toggle', (el) => {
window.$(el).on("click", (ev) => {
if (ev.currentTarget.disabled) return;
window._oaReport("click", {
type: "ai-toggle",
module: "search",
searchContent: decodeURIComponent(
location.search.match(/\bq=([^&]+)/)[1]
),
detail: ev.currentTarget.getAttribute("aria-checked"),
$url: location.href,
});
});
});
onNodeInserted(
".search-advanced .semantic-search__results-toggle",
(el) => {
$(el).on("click", onAIClick);
}
);

onNodeInserted(
".search-results .fps-result:first-child",
() => {
window.$(".search-results .fps-result-entries").on("click", (ev) => {
let link = ev.target;
if (link === ev.currentTarget) return;
while (!link.classList.contains("search-link")) {
link = link.parentElement;
if (link === ev.currentTarget || !link) return;
}

let root = link;
while (!root.classList.contains("fps-result")) {
root = root.parentElement;
if (root === ev.currentTarget || !root) return;
}
const rank = [...root.parentElement.children].indexOf(root) + 1

const target$ = window.$(root);
window._oaReport("click", {
type: "search-result",
module: "search",
target: window.$(link).text().trim(),
searchContent: decodeURIComponent(
location.search.match(/\bq=([^&]+)/)[1]
),
detail: {
rank,
path: window.$(link).attr("href"),
categories: target$.find(".badge-category__name").text().trim(),
tags: target$.find(".discourse-tags").text().trim(),
},
$url: location.href,
});
});
$(".search-results .fps-result-entries").on(
"click",
onClickSearchResultPageTopic
);
},
true
);
Expand Down
7 changes: 3 additions & 4 deletions javascripts/discourse/lib/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import $ from "jquery";
import { onNodeInserted } from "./utils.js";

export function reportSidebarClick() {
onNodeInserted("#sidebar-section-content-categories", (node) => {
window
.$(node)
$(node)
.children()
.on("click", (ev) => {
window._oaReport("click", {
Expand All @@ -15,8 +15,7 @@ export function reportSidebarClick() {
});
});
onNodeInserted("#sidebar-section-content-tags", (node) => {
window
.$(node)
$(node)
.children()
.on("click", (ev) => {
window._oaReport("click", {
Expand Down
2 changes: 1 addition & 1 deletion javascripts/discourse/lib/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function reportTagsClick() {
if (detail.to === "/tags") {
onNodeInserted(
".all-tag-lists .tags-list",
node => {
(node) => {
node.querySelectorAll(".tag-box")?.forEach((el) => {
el.querySelector("a").addEventListener("click", allTagsClick);
});
Expand Down
Loading