From 8e3251c9154ddfd236b48494c9c806666690fd76 Mon Sep 17 00:00:00 2001 From: Rocky Duan Date: Sun, 29 Jul 2012 17:32:00 -0400 Subject: [PATCH] basic tag autocomplete --- common/static/js/vendor/jquery.tagsinput.js | 13 +++++++++++-- lms/djangoapps/django_comment_client/base/urls.py | 1 + lms/djangoapps/django_comment_client/base/views.py | 9 ++++++++- lms/lib/comment_client.py | 6 ++++++ lms/static/coffee/src/discussion.coffee | 5 ++++- lms/templates/discussion/index.html | 2 ++ lms/templates/discussion/thread.html | 3 --- 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/common/static/js/vendor/jquery.tagsinput.js b/common/static/js/vendor/jquery.tagsinput.js index dd39357e82..4ce21b026d 100644 --- a/common/static/js/vendor/jquery.tagsinput.js +++ b/common/static/js/vendor/jquery.tagsinput.js @@ -258,16 +258,26 @@ } if (jQuery.Autocompleter !== undefined) { + onSelectCallback = settings.autocomplete.onItemSelect; + settings.autocomplete.onItemSelect = function() { + console.log("here"); + $(data.real_input).addTag($(data.fake_input).val(), {focus: true, unique: (settings.unique)}); + $(data.fake_input).resetAutosize(settings); + if (onSelectCallback) { + onSelectCallback(); + } + } $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); $(data.fake_input).bind('result',data,function(event,data,formatted) { if (data) { $('#'+id).addTag(data[0] + "",{focus:true,unique:(settings.unique)}); } - }); + }); } else if (jQuery.ui.autocomplete !== undefined) { $(data.fake_input).autocomplete(autocomplete_options); $(data.fake_input).bind('autocompleteselect',data,function(event,ui) { $(event.data.real_input).addTag(ui.item.value,{focus:true,unique:(settings.unique)}); + return false; }); } @@ -299,7 +309,6 @@ return false; } else if (event.data.autosize) { $(event.data.fake_input).doAutosize(settings); - } }); //Delete last tag on backspace diff --git a/lms/djangoapps/django_comment_client/base/urls.py b/lms/djangoapps/django_comment_client/base/urls.py index 5c0c65f859..49c11324b8 100644 --- a/lms/djangoapps/django_comment_client/base/urls.py +++ b/lms/djangoapps/django_comment_client/base/urls.py @@ -4,6 +4,7 @@ import django_comment_client.base.views urlpatterns = patterns('django_comment_client.base.views', url(r'upload$', 'upload', name='upload'), + url(r'threads/tags/autocomplete$', 'tags_autocomplete', name='tags_autocomplete'), url(r'threads/(?P[\w\-]+)/update$', 'update_thread', name='update_thread'), url(r'threads/(?P[\w\-]+)/reply$', 'create_comment', name='create_comment'), url(r'threads/(?P[\w\-]+)/delete', 'delete_thread', name='delete_thread'), diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 0a7d9027da..ac57742ca6 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -188,7 +188,6 @@ def unfollow(request, course_id, followed_user_id): response = comment_client.unfollow(user_id, followed_user_id) return JsonResponse(response) -@login_required @require_GET def search(request, course_id): text = request.GET.get('text', None) @@ -201,6 +200,14 @@ def search(request, course_id): }) return JsonResponse(response) +@require_GET +def tags_autocomplete(request, course_id): + value = request.GET.get('q', None) + results = [] + if value: + results = comment_client.tags_autocomplete(value) + return JsonResponse(results) + @csrf.csrf_exempt @login_required @require_POST diff --git a/lms/lib/comment_client.py b/lms/lib/comment_client.py index 40da1f2912..a23313637a 100644 --- a/lms/lib/comment_client.py +++ b/lms/lib/comment_client.py @@ -14,6 +14,9 @@ def get_threads(commentable_id, recursive=False, *args, **kwargs): def get_threads_tags(*args, **kwargs): return _perform_request('get', _url_for_threads_tags(), {}, *args, **kwargs) +def tags_autocomplete(value, *args, **kwargs): + return _perform_request('get', _url_for_threads_tags_autocomplete(), {'value': value}, *args, **kwargs) + def create_thread(commentable_id, attributes, *args, **kwargs): return _perform_request('post', _url_for_threads(commentable_id), attributes, *args, **kwargs) @@ -132,3 +135,6 @@ def _url_for_search_threads(): def _url_for_threads_tags(): return "{prefix}/threads/tags".format(prefix=PREFIX) + +def _url_for_threads_tags_autocomplete(): + return "{prefix}/threads/tags/autocomplete".format(prefix=PREFIX) diff --git a/lms/static/coffee/src/discussion.coffee b/lms/static/coffee/src/discussion.coffee index 45bd207e4c..a60c8ff6f0 100644 --- a/lms/static/coffee/src/discussion.coffee +++ b/lms/static/coffee/src/discussion.coffee @@ -45,6 +45,7 @@ Discussion = downvote_comment : "/courses/#{$$course_id}/discussion/comments/#{param}/downvote" upload : "/courses/#{$$course_id}/discussion/upload" search : "/courses/#{$$course_id}/discussion/forum/search" + tags_autocomplete : "/courses/#{$$course_id}/discussion/threads/tags/autocomplete" }[name] handleAnchorAndReload: (response) -> @@ -128,7 +129,9 @@ Discussion = if $$tags? $local(".new-post-tags").tagsInput - autocomplete: $$tags + autocomplete_url: Discussion.urlFor('tags_autocomplete') + autocomplete: + remoteDataType: 'json' interactive: true defaultText: "add a tag" height: "30px" diff --git a/lms/templates/discussion/index.html b/lms/templates/discussion/index.html index 57fc69e9f9..2f2abf8c58 100644 --- a/lms/templates/discussion/index.html +++ b/lms/templates/discussion/index.html @@ -32,8 +32,10 @@ + + <%include file="../course_navigation.html" args="active_page='discussion'" /> diff --git a/lms/templates/discussion/thread.html b/lms/templates/discussion/thread.html index 33ebe1f71d..8b0a3406fa 100644 --- a/lms/templates/discussion/thread.html +++ b/lms/templates/discussion/thread.html @@ -3,8 +3,6 @@ <%! from dateutil.parser import parse %> <%! import urllib %> - - <%def name="render_thread(course_id, thread, edit_thread=False, show_comments=False)"> <% if show_comments: @@ -93,7 +91,6 @@ Watch - <%def name="render_vote(content)"> <% upvote = "˄"