@@ -42,18 +42,46 @@ const debounce = (func, wait) => {
42
42
43
43
44
44
/**
45
- * Wrapper around underscorejs's template function.
46
- *
47
- * This is to make it work with new and old versions.
48
- */
49
- const render_template = ( template , data ) => {
50
- // pre-1.7 syntax from underscorejs.
51
- let result = $u . template ( template , data ) ;
52
- if ( typeof result === 'function' ) {
53
- // New syntax.
54
- result = $u . template ( template ) ( data ) ;
45
+ * Build a section with its matching results.
46
+ *
47
+ * A section has the form:
48
+ *
49
+ * <a href="{link}">
50
+ * <div class="outer_div_page_results" id="{id}">
51
+ * <span class="search__result__subheading">
52
+ * {title}
53
+ * </span>
54
+ * <p class="search__result__content">
55
+ * {contents[0]}
56
+ * </p>
57
+ * <p class="search__result__content">
58
+ * {contents[1]}
59
+ * </p>
60
+ * ...
61
+ * </div>
62
+ * </a>
63
+ *
64
+ * @param {String } id.
65
+ * @param {String } title.
66
+ * @param {String } link.
67
+ * @param {Array } contents.
68
+ */
69
+ const buildSection = function ( id , title , link , contents ) {
70
+ let span_element = createDomNode ( "span" , { class : "search__result__subheading" } ) ;
71
+ span_element . innerHTML = title ;
72
+
73
+ let div_element = createDomNode ( "div" , { class : "outer_div_page_results" , id : id } ) ;
74
+ div_element . appendChild ( span_element ) ;
75
+
76
+ for ( var i = 0 ; i < contents . length ; i += 1 ) {
77
+ let p_element = createDomNode ( "p" , { class : "search__result__content" } ) ;
78
+ p_element . innerHTML = contents [ i ] ;
79
+ div_element . appendChild ( p_element ) ;
55
80
}
56
- return result ;
81
+
82
+ let section = createDomNode ( "a" , { href : link } ) ;
83
+ section . appendChild ( div_element ) ;
84
+ return section ;
57
85
} ;
58
86
59
87
@@ -105,8 +133,10 @@ const isModalVisible = () => {
105
133
*/
106
134
const createDomNode = ( nodeName , attributes ) => {
107
135
let node = document . createElement ( nodeName ) ;
108
- for ( let attr in attributes ) {
109
- node . setAttribute ( attr , attributes [ attr ] ) ;
136
+ if ( attributes !== null ) {
137
+ for ( let attr in attributes ) {
138
+ node . setAttribute ( attr , attributes [ attr ] ) ;
139
+ }
110
140
}
111
141
return node ;
112
142
} ;
@@ -135,21 +165,6 @@ const _is_string = str => {
135
165
* @param {Number } id to be used in for this section
136
166
*/
137
167
const get_section_html = ( sectionData , page_link , id ) => {
138
- let section_template =
139
- '<a href="<%= section_link %>"> \
140
- <div class="outer_div_page_results" id="<%= section_id %>"> \
141
- <span class="search__result__subheading"> \
142
- <%= section_subheading %> \
143
- </span> \
144
- <% for (var i = 0; i < section_content.length; ++i) { %> \
145
- <p class="search__result__content"> \
146
- <%= section_content[i] %> \
147
- </p> \
148
- <% } %>\
149
- </div> \
150
- </a> \
151
- <br class="br-for-hits">' ;
152
-
153
168
let section_subheading = sectionData . title ;
154
169
let highlights = sectionData . highlights ;
155
170
if ( highlights . title . length ) {
@@ -173,17 +188,8 @@ const get_section_html = (sectionData, page_link, id) => {
173
188
}
174
189
175
190
let section_link = `${ page_link } #${ sectionData . id } ` ;
176
-
177
191
let section_id = "hit__" + id ;
178
-
179
- let section_html = render_template ( section_template , {
180
- section_link : section_link ,
181
- section_id : section_id ,
182
- section_subheading : section_subheading ,
183
- section_content : section_content
184
- } ) ;
185
-
186
- return section_html ;
192
+ return buildSection ( section_id , section_subheading , section_link , section_content ) ;
187
193
} ;
188
194
189
195
/**
@@ -195,20 +201,6 @@ const get_section_html = (sectionData, page_link, id) => {
195
201
* @param {Number } id to be used in for this section
196
202
*/
197
203
const get_domain_html = ( domainData , page_link , id ) => {
198
- let domain_template =
199
- '<a href="<%= domain_link %>"> \
200
- <div class="outer_div_page_results" id="<%= domain_id %>"> \
201
- <span class="search__result__subheading"> \
202
- <%= domain_subheading %> \
203
- <div class="search__domain_role_name"> \
204
- <%= domain_role_name %> \
205
- </div> \
206
- </span> \
207
- <p class="search__result__content"><%= domain_content %></p> \
208
- </div> \
209
- </a> \
210
- <br class="br-for-hits">' ;
211
-
212
204
let domain_link = `${ page_link } #${ domainData . id } ` ;
213
205
let domain_role_name = domainData . role ;
214
206
let domain_name = domainData . name ;
@@ -224,37 +216,50 @@ const get_domain_html = (domainData, page_link, id) => {
224
216
}
225
217
226
218
let domain_id = "hit__" + id ;
227
- domain_role_name = "[" + domain_role_name + "]" ;
228
-
229
- let domain_html = render_template ( domain_template , {
230
- domain_link : domain_link ,
231
- domain_id : domain_id ,
232
- domain_content : domain_content ,
233
- domain_subheading : domain_name ,
234
- domain_role_name : domain_role_name
235
- } ) ;
236
219
237
- return domain_html ;
220
+ let div_role_name = createDomNode ( "div" , { class : "search__domain_role_name" } ) ;
221
+ div_role_name . innerText = `[${ domain_role_name } ]` ;
222
+ domain_name += div_role_name . outerHTML ;
223
+
224
+ return buildSection (
225
+ domain_id ,
226
+ domain_name ,
227
+ domain_link ,
228
+ [ domain_content ]
229
+ ) ;
238
230
} ;
239
231
232
+
240
233
/**
241
234
* Generate search results for a single page.
242
235
*
236
+ * This has the form:
237
+ * <div>
238
+ * <a href="{link}">
239
+ * <h2 class="search__result__title">
240
+ * {title}
241
+ * <small class="rtd_ui_search_subtitle">{subtitle}</small>
242
+ * <br/>
243
+ * </h2>
244
+ * </a>
245
+ *
246
+ * <a href="{link}">
247
+ * {section}
248
+ * </a>
249
+ * <br class="br-for-hits" />
250
+ *
251
+ * <a href="{link}">
252
+ * {section}
253
+ * </a>
254
+ * <br class="br-for-hits" />
255
+ * </div>
256
+ *
243
257
* @param {Object } resultData search results of a page
244
258
* @param {String } projectName
245
259
* @param {Number } id from the last section
246
260
* @return {Object } a <div> node with the results of a single page
247
261
*/
248
262
const generateSingleResult = ( resultData , projectName , id ) => {
249
- let content = createDomNode ( "div" ) ;
250
-
251
- let page_link_template =
252
- '<a href="<%= page_link %>"> \
253
- <h2 class="search__result__title"> \
254
- <%= page_title %> \
255
- </h2> \
256
- </a>' ;
257
-
258
263
let page_link = resultData . path ;
259
264
let page_title = resultData . title ;
260
265
let highlights = resultData . highlights ;
@@ -263,47 +268,47 @@ const generateSingleResult = (resultData, projectName, id) => {
263
268
page_title = highlights . title [ 0 ] ;
264
269
}
265
270
266
- // if result is not from the same project,
267
- // then it must be from subproject.
271
+ let h2_element = createDomNode ( "h2" , { class : "search__result__title" } ) ;
272
+ h2_element . innerHTML = page_title ;
273
+
274
+ // If the result is not from the same project,
275
+ // then it's from a subproject.
268
276
if ( projectName !== resultData . project ) {
269
- page_title +=
270
- " " +
271
- render_template (
272
- '<small class="rtd_ui_search_subtitle"> \
273
- (from project <%= project %>) \
274
- </small>' ,
275
- {
276
- project : resultData . project
277
- }
278
- ) ;
277
+ let subtitle = createDomNode ( "small" , { class : "rtd_ui_search_subtitle" } ) ;
278
+ subtitle . innerText = `(from project ${ resultData . project } )` ;
279
+ h2_element . appendChild ( subtitle ) ;
279
280
}
281
+ h2_element . appendChild ( createDomNode ( "br" ) )
280
282
281
- page_title += "<br>" ;
283
+ let a_element = createDomNode ( "a" , { href : page_link } ) ;
284
+ a_element . appendChild ( h2_element ) ;
282
285
283
- content . innerHTML += render_template ( page_link_template , {
284
- page_link : page_link ,
285
- page_title : page_title
286
- } ) ;
286
+ let content = createDomNode ( "div" ) ;
287
+ content . appendChild ( a_element ) ;
287
288
289
+ let separator = createDomNode ( "br" , { class : "br-for-hits" } ) ;
288
290
for ( let i = 0 ; i < resultData . blocks . length ; ++ i ) {
289
291
let block = resultData . blocks [ i ] ;
290
- let html_structure = "" ;
291
-
292
+ let section = null ;
292
293
id += 1 ;
293
294
if ( block . type === "section" ) {
294
- html_structure = get_section_html (
295
+ section = get_section_html (
295
296
block ,
296
297
page_link ,
297
298
id ,
298
299
) ;
299
300
} else if ( block . type === "domain" ) {
300
- html_structure = get_domain_html (
301
+ section = get_domain_html (
301
302
block ,
302
303
page_link ,
303
304
id ,
304
305
) ;
305
306
}
306
- content . innerHTML += html_structure ;
307
+
308
+ if ( section !== null ) {
309
+ content . appendChild ( section ) ;
310
+ content . appendChild ( separator ) ;
311
+ }
307
312
}
308
313
return content ;
309
314
} ;
0 commit comments