dojox/data/GoogleSearchStore.js

  • Provides:

    • dojox.data.GoogleSearchStore
  • dojox.data.GoogleSearchStore

    • type
      Function
    • summary
      Initializer for the GoogleSearchStore store.
    • parameters:
      • args: (typeof Object)
    • source: [view]
        if(args){
         if(args.label){
          this.label = args.label;
         }
         if(args.key){
          this._key = args.key;
         }
         if(args.lang){
          this._lang = args.lang;
         }
         if("urlPreventCache" in args){
          this.urlPreventCache = args.urlPreventCache?true:false;
         }
        }
        this._id = dojox.data.GoogleSearchStore.prototype._id++;
    • description
      The GoogleSearchStore is a Datastore interface to
      the Google search service. The constructor accepts the following arguments:
      <ul>
      <li>label - the label attribute to use. Defaults to titleNoFormatting</li>
      <li>key - The API key to use. This is optional</li>
      <li>lang - The language locale to use. Defaults to the browser locale</li>
      </ul>
  • dojox.data.GoogleSearchStore._id

    • type
      Integer
    • summary
      A unique identifier for this store.
  • dojox.data.GoogleSearchStore._requestCount

    • type
      Integer
    • summary
      A counter for the number of requests made. This is used to define
      the callback function that GoogleSearchStore will use.
  • dojox.data.GoogleSearchStore._googleUrl

    • type
      String
    • summary
      The URL to Googles search web service.
  • dojox.data.GoogleSearchStore._storeRef

    • type
      String
    • summary
      The internal reference added to each item pointing at the store which owns it.
  • dojox.data.GoogleSearchStore._attributes

    • type
      Array
    • summary
      The list of attributes that this store supports
  • dojox.data.GoogleSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleSearchStore.label

    • type
      String
    • summary
      The default attribute which acts as a label for each item.
  • dojox.data.GoogleSearchStore._type

    • summary
  • dojox.data.GoogleSearchStore.urlPreventCache

    • type
      boolean
    • summary
      Sets whether or not to pass preventCache to dojo.io.script.
  • dojox.data.GoogleSearchStore._queryAttrs

    • type
      Object
    • summary
  • dojox.data.GoogleSearchStore._assertIsItem

    • type
      Function
    • parameters:
      • item: (typeof item)
        The item to test for being contained by the store.
    • source: [view]
        if(!this.isItem(item)){
         throw new Error("dojox.data.GoogleSearchStore: a function was passed an item argument that was not an item");
        }
    • summary
      This function tests whether the item passed in is indeed an item in the store.
  • dojox.data.GoogleSearchStore._assertIsAttribute

    • type
      Function
    • parameters:
      • attribute: (typeof attribute-name-string)
        The attribute to test for being contained by the store.
    • source: [view]
        if(typeof attribute !== "string"){
         throw new Error("dojox.data.GoogleSearchStore: a function was passed an attribute argument that was not an attribute name string");
        }
    • summary
      This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
  • dojox.data.GoogleSearchStore.getFeatures

    • type
      Function
    • source: [view]
        return {
         'dojo.data.api.Read': true
        };
    • summary
      See dojo.data.api.Read.getFeatures()
  • dojox.data.GoogleSearchStore.getValue

    • type
      Function
    • parameters:
      • item: (typeof )
      • attribute: (typeof )
      • defaultValue: (typeof )
    • source: [view]
        var values = this.getValues(item, attribute);
        if(values && values.length > 0){
         return values[0];
        }
        return defaultValue;
    • summary
      See dojo.data.api.Read.getValue()
  • dojox.data.GoogleSearchStore.getAttributes

    • type
      Function
    • parameters:
      • item: (typeof )
    • source: [view]
        return this._attributes;
    • summary
      See dojo.data.api.Read.getAttributes()
  • dojox.data.GoogleSearchStore.hasAttribute

    • type
      Function
    • parameters:
      • item: (typeof )
      • attribute: (typeof )
    • source: [view]
        if(this.getValue(item,attribute)){
         return true;
        }
        return false;
    • summary
      See dojo.data.api.Read.hasAttributes()
  • dojox.data.GoogleSearchStore.isItemLoaded

    • type
      Function
    • parameters:
      • item: (typeof )
    • source: [view]
         return this.isItem(item);
    • summary
      See dojo.data.api.Read.isItemLoaded()
  • dojox.data.GoogleSearchStore.loadItem

    • type
      Function
    • parameters:
      • keywordArgs: (typeof )
    • source: [view]
        // summary:
        //  See dojo.data.api.Read.loadItem()
    • summary
      See dojo.data.api.Read.loadItem()
  • dojox.data.GoogleSearchStore.getLabel

    • type
      Function
    • parameters:
      • item: (typeof )
    • source: [view]
        return this.getValue(item,this.label);
    • summary
      See dojo.data.api.Read.getLabel()
  • dojox.data.GoogleSearchStore.getLabelAttributes

    • type
      Function
    • parameters:
      • item: (typeof )
    • source: [view]
        return [this.label];
    • summary
      See dojo.data.api.Read.getLabelAttributes()
  • dojox.data.GoogleSearchStore.containsValue

    • type
      Function
    • parameters:
      • item: (typeof )
      • attribute: (typeof )
      • value: (typeof )
    • source: [view]
        var values = this.getValues(item,attribute);
        for(var i = 0; i < values.length; i++){
         if(values[i] === value){
          return true;
         }
        }
        return false;
    • summary
      See dojo.data.api.Read.containsValue()
  • dojox.data.GoogleSearchStore.getValues

    • type
      Function
    • parameters:
      • item: (typeof )
      • attribute: (typeof )
    • source: [view]
        this._assertIsItem(item);
        this._assertIsAttribute(attribute);
        var val = item[attribute];
        if(dojo.isArray(val)) {
         return val;
        }else if(val !== undefined){
         return [val];
        }else{
         return [];
        }
    • summary
      See dojo.data.api.Read.getValue()
  • dojox.data.GoogleSearchStore.isItem

    • type
      Function
    • parameters:
      • item: (typeof )
    • source: [view]
        if(item && item[this._storeRef] === this){
         return true;
        }
        return false;
    • summary
      See dojo.data.api.Read.isItem()
  • dojox.data.GoogleSearchStore.close

    • type
      Function
    • parameters:
      • request: (typeof )
    • source: [view]
        // summary:
        //  See dojo.data.api.Read.close()
    • summary
      See dojo.data.api.Read.close()
  • dojox.data.GoogleSearchStore._format

    • type
      Function
    • parameters:
      • item: (typeof )
      • name: (typeof )
    • source: [view]
        return item;//base implementation does not format any items
    • returns
      base implementation does not format any items
    • summary
  • dojox.data.GoogleSearchStore.fetch

    • type
      Function
    • parameters:
      • request: (typeof A)
        request object
        fetchHandler:
        A function to call for fetched items
        errorHandler:
        A function to call on error
    • source: [view]
        request = request || {};


        var scope = request.scope || dojo.global;


        if(!request.query){
         if(request.onError){
          request.onError.call(scope, new Error(this.declaredClass +
           ": A query must be specified."));
          return;
         }
        }
        //Make a copy of the request object, in case it is
        //modified outside the store in the middle of a request
        var query = {};
        for(var attr in this._queryAttrs) {
         query[attr] = request.query[attr];
        }
        request = {
         query: query,
         onComplete: request.onComplete,
         onError: request.onError,
         onItem: request.onItem,
         onBegin: request.onBegin,
         start: request.start,
         count: request.count
        };


        //Google's web api will only return a max of 8 results per page.
        var pageSize = 8;


        //Generate a unique function to be called back
        var callbackFn = "GoogleSearchStoreCallback_" + this._id + "_" + (++this._requestCount);


        //Build up the content to send the request for.
        //rsz is the result size, "large" gives 8 results each time
        var content = this._createContent(query, callbackFn, request);


        var firstRequest;


        if(typeof(request.start) === "undefined" || request.start === null){
         request.start = 0;
        }


        if(!request.count){
         request.count = pageSize;
        }
        firstRequest = {start: request.start - request.start % pageSize};


        var _this = this;
        var searchUrl = this._googleUrl + this._type;


        var getArgs = {
         url: searchUrl,
         preventCache: this.urlPreventCache,
         content: content
        };


        var items = [];
        var successfulReq = 0;
        var finished = false;
        var lastOnItem = request.start -1;
        var numRequests = 0;
        var scriptIds = [];


        // Performs the remote request.
        function doRequest(req){
         //Record how many requests have been made.
         numRequests ++;
         getArgs.content.context = getArgs.content.start = req.start;


         var deferred = dojo.io.script.get(getArgs);
         scriptIds.push(deferred.ioArgs.id);


         //We only set up the errback, because the callback isn't ever really used because we have
         //to link to the jsonp callback function....
         deferred.addErrback(function(error){
          if(request.onError){
           request.onError.call(scope, error, request);
          }
         });
        }


        // Function to handle returned data.
        var myHandler = function(start, data){
         if (scriptIds.length > 0) {
          // Delete the script node that was created.
          dojo.query("#" + scriptIds.splice(0,1)).forEach(dojo.destroy);
         }
         if(finished){return;}


         var results = _this._getItems(data);
         var cursor = data ? data['cursor']: null;


         if(results){
          //Process the results, adding the store reference to them
          for(var i = 0; i < results.length && i + start < request.count + request.start; i++) {
           _this._processItem(results[i], data);
           items[i + start] = results[i];
          }
          successfulReq ++;
          if(successfulReq == 1){
           // After the first request, we know how many results exist.
           // So perform any follow up requests to retrieve more data.
           var pages = cursor ? cursor.pages : null;
           var firstStart = pages ? Number(pages[pages.length - 1].start) : 0;


           //Call the onBegin method if it exists
           if (request.onBegin){
            var est = cursor ? cursor.estimatedResultCount : results.length;
            var total = est ? Math.min(est, firstStart + results.length) : firstStart + results.length;
            request.onBegin.call(scope, total, request);
           }


           // Request the next pages.
           var nextPage = (request.start - request.start % pageSize) + pageSize;
           var page = 1;
           while(pages){
            if(!pages[page] || Number(pages[page].start) >= request.start + request.count){
             break;
            }
            if(Number(pages[page].start) >= nextPage) {
             doRequest({start: pages[page].start});
            }
            page++;
           }
          }


          // Call the onItem function on all retrieved items.
          if(request.onItem && items[lastOnItem + 1]){
           do{
            lastOnItem++;
            request.onItem.call(scope, items[lastOnItem], request);
           }while(items[lastOnItem + 1] && lastOnItem < request.start + request.count);
          }


          //If this is the last request, call final fetch handler.
          if(successfulReq == numRequests){
           //Process the items...
           finished = true;
           //Clean up the function, it should never be called again
           dojo.global[callbackFn] = null;
           if(request.onItem){
            request.onComplete.call(scope, null, request);
           }else{
            items = items.slice(request.start, request.start + request.count);
            request.onComplete.call(scope, items, request);
           }


          }
         }
        };


        var callbacks = [];
        var lastCallback = firstRequest.start - 1;


        // Attach a callback function to the global namespace, where Google can call it.
        dojo.global[callbackFn] = function(start, data, responseCode, errorMsg){
         try {
          if(responseCode != 200){
           if(request.onError){
            request.onError.call(scope, new Error("Response from Google was: " + responseCode), request);
           }
           dojo.global[callbackFn] = function(){};//an error occurred, do not return anything else.
           return;
          }

       
          if(start == lastCallback + 1){
           myHandler(Number(start), data);
           lastCallback += pageSize;

       
           //make sure that the callbacks happen in the correct sequence
           if(callbacks.length > 0){
            callbacks.sort(_this._getSort());
            //In case the requsts do not come back in order, sort the returned results.
            while(callbacks.length > 0 && callbacks[0].start == lastCallback + 1){
             myHandler(Number(callbacks[0].start), callbacks[0].data);
             callbacks.splice(0,1);
             lastCallback += pageSize;
            }
           }
          }else{
           callbacks.push({start:start, data: data});
          }
         } catch (e) {
          request.onError.call(scope, e, request);
         }
        };


        // Perform the first request. When this has finished
        // we will have a list of pages, which can then be
        // gone through
        doRequest(firstRequest);
    • summary
      Fetch Google search items that match to a query
    • returns
      an error occurred, do not return anything else.
    • chains:
      • request.onError: (call)
      • request.onBegin: (call)
      • request.onItem: (call)
      • request.onComplete: (call)
  • dojox.data.GoogleSearchStore._getSort

    • type
      Function
    • source: [view]
        return function(a,b){
         if(a.start < b.start){return -1;}
         if(b.start < a.start){return 1;}
         return 0;
        };
    • summary
  • dojox.data.GoogleSearchStore._processItem

    • type
      Function
    • parameters:
      • item: (typeof )
      • data: (typeof )
    • source: [view]
        item[this._storeRef] = this;
        // Copy aggregated attributes from query results to the item.
        for(var attribute in this._aggregatedAttributes) {
         item[attribute] = dojo.getObject(this._aggregatedAttributes[attribute], false, data);
        }
    • summary
  • dojox.data.GoogleSearchStore._getItems

    • type
      Function
    • parameters:
      • data: (typeof )
    • source: [view]
        return data['results'] || data;
    • summary
  • dojox.data.GoogleSearchStore._createContent

    • type
      Function
    • parameters:
      • query: (typeof )
      • callback: (typeof )
      • request: (typeof )
    • source: [view]
        var content = {
         v: "1.0",
         rsz: "large",
         callback: callback,
         key: this._key,
         hl: this._lang
        };
        for(var attr in this._queryAttrs) {
         content[this._queryAttrs[attr]] = query[attr];
        }
        return content;
    • summary
  • dojox.data.GoogleSearchStore._aggregatedAttributes.estimatedResultCount

    • summary
  • dojox.data.GoogleSearchStore._queryAttrs.text

    • summary
  • dojox.data.GoogleSearchStore._key

    • summary
  • dojox.data.GoogleSearchStore._lang

    • summary
  • dojox.data.GoogleWebSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
  • dojox.data.GoogleBlogSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
  • dojox.data.GoogleBlogSearchStore._type

    • summary
  • dojox.data.GoogleBlogSearchStore._attributes

    • summary
  • dojox.data.GoogleBlogSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleLocalSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
      A data store for retrieving search results from Google.
      The following attributes are supported on each item:
      &lt;ul&gt;
      &lt;li&gt;title - The blog post title in HTML format.&lt;/li&gt;
      &lt;li&gt;titleNoFormatting - The  blog post title in plain text&lt;/li&gt;
      &lt;li&gt;content - A snippet of information about the blog post&lt;/li&gt;
      &lt;li&gt;url - The URL for the item&lt;/li&gt;
      &lt;li&gt;lat - The latitude.&lt;/li&gt;
      &lt;li&gt;lng - The longtitude.&lt;/li&gt;
      &lt;li&gt;streetAddress - The street address&lt;/li&gt;
      &lt;li&gt;city - The city&lt;/li&gt;
      &lt;li&gt;region - The region&lt;/li&gt;
      &lt;li&gt;country - The country&lt;/li&gt;
      &lt;li&gt;phoneNumbers - Phone numbers associated with this address. Can be one or more.&lt;/li&gt;
      &lt;li&gt;ddUrl - A URL that can be used to provide driving directions from the center of the search results to this search results&lt;/li&gt;
      &lt;li&gt;ddUrlToHere - A URL that can be used to provide driving directions from this search result to a user specified location&lt;/li&gt;
      &lt;li&gt;staticMapUrl - The published date, in RFC-822 format&lt;/li&gt;
      &lt;li&gt;viewport - Recommended viewport for the query results (same for all results in a query)
      &lt;ul&gt;
      &lt;li&gt;center - contains lat, lng properties&lt;/li&gt;
      &lt;li&gt;span - lat, lng properties for the viewport span&lt;/li&gt;
      &lt;li&gt;ne, sw - lat, lng properties for the viewport corners&lt;li&gt;
      &lt;/ul&gt;
      &lt;/li&gt;
      &lt;/ul&gt;
      The query accepts the following parameters:
      &lt;ul&gt;
      &lt;li&gt;text - The string to search for&lt;/li&gt;
      &lt;li&gt;centerLatLong - Comma-separated lat &amp; long for the center of the search (e.g. &quot;48.8565,2.3509&quot;)&lt;/li&gt;
      &lt;li&gt;searchSpan - Comma-separated lat &amp; long degrees indicating the size of the desired search area (e.g. &quot;0.065165,0.194149&quot;)&lt;/li&gt;
      &lt;/ul&gt;
  • dojox.data.GoogleLocalSearchStore._type

    • summary
  • dojox.data.GoogleLocalSearchStore._attributes

    • summary
  • dojox.data.GoogleLocalSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleLocalSearchStore._queryAttrs

    • type
      Object
    • summary
  • dojox.data.GoogleLocalSearchStore._aggregatedAttributes.viewport

    • summary
  • dojox.data.GoogleLocalSearchStore._queryAttrs.text

    • summary
  • dojox.data.GoogleLocalSearchStore._queryAttrs.centerLatLong

    • summary
  • dojox.data.GoogleLocalSearchStore._queryAttrs.searchSpan

    • summary
  • dojox.data.GoogleVideoSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
      A data store for retrieving search results from Google.
      The following attributes are supported on each item:
      &lt;ul&gt;
      &lt;li&gt;title - The blog post title in HTML format.&lt;/li&gt;
      &lt;li&gt;titleNoFormatting - The  blog post title in plain text&lt;/li&gt;
      &lt;li&gt;content - A snippet of information about the blog post&lt;/li&gt;
      &lt;li&gt;url - The URL for the item&lt;/li&gt;
      &lt;li&gt;published - The published date, in RFC-822 format.&lt;/li&gt;
      &lt;li&gt;publisher - The name of the publisher.&lt;/li&gt;
      &lt;li&gt;duration - The approximate duration, in seconds, of the video.&lt;/li&gt;
      &lt;li&gt;tbWidth - The width in pixels of the video.&lt;/li&gt;
      &lt;li&gt;tbHeight - The height in pixels of the video&lt;/li&gt;
      &lt;li&gt;tbUrl - The URL to a thumbnail representation of the video.&lt;/li&gt;
      &lt;li&gt;playUrl - If present, supplies the url of the flash version of the video that can be played inline on your page. To play this video simply create and &lt;embed&gt; element on your page using this value as the src attribute and using application/x-shockwave-flash as the type attribute. If you want the video to play right away, make sure to append &amp;autoPlay=true to the url..&lt;/li&gt;
      &lt;/ul&gt;
      The query accepts one parameter: text - The string to search for
  • dojox.data.GoogleVideoSearchStore._type

    • summary
  • dojox.data.GoogleVideoSearchStore._attributes

    • summary
  • dojox.data.GoogleVideoSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleNewsSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
      A data store for retrieving search results from Google.
      The following attributes are supported on each item:
      &lt;ul&gt;
      &lt;li&gt;title - The news story title in HTML format.&lt;/li&gt;
      &lt;li&gt;titleNoFormatting - The news story title in plain text&lt;/li&gt;
      &lt;li&gt;content - A snippet of information about the news story&lt;/li&gt;
      &lt;li&gt;url - The URL for the item&lt;/li&gt;
      &lt;li&gt;unescapedUrl - The URL for the item, with URL escaping. This is often more readable&lt;/li&gt;
      &lt;li&gt;publisher - The name of the publisher&lt;/li&gt;
      &lt;li&gt;clusterUrl - A URL pointing to a page listing related storied.&lt;/li&gt;
      &lt;li&gt;location - The location of the news story.&lt;/li&gt;
      &lt;li&gt;publishedDate - The date of publication, in RFC-822 format.&lt;/li&gt;
      &lt;li&gt;relatedStories - An optional array of objects specifying related stories.
      Each object has the following subset of properties:
      &quot;title&quot;, &quot;titleNoFormatting&quot;, &quot;url&quot;, &quot;unescapedUrl&quot;, &quot;publisher&quot;, &quot;location&quot;, &quot;publishedDate&quot;.
      &lt;/li&gt;
      &lt;/ul&gt;
      The query accepts one parameter: text - The string to search for
  • dojox.data.GoogleNewsSearchStore._type

    • summary
  • dojox.data.GoogleNewsSearchStore._attributes

    • summary
  • dojox.data.GoogleNewsSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleBookSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
      A data store for retrieving search results from Google.
      The following attributes are supported on each item:
      &lt;ul&gt;
      &lt;li&gt;title - The book title in HTML format.&lt;/li&gt;
      &lt;li&gt;titleNoFormatting - The book title in plain text&lt;/li&gt;
      &lt;li&gt;authors - An array of authors&lt;/li&gt;
      &lt;li&gt;url - The URL for the item&lt;/li&gt;
      &lt;li&gt;unescapedUrl - The URL for the item, with URL escaping. This is often more readable&lt;/li&gt;
      &lt;li&gt;bookId - An identifier for the book, usually an ISBN.&lt;/li&gt;
      &lt;li&gt;pageCount - The number of pages in the book.&lt;/li&gt;
      &lt;li&gt;publishedYear - The year of publication.&lt;/li&gt;
      &lt;/ul&gt;
      The query accepts one parameter: text - The string to search for
  • dojox.data.GoogleBookSearchStore._type

    • summary
  • dojox.data.GoogleBookSearchStore._attributes

    • summary
  • dojox.data.GoogleBookSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data.GoogleImageSearchStore

    • type
      Function
    • chains:
      • dojox.data.GoogleSearchStore: (prototype)
      • dojox.data.GoogleSearchStore: (call)
    • summary
      A data store for retrieving search results from Google.
      The following attributes are supported on each item:
      &lt;ul&gt;
      &lt;li&gt;title - The image title in HTML format.&lt;/li&gt;
      &lt;li&gt;titleNoFormatting - The image title in plain text&lt;/li&gt;
      &lt;li&gt;url - The URL for the image&lt;/li&gt;
      &lt;li&gt;unescapedUrl - The URL for the image, with URL escaping. This is often more readable&lt;/li&gt;
      &lt;li&gt;tbUrl - The URL for the image thumbnail&lt;/li&gt;
      &lt;li&gt;visibleUrl - A shortened version of the URL associated with the result, stripped of a protocol and path&lt;/li&gt;
      &lt;li&gt;originalContextUrl - The URL of the page containing the image.&lt;/li&gt;
      &lt;li&gt;width - The width of the image in pixels.&lt;/li&gt;
      &lt;li&gt;height - The height of the image in pixels.&lt;/li&gt;
      &lt;li&gt;tbWidth - The width of the image thumbnail in pixels.&lt;/li&gt;
      &lt;li&gt;tbHeight - The height of the image thumbnail in pixels.&lt;/li&gt;
      &lt;li&gt;content - A snippet of information about the image, in HTML format&lt;/li&gt;
      &lt;li&gt;contentNoFormatting - A snippet of information about the image, in plain text&lt;/li&gt;
      &lt;/ul&gt;
      The query accepts one parameter: text - The string to search for
  • dojox.data.GoogleImageSearchStore._type

    • summary
  • dojox.data.GoogleImageSearchStore._attributes

    • summary
  • dojox.data.GoogleImageSearchStore._aggregatedAttributes

    • type
      Object
    • summary
  • dojox.data

    • type
      Object
    • summary
  • dojox

    • type
      Object
    • summary