Added docs for result list template. Moved success/error handling into hooks on sobol...
authorStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 9 Jun 2011 17:16:56 +0000 (13:16 -0400)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Thu, 9 Jun 2011 17:16:56 +0000 (13:16 -0400)
philo/contrib/sobol/__init__.py
philo/contrib/sobol/models.py
philo/contrib/sobol/search.py
philo/contrib/sobol/static/sobol/ajax_search.js
philo/contrib/sobol/templates/sobol/search/_list.html

index 74ca4f1..0458a83 100644 (file)
@@ -10,6 +10,13 @@ Settings
 :setting:`SOBOL_USE_EVENTLET`
        If :mod:`eventlet` is installed and this setting is ``True``, sobol web searches will use :mod:`eventlet.green.urllib2` instead of the built-in :mod:`urllib2` module. Default: ``False``.
 
+Templates
+---------
+
+For convenience, :mod:`.sobol` provides a template at ``sobol/search/_list.html`` which can be used with an ``{% include %}`` tag inside a full search page template to list the search results. The ``_list.html`` template also uses a basic jQuery script (``static/sobol/ajax_search.js``) to handle AJAX search result loading if the AJAX API of the current :class:`.SearchView` is enabled. If you want to use ``_list.html``, but want to provide your own version of jQuery or your own AJAX loading script, or if you want to include the basic script somewhere else (like inside the ``<head>``) simply do the following::
+
+       {% include "sobol/search/_list.html" with suppress_scripts=1 %}
+
 """
 
 from philo.contrib.sobol.search import *
\ No newline at end of file
index 27f91d1..1bef3cd 100644 (file)
@@ -287,6 +287,7 @@ class SearchView(MultiView):
                search_instance = get_search_instance(slug, search_string)
                
                return HttpResponse(json.dumps({
+                       'search': search_instance.slug,
                        'results': [result.render() for result in search_instance.results],
                        'hasMoreResults': search_instance.has_more_results,
                        'moreResultsURL': (u"?%s" % search_instance.more_results_querydict.urlencode()) if search_instance.more_results_querydict else None,
index b117eaa..2c31158 100644 (file)
@@ -116,7 +116,9 @@ def get_search_instance(slug, search_arg):
                cached = cache.get(key)
                if cached:
                        return cached
-       return search(search_arg)
+       instance = search(search_arg)
+       instance.slug = slug
+       return instance
        
 
 
index d4885b6..33fdf4f 100644 (file)
@@ -1,33 +1,40 @@
 (function($){
-       var sobol = window.sobol = {}
-       sobol.setup = function(){
+       var sobol = window.sobol = {};
+       sobol.search = function(){
                var searches = sobol.searches = $('article.search');
-               for (i=0;i<searches.length;i++) {
+               for (var 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>"
-                                               }
+                                               sobol.onSuccess($(s), data);
                                        },
                                        error: function(data, textStatus, errorThrown){
-                                               $(s).removeClass('loading');
-                                               text = errorThrown ? errorThrown : textStatus ? textStatus : "Error occurred."
-                                               if (errorThrown) {
-                                                       s.innerHTML += "<p>" + errorThrown + "</p>"
-                                               };
+                                               sobol.onError($(s), textStatus, errorThrown);
                                        }
                                });
                        }());
                };
+       }
+       sobol.onSuccess = function(ele, data){
+               // hook for success!
+               ele.removeClass('loading')
+               if (data['results'].length) {
+                       ele[0].innerHTML += "<dl>" + data['results'].join("") + "</dl>";
+                       if(data['hasMoreResults'] && data['moreResultsURL']) ele[0].innerHTML += "<footer><p><a href='" + data['moreResultsURL'] + "'>See more results</a></p></footer>";
+               } else {
+                       ele.addClass('empty');
+                       ele[0].innerHTML += "<p>No results found.</p>";
+                       ele.slideUp();
+               }
        };
-       $(sobol.setup);
+       sobol.onError = function(ele, textStatus, errorThrown){
+               // Hook for error...
+               ele.removeClass('loading');
+               text = errorThrown ? errorThrown : textStatus ? textStatus : "Error occurred.";
+               ele[0].innerHTML += "<p>" + text + "</p>";
+       };
+       $(sobol.search);
 }(jQuery));
\ No newline at end of file
index f78d861..8fed939 100644 (file)
@@ -1,21 +1,25 @@
 {% with node.view.enable_ajax_api as ajax %}
-{% if ajax %}<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script><script type="text/javascript" src="{{ STATIC_URL }}sobol/ajax_search.js"></script>{% endif %}
+{% if ajax and not suppress_scripts %}<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script><script type="text/javascript" src="{{ STATIC_URL }}sobol/ajax_search.js"></script>{% endif %}
 {% for search in searches %}
-<article {% if ajax %}class="search loading {{ search.slug }}" data-url="{{ search.ajax_api_url }}" data-title="{{ search }}"{% else %}class="search {{ search.slug }}"{% endif %}>
+<article {% if ajax %}class="search loading {{ search.slug }}" data-url="{{ search.ajax_api_url }}"{% else %}class="search {{ search.slug }}{% if not search.results %} empty{% endif %}"{% endif %}>
        <header>
                <a name='{{ search.slug }}'></a>
                <h1>{{ search }}</h1>
        </header>
        {% if not ajax %}
-               <dl>
-               {% for result in search.results %}
-               {{ result }}
-               {% endfor %}
-               </dl>
-               {% if search.has_more_results and search.more_results_url %}
-               <footer>
-                       <p><a href="?{{ search.more_results_querydict.urlencode }}">See more results</a></p>
-               </footer>
+               {% if search.results %}
+                       <dl>
+                       {% for result in search.results %}
+                       {{ result }}
+                       {% endfor %}
+                       </dl>
+                       {% if search.has_more_results and search.more_results_url %}
+                       <footer>
+                               <p><a href="?{{ search.more_results_querydict.urlencode }}">See more results</a></p>
+                       </footer>
+                       {% endif %}
+               {% else %}
+                       <p>No results found.</p>
                {% endif %}
        {% endif %}
 </article>