|
1 | 1 | .container |
2 | 2 | .content |
3 | 3 | .wrapper |
4 | | - .proper-content |
| 4 | + .proper-content |
5 | 5 | %form{ :action => "#", :id => "grok-form" } |
6 | | - %textarea{ :type => "text", :class => "span12", :placeholder => "Input", :id => "input" } |
| 6 | + %textarea{ :type => "text", :class => "span12", :placeholder => "Input", :id => "input", :rows => "5" } |
7 | 7 | .ui-widget |
8 | | - %textarea{ :type => "text", :class => "span12", :placeholder => "Pattern", :id => "tags" } |
9 | | - %label{ :class => "checkbox inline",} |
| 8 | + %textarea{ :type => "text", :class => "span12", :placeholder => "Pattern", :id => "tags", :rows => "5" } |
| 9 | + %label{ :for => "add_custom_patterns", :class => "checkbox inline",} |
10 | 10 | %input{ :id => "add_custom_patterns", :type => "checkbox", :value= => "" } |
11 | 11 | Add custom patterns |
12 | | - %label{ :class => "checkbox inline",} |
| 12 | + %label{ :for => "keep_empty_captures", :class => "checkbox inline",} |
13 | 13 | %input{ :id => "keep_empty_captures", :type => "checkbox", :value= => "" } |
14 | 14 | Keep Empty Captures |
15 | | - %label{ :class => "checkbox inline" } |
| 15 | + %label{ :for => "named_captures_only", :class => "checkbox inline" } |
16 | 16 | %input{ :id => "named_captures_only", :type => "checkbox", :value => "" } |
17 | 17 | Named Captures Only |
18 | | - %label{ :class => "checkbox inline" } |
| 18 | + %label{ :for => "singles", :class => "checkbox inline" } |
19 | 19 | %input{ :id => "singles", :type => "checkbox", :value => "" } |
20 | 20 | Singles |
21 | | - %label{ :class => "checkbox inline pull-right" } |
22 | | - %input{ :id => "autocomplete", :type => "checkbox", :value => "" } |
23 | | - Autocomplete |
| 21 | + %span{ :class => "pull-right" } |
| 22 | + %label{ :for => "autocomplete", :class => "checkbox inline" } |
| 23 | + %input{ :id => "autocomplete", :type => "checkbox", :value => "" } |
| 24 | + Autocomplete |
| 25 | + |
| 26 | + %input{ :type => "submit", :class => "inline btn btn-primary", :value => "Go"} |
24 | 27 | %div{:id => "custom_patterns_container", :style=>"display:none;"} |
25 | 28 | %p |
26 | 29 | One per line, the syntax for a grok pattern is <code>%{SYNTAX:SEMANTIC}</code> |
27 | | - %textarea{ :class => "span12", :placeholder => "Custom patterns", :id => "custom_patterns", } |
| 30 | + %textarea{ :class => "span12", :placeholder => "Custom patterns", :id => "custom_patterns", :rows => "5" } |
28 | 31 | %div{ :class => "well" } |
29 | 32 | %pre{ :id => "grok" } |
30 | 33 | .push |
|
36 | 39 | %script{ :type => "text/javascript", :src => "//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js" } |
37 | 40 | %script{ :type => "text/javascript", :src => "//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js" } |
38 | 41 | :javascript |
| 42 | + // function to load settings from localstorage |
| 43 | + var loadLocalstorage = function() { |
| 44 | + $('#input').val(localStorage.getItem('input')); |
| 45 | + $('#tags').val(localStorage.getItem('pattern')); |
| 46 | + $('#add_custom_patterns').prop('checked', parseInt(localStorage.getItem('add_custom_patterns'))); |
| 47 | + $('#named_captures_only').prop('checked', parseInt(localStorage.getItem('named_captures_only'))); |
| 48 | + $('#keep_empty_captures').prop('checked', parseInt(localStorage.getItem('keep_empty_captures'))); |
| 49 | + $('#singles').prop('checked', parseInt(localStorage.getItem('singles'))); |
| 50 | + $('#autocomplete').prop('checked', parseInt(localStorage.getItem('autocomplete'))); |
| 51 | + |
| 52 | + // show custom pattern window and fill valueif needed |
| 53 | + if ($('#add_custom_patterns').is(':checked')) { |
| 54 | + $('#custom_patterns').val(localStorage.getItem('custom_patterns')); |
| 55 | + $('#custom_patterns_container').show(); |
| 56 | + } |
| 57 | + }; |
| 58 | + // function to save settings to localstorage |
| 59 | + var saveLocalstorage = function(custom_patterns, input, pattern, add_custom_patterns, named_captures_only, keep_empty_captures, singles, autocomplete) { |
| 60 | + localStorage.setItem('custom_patterns', custom_patterns); |
| 61 | + localStorage.setItem('input', input); |
| 62 | + localStorage.setItem('pattern', pattern); |
| 63 | + localStorage.setItem('add_custom_patterns', (add_custom_patterns == true ? 1 : 0)); |
| 64 | + localStorage.setItem('named_captures_only', (named_captures_only == true ? 1 : 0)); |
| 65 | + localStorage.setItem('keep_empty_captures', (keep_empty_captures == true ? 1 : 0)); |
| 66 | + localStorage.setItem('singles', (singles == true ? 1 : 0)); |
| 67 | + localStorage.setItem('autocomplete', (autocomplete == true ? 1 : 0)); |
| 68 | + }; |
| 69 | + |
| 70 | + // function to execute AJAX matching |
39 | 71 | var match = function() { |
40 | | - var custom_patterns = $('#custom_patterns').val(); |
| 72 | + var custom_patterns = $('#custom_patterns').val(); |
41 | 73 | var input = $('#input').val(); |
42 | 74 | var pattern = $('#tags').val(); |
| 75 | + var add_custom_patterns = $('#add_custom_patterns').is(':checked'); |
43 | 76 | var named_captures_only = $('#named_captures_only').is(':checked'); |
44 | 77 | var keep_empty_captures = $('#keep_empty_captures').is(':checked'); |
45 | 78 | var singles = $('#singles').is(':checked'); |
46 | | - |
| 79 | + var autocomplete = $('#autocomplete').is(':checked'); |
| 80 | + |
| 81 | + // Save the user's settings to localstorage for next time |
| 82 | + saveLocalstorage(custom_patterns, input, pattern, add_custom_patterns, named_captures_only, keep_empty_captures, singles, autocomplete); |
| 83 | + |
| 84 | + // Display a loading indicator |
| 85 | + $('#grok').parent().prepend("<i id='grokSpinner' class='icon icon-refresh icon-spin pull-right'></i>"); |
| 86 | + |
| 87 | + // Get the results from Grok |
47 | 88 | $.post('/grok', |
48 | 89 | { |
49 | | - "custom_patterns":custom_patterns, |
| 90 | + "custom_patterns":custom_patterns, |
50 | 91 | "input":input, |
51 | 92 | "pattern":pattern, |
52 | 93 | "named_captures_only":named_captures_only, |
53 | 94 | "keep_empty_captures":keep_empty_captures, |
54 | 95 | "singles":singles |
55 | 96 | }, function(data){ |
56 | | - $('#grok').html(data); |
57 | | - }); |
| 97 | + // Display the results to the user |
| 98 | + $('#grok').html(data); |
| 99 | + |
| 100 | + // Remove the loading indicator after a short time |
| 101 | + setTimeout(function(){$('#grokSpinner').remove();}, 500); |
| 102 | + } |
| 103 | + ); |
58 | 104 | }; |
| 105 | + |
| 106 | + // load settings from localstorage |
| 107 | + loadLocalstorage(); |
| 108 | + |
| 109 | + // go ahead and match just in case we have something already |
| 110 | + match(); |
| 111 | + |
| 112 | + // call match() if pattern, input, or custom patterns change |
59 | 113 | var oldPat = $("#tags").val(); |
60 | 114 | var oldIn = $("#input").val(); |
| 115 | + var oldCust = $("#custom_patterns").val(); |
61 | 116 | setInterval(function (){ |
62 | 117 | pat = $("#tags").val(); |
63 | 118 | inp = $("#input").val(); |
64 | | - if(inp == oldIn && pat == oldPat){ |
| 119 | + cust = $("#custom_patterns").val(); |
| 120 | + if(inp == oldIn && pat == oldPat && cust == oldCust){ |
65 | 121 | return; |
66 | 122 | } else { |
67 | 123 | oldPat = pat; |
68 | 124 | oldIn = inp; |
| 125 | + oldCust = cust; |
69 | 126 | match(); |
70 | 127 | } |
71 | 128 | }, 1000); |
72 | | - $("#add_custom_patterns").click(function(){ |
73 | | - $( "#custom_patterns_container" ).toggle(function() { |
74 | | - $( "#custom_patterns" ).val("") |
75 | | - }) |
76 | | - }) |
| 129 | + |
| 130 | + // also match if various buttons are clicked |
77 | 131 | $("#named_captures_only").click(function(){ |
78 | | - match(); |
| 132 | + match(); |
79 | 133 | }) |
80 | 134 | $("#keep_empty_captures").click(function(){ |
81 | | - match(); |
| 135 | + match(); |
82 | 136 | }) |
83 | 137 | $("#singles").click(function(){ |
84 | 138 | match(); |
85 | 139 | }) |
| 140 | + $("#grok-form").submit(function(){ |
| 141 | + match(); |
| 142 | + return false; |
| 143 | + }); |
| 144 | + |
| 145 | + // show/hide custom patterns container, and remove patterns |
| 146 | + $("#add_custom_patterns").click(function(){ |
| 147 | + $( "#custom_patterns_container" ).toggle(function() { |
| 148 | + $( "#custom_patterns" ).val("") |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + |
| 153 | + // autocomplete functionality |
86 | 154 | $("#autocomplete").click(function(){ |
87 | 155 | var autocomplete = $('#autocomplete').is(':checked'); |
88 | 156 | $( "#tags" ).autocomplete({ disabled: !autocomplete }); |
89 | 157 | }); |
90 | | - $("#grok-form").submit(match); |
91 | 158 |
|
92 | 159 | var availableTags = #{@tags}; |
93 | 160 |
|
94 | 161 | function split( val ) { |
95 | | - return val.split( /}\s*/ ); |
| 162 | + return val.split( /}\s*/ ); |
96 | 163 | } |
97 | 164 | function extractLast( term ) { |
98 | | - return split( term ).pop(); |
| 165 | + return split( term ).pop(); |
99 | 166 | } |
100 | 167 |
|
101 | 168 | $( "#tags" ) |
102 | | - // don't navigate away from the field on tab when selecting an item |
103 | | - .bind( "keydown", function( event ) { |
104 | | - if ( event.keyCode === $.ui.keyCode.TAB && |
105 | | - $( this ).data( "autocomplete" ).menu.active ) { |
106 | | - event.preventDefault(); |
107 | | - } |
108 | | - }) |
109 | | - .autocomplete({ |
110 | | - minLength: 3, |
111 | | - disabled: true, |
112 | | - source: function( request, response ) { |
113 | | - $.each($( "#custom_patterns" ).val().split('\n'), function(){ |
| 169 | + // don't navigate away from the field on tab when selecting an item |
| 170 | + .bind( "keydown", function( event ) { |
| 171 | + if ( event.keyCode === $.ui.keyCode.TAB && |
| 172 | + $( this ).data( "autocomplete" ).menu.active ) { |
| 173 | + event.preventDefault(); |
| 174 | + } |
| 175 | + }) |
| 176 | + .autocomplete({ |
| 177 | + minLength: 3, |
| 178 | + disabled: true, |
| 179 | + source: function( request, response ) { |
| 180 | + $.each($( "#custom_patterns" ).val().split('\n'), function(){ |
114 | 181 | var line_splitted = this.replace(/^\s*/, "").split(/\s+/) |
115 | 182 | var name = line_splitted[0] |
116 | 183 | if ( $.inArray(name, availableTags) == -1 ) { |
117 | 184 | availableTags.push("%{" + name) |
118 | 185 | } |
119 | 186 | }); |
120 | 187 |
|
121 | | - // delegate back to autocomplete, but extract the last term |
122 | | - response( $.ui.autocomplete.filter( |
123 | | - availableTags, extractLast( request.term ) ) ); |
124 | | - }, |
125 | | - focus: function() { |
126 | | - // prevent value inserted on focus |
127 | | - return false; |
128 | | - }, |
129 | | - select: function( event, ui ) { |
130 | | - var terms = split( this.value ); |
131 | | - // remove the current input |
132 | | - terms.pop(); |
133 | | - // add the selected item |
134 | | - terms.push( ui.item.value ); |
135 | | - // add placeholder to get the comma-and-space at the end |
136 | | - terms.push( "" ); |
137 | | - this.value = terms.join( "}" ); |
138 | | - return false; |
139 | | - } |
140 | | - }) |
| 188 | + // delegate back to autocomplete, but extract the last term |
| 189 | + response( $.ui.autocomplete.filter( |
| 190 | + availableTags, extractLast( request.term ) ) ); |
| 191 | + }, |
| 192 | + focus: function() { |
| 193 | + // prevent value inserted on focus |
| 194 | + return false; |
| 195 | + }, |
| 196 | + select: function( event, ui ) { |
| 197 | + var terms = split( this.value ); |
| 198 | + // remove the current input |
| 199 | + terms.pop(); |
| 200 | + // add the selected item |
| 201 | + terms.push( ui.item.value ); |
| 202 | + // add placeholder to get the comma-and-space at the end |
| 203 | + terms.push( "" ); |
| 204 | + this.value = terms.join( "}" ); |
| 205 | + return false; |
| 206 | + } |
| 207 | + }) |
0 commit comments