changeset 2530:bfb653c10406

Adds AJAX search feature, for JS disabled, fallback mode and search still works
author sharky93 <rishabhr123@gmail.com>
date Mon, 22 Jul 2013 03:29:40 +0530
parents 9d9f6a7be79e
children 7ec50a76110a
files MoinMoin/apps/frontend/views.py MoinMoin/static/js/search.js MoinMoin/templates/ajaxsearch.html MoinMoin/templates/search.html
diffstat 4 files changed, 113 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Mon Jul 08 15:01:02 2013 +0530
+++ b/MoinMoin/apps/frontend/views.py	Mon Jul 22 03:29:40 2013 +0530
@@ -255,10 +255,15 @@
 @frontend.route('/+search', defaults=dict(item_name=u''), methods=['GET', 'POST'])
 def search(item_name):
     search_form = SearchForm.from_flat(request.values)
+    ajax = True if request.args.get('boolajax') else False
     valid = search_form.validate()
-    query = search_form['q'].value
-    if valid:
+    if ajax:
+        query = request.args.get('q')
+        history = request.args.get('history')
+    else:
+        query = search_form['q'].value
         history = bool(request.values.get('history'))
+    if valid or ajax:
         idx_name = ALL_REVS if history else LATEST_REVS
         qp = flaskg.storage.query_parser([NAME_EXACT, NAME, SUMMARY, CONTENT], idx_name=idx_name)
         q = qp.parse(query)
@@ -300,14 +305,21 @@
             content_suggestions = [word for word, score in results.key_terms(CONTENT, docs=20, numterms=10)]
             flaskg.clock.stop('search suggestions')
             flaskg.clock.start('search render')
-            html = render_template('search.html',
+            if ajax:
+                html = render_template('ajaxsearch.html',
+                                   results=results,
+                                   name_suggestions=u', '.join(name_suggestions),
+                                   content_suggestions=u', '.join(content_suggestions),
+                )
+            else:
+                html = render_template('search.html',
                                    results=results,
                                    name_suggestions=u', '.join(name_suggestions),
                                    content_suggestions=u', '.join(content_suggestions),
                                    query=query,
                                    medium_search_form=search_form,
                                    item_name=item_name,
-            )
+                )
             flaskg.clock.stop('search render')
     else:
         html = render_template('search.html',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/static/js/search.js	Mon Jul 22 03:29:40 2013 +0530
@@ -0,0 +1,27 @@
+$(document).ready(function(){
+	// kill form action on pressing Enter
+	$('#moin-long-searchform').submit(function(e){
+		e.preventDefault();
+		return false;
+	});
+
+	// hide form submit button
+	$('#moin-long-searchform .button').hide();
+
+	function ajaxify(query, allrevs) {
+		$.ajax({
+  			type: "GET",
+  			url: "/+search",
+  			data: { q: query, history: allrevs, boolajax: true }
+		}).done(function( html ) {
+			$('#finalresults').html(html)
+		});
+	}
+	$('#moin-search-query').keyup(function() {
+		var allrev = false
+		if($('[name="history"]').prop('checked')){
+ 			allrev = true;
+		}		
+  		ajaxify($(this).val(), allrev);
+	});
+});
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/templates/ajaxsearch.html	Mon Jul 22 03:29:40 2013 +0530
@@ -0,0 +1,61 @@
+{% if results is defined %}
+    <p class="searchstats">
+        {% if results %}
+        {{ _("%(result_len)d results found (%(runtime).3f secs).",
+              result_len=results|length, runtime=results.runtime
+            )
+        }}
+        {% else %}
+        {{ _("No results found (%(runtime).3f secs).", runtime=results.runtime) }}
+        {% endif %}
+    </p>
+    {% endif %}
+
+    {% if results is defined %}
+        {% if name_suggestions or content_suggestions %}
+            <p>{{ _("name term suggestions: %(termlist)s", termlist=name_suggestions) }}</p>
+            <p>{{ _("content term suggestions: %(termlist)s", termlist=content_suggestions) }}</p>
+        {% endif %}
+        {% if results %}
+            <div class="searchresults">
+                <table>
+                    {% for result in results %}
+                        {% if result['wikiname'] == cfg.interwikiname %}
+                            <tr>
+                                <td class="moin-wordbreak">{{ result.pos + 1 }}
+                                <a href="{{ url_for_item(item_name=result['name'][0], wiki_name='Self', rev=result['revid']) }}"><b>{{ result['name'] | join(' | ')}}</b></a>
+                                </td>
+                            </tr>
+                            {% if result['summary'] %}
+                                <tr>
+                                    <td>
+                                        <p class="info searchhitinfobar">{{ _("Summary: %(summary)s", summary=result['summary']) }}</p>
+                                    </td>
+                                </tr>
+                            {% endif %}
+                            <tr>
+                                <td>
+                                    <p class="info searchhitinfobar">{{ _("Revision: %(revid)s Last Change: %(mtime)s", revid=result['revid']|shorten_id, mtime=result['mtime']|datetimeformat) }}</p>
+                                </td>
+                            </tr>
+                            <tr>
+                                <td>
+                                    {% if user.may.read(result['name']) %}
+                                        <p class="info foundtext">{{ result.highlights('content')|safe }}</p>
+                                    {% else %}
+                                        <p class="info foundtext">{{ _("You don't have read permission for this item.") }}</p>
+                                    {% endif %}
+                                </td>
+                            </tr>
+                        {% else %}
+                            <tr>
+                                <td class="moin-wordbreak">{{ result.pos + 1 }}
+                                <a class="moin-interwiki" href="{{ url_for_item(item_name=result['name'], wiki_name=result['wikiname'], rev=result['revid']) }}"><b>{{ "%s:%s" % (result['wikiname'], result['name']) }}</b></a>
+                                </td>
+                            </tr>
+                        {% endif %}
+                    {% endfor %}
+                </table>
+            </div>
+        {% endif %}
+{% endif %}
--- a/MoinMoin/templates/search.html	Mon Jul 08 15:01:02 2013 +0530
+++ b/MoinMoin/templates/search.html	Mon Jul 22 03:29:40 2013 +0530
@@ -9,19 +9,7 @@
     {{ super() }}
 {% endblock %}
 
-{% block content %}
-    {% if results is defined %}
-    <p class="searchstats">
-        {% if results %}
-        {{ _("%(result_len)d results found (%(runtime).3f secs).",
-              result_len=results|length, runtime=results.runtime
-            )
-        }}
-        {% else %}
-        {{ _("No results found (%(runtime).3f secs).", runtime=results.runtime) }}
-        {% endif %}
-    </p>
-    {% endif %}
+{% block content %}    
     {{ gen.form.open(medium_search_form, id='moin-long-searchform', method='get', action=url_for('frontend.search', item_name=item_name)) }}
         <p>
         {{ forms.render(medium_search_form['q']) }}
@@ -32,52 +20,12 @@
         {{ forms.render_errors(medium_search_form) }}
         </p>
     {{ gen.form.close() }}
-    {% if results is defined %}
-        {% if name_suggestions or content_suggestions %}
-            <p>{{ _("name term suggestions: %(termlist)s", termlist=name_suggestions) }}</p>
-            <p>{{ _("content term suggestions: %(termlist)s", termlist=content_suggestions) }}</p>
-        {% endif %}
-        {% if results %}
-            <div class="searchresults">
-                <table>
-                    {% for result in results %}
-                        {% if result['wikiname'] == cfg.interwikiname %}
-                            <tr>
-                                <td class="moin-wordbreak">{{ result.pos + 1 }}
-                                <a href="{{ url_for_item(item_name=result['name'][0], wiki_name='Self', rev=result['revid']) }}"><b>{{ result['name'] | join(' | ')}}</b></a>
-                                </td>
-                            </tr>
-                            {% if result['summary'] %}
-                                <tr>
-                                    <td>
-                                        <p class="info searchhitinfobar">{{ _("Summary: %(summary)s", summary=result['summary']) }}</p>
-                                    </td>
-                                </tr>
-                            {% endif %}
-                            <tr>
-                                <td>
-                                    <p class="info searchhitinfobar">{{ _("Revision: %(revid)s Last Change: %(mtime)s", revid=result['revid']|shorten_id, mtime=result['mtime']|datetimeformat) }}</p>
-                                </td>
-                            </tr>
-                            <tr>
-                                <td>
-                                    {% if user.may.read(result['name']) %}
-                                        <p class="info foundtext">{{ result.highlights('content')|safe }}</p>
-                                    {% else %}
-                                        <p class="info foundtext">{{ _("You don't have read permission for this item.") }}</p>
-                                    {% endif %}
-                                </td>
-                            </tr>
-                        {% else %}
-                            <tr>
-                                <td class="moin-wordbreak">{{ result.pos + 1 }}
-                                <a class="moin-interwiki" href="{{ url_for_item(item_name=result['name'], wiki_name=result['wikiname'], rev=result['revid']) }}"><b>{{ "%s:%s" % (result['wikiname'], result['name']) }}</b></a>
-                                </td>
-                            </tr>
-                        {% endif %}
-                    {% endfor %}
-                </table>
-            </div>
-        {% endif %}
-    {% endif %}
+    <div id="finalresults">
+        {% include "ajaxsearch.html" %}
+    </div>
 {% endblock %}
+
+{% block body_scripts %}
+    <script src="{{ url_for('serve.files', name='jquery', filename='jquery.min.js') }}"></script>
+    <script src="{{ url_for('static', filename='js/search.js') }}"></script>
+{% endblock %}
\ No newline at end of file