Skip to content

Commit d2806bc

Browse files
committed
fix: lint问题修复、帖子点击、搜索结果点击上报数据问题修复
1 parent 7c30f51 commit d2806bc

File tree

5 files changed

+136
-125
lines changed

5 files changed

+136
-125
lines changed

eslint.config.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
import DiscourseRecommendedTheme from "@discourse/lint-configs/eslint-theme";
22

3-
export default [...DiscourseRecommendedTheme];
3+
export default [...DiscourseRecommendedTheme, {
4+
languageOptions: {
5+
ecmaVersion: 2022,
6+
sourceType: "module",
7+
globals: {
8+
_oaReport: 'readonly',
9+
},
10+
}
11+
}];
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import $ from "jquery";
12
import { onNodeInserted } from "./utils.js";
23

34
export function reportNavigationClick() {
45
// 类别下拉点击
56
onNodeInserted(
67
".navigation-container .category-drop.is-expanded .select-kit-collection",
78
(node) => {
8-
window
9-
.$(node)
9+
$(node)
1010
.children()
1111
.on("click", (ev) =>
1212
window._oaReport("click", {
@@ -22,8 +22,7 @@ export function reportNavigationClick() {
2222
onNodeInserted(
2323
".navigation-container .tag-drop.is-expanded .select-kit-collection",
2424
(node) => {
25-
window
26-
.$(node)
25+
$(node)
2726
.children()
2827
.on("click", (ev) =>
2928
window._oaReport("click", {
@@ -39,8 +38,7 @@ export function reportNavigationClick() {
3938
onNodeInserted(
4039
".navigation-container .solved-status-filter.is-expanded .select-kit-collection",
4140
(node) => {
42-
window
43-
.$(node)
41+
$(node)
4442
.children()
4543
.on("click", (ev) =>
4644
window._oaReport("click", {
@@ -53,19 +51,15 @@ export function reportNavigationClick() {
5351
}
5452
);
5553

56-
onNodeInserted(
57-
"#navigation-bar",
58-
(node) => {
59-
window
60-
.$(node)
61-
.children()
62-
.on("click", (ev) =>
63-
window._oaReport("click", {
64-
target: ev.currentTarget.textContent.trim(),
65-
module: "navigation",
66-
$url: location.href,
67-
})
68-
);
69-
}
70-
);
54+
onNodeInserted("#navigation-bar", (node) => {
55+
$(node)
56+
.children()
57+
.on("click", (ev) =>
58+
window._oaReport("click", {
59+
target: ev.currentTarget.textContent.trim(),
60+
module: "navigation",
61+
$url: location.href,
62+
})
63+
);
64+
});
7165
}

javascripts/discourse/lib/search.js

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import $ from "jquery";
12
import { debounce, onNodeInserted } from "./utils.js";
23

4+
let searchKey = '';
5+
36
function onClickSearchInput() {
47
window._oaReport("click", {
58
type: "search-input",
@@ -39,33 +42,80 @@ function onClickSuggestion(ev) {
3942
type: "search-suggestion",
4043
module: "search",
4144
target: ev.currentTarget.textContent.trim(),
42-
searchContent: window.$(".search-input-wrapper input").get(0).value,
45+
searchContent: searchKey,
4346
detail: ev.currentTarget.href,
4447
$url: location.href,
4548
});
4649
}
4750

4851
function onClickSearchResultTopic(ev) {
49-
const current$ = window.$(ev.currentTarget);
52+
const current$ = $(ev.currentTarget);
5053
window._oaReport("click", {
5154
type: "search-result",
5255
module: "search",
5356
target: current$.find(".first-line").text().trim(),
54-
searchContent: window.$(".search-input-wrapper input").get(0).value,
55-
detail: {
56-
path: ev.currentTarget.href,
57-
categories: current$.find(".badge-category__name").text().trim(),
58-
tags: current$.find(".discourse-tags").text().trim(),
59-
},
57+
searchContent: searchKey,
58+
path: ev.currentTarget.href,
59+
categories: current$.find(".badge-category__name").text().trim(),
60+
tags: current$.find(".discourse-tags").text().trim(),
61+
$url: location.href,
62+
});
63+
}
64+
65+
function onAIClick(ev) {
66+
if (ev.currentTarget.disabled) {
67+
return;
68+
}
69+
window._oaReport("click", {
70+
type: "ai-toggle",
71+
module: "search",
72+
searchContent: decodeURIComponent(location.search.match(/\bq=([^&]+)/)[1]),
73+
detail: ev.currentTarget.getAttribute("aria-checked"),
74+
$url: location.href,
75+
});
76+
}
77+
78+
// 搜索结果页帖子的点击
79+
function onClickSearchResultPageTopic(ev) {
80+
let link = ev.target;
81+
if (link === ev.currentTarget) {
82+
return;
83+
}
84+
while (!link.classList.contains("search-link")) {
85+
link = link.parentElement;
86+
if (link === ev.currentTarget || !link) {
87+
return;
88+
}
89+
}
90+
91+
let root = link;
92+
while (!root.classList.contains("fps-result")) {
93+
root = root.parentElement;
94+
if (root === ev.currentTarget || !root) {
95+
return;
96+
}
97+
}
98+
const rank = [...root.parentElement.children].indexOf(root) + 1;
99+
100+
const target$ = $(root);
101+
window._oaReport("click", {
102+
type: "search-result",
103+
module: "search",
104+
target: $(link).text().trim(),
105+
searchContent: decodeURIComponent(location.search.match(/\bq=([^&]+)/)[1]),
106+
rank,
107+
path: $(link).attr("href"),
108+
categories: target$.find(".badge-category__name").text().trim(),
109+
tags: target$.find(".discourse-tags").text().trim(),
60110
$url: location.href,
61111
});
62112
}
63113

64114
export default function reportSearch() {
65115
onNodeInserted(".search-input-wrapper input", (node) => {
66-
window.$(node).on("focus", onClickSearchInput);
67-
window.$(node).on("input", debounce(onInputSearchInput, 300));
68-
window.$(node).on("keydown", (ev) => {
116+
$(node).on("focus", onClickSearchInput);
117+
$(node).on("input", debounce(onInputSearchInput, 300));
118+
$(node).on("keydown", (ev) => {
69119
if (ev.key === "Enter") {
70120
window._oaReport("input", {
71121
type: "search",
@@ -79,13 +129,11 @@ export default function reportSearch() {
79129

80130
// 历史记录点击
81131
onNodeInserted(".search-menu-panel .search-menu-recent", (node) => {
82-
window
83-
.$(node)
132+
$(node)
84133
.children(".search-menu-assistant-item")
85134
.on("click", onClickSearchHistory);
86135
// 清除历史记录
87-
window
88-
.$(node)
136+
$(node)
89137
.find(".clear-recent-searches")
90138
.on("click", onClearSearchHistoryClick);
91139
});
@@ -94,72 +142,31 @@ export default function reportSearch() {
94142
onNodeInserted(
95143
".search-menu-panel .results div[class^=search-result]",
96144
(node) => {
145+
searchKey = $(".search-input-wrapper input").get(0).value;
97146
if (node.classList.contains("search-result-topic")) {
98147
// 话题结果点击
99-
window
100-
.$(node)
101-
.find(".list .item a")
102-
.on("click", onClickSearchResultTopic);
148+
$(node).find(".list .item a").on("click", onClickSearchResultTopic);
103149
} else {
104150
// 联想结果点击
105-
window.$(node).find(".list .item a").on("click", onClickSuggestion);
151+
$(node).find(".list .item a").on("click", onClickSuggestion);
106152
}
107153
}
108154
);
109155

110156
window.addEventListener("afterRouteChange", ({ detail }) => {
111157
if (detail.to === "/search") {
112158
// 监听是否显示AI搜索结果的点击
113-
onNodeInserted('.search-advanced .semantic-search__results-toggle', (el) => {
114-
window.$(el).on("click", (ev) => {
115-
if (ev.currentTarget.disabled) return;
116-
window._oaReport("click", {
117-
type: "ai-toggle",
118-
module: "search",
119-
searchContent: decodeURIComponent(
120-
location.search.match(/\bq=([^&]+)/)[1]
121-
),
122-
detail: ev.currentTarget.getAttribute("aria-checked"),
123-
$url: location.href,
124-
});
125-
});
126-
});
159+
onNodeInserted(
160+
".search-advanced .semantic-search__results-toggle",
161+
(el) => {
162+
$(el).on("click", onAIClick);
163+
}
164+
);
127165

128166
onNodeInserted(
129167
".search-results .fps-result:first-child",
130168
() => {
131-
window.$(".search-results .fps-result-entries").on("click", (ev) => {
132-
let link = ev.target;
133-
if (link === ev.currentTarget) return;
134-
while (!link.classList.contains("search-link")) {
135-
link = link.parentElement;
136-
if (link === ev.currentTarget || !link) return;
137-
}
138-
139-
let root = link;
140-
while (!root.classList.contains("fps-result")) {
141-
root = root.parentElement;
142-
if (root === ev.currentTarget || !root) return;
143-
}
144-
const rank = [...root.parentElement.children].indexOf(root) + 1
145-
146-
const target$ = window.$(root);
147-
window._oaReport("click", {
148-
type: "search-result",
149-
module: "search",
150-
target: window.$(link).text().trim(),
151-
searchContent: decodeURIComponent(
152-
location.search.match(/\bq=([^&]+)/)[1]
153-
),
154-
detail: {
155-
rank,
156-
path: window.$(link).attr("href"),
157-
categories: target$.find(".badge-category__name").text().trim(),
158-
tags: target$.find(".discourse-tags").text().trim(),
159-
},
160-
$url: location.href,
161-
});
162-
});
169+
$(".search-results .fps-result-entries").on("click", onClickSearchResultPageTopic);
163170
},
164171
true
165172
);

javascripts/discourse/lib/tags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function reportTagsClick() {
1313
if (detail.to === "/tags") {
1414
onNodeInserted(
1515
".all-tag-lists .tags-list",
16-
node => {
16+
(node) => {
1717
node.querySelectorAll(".tag-box")?.forEach((el) => {
1818
el.querySelector("a").addEventListener("click", allTagsClick);
1919
});

0 commit comments

Comments
 (0)