| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/declare", "dojo/_base/query", |
| 2 |
"dojo/dom-construct","dojo/io/script"], |
| 3 |
function(kernel, lang, declare, domQuery, domConstruct, scriptIO) { |
| 4 |
|
| 5 |
kernel.experimental("dojox.data.GoogleSearchStore"); |
| 6 |
|
| 7 |
var SearchStore = declare("dojox.data.GoogleSearchStore",null,{ |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
constructor: function(args){ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if(args){ |
| 37 |
if(args.label){ |
| 38 |
this.label = args.label; |
| 39 |
} |
| 40 |
if(args.key){ |
| 41 |
this._key = args.key; |
| 42 |
} |
| 43 |
if(args.lang){ |
| 44 |
this._lang = args.lang; |
| 45 |
} |
| 46 |
if("urlPreventCache" in args){ |
| 47 |
this.urlPreventCache = args.urlPreventCache?true:false; |
| 48 |
} |
| 49 |
} |
| 50 |
this._id = dojox.data.GoogleSearchStore.prototype._id++; |
| 51 |
}, |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
_id: 0, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
_requestCount: 0, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
_googleUrl: "http://ajax.googleapis.com/ajax/services/search/", |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
_storeRef: "_S", |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
_attributes: [ "unescapedUrl", "url", "visibleUrl", "cacheUrl", "title", |
| 73 |
"titleNoFormatting", "content", "estimatedResultCount"], |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
_aggregatedAttributes: { |
| 78 |
estimatedResultCount: "cursor.estimatedResultCount" |
| 79 |
}, |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
label: "titleNoFormatting", |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
_type: "web", |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
urlPreventCache: true, |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
_queryAttrs: { |
| 98 |
text: 'q' |
| 99 |
}, |
| 100 |
|
| 101 |
_assertIsItem: function( item){ |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if(!this.isItem(item)){ |
| 107 |
throw new Error("dojox.data.GoogleSearchStore: a function was passed an item argument that was not an item"); |
| 108 |
} |
| 109 |
}, |
| 110 |
|
| 111 |
_assertIsAttribute: function( attribute){ |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if(typeof attribute !== "string"){ |
| 117 |
throw new Error("dojox.data.GoogleSearchStore: a function was passed an attribute argument that was not an attribute name string"); |
| 118 |
} |
| 119 |
}, |
| 120 |
|
| 121 |
getFeatures: function(){ |
| 122 |
|
| 123 |
|
| 124 |
return { |
| 125 |
'dojo.data.api.Read': true |
| 126 |
}; |
| 127 |
}, |
| 128 |
|
| 129 |
getValue: function(item, attribute, defaultValue){ |
| 130 |
|
| 131 |
|
| 132 |
var values = this.getValues(item, attribute); |
| 133 |
if(values && values.length > 0){ |
| 134 |
return values[0]; |
| 135 |
} |
| 136 |
return defaultValue; |
| 137 |
}, |
| 138 |
|
| 139 |
getAttributes: function(item){ |
| 140 |
|
| 141 |
|
| 142 |
return this._attributes; |
| 143 |
}, |
| 144 |
|
| 145 |
hasAttribute: function(item, attribute){ |
| 146 |
|
| 147 |
|
| 148 |
if(this.getValue(item,attribute)){ |
| 149 |
return true; |
| 150 |
} |
| 151 |
return false; |
| 152 |
}, |
| 153 |
|
| 154 |
isItemLoaded: function(item){ |
| 155 |
|
| 156 |
|
| 157 |
return this.isItem(item); |
| 158 |
}, |
| 159 |
|
| 160 |
loadItem: function(keywordArgs){ |
| 161 |
|
| 162 |
|
| 163 |
}, |
| 164 |
|
| 165 |
getLabel: function(item){ |
| 166 |
|
| 167 |
|
| 168 |
return this.getValue(item,this.label); |
| 169 |
}, |
| 170 |
|
| 171 |
getLabelAttributes: function(item){ |
| 172 |
|
| 173 |
|
| 174 |
return [this.label]; |
| 175 |
}, |
| 176 |
|
| 177 |
containsValue: function(item, attribute, value){ |
| 178 |
|
| 179 |
|
| 180 |
var values = this.getValues(item,attribute); |
| 181 |
for(var i = 0; i < values.length; i++){ |
| 182 |
if(values[i] === value){ |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
return false; |
| 187 |
}, |
| 188 |
|
| 189 |
getValues: function(item, attribute){ |
| 190 |
|
| 191 |
|
| 192 |
this._assertIsItem(item); |
| 193 |
this._assertIsAttribute(attribute); |
| 194 |
var val = item[attribute]; |
| 195 |
if(lang.isArray(val)) { |
| 196 |
return val; |
| 197 |
}else if(val !== undefined){ |
| 198 |
return [val]; |
| 199 |
}else{ |
| 200 |
return []; |
| 201 |
} |
| 202 |
}, |
| 203 |
|
| 204 |
isItem: function(item){ |
| 205 |
|
| 206 |
|
| 207 |
if(item && item[this._storeRef] === this){ |
| 208 |
return true; |
| 209 |
} |
| 210 |
return false; |
| 211 |
}, |
| 212 |
|
| 213 |
close: function(request){ |
| 214 |
|
| 215 |
|
| 216 |
}, |
| 217 |
|
| 218 |
_format: function(item, name){ |
| 219 |
return item; |
| 220 |
}, |
| 221 |
|
| 222 |
fetch: function(request){ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
request = request || {}; |
| 232 |
|
| 233 |
var scope = request.scope || kernel.global; |
| 234 |
|
| 235 |
if(!request.query){ |
| 236 |
if(request.onError){ |
| 237 |
request.onError.call(scope, new Error(this.declaredClass + |
| 238 |
": A query must be specified.")); |
| 239 |
return; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
var query = {}; |
| 245 |
for(var attr in this._queryAttrs) { |
| 246 |
query[attr] = request.query[attr]; |
| 247 |
} |
| 248 |
request = { |
| 249 |
query: query, |
| 250 |
onComplete: request.onComplete, |
| 251 |
onError: request.onError, |
| 252 |
onItem: request.onItem, |
| 253 |
onBegin: request.onBegin, |
| 254 |
start: request.start, |
| 255 |
count: request.count |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
var pageSize = 8; |
| 260 |
|
| 261 |
|
| 262 |
var callbackFn = "GoogleSearchStoreCallback_" + this._id + "_" + (++this._requestCount); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
var content = this._createContent(query, callbackFn, request); |
| 267 |
|
| 268 |
var firstRequest; |
| 269 |
|
| 270 |
if(typeof(request.start) === "undefined" || request.start === null){ |
| 271 |
request.start = 0; |
| 272 |
} |
| 273 |
|
| 274 |
if(!request.count){ |
| 275 |
request.count = pageSize; |
| 276 |
} |
| 277 |
firstRequest = {start: request.start - request.start % pageSize}; |
| 278 |
|
| 279 |
var _this = this; |
| 280 |
var searchUrl = this._googleUrl + this._type; |
| 281 |
|
| 282 |
var getArgs = { |
| 283 |
url: searchUrl, |
| 284 |
preventCache: this.urlPreventCache, |
| 285 |
content: content |
| 286 |
}; |
| 287 |
|
| 288 |
var items = []; |
| 289 |
var successfulReq = 0; |
| 290 |
var finished = false; |
| 291 |
var lastOnItem = request.start -1; |
| 292 |
var numRequests = 0; |
| 293 |
var scriptIds = []; |
| 294 |
|
| 295 |
|
| 296 |
function doRequest(req){ |
| 297 |
|
| 298 |
numRequests ++; |
| 299 |
getArgs.content.context = getArgs.content.start = req.start; |
| 300 |
|
| 301 |
var deferred = scriptIO.get(getArgs); |
| 302 |
scriptIds.push(deferred.ioArgs.id); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
deferred.addErrback(function(error){ |
| 307 |
if(request.onError){ |
| 308 |
request.onError.call(scope, error, request); |
| 309 |
} |
| 310 |
}); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
var myHandler = function(start, data){ |
| 315 |
if (scriptIds.length > 0) { |
| 316 |
|
| 317 |
domQuery("#" + scriptIds.splice(0,1)).forEach(domConstruct.destroy); |
| 318 |
} |
| 319 |
if(finished){return;} |
| 320 |
|
| 321 |
var results = _this._getItems(data); |
| 322 |
var cursor = data ? data['cursor']: null; |
| 323 |
|
| 324 |
if(results){ |
| 325 |
|
| 326 |
for(var i = 0; i < results.length && i + start < request.count + request.start; i++) { |
| 327 |
_this._processItem(results[i], data); |
| 328 |
items[i + start] = results[i]; |
| 329 |
} |
| 330 |
successfulReq ++; |
| 331 |
if(successfulReq == 1){ |
| 332 |
|
| 333 |
|
| 334 |
var pages = cursor ? cursor.pages : null; |
| 335 |
var firstStart = pages ? Number(pages[pages.length - 1].start) : 0; |
| 336 |
|
| 337 |
|
| 338 |
if (request.onBegin){ |
| 339 |
var est = cursor ? cursor.estimatedResultCount : results.length; |
| 340 |
var total = est ? Math.min(est, firstStart + results.length) : firstStart + results.length; |
| 341 |
request.onBegin.call(scope, total, request); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
var nextPage = (request.start - request.start % pageSize) + pageSize; |
| 346 |
var page = 1; |
| 347 |
while(pages){ |
| 348 |
if(!pages[page] || Number(pages[page].start) >= request.start + request.count){ |
| 349 |
break; |
| 350 |
} |
| 351 |
if(Number(pages[page].start) >= nextPage) { |
| 352 |
doRequest({start: pages[page].start}); |
| 353 |
} |
| 354 |
page++; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
if(request.onItem && items[lastOnItem + 1]){ |
| 360 |
do{ |
| 361 |
lastOnItem++; |
| 362 |
request.onItem.call(scope, items[lastOnItem], request); |
| 363 |
}while(items[lastOnItem + 1] && lastOnItem < request.start + request.count); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
if(successfulReq == numRequests){ |
| 368 |
|
| 369 |
finished = true; |
| 370 |
|
| 371 |
kernel.global[callbackFn] = null; |
| 372 |
if(request.onItem){ |
| 373 |
request.onComplete.call(scope, null, request); |
| 374 |
}else{ |
| 375 |
items = items.slice(request.start, request.start + request.count); |
| 376 |
request.onComplete.call(scope, items, request); |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
var callbacks = []; |
| 384 |
var lastCallback = firstRequest.start - 1; |
| 385 |
|
| 386 |
|
| 387 |
kernel.global[callbackFn] = function(start, data, responseCode, errorMsg){ |
| 388 |
try { |
| 389 |
if(responseCode != 200){ |
| 390 |
if(request.onError){ |
| 391 |
request.onError.call(scope, new Error("Response from Google was: " + responseCode), request); |
| 392 |
} |
| 393 |
kernel.global[callbackFn] = function(){}; |
| 394 |
return; |
| 395 |
} |
| 396 |
|
| 397 |
if(start == lastCallback + 1){ |
| 398 |
myHandler(Number(start), data); |
| 399 |
lastCallback += pageSize; |
| 400 |
|
| 401 |
|
| 402 |
if(callbacks.length > 0){ |
| 403 |
callbacks.sort(_this._getSort()); |
| 404 |
|
| 405 |
while(callbacks.length > 0 && callbacks[0].start == lastCallback + 1){ |
| 406 |
myHandler(Number(callbacks[0].start), callbacks[0].data); |
| 407 |
callbacks.splice(0,1); |
| 408 |
lastCallback += pageSize; |
| 409 |
} |
| 410 |
} |
| 411 |
}else{ |
| 412 |
callbacks.push({start:start, data: data}); |
| 413 |
} |
| 414 |
} catch (e) { |
| 415 |
request.onError.call(scope, e, request); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
doRequest(firstRequest); |
| 423 |
}, |
| 424 |
|
| 425 |
_getSort: function() { |
| 426 |
return function(a,b){ |
| 427 |
if(a.start < b.start){return -1;} |
| 428 |
if(b.start < a.start){return 1;} |
| 429 |
return 0; |
| 430 |
}; |
| 431 |
}, |
| 432 |
|
| 433 |
_processItem: function(item, data) { |
| 434 |
item[this._storeRef] = this; |
| 435 |
|
| 436 |
for(var attribute in this._aggregatedAttributes) { |
| 437 |
item[attribute] = lang.getObject(this._aggregatedAttributes[attribute], false, data); |
| 438 |
} |
| 439 |
}, |
| 440 |
|
| 441 |
_getItems: function(data){ |
| 442 |
return data['results'] || data; |
| 443 |
}, |
| 444 |
|
| 445 |
_createContent: function(query, callback, request){ |
| 446 |
var content = { |
| 447 |
v: "1.0", |
| 448 |
rsz: "large", |
| 449 |
callback: callback, |
| 450 |
key: this._key, |
| 451 |
hl: this._lang |
| 452 |
}; |
| 453 |
for(var attr in this._queryAttrs) { |
| 454 |
content[this._queryAttrs[attr]] = query[attr]; |
| 455 |
} |
| 456 |
return content; |
| 457 |
} |
| 458 |
}); |
| 459 |
|
| 460 |
var WebSearchStore = declare("dojox.data.GoogleWebSearchStore", SearchStore,{ |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
}); |
| 476 |
|
| 477 |
var BlogSearchStore = declare("dojox.data.GoogleBlogSearchStore", SearchStore,{ |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
_type: "blogs", |
| 494 |
_attributes: ["blogUrl", "postUrl", "title", "titleNoFormatting", "content", |
| 495 |
"author", "publishedDate"], |
| 496 |
_aggregatedAttributes: { } |
| 497 |
}); |
| 498 |
|
| 499 |
|
| 500 |
var LocalSearchStore = declare("dojox.data.GoogleLocalSearchStore", SearchStore,{ |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
_type: "local", |
| 531 |
_attributes: ["title", "titleNoFormatting", "url", "lat", "lng", "streetAddress", |
| 532 |
"city", "region", "country", "phoneNumbers", "ddUrl", "ddUrlToHere", |
| 533 |
"ddUrlFromHere", "staticMapUrl", "viewport"], |
| 534 |
_aggregatedAttributes: { |
| 535 |
viewport: "viewport" |
| 536 |
}, |
| 537 |
_queryAttrs: { |
| 538 |
text: 'q', |
| 539 |
centerLatLong: 'sll', |
| 540 |
searchSpan: 'sspn' |
| 541 |
} |
| 542 |
}); |
| 543 |
|
| 544 |
var VideoSearchStore = declare("dojox.data.GoogleVideoSearchStore", SearchStore,{ |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
_type: "video", |
| 563 |
_attributes: ["title", "titleNoFormatting", "content", "url", "published", "publisher", |
| 564 |
"duration", "tbWidth", "tbHeight", "tbUrl", "playUrl"], |
| 565 |
_aggregatedAttributes: { } |
| 566 |
}); |
| 567 |
|
| 568 |
var NewsSearchStore = declare("dojox.data.GoogleNewsSearchStore", SearchStore,{ |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
_type: "news", |
| 588 |
_attributes: ["title", "titleNoFormatting", "content", "url", "unescapedUrl", "publisher", |
| 589 |
"clusterUrl", "location", "publishedDate", "relatedStories" ], |
| 590 |
_aggregatedAttributes: { } |
| 591 |
}); |
| 592 |
|
| 593 |
var BookSearchStore = declare("dojox.data.GoogleBookSearchStore", SearchStore,{ |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
_type: "books", |
| 609 |
_attributes: ["title", "titleNoFormatting", "authors", "url", "unescapedUrl", "bookId", |
| 610 |
"pageCount", "publishedYear"], |
| 611 |
_aggregatedAttributes: { } |
| 612 |
}); |
| 613 |
|
| 614 |
var ImageSearchStore = declare("dojox.data.GoogleImageSearchStore", SearchStore,{ |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
_type: "images", |
| 635 |
_attributes: ["title", "titleNoFormatting", "visibleUrl", "url", "unescapedUrl", |
| 636 |
"originalContextUrl", "width", "height", "tbWidth", "tbHeight", |
| 637 |
"tbUrl", "content", "contentNoFormatting"], |
| 638 |
_aggregatedAttributes: { } |
| 639 |
}); |
| 640 |
|
| 641 |
return { |
| 642 |
Search: SearchStore, |
| 643 |
ImageSearch: ImageSearchStore, |
| 644 |
BookSearch: BookSearchStore, |
| 645 |
NewsSearch: NewsSearchStore, |
| 646 |
VideoSearch: VideoSearchStore, |
| 647 |
LocalSearch: LocalSearchStore, |
| 648 |
BlogSearch: BlogSearchStore, |
| 649 |
WebSearch: WebSearchStore |
| 650 |
} |
| 651 |
}); |