11export class BaseSearchType {
2- constructor ( ) {
2+ constructor ( context = { } ) {
3+ this . app = context . app
34 this . type = null
45 this . name = null
56 this . icon = 'iconoir-search'
67 this . priority = 10
78 }
89
10+ /**
11+ * Builds the URL for navigating to this search result.
12+ * Must be implemented by subclasses.
13+ * @param {Object } result - The search result object
14+ * @param {Object } context - Context containing store reference
15+ * @returns {string|Object|null } URL string, route object, or null if not navigable
16+ */
917 buildUrl ( result , context = null ) {
1018 throw new Error ( 'buildUrl must be implemented by subclass' )
1119 }
@@ -26,12 +34,134 @@ export class BaseSearchType {
2634 return this . priority
2735 }
2836
29- // Default formatting returns plain title/subtitle and no description segments
37+ /**
38+ * Formats the result for display in the search modal.
39+ * Override in subclasses to provide custom formatting.
40+ * @param {Object } result - The search result object
41+ * @param {Object } context - Context (e.g., searchTerm for highlighting)
42+ * @returns {Object } Object with title, subtitle, and descriptionSegments
43+ */
3044 formatResultDisplay ( result , context = null ) {
3145 return {
3246 title : result . title ,
3347 subtitle : result . subtitle ,
3448 descriptionSegments : [ ] ,
3549 }
3650 }
51+
52+ /**
53+ * Returns true if the result can be navigated to (has a valid URL).
54+ * Override in subclasses to provide custom logic.
55+ * @param {Object } result - The search result object
56+ * @param {Object } context - Context containing store reference
57+ * @returns {boolean }
58+ */
59+ isNavigable ( result , context = null ) {
60+ return true
61+ }
62+
63+ /**
64+ * Gets the application ID from a result.
65+ * Override in subclasses for custom ID extraction logic.
66+ * @param {Object } result - The search result object
67+ * @returns {number|null }
68+ */
69+ _getApplicationId ( result ) {
70+ const id = parseInt ( result ?. id )
71+ return isNaN ( id ) ? null : id
72+ }
73+
74+ /**
75+ * Attempts to focus/select the application in the sidebar as a fallback action.
76+ * @param {Object } result - The search result object
77+ * @param {Object } context - Context containing store reference
78+ * @returns {boolean } True if the action was taken, false otherwise
79+ */
80+ focusInSidebar ( result , context = null ) {
81+ const appId = this . _getApplicationId ( result )
82+ if ( ! appId || ! context ?. store ) {
83+ return false
84+ }
85+ const application = context . store . getters [ 'application/get' ] ( appId )
86+ if ( application ) {
87+ context . store . dispatch ( 'application/select' , application )
88+
89+ const applicationType = this . app . $registry . get (
90+ 'application' ,
91+ application . type
92+ )
93+ applicationType . select ( application , {
94+ $router : this . app . router ,
95+ $store : context . store ,
96+ $i18n : this . app . i18n ,
97+ } )
98+ return true
99+ }
100+ return false
101+ }
102+
103+ /**
104+ * Returns the i18n key suffix for the label to display when item is not navigable.
105+ * Returns null if no label should be shown.
106+ * @param {Object } result - The search result object
107+ * @param {Object } context - Context containing store reference
108+ * @returns {string|null }
109+ */
110+ getEmptyLabel ( result , context = null ) {
111+ if ( this . isNavigable ( result , context ) ) {
112+ return null
113+ }
114+ return this . app . i18n . t ( 'workspaceSearch.empty' )
115+ }
116+ }
117+
118+ export class ApplicationSearchType extends BaseSearchType {
119+ constructor ( context = { } ) {
120+ super ( context )
121+ }
122+
123+ _getApplicationChildren ( application ) {
124+ throw new Error ( '_getApplicationChildren must be implemented' )
125+ }
126+
127+ _getApplicationPath ( application , children ) {
128+ throw new Error ( '_getApplicationPath must be implemented' )
129+ }
130+
131+ _getApplicationId ( result ) {
132+ throw new Error ( '_getApplicationId must be implemented' )
133+ }
134+
135+ _getApplicationWithChildren ( result , context ) {
136+ const applicationId = this . _getApplicationId ( result )
137+ if ( ! applicationId || ! context ?. store ) {
138+ return null
139+ }
140+ const application = context . store . getters [ 'application/get' ] ( applicationId )
141+ if ( ! application ) {
142+ return null
143+ }
144+ const children = this . _getApplicationChildren ( application )
145+ if ( children && children . length > 0 ) {
146+ return application
147+ }
148+ return null
149+ }
150+
151+ buildUrl ( result , context = null ) {
152+ const application = this . _getApplicationWithChildren ( result , context )
153+ if ( ! application ) {
154+ return null
155+ }
156+
157+ const children = [ ...this . _getApplicationChildren ( application ) ] . sort (
158+ ( a , b ) => a . order - b . order
159+ )
160+
161+ return this . _getApplicationPath ( application , children )
162+ }
163+
164+ isNavigable ( result , context = null ) {
165+ return this . _getApplicationWithChildren ( result , context ) !== null
166+ }
37167}
0 commit comments