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 | // #/:core/analysis |
---|
19 | sammy.get |
---|
20 | ( |
---|
21 | new RegExp( app.core_regex_base + '\\/(analysis)$' ), |
---|
22 | function( context ) |
---|
23 | { |
---|
24 | var active_core = this.active_core; |
---|
25 | var core_basepath = active_core.attr( 'data-basepath' ); |
---|
26 | var content_element = $( '#content' ); |
---|
27 | |
---|
28 | $.get |
---|
29 | ( |
---|
30 | 'tpl/analysis.html', |
---|
31 | function( template ) |
---|
32 | { |
---|
33 | content_element |
---|
34 | .html( template ); |
---|
35 | |
---|
36 | var analysis_element = $( '#analysis', content_element ); |
---|
37 | var analysis_form = $( 'form', analysis_element ); |
---|
38 | var analysis_result = $( '#analysis-result', analysis_element ); |
---|
39 | analysis_result.hide(); |
---|
40 | |
---|
41 | var verbose_link = $( '.verbose_output a', analysis_element ); |
---|
42 | |
---|
43 | var type_or_name = $( '#type_or_name', analysis_form ); |
---|
44 | var schema_browser_element = $( '#tor_schema' ); |
---|
45 | var schema_browser_path = app.core_menu.find( '.schema-browser a' ).attr( 'href' ); |
---|
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] && 0 !== context.params[key].length ) |
---|
129 | { |
---|
130 | fields++; |
---|
131 | $( '[name="' + key + '"]', analysis_form ) |
---|
132 | .val( context.params[key] ); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | if( 'undefined' !== typeof context.params.verbose_output ) |
---|
137 | { |
---|
138 | verbose_link.trigger( 'toggle', !!context.params.verbose_output.match( /^(1|true)$/ ) ); |
---|
139 | } |
---|
140 | |
---|
141 | if( 0 !== fields ) |
---|
142 | { |
---|
143 | analysis_form |
---|
144 | .trigger( 'execute' ); |
---|
145 | } |
---|
146 | }, |
---|
147 | error : function( xhr, text_status, error_thrown) |
---|
148 | { |
---|
149 | }, |
---|
150 | complete : function( xhr, text_status ) |
---|
151 | { |
---|
152 | this |
---|
153 | .removeClass( 'loader' ); |
---|
154 | } |
---|
155 | } |
---|
156 | ); |
---|
157 | |
---|
158 | $( '.analysis-error .head a', analysis_element ) |
---|
159 | .die( 'click' ) |
---|
160 | .live |
---|
161 | ( |
---|
162 | 'click', |
---|
163 | function( event ) |
---|
164 | { |
---|
165 | $( this ).parents( '.analysis-error' ) |
---|
166 | .toggleClass( 'expanded' ); |
---|
167 | } |
---|
168 | ); |
---|
169 | |
---|
170 | var check_empty_spacer = function() |
---|
171 | { |
---|
172 | var spacer_holder = $( 'td.part.data.spacer .holder', analysis_result ); |
---|
173 | |
---|
174 | if( 0 === spacer_holder.size() ) |
---|
175 | { |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | var verbose_output = analysis_result.hasClass( 'verbose_output' ); |
---|
180 | |
---|
181 | spacer_holder |
---|
182 | .each |
---|
183 | ( |
---|
184 | function( index, element ) |
---|
185 | { |
---|
186 | element = $( element ); |
---|
187 | |
---|
188 | if( verbose_output ) |
---|
189 | { |
---|
190 | var cell = element.parent(); |
---|
191 | element.height( cell.height() ); |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | element.removeAttr( 'style' ); |
---|
196 | } |
---|
197 | } |
---|
198 | ); |
---|
199 | } |
---|
200 | |
---|
201 | verbose_link |
---|
202 | .die( 'toggle' ) |
---|
203 | .live |
---|
204 | ( |
---|
205 | 'toggle', |
---|
206 | function( event, state ) |
---|
207 | { |
---|
208 | $( this ).parent() |
---|
209 | .toggleClass( 'active', state ); |
---|
210 | |
---|
211 | analysis_result |
---|
212 | .toggleClass( 'verbose_output', state ); |
---|
213 | |
---|
214 | check_empty_spacer(); |
---|
215 | } |
---|
216 | ) |
---|
217 | .die( 'click' ) |
---|
218 | .live |
---|
219 | ( |
---|
220 | 'click', |
---|
221 | function( event ) |
---|
222 | { |
---|
223 | $( this ).parent() |
---|
224 | .toggleClass( 'active' ); |
---|
225 | |
---|
226 | analysis_form.trigger( 'submit' ); |
---|
227 | } |
---|
228 | ); |
---|
229 | |
---|
230 | var button = $( 'button', analysis_form ) |
---|
231 | |
---|
232 | var compute_analysis_params = function() |
---|
233 | { |
---|
234 | var params = analysis_form.formToArray(); |
---|
235 | |
---|
236 | var type_or_name = $( '#type_or_name', analysis_form ).val().split( '=' ); |
---|
237 | params.push( { name: 'analysis.' + type_or_name[0], value: type_or_name[1] } ); |
---|
238 | params.push( { name: 'verbose_output', value: $( '.verbose_output', analysis_element ).hasClass( 'active' ) ? 1 : 0 } ); |
---|
239 | |
---|
240 | return params; |
---|
241 | } |
---|
242 | |
---|
243 | analysis_form |
---|
244 | .die( 'submit' ) |
---|
245 | .live |
---|
246 | ( |
---|
247 | 'submit', |
---|
248 | function( event ) |
---|
249 | { |
---|
250 | var params = $.param( compute_analysis_params() ) |
---|
251 | .replace( /[\w\.]+=\+*(&)/g, '$1' ) // remove empty parameters |
---|
252 | .replace( /(&)+/, '$1' ) // reduce multiple ampersands |
---|
253 | .replace( /^&/, '' ) // remove leading ampersand |
---|
254 | .replace( /\+/g, '%20' ); // replace plus-signs with encoded whitespaces |
---|
255 | |
---|
256 | context.redirect( context.path.split( '?' ).shift() + '?' + params ); |
---|
257 | return false; |
---|
258 | } |
---|
259 | ) |
---|
260 | .die( 'execute' ) |
---|
261 | .live |
---|
262 | ( |
---|
263 | 'execute', |
---|
264 | function( event ) |
---|
265 | { |
---|
266 | var url = core_basepath + '/analysis/field?wt=json&analysis.showmatch=true&' + context.path.split( '?' ).pop(); |
---|
267 | url = url.replace( /&verbose_output=\d/, '' ); |
---|
268 | |
---|
269 | $.ajax |
---|
270 | ( |
---|
271 | { |
---|
272 | url : url, |
---|
273 | dataType : 'json', |
---|
274 | beforeSend : function( xhr, settings ) |
---|
275 | { |
---|
276 | loader.show( $( 'span', button ) ); |
---|
277 | button.attr( 'disabled', true ); |
---|
278 | }, |
---|
279 | success : function( response, status_text, xhr, form ) |
---|
280 | { |
---|
281 | $( '.analysis-error', analysis_element ) |
---|
282 | .hide(); |
---|
283 | |
---|
284 | analysis_result |
---|
285 | .empty() |
---|
286 | .show(); |
---|
287 | |
---|
288 | for( var name in response.analysis.field_names ) |
---|
289 | { |
---|
290 | build_analysis_table( 'name', name, response.analysis.field_names[name] ); |
---|
291 | } |
---|
292 | |
---|
293 | for( var name in response.analysis.field_types ) |
---|
294 | { |
---|
295 | build_analysis_table( 'type', name, response.analysis.field_types[name] ); |
---|
296 | } |
---|
297 | |
---|
298 | check_empty_spacer(); |
---|
299 | }, |
---|
300 | error : function( xhr, text_status, error_thrown ) |
---|
301 | { |
---|
302 | analysis_result |
---|
303 | .empty() |
---|
304 | .hide(); |
---|
305 | |
---|
306 | if( 404 === xhr.status ) |
---|
307 | { |
---|
308 | $( '#analysis-handler-missing', analysis_element ) |
---|
309 | .show(); |
---|
310 | } |
---|
311 | else |
---|
312 | { |
---|
313 | $( '#analysis-error', analysis_element ) |
---|
314 | .show(); |
---|
315 | |
---|
316 | var response = null; |
---|
317 | try |
---|
318 | { |
---|
319 | eval( 'response = ' + xhr.responseText + ';' ); |
---|
320 | } |
---|
321 | catch( e ) |
---|
322 | { |
---|
323 | console.error( e ); |
---|
324 | } |
---|
325 | |
---|
326 | $( '#analysis-error .body', analysis_element ) |
---|
327 | .text( response ? response.error.msg : xhr.responseText ); |
---|
328 | } |
---|
329 | }, |
---|
330 | complete : function() |
---|
331 | { |
---|
332 | loader.hide( $( 'span', button ) ); |
---|
333 | button.removeAttr( 'disabled' ); |
---|
334 | } |
---|
335 | } |
---|
336 | ); |
---|
337 | } |
---|
338 | ); |
---|
339 | |
---|
340 | var generate_class_name = function( type ) |
---|
341 | { |
---|
342 | var classes = [type]; |
---|
343 | if( 'text' !== type ) |
---|
344 | { |
---|
345 | classes.push( 'verbose_output' ); |
---|
346 | } |
---|
347 | return classes.join( ' ' ); |
---|
348 | } |
---|
349 | |
---|
350 | var build_analysis_table = function( field_or_name, name, analysis_data ) |
---|
351 | { |
---|
352 | for( var type in analysis_data ) |
---|
353 | { |
---|
354 | var type_length = analysis_data[type].length; |
---|
355 | if( 0 !== type_length ) |
---|
356 | { |
---|
357 | var global_elements_count = 0; |
---|
358 | if( 'string' === typeof analysis_data[type][1] ) |
---|
359 | { |
---|
360 | analysis_data[type][1] = [{ 'text': analysis_data[type][1] }] |
---|
361 | } |
---|
362 | |
---|
363 | for( var i = 1; i < type_length; i += 2 ) |
---|
364 | { |
---|
365 | var tmp_type_length = analysis_data[type][i].length; |
---|
366 | for( var j = 0; j < tmp_type_length; j++ ) |
---|
367 | { |
---|
368 | global_elements_count = Math.max |
---|
369 | ( |
---|
370 | ( analysis_data[type][i][j].positionHistory || [] )[0] || 1, |
---|
371 | global_elements_count |
---|
372 | ); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | var content = '<div class="' + type + '">' + "\n"; |
---|
377 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
378 | |
---|
379 | for( var i = 0; i < analysis_data[type].length; i += 2 ) |
---|
380 | { |
---|
381 | var colspan = 1; |
---|
382 | var elements = analysis_data[type][i+1]; |
---|
383 | var elements_count = global_elements_count; |
---|
384 | |
---|
385 | if( !elements[0] || !elements[0].positionHistory ) |
---|
386 | { |
---|
387 | colspan = elements_count; |
---|
388 | elements_count = 1; |
---|
389 | } |
---|
390 | |
---|
391 | var legend = []; |
---|
392 | for( var key in elements[0] ) |
---|
393 | { |
---|
394 | var key_parts = key.split( '#' ); |
---|
395 | var used_key = key_parts.pop(); |
---|
396 | var short_key = used_key; |
---|
397 | |
---|
398 | if( 1 === key_parts.length ) |
---|
399 | { |
---|
400 | used_key = '<abbr title="' + key + '">' + used_key + '</abbr>'; |
---|
401 | } |
---|
402 | |
---|
403 | if( 'positionHistory' === short_key || 'match' === short_key ) |
---|
404 | { |
---|
405 | continue; |
---|
406 | } |
---|
407 | |
---|
408 | legend.push |
---|
409 | ( |
---|
410 | '<tr class="' + generate_class_name( short_key ) + '">' + |
---|
411 | '<td>' + used_key + '</td>' + |
---|
412 | '</tr>' |
---|
413 | ); |
---|
414 | } |
---|
415 | |
---|
416 | content += '<tbody>' + "\n"; |
---|
417 | content += '<tr class="step">' + "\n"; |
---|
418 | |
---|
419 | // analyzer |
---|
420 | var analyzer_name = analysis_data[type][i].replace( /(\$1)+$/g, '' ); |
---|
421 | |
---|
422 | var analyzer_short = -1 !== analyzer_name.indexOf( '$' ) |
---|
423 | ? analyzer_name.split( '$' )[1] |
---|
424 | : analyzer_name.split( '.' ).pop(); |
---|
425 | analyzer_short = analyzer_short.match( /[A-Z]/g ).join( '' ); |
---|
426 | |
---|
427 | content += '<td class="part analyzer"><div>' + "\n"; |
---|
428 | content += '<abbr title="' + analysis_data[type][i].esc() + '">' + "\n"; |
---|
429 | content += analyzer_short.esc() + '</abbr></div></td>' + "\n"; |
---|
430 | |
---|
431 | // legend |
---|
432 | content += '<td class="part legend"><div class="holder">' + "\n"; |
---|
433 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
434 | content += '<tr><td>' + "\n"; |
---|
435 | content += '<table border="0" cellspacing="0" cellpadding="0">' + "\n"; |
---|
436 | content += legend.join( "\n" ) + "\n"; |
---|
437 | content += '</table></td></tr></table></td>' + "\n"; |
---|
438 | |
---|
439 | // data |
---|
440 | var cell_content = '<td class="part data spacer" colspan="' + colspan + '"><div class="holder"> </div></td>'; |
---|
441 | var cells = new Array( elements_count + 1 ).join( cell_content ); |
---|
442 | content += cells + "\n"; |
---|
443 | |
---|
444 | content += '</tr>' + "\n"; |
---|
445 | content += '</tbody>' + "\n"; |
---|
446 | } |
---|
447 | content += '</table>' + "\n"; |
---|
448 | content += '</div>' + "\n"; |
---|
449 | |
---|
450 | $( '.' + type, analysis_result ) |
---|
451 | .remove(); |
---|
452 | |
---|
453 | analysis_result |
---|
454 | .append( content ); |
---|
455 | |
---|
456 | var analysis_result_type = $( '.' + type, analysis_result ); |
---|
457 | |
---|
458 | for( var i = 0; i < analysis_data[type].length; i += 2 ) |
---|
459 | { |
---|
460 | for( var j = 0; j < analysis_data[type][i+1].length; j += 1 ) |
---|
461 | { |
---|
462 | var pos = analysis_data[type][i+1][j].positionHistory |
---|
463 | ? analysis_data[type][i+1][j].positionHistory[0] |
---|
464 | : 1; |
---|
465 | var selector = 'tr.step:eq(' + ( i / 2 ) +') ' |
---|
466 | + 'td.data:eq(' + ( pos - 1 ) + ') ' |
---|
467 | + '.holder'; |
---|
468 | var cell = $( selector, analysis_result_type ); |
---|
469 | |
---|
470 | cell.parent() |
---|
471 | .removeClass( 'spacer' ); |
---|
472 | |
---|
473 | var table = $( 'table tr.details', cell ); |
---|
474 | if( 0 === table.size() ) |
---|
475 | { |
---|
476 | cell |
---|
477 | .html |
---|
478 | ( |
---|
479 | '<table border="0" cellspacing="0" cellpadding="0">' + |
---|
480 | '<tr class="details"></tr></table>' |
---|
481 | ); |
---|
482 | var table = $( 'table tr.details', cell ); |
---|
483 | } |
---|
484 | |
---|
485 | var tokens = []; |
---|
486 | for( var key in analysis_data[type][i+1][j] ) |
---|
487 | { |
---|
488 | var short_key = key.split( '#' ).pop(); |
---|
489 | |
---|
490 | if( 'positionHistory' === short_key || 'match' === short_key ) |
---|
491 | { |
---|
492 | continue; |
---|
493 | } |
---|
494 | |
---|
495 | var classes = []; |
---|
496 | classes.push( generate_class_name( short_key ) ); |
---|
497 | |
---|
498 | var data = analysis_data[type][i+1][j][key]; |
---|
499 | if( 'object' === typeof data && data instanceof Array ) |
---|
500 | { |
---|
501 | data = data.join( ' ' ); |
---|
502 | } |
---|
503 | if( 'string' === typeof data ) |
---|
504 | { |
---|
505 | data = data.esc(); |
---|
506 | } |
---|
507 | |
---|
508 | if( null === data || 0 === data.length ) |
---|
509 | { |
---|
510 | classes.push( 'empty' ); |
---|
511 | data = '∅'; |
---|
512 | } |
---|
513 | |
---|
514 | if( analysis_data[type][i+1][j].match && |
---|
515 | ( 'text' === short_key || 'raw_bytes' === short_key ) ) |
---|
516 | { |
---|
517 | classes.push( 'match' ); |
---|
518 | } |
---|
519 | |
---|
520 | tokens.push |
---|
521 | ( |
---|
522 | '<tr class="' + classes.join( ' ' ) + '">' + |
---|
523 | '<td>' + data + '</td>' + |
---|
524 | '</tr>' |
---|
525 | ); |
---|
526 | } |
---|
527 | table |
---|
528 | .append |
---|
529 | ( |
---|
530 | '<td class="details">' + |
---|
531 | '<table border="0" cellspacing="0" cellpadding="0">' + |
---|
532 | tokens.join( "\n" ) + |
---|
533 | '</table></td>' |
---|
534 | ); |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | } |
---|
543 | ); |
---|
544 | } |
---|
545 | ); |
---|