1 | /* |
---|
2 | Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | contributor license agreements. See the NOTICE file distributed with |
---|
4 | this work for additional information regarding copyright ownership. |
---|
5 | The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | (the "License"); you may not use this file except in compliance with |
---|
7 | the License. You may obtain a copy of the License at |
---|
8 | |
---|
9 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | |
---|
11 | Unless required by applicable law or agreed to in writing, software |
---|
12 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | See the License for the specific language governing permissions and |
---|
15 | limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | var cookie_analysis_verbose = 'analysis_verbose'; |
---|
19 | |
---|
20 | // #/:core/analysis |
---|
21 | sammy.get |
---|
22 | ( |
---|
23 | /^#\/([\w\d-]+)\/(analysis)$/, |
---|
24 | function( context ) |
---|
25 | { |
---|
26 | var active_core = this.active_core; |
---|
27 | var core_basepath = active_core.attr( 'data-basepath' ); |
---|
28 | var content_element = $( '#content' ); |
---|
29 | |
---|
30 | $.get |
---|
31 | ( |
---|
32 | 'tpl/analysis.html', |
---|
33 | function( template ) |
---|
34 | { |
---|
35 | content_element |
---|
36 | .html( template ); |
---|
37 | |
---|
38 | var analysis_element = $( '#analysis', content_element ); |
---|
39 | var analysis_form = $( 'form', analysis_element ); |
---|
40 | var analysis_result = $( '#analysis-result', analysis_element ); |
---|
41 | analysis_result.hide(); |
---|
42 | |
---|
43 | var type_or_name = $( '#type_or_name', analysis_form ); |
---|
44 | var schema_browser_element = $( '#tor_schema' ); |
---|
45 | var schema_browser_path = $( 'p > a', active_core ).attr( 'href' ) + '/schema-browser' |
---|
46 | var schema_browser_map = { 'fieldname' : 'field', 'fieldtype' : 'type' }; |
---|
47 | |
---|
48 | type_or_name |
---|
49 | .die( 'change' ) |
---|
50 | .live |
---|
51 | ( |
---|
52 | 'change', |
---|
53 | function( event ) |
---|
54 | { |
---|
55 | var info = $( this ).val().split( '=' ); |
---|
56 | |
---|
57 | schema_browser_element |
---|
58 | .attr( 'href', schema_browser_path + '?' + schema_browser_map[info[0]] + '=' + info[1] ); |
---|
59 | } |
---|
60 | ); |
---|
61 | |
---|
62 | $.ajax |
---|
63 | ( |
---|
64 | { |
---|
65 | url : core_basepath + '/admin/luke?wt=json&show=schema', |
---|
66 | dataType : 'json', |
---|
67 | context : type_or_name, |
---|
68 | beforeSend : function( xhr, settings ) |
---|
69 | { |
---|
70 | this |
---|
71 | .html( '<option value="">Loading ... </option>' ) |
---|
72 | .addClass( 'loader' ); |
---|
73 | }, |
---|
74 | success : function( response, text_status, xhr ) |
---|
75 | { |
---|
76 | var content = ''; |
---|
77 | |
---|
78 | var fields = []; |
---|
79 | for( var field_name in response.schema.fields ) |
---|
80 | { |
---|
81 | fields.push |
---|
82 | ( |
---|
83 | '<option value="fieldname=' + field_name + '">' + field_name + '</option>' |
---|
84 | ); |
---|
85 | } |
---|
86 | if( 0 !== fields.length ) |
---|
87 | { |
---|
88 | content += '<optgroup label="Fields">' + "\n"; |
---|
89 | content += fields.sort().join( "\n" ) + "\n"; |
---|
90 | content += '</optgroup>' + "\n"; |
---|
91 | } |
---|
92 | |
---|
93 | var types = []; |
---|
94 | for( var type_name in response.schema.types ) |
---|
95 | { |
---|
96 | types.push |
---|
97 | ( |
---|
98 | '<option value="fieldtype=' + type_name + '">' + type_name + '</option>' |
---|
99 | ); |
---|
100 | } |
---|
101 | if( 0 !== types.length ) |
---|
102 | { |
---|
103 | content += '<optgroup label="Types">' + "\n"; |
---|
104 | content += types.sort().join( "\n" ) + "\n"; |
---|
105 | content += '</optgroup>' + "\n"; |
---|
106 | } |
---|
107 | |
---|
108 | this |
---|
109 | .html( content ); |
---|
110 | |
---|
111 | var defaultSearchField = 'fieldname\=' + ( context.params['analysis.fieldname'] || response.schema.defaultSearchField ); |
---|
112 | |
---|
113 | if( context.params['analysis.fieldtype'] ) |
---|
114 | { |
---|
115 | defaultSearchField = 'fieldtype\=' + context.params['analysis.fieldtype']; |
---|
116 | } |
---|
117 | |
---|
118 | $( 'option[value="' + defaultSearchField + '"]', this ) |
---|
119 | .attr( 'selected', 'selected' ); |
---|
120 | |
---|
121 | this |
---|
122 | .chosen() |
---|
123 | .trigger( 'change' ); |
---|
124 | |
---|
125 | var fields = 0; |
---|
126 | for( var key in context.params ) |
---|
127 | { |
---|
128 | if( 'string' === typeof context.params[key] ) |
---|
129 | { |
---|
130 | fields++; |
---|
131 | $( '[name="' + key + '"]', analysis_form ) |
---|
132 | .val( decodeURIComponent( context.params[key].replace( /\+/g, '%20' ) ) ); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | if( 0 !== fields ) |
---|
137 | { |
---|
138 | analysis_form |
---|
139 | .trigger( 'execute' ); |
---|
140 | } |
---|
141 | }, |
---|
142 | error : function( xhr, text_status, error_thrown) |
---|
143 | { |
---|
144 | }, |
---|
145 | complete : function( xhr, text_status ) |
---|
146 | { |
---|
147 | this |
---|
148 | .removeClass( 'loader' ); |
---|
149 | } |
---|
150 | } |
---|
151 | ); |
---|
152 | |
---|
153 | $( '.analysis-error .head a', analysis_element ) |
---|
154 | .die( 'click' ) |
---|
155 | .live |
---|
156 | ( |
---|
157 | 'click', |
---|
158 | function( event ) |
---|
159 | { |
---|
160 | $( this ).parents( '.analysis-error' ) |
---|
161 | .toggleClass( 'expanded' ); |
---|
162 | } |
---|
163 | ); |
---|
164 | |
---|
165 | var check_empty_spacer = function() |
---|
166 | { |
---|
167 | var spacer_holder = $( 'td.part.data.spacer .holder', analysis_result ); |
---|
168 | |
---|
169 | if( 0 === spacer_holder.size() ) |
---|
170 | { |
---|
171 | return false; |
---|
172 | } |
---|
173 | |
---|
174 | var verbose_output = analysis_result.hasClass( 'verbose_output' ); |
---|
175 | |
---|
176 | spacer_holder |
---|
177 | .each |
---|
178 | ( |
---|
179 | function( index, element ) |
---|
180 | { |
---|
181 | element = $( element ); |
---|
182 | |
---|
183 | if( verbose_output ) |
---|
184 | { |
---|
185 | var cell = element.parent(); |
---|
186 | element.height( cell.height() ); |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | element.removeAttr( 'style' ); |
---|
191 | } |
---|
192 | } |
---|
193 | ); |
---|
194 | } |
---|
195 | |
---|
196 | var verbose_link = $( '.verbose_output a', analysis_element ); |
---|
197 | |
---|
198 | verbose_link |
---|
199 | .die( 'toggle' ) |
---|
200 | .live |
---|
201 | ( |
---|
202 | 'toggle', |
---|
203 | function( event ) |
---|
204 | { |
---|
205 | $( this ).parent() |
---|
206 | .toggleClass( 'active' ); |
---|
207 | |
---|
208 | analysis_result |
---|
209 | .toggleClass( 'verbose_output' ); |
---|
210 | |
---|
211 | check_empty_spacer(); |
---|
212 | } |
---|
213 | ) |
---|
214 | .die( 'click' ) |
---|
215 | .live |
---|
216 | ( |
---|
217 | 'click', |
---|
218 | function( event ) |
---|
219 | { |
---|
220 | $.cookie( cookie_analysis_verbose, $.cookie( cookie_analysis_verbose ) ? null : true ); |
---|
221 | |
---|
222 | $( this ) |
---|
223 | .trigger( 'toggle' ); |
---|
224 | } |
---|
225 | ); |
---|
226 | |
---|
227 | if( $.cookie( cookie_analysis_verbose ) ) |
---|
228 | { |
---|
229 | verbose_link |
---|
230 | .trigger( 'toggle' ); |
---|
231 | } |
---|
232 | |
---|
233 | var button = $( 'button', analysis_form ) |
---|
234 | |
---|
235 | var compute_analysis_params = function() |
---|
236 | { |
---|
237 | var params = analysis_form.formToArray(); |
---|
238 | |
---|
239 | var type_or_name = $( '#type_or_name', analysis_form ).val().split( '=' ); |
---|
240 | params.push( { name: 'analysis.' + type_or_name[0], value: type_or_name[1] } ); |
---|
241 | |
---|
242 | return params; |
---|
243 | } |
---|
244 | |
---|
245 | analysis_form |
---|
246 | .die( 'submit' ) |
---|
247 | .live |
---|
248 | ( |
---|
249 | 'submit', |
---|
250 | function( event ) |
---|
251 | { |
---|
252 | var params = compute_analysis_params(); |
---|
253 | |
---|
254 | context.redirect( context.path.split( '?' ).shift() + '?' + $.param( params ) ); |
---|
255 | return false; |
---|
256 | } |
---|
257 | ) |
---|
258 | .die( 'execute' ) |
---|
259 | .live |
---|
260 | ( |
---|
261 | 'execute', |
---|
262 | function( event ) |
---|
263 | { |
---|
264 | $.ajax |
---|
265 | ( |
---|
266 | { |
---|
267 | url : core_basepath + '/analysis/field?wt=json&analysis.showmatch=true&' + context.path.split( '?' ).pop(), |
---|
268 | dataType : 'json', |
---|
269 | beforeSend : function( xhr, settings ) |
---|
270 | { |
---|
271 | loader.show( button ); |
---|
272 | button.attr( 'disabled', true ); |
---|
273 | }, |
---|
274 | success : function( response, status_text, xhr, form ) |
---|
275 | { |
---|
276 | $( '.analysis-error', analysis_element ) |
---|
277 | .hide(); |
---|
278 | |
---|
279 | analysis_result |
---|
280 | .empty() |
---|
281 | .show(); |
---|
282 | |
---|
283 | for( var name in response.analysis.field_names ) |
---|
284 | { |
---|
285 | build_analysis_table( 'name', name, response.analysis.field_names[name] ); |
---|
286 | } |
---|
287 | |
---|
288 | for( var name in response.analysis.field_types ) |
---|
289 | { |
---|
290 | build_analysis_table( 'type', name, response.analysis.field_types[name] ); |
---|
291 | } |
---|
292 | |
---|
293 | check_empty_spacer(); |
---|
294 | }, |
---|
295 | error : function( xhr, text_status, error_thrown ) |
---|
296 | { |
---|
297 | analysis_result |
---|
298 | .empty() |
---|
299 | .hide(); |
---|
300 | |
---|
301 | if( 404 === xhr.status ) |
---|
302 | { |
---|
303 | $( '#analysis-handler-missing', analysis_element ) |
---|
304 | .show(); |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | $( '#analysis-error', analysis_element ) |
---|
309 | .show(); |
---|
310 | |
---|
311 | var response = null; |
---|
312 | try |
---|
313 | { |
---|
314 | eval( 'response = ' + xhr.responseText + ';' ); |
---|
315 | } |
---|
316 | catch( e ) |
---|
317 | { |
---|
318 | console.error( e ); |
---|
319 | } |
---|
320 | |
---|
321 | $( '#analysis-error .body', analysis_element ) |
---|
322 | .text( response ? response.error.msg : xhr.responseText ); |
---|
323 | } |
---|
324 | }, |
---|
325 | complete : function() |
---|
326 | { |
---|
327 | loader.hide( $( 'button', analysis_form ) ); |
---|
328 | button.removeAttr( 'disabled' ); |
---|
329 | } |
---|
330 | } |
---|
331 | ); |
---|
332 | } |
---|
333 | ); |
---|
334 | |
---|
335 | var generate_class_name = function( type ) |
---|
336 | { |
---|
337 | var classes = [type]; |
---|
338 | if( 'text' !== type ) |
---|
339 | { |
---|
340 | classes.push( 'verbose_output' ); |
---|
341 | } |
---|
342 | return classes.join( ' ' ); |
---|
343 | } |
---|
344 | |
---|
345 | var build_analysis_table = function( field_or_name, name, analysis_data ) |
---|
346 | { |
---|
347 | for( var type in analysis_data ) |
---|
348 | { |
---|
349 | var type_length = analysis_data[type].length; |
---|
350 | if( 0 !== type_length ) |
---|
351 | { |
---|
352 | var global_elements_count = 0; |
---|
353 | for( var i = 0; i < analysis_data[type].length; i += 2 ) |
---|
354 | { |
---|
355 | if( 'string' === typeof analysis_data[type][i+1] ) |
---|
356 | { |
---|
357 | analysis_data[type][i+1] = [{ 'text': analysis_data[type][i+1] }] |
---|
358 | } |
---|
359 | |
---|
360 | var tmp = {}; |
---|
361 | var cols = analysis_data[type][i+1].filter |
---|
362 | ( |
---|
363 | function( obj ) |
---|
364 | { |
---|
365 | var obj_position = obj.position || 0; |
---|
366 | if( !tmp[obj_position] ) |
---|
367 | { |
---|
368 | tmp[obj_position] = true; |
---|
369 | return true; |
---|
370 | } |
---|
371 | |
---|
372 | return false; |
---|
373 | } |
---|
374 | ); |
---|
375 | |
---|
376 | global_elements_count = Math.max( global_elements_count, cols.length ); |
---|
377 | } |
---|
378 | |
---|
379 | var content = '<div class="' + type + '">' + "\n"; |
---|
380 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
381 | |
---|
382 | for( var i = 0; i < analysis_data[type].length; i += 2 ) |
---|
383 | { |
---|
384 | var colspan = 1; |
---|
385 | var elements = analysis_data[type][i+1]; |
---|
386 | var elements_count = global_elements_count; |
---|
387 | |
---|
388 | if( !elements[0] || !elements[0].positionHistory ) |
---|
389 | { |
---|
390 | colspan = elements_count; |
---|
391 | elements_count = 1; |
---|
392 | } |
---|
393 | |
---|
394 | var legend = []; |
---|
395 | for( var key in elements[0] ) |
---|
396 | { |
---|
397 | var key_parts = key.split( '#' ); |
---|
398 | var used_key = key_parts.pop(); |
---|
399 | var short_key = used_key; |
---|
400 | |
---|
401 | if( 1 === key_parts.length ) |
---|
402 | { |
---|
403 | used_key = '<abbr title="' + key + '">' + used_key + '</abbr>'; |
---|
404 | } |
---|
405 | |
---|
406 | if( 'positionHistory' === short_key || 'match' === short_key ) |
---|
407 | { |
---|
408 | continue; |
---|
409 | } |
---|
410 | |
---|
411 | legend.push |
---|
412 | ( |
---|
413 | '<tr class="' + generate_class_name( short_key ) + '">' + |
---|
414 | '<td>' + used_key + '</td>' + |
---|
415 | '</tr>' |
---|
416 | ); |
---|
417 | } |
---|
418 | |
---|
419 | content += '<tbody>' + "\n"; |
---|
420 | content += '<tr class="step">' + "\n"; |
---|
421 | |
---|
422 | // analyzer |
---|
423 | var analyzer_name = analysis_data[type][i].replace( /(\$1)+$/g, '' ); |
---|
424 | |
---|
425 | var analyzer_short = -1 !== analyzer_name.indexOf( '$' ) |
---|
426 | ? analyzer_name.split( '$' )[1] |
---|
427 | : analyzer_name.split( '.' ).pop(); |
---|
428 | analyzer_short = analyzer_short.match( /[A-Z]/g ).join( '' ); |
---|
429 | |
---|
430 | content += '<td class="part analyzer"><div>' + "\n"; |
---|
431 | content += '<abbr title="' + analysis_data[type][i].esc() + '">' + "\n"; |
---|
432 | content += analyzer_short.esc() + '</abbr></div></td>' + "\n"; |
---|
433 | |
---|
434 | // legend |
---|
435 | content += '<td class="part legend"><div class="holder">' + "\n"; |
---|
436 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
437 | content += '<tr><td>' + "\n"; |
---|
438 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
439 | content += legend.join( "\n" ) + "\n"; |
---|
440 | content += '</table></td></tr></table></td>' + "\n"; |
---|
441 | |
---|
442 | // data |
---|
443 | var cell_content = '<td class="part data spacer" colspan="' + colspan + '"><div class="holder"> </div></td>'; |
---|
444 | var cells = new Array( elements_count + 1 ).join( cell_content ); |
---|
445 | content += cells + "\n"; |
---|
446 | |
---|
447 | content += '</tr>' + "\n"; |
---|
448 | content += '</tbody>' + "\n"; |
---|
449 | } |
---|
450 | content += '</table>' + "\n"; |
---|
451 | content += '</div>' + "\n"; |
---|
452 | |
---|
453 | $( '.' + type, analysis_result ) |
---|
454 | .remove(); |
---|
455 | |
---|
456 | analysis_result |
---|
457 | .append( content ); |
---|
458 | |
---|
459 | var analysis_result_type = $( '.' + type, analysis_result ); |
---|
460 | |
---|
461 | for( var i = 0; i < analysis_data[type].length; i += 2 ) |
---|
462 | { |
---|
463 | for( var j = 0; j < analysis_data[type][i+1].length; j += 1 ) |
---|
464 | { |
---|
465 | var pos = analysis_data[type][i+1][j].positionHistory |
---|
466 | ? analysis_data[type][i+1][j].positionHistory[0] |
---|
467 | : 1; |
---|
468 | var selector = 'tr.step:eq(' + ( i / 2 ) +') ' |
---|
469 | + 'td.data:eq(' + ( pos - 1 ) + ') ' |
---|
470 | + '.holder'; |
---|
471 | var cell = $( selector, analysis_result_type ); |
---|
472 | |
---|
473 | cell.parent() |
---|
474 | .removeClass( 'spacer' ); |
---|
475 | |
---|
476 | var table = $( 'table tr.details', cell ); |
---|
477 | if( 0 === table.size() ) |
---|
478 | { |
---|
479 | cell |
---|
480 | .html |
---|
481 | ( |
---|
482 | '<table border="0" cellspacing="0" cellpadding="0">' + |
---|
483 | '<tr class="details"></tr></table>' |
---|
484 | ); |
---|
485 | var table = $( 'table tr.details', cell ); |
---|
486 | } |
---|
487 | |
---|
488 | var tokens = []; |
---|
489 | for( var key in analysis_data[type][i+1][j] ) |
---|
490 | { |
---|
491 | var short_key = key.split( '#' ).pop(); |
---|
492 | |
---|
493 | if( 'positionHistory' === short_key || 'match' === short_key ) |
---|
494 | { |
---|
495 | continue; |
---|
496 | } |
---|
497 | |
---|
498 | var classes = []; |
---|
499 | classes.push( generate_class_name( short_key ) ); |
---|
500 | |
---|
501 | var data = analysis_data[type][i+1][j][key]; |
---|
502 | if( 'object' === typeof data && data instanceof Array ) |
---|
503 | { |
---|
504 | data = data.join( ' ' ); |
---|
505 | } |
---|
506 | if( 'string' === typeof data ) |
---|
507 | { |
---|
508 | data = data.esc(); |
---|
509 | } |
---|
510 | |
---|
511 | if( null === data || 0 === data.length ) |
---|
512 | { |
---|
513 | classes.push( 'empty' ); |
---|
514 | data = '∅'; |
---|
515 | } |
---|
516 | |
---|
517 | if( analysis_data[type][i+1][j].match && |
---|
518 | ( 'text' === short_key || 'raw_bytes' === short_key ) ) |
---|
519 | { |
---|
520 | classes.push( 'match' ); |
---|
521 | } |
---|
522 | |
---|
523 | tokens.push |
---|
524 | ( |
---|
525 | '<tr class="' + classes.join( ' ' ) + '">' + |
---|
526 | '<td>' + data + '</td>' + |
---|
527 | '</tr>' |
---|
528 | ); |
---|
529 | } |
---|
530 | table |
---|
531 | .append |
---|
532 | ( |
---|
533 | '<td class="details">' + |
---|
534 | '<table border="0" cellspacing="0" cellpadding="0">' + |
---|
535 | tokens.join( "\n" ) + |
---|
536 | '</table></td>' |
---|
537 | ); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | } |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | } |
---|
546 | ); |
---|
547 | } |
---|
548 | ); |
---|