1+ import $ from "jquery" ;
12import { debounce , onNodeInserted } from "./utils.js" ;
23
4+ let searchKey = '' ;
5+
36function 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
4851function 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 ( / \b q = ( [ ^ & ] + ) / ) [ 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 ( / \b q = ( [ ^ & ] + ) / ) [ 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
64114export 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 ( / \b q = ( [ ^ & ] + ) / ) [ 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 ( / \b q = ( [ ^ & ] + ) / ) [ 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 ) ;
0 commit comments