Removed dep on API
[ninja.git] / application / views / saved_searches.php
blob4e9e082eb0fdb2d9bdb6be230ebd28facb34b383
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 if (isset($searches) && !empty($searches)) { ?>
3 <script>
4 $(function() {
5 $('<span id="my_saved_searches" style="padding: 4px; vertical-align: text-bottom; cursor: pointer;"><img id="my_saved_searches_img" title="Click to view your saved searches" src="/monitor/application/views/icons/16x16/save_search.png" /></span>').insertBefore('#query');
7 var name = $("#search_name"),
8 query = $('#query'),
9 allFields = $([]).add(name).add(query),
10 tips = $(".validateTips"),
11 description = $('#search_description');
14 function check_save_search_fields(o, n, min, max)
16 if ( o.val.length > max || o.val().length < min ) {
17 o.addClass("ui-state-error");
18 updateTips(_search_save_error, n, min, max);
19 return false;
20 } else {
21 return true;
25 function updateTips(t, n, min, max) {
26 tips.text( sprintf(t, n, min, max) ).addClass( "ui-state-highlight" );
27 setTimeout(function() {
28 tips.removeClass( "ui-state-highlight", 1500 );
29 }, 500 );
32 $( "#save-search-form" ).dialog({
33 autoOpen: false,
34 height: 330,
35 width: 350,
36 closeOnEscape: true,
37 modal: true,
38 buttons: {
39 Cancel: function() {
40 $(this).dialog( "close" );
42 "Save this search": function() {
43 var bValid = true;
44 allFields.removeClass( "ui-state-error" );
45 bValid = bValid && check_save_search_fields(query, _search_string_field, 1, 100);
46 bValid = bValid && check_save_search_fields(name, _search_name_field, 1, 100);
48 if (!bValid) {
49 $(this).dialog("close");
50 return;
52 // save form to db
53 $.ajax({
54 url:_site_domain + _index_page + '/' + '/ajax/save_search',
55 type: 'POST',
56 data: {name: name.val(), query: query.val(), description: description.val(), search_id: $('#search_id').val()},
57 success: function(data) {
58 data = parseInt(data, 10);
59 if (isNaN(data)) { // return value should be an integer if OK
60 jgrowl_message(_search_saved_error, _search_save_error);
61 return;
63 jgrowl_message(_search_saved_ok, _search_save_ok);
65 // update/edit
66 if ($('#search_id').val() !== 0 && $('#saved_searchrow_' + $('#search_id').val())) {
67 // update list of saved searches
68 $('#searchname_' + $('#search_id').val()).html(name.val());
69 $('#searchquery_' + $('#search_id').val()).html('<a href="' + _site_domain + _index_page + '/' + 'search/lookup?query=' + query.val() + '">' + query.val() + '</a>');
70 $('#searchqueryimg_' + $('#search_id').val()).html('<a href="' + _site_domain + _index_page + '/' + 'search/lookup?query=' + query.val() + '"><img src="' + _site_domain + 'icons/16x16/use_search.png" /></a>');
71 $('#searchdescription_' + $('#search_id').val()).html(description.val());
73 } else if($('#search_id').val() === 0) {
74 var previously_saved_searches_for_same_query = $('#saved_searches_table td[id^=searchquery_]:contains("'+query.val()+'")');
75 if(previously_saved_searches_for_same_query.length) {
76 previously_saved_searches_for_same_query.parent('tr').remove();
78 // created new search - add rows
79 var new_data = '<td class="edit_search_query" id="searchquery_' + data + '"><a href="' + _site_domain + _index_page + '/' + 'search/lookup?query=' + query.val() + '">' + query.val() + '</a></td>';
80 new_data += '<td class="edit_search_name" id="searchname_' + data + '">' + name.val() + '</td>';
81 new_data += '<td class="edit_search_description" id="searchdescription_' + data + '">' + description.val() + '</td>';
82 new_data += '<td id="searchqueryimg_' + data + '"><a href="' + _site_domain + _index_page + '/' + 'search/lookup?query=' + query.val() + '"><img src="' + _site_domain + 'icons/16x16/use_search.png" /></a></td>';
83 new_data += '<td class="edit_search_item" id="editsearch_' + data + '"><img style="cursor:pointer" src="' + _site_domain + 'icons/16x16/edit.png" id="editsearchimg_' + data + '" /></td>';
84 new_data += '<td class="remove_search_item" id="removesearch_' + data + '"><img style="cursor:pointer" src="' + _site_domain + 'icons/16x16/remove.png" id="removesearchimg_' + data + '" /></td>';
85 $('#saved_searches_table').append('<tr id="saved_searchrow_' + data + '">' + new_data + '</tr>');
86 if (!$('#my_saved_searches').is(':visible')) {
87 $('#my_saved_searches').show();
90 $('#search_id').attr('value', data);
93 });
94 $(this).dialog("close");
97 });
99 $('.edit_search_item').css('cursor', 'pointer');
100 $('.remove_search_item').css('cursor', 'pointer');
101 $('#save_search').css('cursor', 'pointer');
103 $('#save_search').click(function() {
104 $( "#save-search-form" ).dialog("open");
106 // reset form
107 var old_query = query.val(); // stash current query
108 allFields.val('');
109 query.val(old_query);
110 $('#search_id').val(0);
111 description.val('');
113 // ... unless there's already a saved search for the same query
114 $.ajax(
115 _site_domain + _index_page + '/ajax/fetch_saved_search_by_query',
117 data: {
118 query: old_query
120 error: function(data) {
121 jgrowl_message(data.responseText);
122 $('#search_query').attr('value', old_query);
124 complete: function(data) {
125 // set fetched values to edit dialog
126 $('#search_name').attr('value', data['search_name']);
127 $('#search_description').attr('value', data['search_description']);
128 $('#search_id').attr('value', data['search_id']);
130 dataType: 'json'
135 // hide/show layer with saved searches
136 $('#my_saved_searches').click(function() {
137 // try to position the dialog box
138 if ($( "#saved_searches_table" ).is(':visible')) {
139 $('#saved_searches_table').dialog('close');
140 } else {
141 $( "#saved_searches_table" ).dialog( "open" );
145 $("#saved_searches_table").dialog({
146 dialogClass: 'saved_searches',
147 autoOpen: false,
148 height: 'auto',
149 width: 'auto',
150 modal: false,
151 buttons: {
152 Close: function() {
153 $(this).dialog( "close" );
158 // handle edit click for saved searches
159 $('.edit_search_item').live('click', function() {
160 var the_id = $(this).attr('id');
161 the_id = the_id.replace('editsearch_', '');
162 var original_img_src = $('#editsearchimg_' + the_id).attr('src');
163 switch_image('editsearchimg_' + the_id, loadimg_sml.src);
165 $.ajax({
166 url:_site_domain + _index_page + '/' + '/ajax/fetch_saved_search',
167 type: 'POST',
168 data: {search_id: the_id},
169 success: function(data) {
170 if (data == 'Error') {
171 jgrowl_message(_search_saved_error, _search_save_error);
172 } else {
173 data = eval( "(" + data + ")" );
175 // set fetched values to edit dialog
176 $('#saved_searches_table').dialog('close');
177 $( "#save-search-form" ).dialog('open');
178 $('#search_name').attr('value', data['search_name']);
179 $('#search_query').attr('value', data['search_query']);
180 $('#search_description').attr('value', data['search_description']);
181 $('#search_id').attr('value', data['search_id']);
186 // restore original image with a timeout
187 setTimeout(function() {switch_image('editsearchimg_' + the_id, original_img_src);}, 3000);
190 // handle remove click for saved searches
191 $('.remove_search_item').live('click', function() {
192 if (!confirm(_search_remove_confirm)) {
193 return false;
196 var the_id = $(this).attr('id');
197 the_id = the_id.replace('removesearch_', '');
198 var original_img_src = $('#removesearchimg_' + the_id).attr('src');
199 switch_image('removesearchimg_' + the_id, loadimg_sml.src);
201 $.ajax({
202 url:_site_domain + _index_page + '/' + '/ajax/remove_search',
203 type: 'POST',
204 data: {search_id: the_id},
205 success: function(data) {
206 if (data == 'OK') {
207 // remove row
208 $('#saved_searchrow_' + the_id).remove();
209 if ($('#saved_searches_table tr').length == 1) {
210 $('#saved_searches_table').dialog('close');
211 $('#my_saved_searches').hide();
217 // restore original image with a timeout
218 setTimeout(function() {switch_image('removesearchimg_' + the_id, original_img_src);}, 3000);
221 </script>
222 <div id="save-search-form" title="<?php echo _('Save search') ?>" style="display:none">
223 <form>
224 <p class="validateTips"></p>
225 <fieldset>
226 <label for="search_query"><?php echo _('Search string') ?></label>
227 <input type="text" name="search_query" id="search_query" value="<?php echo isset($query_str) ? $query_str : '' ?>" class="texts search_query ui-widget-content ui-corner-all" />
228 <label for="search_name"><?php echo _('Name') ?></label>
229 <input type="text" name="search_name" id="search_name" class="texts ui-widget-content ui-corner-all" />
230 <label for="search_description"><?php echo _('Description') ?></label>
231 <textarea cols="30" rows="3" name="search_description" id="search_description" class="texts ui-widget-content ui-corner-all"></textarea>
232 <input type="hidden" name="search_id" id="search_id" value="0">
233 </fieldset>
234 </form>
235 </div>
237 <table id="saved_searches_table" title="<?php echo _('Saved searches') ?>" style="display:none">
238 <tr style="height:20px">
239 <td><strong><?php echo _('Search string') ?></strong></td>
240 <td><strong><?php echo _('Name') ?></strong></td>
241 <td><strong><?php echo _('Description') ?></strong></td>
242 <td colspan="3"></td>
243 </tr>
244 <?php foreach ($searches as $s) { ?>
245 <tr id="saved_searchrow_<?php echo $s->id ?>">
246 <td id="searchquery_<?php echo $s->id ?>"><?php echo html::anchor('search/lookup?query='.$s->search_query, $s->search_query, array('title' => _('Use this search'))) ?></td>
247 <td id="searchname_<?php echo $s->id ?>"><?php echo $s->search_name ?></td>
248 <td id="searchdescription_<?php echo $s->id ?>"><?php echo $s->search_description ?></td>
249 <td id="searchqueryimg_<?php echo $s->id ?>"><?php echo html::anchor('search/lookup?query='.$s->search_query, html::image($this->add_path('icons/16x16/use_search.png'), array('title' => _('Use this search'))) ) ?></td>
250 <td class="edit_search_item" id="editsearch_<?php echo $s->id ?>"><?php echo html::image($this->add_path('icons/16x16/edit.png'), array('title' => _('Edit this search'), 'id' => 'editsearchimg_'.$s->id)) ?></td>
251 <td class="remove_search_item" id="removesearch_<?php echo $s->id ?>"><?php echo html::image($this->add_path('icons/16x16/remove.png'), array('title' => _('Remove this search'), 'id' => 'removesearchimg_'.$s->id)) ?></td>
252 </tr>
253 <?php } ?>
255 </table>
256 <?php } ?>