Removed json version of results from the ajax API. Improved checks for search existen...
[philo.git] / philo / contrib / sobol / static / sobol / ajax_search.js
diff --git a/philo/contrib/sobol/static/sobol/ajax_search.js b/philo/contrib/sobol/static/sobol/ajax_search.js
new file mode 100644 (file)
index 0000000..d4885b6
--- /dev/null
@@ -0,0 +1,33 @@
+(function($){
+       var sobol = window.sobol = {}
+       sobol.setup = function(){
+               var searches = sobol.searches = $('article.search');
+               for (i=0;i<searches.length;i++) {
+                       (function(){
+                               var s = searches[i];
+                               $.ajax({
+                                       url: s.getAttribute('data-url'),
+                                       dataType: 'json',
+                                       success: function(data){
+                                               $(s).removeClass('loading')
+                                               if (data['results'].length) {
+                                                       s.innerHTML += "<dl>" + data['results'].join("") + "</dl>";
+                                                       if(data['hasMoreResults'] && data['moreResultsURL']) s.innerHTML += "<footer><p><a href='" + data['moreResultsURL'] + "'>See more results</a></p></footer>";
+                                               } else {
+                                                       $(s).addClass('empty')
+                                                       s.innerHTML += "<p>No results found.</p>"
+                                               }
+                                       },
+                                       error: function(data, textStatus, errorThrown){
+                                               $(s).removeClass('loading');
+                                               text = errorThrown ? errorThrown : textStatus ? textStatus : "Error occurred."
+                                               if (errorThrown) {
+                                                       s.innerHTML += "<p>" + errorThrown + "</p>"
+                                               };
+                                       }
+                               });
+                       }());
+               };
+       };
+       $(sobol.setup);
+}(jQuery));
\ No newline at end of file