jquery.gbsearch.js
11.8 KB
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
377
378
$(document).ready(function(){
var config = {
siteURL : 'gbtags.com', // Change this to your site
searchSite : true,
searchWeb : false,
searchTag : false,
searchGBTags : false,
type : 'web',
append : false,
perPage : 8, // A maximum of 8 is allowed by Google
page : 0 // The start page
};
// The small arrow that marks the active search icon:
var arrow = $('<span>',{className:'arrow'}).appendTo('ul.icons');
$('ul.icons li').click(function(){
var el = $(this);
if(el.hasClass('active')){
// The icon is already active, exit
return false;
}
el.siblings().removeClass('active');
el.addClass('active');
// Move the arrow below this icon
arrow.stop().animate({
left : el.position().left,
marginLeft : (el.width()/2)-4
});
// Set the search type
config.type = el.attr('data-searchType');
$('#more').hide();
});
// Adding the site domain as a label for the first radio button:
//$('#siteNameLabel').html(config.siteURL);
// Marking the Search gbin1 radio as active:
if(source === 'gbtags'){
$('#gbtagsNameLabel').addClass('selected');
$('#resultnum').show();
config.searchSite = false;
config.searchGBtags = true;
}else{
$('#siteNameLabel').addClass('selected');
}
// Marking the web search icon as active:
var contenttypepanel = $('.icons');
contenttypepanel.find('li');
$('li.web').click();
// Focusing the input text box:
$('#s').focus();
var canceltimetout = null,
confirmcancel = $('<button/>',{'id':'confirmcancel','text':'取消','style':'text-align:center;'});
var changeflag = false;
$('#searchGBTags,#searchTag,#searchSite,#searchWeb').change(function(){
changeflag = true;
clearTimeout(canceltimetout);
// Listening for a click on one of the radio buttons.
// config.searchSite is either true or false.
$(this).next().addClass('selected').siblings().removeClass('selected');
config.searchSite = this.id == 'searchSite';
config.searchWeb = this.id == 'searchWeb';
config.searchGBTags = this.id == 'searchGBTags';
config.searchTag = this.id == 'searchTag';
if(config.searchWeb||config.searchSite){
contenttypepanel.find('li').show();
googleSearch();
$('#resultnum').hide();
}else if(config.searchTag){
contenttypepanel.find('li').hide();
gbtagSearch();
$('#resultnum').hide();
}else{
contenttypepanel.find('li').hide();
gbtagsSearch();
$('#resultnum').show();
}
});
if(search!==''||search!=='undefine'){
$('#s').val(search);
if(config.searchSite||config.searchWeb){
googleSearch();
}else if(config.searchTag){
gbtagSearch();
}else{
gbtagsSearch();
}
}
$('#searchForm').submit(function(){
$('#resultsDiv').html('');
$('#more').remove();
if(config.searchSite||config.searchWeb){
googleSearch();
}else if(config.searchTag){
gbtagSearch();
}else{
gbtagsSearch();
}
return false;
});
function gbtagsSearch(settings){
var resultsDiv = $('#resultsDiv');
settings = $.extend({},config,settings);
settings.term = settings.term || $('#s').val();
var loading= $('<div class="loading"></div>');
if(changeflag){
resultsDiv.html('').append(loading);
}
$('#submitButton').attr('disabled', 'disabled');
if(settings.term === '') settings.term = '*';
var term = encodeURIComponent(settings.term);
$.getJSON('/gbsearch/gbtags/select?q=title:' + term + '&start=' + settings.perPage*settings.page + '&hl=true&hl.fl=title&hl.simple.pre=<em>&hl.simple.post=</em>&wt=json&indent=false', function(data) {
$('#numfound').html(data.response.numFound);
if(settings.perPage*settings.page<data.response.numFound){
var template = Handlebars.compile($("#gbtags-data-tmpl").html());
var highlight = data.highlighting,
rawresultlist = data.response.docs,
resultlist = [];
for(var i=0;i<rawresultlist.length;i++){
var id = rawresultlist[i].id,
title = rawresultlist[i].title;
description = rawresultlist[i].description;
for (var idstr in highlight) {
if(idstr === id){
if(highlight[idstr].title!==undefined){
title = highlight[idstr].title;
}
break;
}
}
resultlist.push({title:title,id:id,description:description});
}
var htmlcontent = template(resultlist);
$('#resultsDiv').append( htmlcontent );
$('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
changeflag = false;
gbtagsSearch({append:true,page:settings.page+1});
$(this).hide();
});
}else{
$('#resultsDiv').append( '<div style="text-align:center;">没有更多相关搜索结果</div>' );
$('#more').hide();
}
$('#submitButton').removeAttr('disabled');
loading.remove();
});
}
function gbtagSearch(settings){
var resultsDiv = $('#resultsDiv');
settings = $.extend({},config,settings);
settings.term = settings.term || $('#s').val();
var loading= $('<div class="loading"></div>');
if(changeflag){
resultsDiv.html('').append(loading);
}
$('#submitButton').attr('disabled', 'disabled');
identityService.findAllPostByTagName({tags:settings.term,start:(settings.page*settings.perPage),offset:settings.perPage},{
callback:function(data){
if(data.length>0){
var template = Handlebars.compile($("#data-tmpl").html());
var htmlcontent = template(data);
resultsDiv.append( htmlcontent );
$('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
changeflag = false;
gbtagSearch({append:true,page:settings.page+1});
$(this).hide();
});
}else{
if(settings.page===0){
var infop = $('<p>',{'class':'notFound','html':'社区标签没有相关结果 <div id="autosearchwrapper">(<span id="searchcounter">3</span>秒后将自动尝试"GBin1.com站内搜索")</div>'});
infop.append(confirmcancel).hide().appendTo(resultsDiv).show();
canceltimetout = setTimeout(function(){$('#searchSite').click();}, 3000);
var count = 2, countinterval = setInterval(function(){
infop.find('#searchcounter').html(count--);
if(count == 0){clearInterval(countinterval);}
}, 1000);
}
}
$('#submitButton').removeAttr('disabled');
loading.remove();
}
});
}
$('#resultsDiv').on('click','#confirmcancel', function(){
clearTimeout(canceltimetout);
$('#autosearchwrapper').hide();
confirmcancel.hide();
});
function googleSearch(settings){
// If no parameters are supplied to the function,
// it takes its defaults from the config object above:
settings = $.extend({},config,settings);
settings.term = settings.term || $('#s').val();
if(settings.searchSite){
// Using the Google site:example.com to limit the search to a
// specific domain:
settings.term = 'site:'+settings.siteURL+' '+settings.term;
}
// URL of Google's AJAX search API
var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?';
var resultsDiv = $('#resultsDiv');
var loading= $('<div class="loading"></div>');
resultsDiv.append(loading);
$('#submitButton').attr('disabled', 'disabled');
$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
var results = r.responseData.results;
$('#more').remove();
if(results.length){
// If results were returned, add them to a pageContainer div,
// after which append them to the #resultsDiv:
var pageContainer = $('<div>',{'class':'pageContainer'});
for(var i=0;i<results.length;i++){
// Creating a new result object and firing its toString method:
pageContainer.append(new result(results[i]) + '');
}
if(!settings.append){
// This is executed when running a new search,
// instead of clicking on the More button:
resultsDiv.empty();
}
pageContainer.append('<div class="clear"></div>')
.hide().appendTo(resultsDiv)
.fadeIn('slow');
var cursor = r.responseData.cursor;
// Checking if there are more pages with results,
// and deciding whether to show the More button:
if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
$('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
googleSearch({append:true,page:settings.page+1});
$(this).hide();
});
}
}
else {
resultsDiv.empty();
if($('#searchForm label.selected').text()==='Google.com'){
resultsDiv.html('<p class="notFound" style="display: block;">Google.com没有相关结果<p>');
return;
}
var infop = $('<p>',{'class':'notFound','html':'gbtags.com没有相关结果 <div id="autosearchwrapper">(<span id="searchcounter">3</span>秒后将自动尝试"谷歌搜索")</div>'});
infop.append(confirmcancel).hide().appendTo(resultsDiv).show();
canceltimetout = setTimeout(function(){$('#searchWeb').click();}, 3000);
var count = 2, countinterval = setInterval(function(){
infop.find('#searchcounter').html(count--);
if(count == 0){clearInterval(countinterval);}
}, 1000);
}
loading.remove();
$('#submitButton').removeAttr('disabled');
});
}
function result(r){
// This is class definition. Object of this class are created for
// each result. The markup is generated by the .toString() method.
var arr = [];
// GsearchResultClass is passed by the google API
switch(r.GsearchResultClass){
case 'GwebSearch':
arr = [
'<div class="webResult">',
'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
'<p>',r.content,'</p>',
'<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',
'</div>'
];
break;
case 'GimageSearch':
arr = [
'<div class="imageResult">',
'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
'</div>'
];
break;
case 'GvideoSearch':
arr = [
'<div class="imageResult">',
'<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
'<img src="',r.tbUrl,'" width="100%" /></a>',
'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
'</div>'
];
break;
case 'GnewsSearch':
arr = [
'<div class="webResult">',
'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
'<p>',r.content,'</p>',
'<a href="',r.unescapedUrl,'" target="_blank">',r.publisher,'</a>',
'</div>'
];
break;
}
// The toString method.
this.toString = function(){
return arr.join('');
};
}
Handlebars.registerHelper('shortenTextBy50', function(text) {
return text.trunc(50);
});
Handlebars.registerHelper('shortenTextBy280', function(text) {
return text.trunc(150);
});
Handlebars.registerHelper('gbtagsTitle', function(obj) {
return obj.title[0];
});
Handlebars.registerHelper('gbtagShortenTextBy280', function(text) {
return $('<div/>').append(text).text().trunc(150);
});
});