forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
376 lines (333 loc) · 12.4 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
Copyright 2014 Red Hat, Inc.
This software is licensed to you under the GNU General Public
License as published by the Free Software Foundation; either version
2 of the License (GPLv2) or (at your option) any later version.
There is NO WARRANTY for this software, express or implied,
including the implied warranties of MERCHANTABILITY,
NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
have received a copy of GPLv2 along with this software; if not, see
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*/
KT.favorite = function(form, input) {
var success = function(data) {
form.find(".qdropdown").html(data);
// process the elements of the list and truncate any that are too long with ellipsis (...) (e.g. jquery.text-overflow.js)
$(".one-line-ellipsis").trunk8({lines: 1});
},
error = function(data) {
},
save = function(event) {
// we want to submit the request using Ajax (prevent page refresh)
event.preventDefault();
var newFavorite = form.find('input').attr('value');
var url = $(this).attr('data-url');
// send a request to the server to save/create this favorite
$.ajax({
type: "POST",
url: url,
data: {"favorite": newFavorite},
cache: false,
success: success,
error: error
});
},
destroy = function (data) {
var id = $(this).attr('data-id');
var url = $(this).attr('data-url');
// send a request to the server to save/create this favorite
client_common.destroy(url, success, error);
},
clear = function(data){
var search_input = form.find('input');
search_input.val('');
search_input.change();
form.find('.qdropdown').hide();
};
return {
save: save,
destroy: destroy,
clear: clear
};
};
/**
*
* @param input_id id of the search input
* @param list_id id of the list
* @param list_module module that provides the following api:
* replace_list(html, args) //insert a new list
* append(html) //append new data to the list
* update_counts(current, total, results, clear) //update counts
* full_spinner() //clear list and show full list spinner (for full list reload/search)
* current_count() //returns # of items in list (used for offset)
*
* @param params list of options:
* disable_fancy - disable fancy queries
*
* @param extra_params list of extra parameters to pass along with search
*/
KT.search = function(form_id, list_id, list_module, params, extra_params){
var form = $('#' + form_id),
input = form.find('input'),
list_elem = $('#' + list_id),
url = params.url,
search_key = "_search",
search_hash = list_id + search_key,
event_name = list_id + ".search",
extend_event_name = list_id + ".search.extend",
url_param = "search",
favorite = KT.favorite(form, input),
current_search = {},
retrievingNewContent = false,
trigger_name = params.trigger || "hashchange", //custom event to trigger search
init = function(){
$(window).scroll(extend);
$(window).bind(trigger_name, hash_change);
if (!params.disable_fancy) {
form.fancyQueries();
}
setupSearch();
//TODO make these classes
form.delegate('#search_favorite_save', "click", favorite.save);
form.delegate('#search_favorite_destroy', "click", favorite.destroy);
form.delegate('#search_clear', "click", favorite.clear);
form.delegate('.search_query', 'click', function(){
input.val($(this).html());
form.find('button').click();
form.find('.qdropdown').hide();
});
reset_current_search();
},
search_bbq = function(){
return search_hash;
},
search_event = function(){
return event_name;
},
extend_event = function(){
return extend_event_name;
},
hash_change = function(event, args){
if (url){
start_search(args);
}
},
set_url = function(url_in){
if (url !== url_in){
reset_current_search();
url = url_in;
}
},
reset_current_search = function(){
current_search = {};
current_search[search_hash] = null;
},
refresh_search = function() {
reset_current_search();
start_search({});
},
setupSearch = function() {
var button = form.find('button');
button.click(function(){
var value = input.val();
if( button.attr('disabled') !== "disabled" ){
if( value === "" ){
$.bbq.removeState(search_hash);
} else {
var obj = {};
obj[search_hash] = value;
$.bbq.pushState(obj);
}
}
});
input.live('change', function(){
if( $(this).val() === "" ){
$.bbq.removeState(search_hash);
}
}).live('keypress', function(event){
if( event.keyCode === 13 ){
event.preventDefault();
button.click();
return false;
}
});
form.live('submit', function(e){
e.preventDefault();
start_search();
});
},
start_search = function(args){
var button = form.find('button'),
data = get_params(),
search_val = $.bbq.getState(search_hash),
event = $.Deferred(),
pre_state,
params_changed = function(){
var changed = KT.utils.all(current_search, function(item, index){
return item === $.bbq.getState(index);
});
return !changed;
};
if (params.pre_search_state){
pre_state = params.pre_search_state();
}
//search already in process, or already searched for
if ( !params_changed() ){
$(document).trigger(event_name);
return;
}
current_search[search_hash] = search_val;
input.val(search_val);
$(document).trigger(event_name, [event.promise()]);
list_module.full_spinner();
button.attr("disabled", "disabled");
if(search_val) {
data[url_param] = search_val;
}
if (extra_params) {
$.each(extra_params, function(index, item){
var item_id = item['hash_id'];
data[item_id] = $.bbq.getState(item_id);
current_search[item_id] = $.bbq.getState(item_id);
});
}
$.ajax({
url: url,
data: data,
cache: false,
success: function (data) {
var to_append = data.html ? data.html : data;
list_module.replace_list(to_append, pre_state);
button.removeAttr('disabled');
list_module.update_counts(data['current_items'], data['total_items'], data['results_count'], true, pre_state);
$('.ui-autocomplete').hide();
list_elem.addClass("ajaxScroll");
event.resolve();
},
error: function (e) {
button.removeAttr('disabled');
event.fail();
}
});
},
get_params = function(){
return KT.panel.queryParameters();
},
extend = function(){
var offset = list_module.current_count(),
page_size = list_elem.attr("data-page_size"),
search,
pre_state = params.pre_search_state ? params.pre_search_state() : undefined,
ajax_params = {
"offset": offset
},
expand_list = list_elem.hasClass("expand_list") ? list_elem : list_elem.find(".expand_list");
if ($.bbq) {
search = $.bbq.getState(search_hash);
}
if (!url) {
return;
}
if (list_elem.hasClass("ajaxScroll") && !retrievingNewContent && KT.common.scrollTop() >= ($(document).height() - $(window).height()) - 700) {
retrievingNewContent = true;
if (parseInt(page_size, 10) > parseInt(offset, 10)) {
return; //If we have fewer items than the pagesize, don't try to fetch anything else
}
$.extend(ajax_params, get_params());
if (search) {
$.extend(ajax_params, {search:search});
}
expand_list.append('<div class="list-spinner"> <img src="' + KT.common.spinner_path() + '" class="ajax_scroll"> </div>');
$.ajax({
type: "GET",
url: url,
data: ajax_params,
cache: false,
success: function (data) {
retrievingNewContent = false;
expand_list.find('.list-spinner').remove();
list_module.append(data['html'], pre_state);
if (data['current_items'] + offset >= data["total_items"]) {
list_elem.removeClass("ajaxScroll");
}
list_module.update_counts(data['current_items'], 0, 0);
$(window).trigger(extend_event_name);
},
error: function () {
expand_list.find('.list-spinner').remove();
retrievingNewContent = false;
}
});
}
},
enableAutoComplete = function(params){
var url = params['url'],
data = params['data'],
request_issued = false,
getAutoCompleteData;
if(url) {
getAutoCompleteData = function(request, response){
if( !request_issued ){
request_issued = true;
$.getJSON(url, { search : request.term },
function(json){
request_issued = false;
response(json);
})
.error(function(){
request_issued = false;
});
}
};
}
else {
getAutoCompleteData = data;
}
autocomplete_override();
input.catcomplete({
source : getAutoCompleteData,
minLength: 0,
delay : 200,
search : function(event, ui) { $(".auto_complete_clear").hide(); },
open : function(event, ui) { $(".auto_complete_clear").show(); }
});
input.focus(function( event ) {
if( $( this )[0].value === "" ) {
$( this ).catcomplete( "search" );
}
});
},
autocomplete_override = function(){
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category !== undefined && item.category !== currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
currentCategory = item.category;
}
if ( item.error !== undefined ) {
ul.append( "<li class='ui-autocomplete-error'>" + item.error + "</li>" );
}
if( item.completed !== undefined ) {
$( "<li></li>" ).data( "item.autocomplete", item )
.append( "<a>" + "<strong class='ui-autocomplete-completed'>" + item.completed + "</strong>" + item.part + "</a>" )
.appendTo( ul );
} else {
self._renderItem( ul, item );
}
});
}
});
};
init();
return {
enableAutoComplete : enableAutoComplete,
search_event : search_event,
extend_event : extend_event,
search_bbq : search_bbq,
set_url : set_url,
refresh_search : refresh_search
};
};