Created
February 28, 2013 16:49
-
-
Save thinkphp/5058128 to your computer and use it in GitHub Desktop.
Google Search based on CUI2 AMD and Router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(["cui","dom","event","jsonp","router"], function(cui, dom, evt, $, app){ | |
| //show me love to the module pattern | |
| var googlesearch = function(){ | |
| var f, | |
| resultscontainer, | |
| send, | |
| q, | |
| appid = 'B59F3913692A46D75ED39BA8F472DF267E24B611'; | |
| function init() { | |
| f = dom.select('#googleform'), | |
| send = dom.select('#send'), | |
| q = dom.select('#q'), | |
| resultscontainer = dom.select('#results'); | |
| if(f && resultscontainer && send && q) { | |
| dom.select("#googleform").Submit(function( evt ){ | |
| googlesearch.get( evt ) | |
| }); | |
| } | |
| } | |
| function get( e ) { | |
| evt.stopPropagation( e ) | |
| send.attr('value', 'Loading...') | |
| var searchterm = q.value; | |
| window.location = '#/search/' + searchterm | |
| } | |
| function incoming(o) { | |
| //for debugger | |
| if(window.console) { console.log(o); } | |
| dom.attr(dom.select('#send'), 'value','Go!') | |
| var out = ''; | |
| if(o.responseStatus == 200) { | |
| out = '<h2>Results</h2>' + | |
| '<div>' + | |
| renderWeb(o) + | |
| '</div>'; | |
| } else { | |
| out = '<div class="error"><h2>Error</h2>' + | |
| '<p>'+ o.responseDetails +'</p>' + | |
| '</div>'; | |
| } | |
| resultscontainer.html( out ) | |
| if(undefined !== resultscontainer.getElementsByTagName("a")[0]) { | |
| resultscontainer.getElementsByTagName("a")[0].focus(); | |
| } | |
| } | |
| function renderWeb(o) { | |
| var out = ''; | |
| var results = o.responseData.results; | |
| if(undefined !== results) { | |
| var all = results.length; | |
| out += '<h3>Web Results</h3><ol>'; | |
| for(var i=0;i<all;i++) { | |
| out += '<li><h4><a href="' + results[i].url + '">' + results[i].title + '</a></h4>' + | |
| '<p>' + results[i].content + '<span>(' + results[i].url + ')</span></p>'; | |
| } | |
| out += '</ol>'; | |
| } | |
| return out; | |
| } | |
| return {init : init, | |
| get : get, | |
| incoming: incoming}; | |
| }(); | |
| googlesearch.init() | |
| var f; | |
| app.get("/search/:val", f = function( data ){ | |
| //For debugging | |
| if(window.console) console.log(data) | |
| //set attribute value | |
| q.attr('value', data.val); | |
| //up-to-date value | |
| q.value = data.val; | |
| //set loading value to the button search | |
| send.attr('value','Loading...') | |
| //get the value to search | |
| var searchterm = data.val; | |
| //prepare the URL for REST GET request | |
| var url = 'http://ajax.googleapis.com/ajax/services/search/web?gl=en'+ | |
| '&userip=&'+ | |
| 'hl=en&'+ | |
| 'v=1.0&'+ | |
| 'start=0&'+ | |
| 'rsz=8'+ | |
| '&q='+searchterm+ | |
| '&callback=?'; | |
| //make a request JSONP | |
| $.jsonp(url, function( data ){ | |
| this.incoming( data ) | |
| //binding to object googlesearch | |
| }.binding( googlesearch )) | |
| }) | |
| //rounter execute | |
| app.run() | |
| //if we have hash in location then begin | |
| if(window.location.hash) { | |
| //get the search value from location.hash | |
| var _val = location.hash.split("/")[2] | |
| //set attribute value to the textfield input | |
| q.attr('value', _val); | |
| //call the function with this object | |
| f({val: _val}) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment