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
|
var liveSearch = function(query_string) {
var query = $('input#keywords');
var query_val = function () {return $.trim(query.val());};
var prev_text = query_val();
var running = false;
var q_list_sel = 'question-list';//id of question listing div
var search_url = askbot['urls']['questions'];
var x_button = $('input[name=reset_query]');
var refresh_x_button = function(){
if(query_val().length > 0){
if (query.hasClass('searchInput')){
query.attr('class', 'searchInputCancelable');
x_button.show();
}
} else {
x_button.hide();
query.attr('class', 'searchInput');
}
};
var restart_query = function() {
sortMethod = 'activity-desc';
query.val('');
refresh_x_button();
send_query();
};
var eval_query = function(){
cur_query = query_val();
if (cur_query !== prev_text && running === false){
if (cur_query.length >= minSearchWordLength){
send_query(cur_query);
} else if (cur_query.length === 0){
restart_query();
}
}
};
var update_query_string = function(query_text){
if(query_text === undefined) { // handle missing parameter
query_text = query_val();
}
query_string = QSutils.patch_query_string(
query_string,
'query:' + encodeURIComponent(query_text),
query_text === '' // remove if empty
);
return query_text;
};
var send_query = function(query_text){
running = true;
if(!prev_text && query_text && showSortByRelevance) {
// If there was no query but there is some query now - and we support relevance search - then switch to it */
query_string = QSutils.patch_query_string(query_string, 'sort:relevance-desc');
}
prev_text = update_query_string(query_text);
query_string = QSutils.patch_query_string(query_string, 'page:1'); /* if something has changed, then reset the page no. */
var url = search_url + query_string;
$.ajax({
url: url,
dataType: 'json',
success: render_result,
complete: function(){
running = false;
eval_query();
},
cache: false
});
var context = { state:1, rand:Math.random() };
History.pushState( context, "Questions", url );
setTimeout(function (){
/* HACK: For some weird reson, sometimes something overrides the above pushState so we re-aplly it
This might be caused by some other JS plugin.
The delay of 10msec allows the other plugin to override the URL.
*/
History.replaceState( context, "Questions", url );
}, 10);
};
/* *********************************** */
var render_related_tags = function(tags, query_string){
if (tags.length === 0) return;
var html_list = [];
for (var i=0; i<tags.length; i++){
var tag = new Tag();
tag.setName(tags[i]['name']);
tag.setDeletable(false);
tag.setLinkable(true);
tag.setUrlParams(query_string);
html_list.push(tag.getElement().outerHTML());
html_list.push('<span class="tag-number">× ');
html_list.push(tags[i]['used_count']);
html_list.push('</span>');
html_list.push('<br />');
}
$('#related-tags').html(html_list.join(''));
};
var render_search_tags = function(tags, query_string){
var search_tags = $('#searchTags');
search_tags.empty();
if (tags.length === 0){
$('#listSearchTags').hide();
$('#search-tips').hide();//wrong - if there are search users
} else {
$('#listSearchTags').show();
$('#search-tips').show();
$.each(tags, function(idx, tag_name){
var tag = new Tag();
tag.setName(tag_name);
tag.setLinkable(false);
tag.setDeletable(true);
tag.setDeleteHandler(
function(){
remove_search_tag(tag_name, query_string);
}
);
search_tags.append(tag.getElement());
});
}
};
var create_relevance_tab = function(query_string){
relevance_tab = $('<a></a>');
href = search_url + QSutils.patch_query_string(query_string, 'sort:relevance-desc');
relevance_tab.attr('href', href);
relevance_tab.attr('id', 'by_relevance');
relevance_tab.html('<span>' + sortButtonData['relevance']['label'] + '</span>');
return relevance_tab;
};
/* *************************************** */
var remove_search_tag = function(tag){
query_string = QSutils.remove_search_tag(query_string, tag);
send_query();
};
var set_active_sort_tab = function(sort_method, query_string){
var tabs = $('#sort_tabs > a');
tabs.attr('class', 'off');
tabs.each(function(index, element){
var tab = $(element);
var tab_name = tab.attr('id').replace(/^by_/,'');
if (tab_name in sortButtonData){
href = search_url + QSutils.patch_query_string(query_string, 'sort:'+tab_name+'-desc');
tab.attr('href', href);
tab.attr('title', sortButtonData[tab_name]['desc_tooltip']);
tab.html(sortButtonData[tab_name]['label']);
}
});
var bits = sort_method.split('-', 2);
var name = bits[0];
var sense = bits[1];//sense of sort
var antisense = (sense == 'asc' ? 'desc':'asc');
var arrow = (sense == 'asc' ? ' ▲':' ▼');
var active_tab = $('#by_' + name);
active_tab.attr('class', 'on');
active_tab.attr('title', sortButtonData[name][antisense + '_tooltip']);
active_tab.html(sortButtonData[name]['label'] + arrow);
};
var render_relevance_sort_tab = function(query_string){
if (showSortByRelevance === false){
return;
}
var relevance_tab = $('#by_relevance');
if (prev_text && prev_text.length > 0){
if (relevance_tab.length == 0){
relevance_tab = create_relevance_tab(query_string);
$('#sort_tabs>span').after(relevance_tab);
}
}
else {
if (relevance_tab.length > 0){
relevance_tab.remove();
}
}
};
var render_result = function(data, text_status, xhr){
if (data['questions'].length > 0){
$('#pager').toggle(data['paginator'] !== '').html(data['paginator']);
$('#questionCount').html(data['question_counter']);
render_search_tags(data['query_data']['tags'], data['query_string']);
if(data['faces'].length > 0) {
$('#contrib-users > a').remove();
$('#contrib-users').append(data['faces'].join(''));
}
render_related_tags(data['related_tags'], data['query_string']);
render_relevance_sort_tab(data['query_string']);
set_active_sort_tab(data['query_data']['sort_order'], data['query_string']);
if(data['feed_url']){
// Change RSS URL
$("#ContentLeft a.rss:first").attr("href", data['feed_url']);
}
// Patch scope selectors
$('#scopeWrapper > a.scope-selector').each(function(index) {
var old_qs = $(this).attr('href').replace(search_url, '');
var scope = QSutils.get_query_string_selector_value(old_qs, 'scope');
qs = QSutils.patch_query_string(data['query_string'], 'scope:' + scope);
$(this).attr('href', search_url + qs);
});
// Patch "Ask your question"
var askButton = $('#askButton');
var askHrefBase = askButton.attr('href').split('?')[0];
askButton.attr('href', askHrefBase + data['query_data']['ask_query_string']); /* INFO: ask_query_string should already be URL-encoded! */
query.focus();
var old_list = $('#' + q_list_sel);
var new_list = $('<div></div>').hide().html(data['questions']);
old_list.stop(true).after(new_list).fadeOut(200, function() {
//show new div with a fadeIn effect
old_list.remove();
new_list.attr('id', q_list_sel);
new_list.fadeIn(400);
});
}
};
/* *********************************** */
// Wire search tags
var search_tags = $('#searchTags .tag-left');
$.each(search_tags, function(idx, element){
var tag = new Tag();
tag.decorate($(element));
//todo: setDeleteHandler and setHandler
//must work after decorate & must have getName
tag.setDeleteHandler(
function(){
remove_search_tag(tag.getName(), query_string);
}
);
});
// Wire X button
x_button.click(function () {
restart_query(); /* wrapped in closure because it's not yet defined at this point */
});
refresh_x_button();
// Wire query box
var main_page_eval_handle;
query.keyup(function(e){
refresh_x_button();
if (running === false){
clearTimeout(main_page_eval_handle);
main_page_eval_handle = setTimeout(eval_query, 400);
}
});
$("form#searchForm").submit(function(event) {
// if user clicks the button the s(h)e probably wants page reload,
// so provide that experience but first update the query string
event.preventDefault();
update_query_string();
window.location.href = search_url + query_string;
});
/* *********************************** */
// Hook for tag_selector.js
liveSearch.refresh = function () {
send_query();
};
};
|