From 85d5f606f656fc8680e28baf3d58d24898d23261 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 18 Dec 2012 06:14:05 +0300 Subject: [PATCH] Labels autocomplete --- app/assets/javascripts/main.js.coffee | 6 +++++ app/helpers/issues_helper.rb | 6 +++++ app/views/issues/_form.html.haml | 33 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/app/assets/javascripts/main.js.coffee b/app/assets/javascripts/main.js.coffee index 3334439c..f6c398c0 100644 --- a/app/assets/javascripts/main.js.coffee +++ b/app/assets/javascripts/main.js.coffee @@ -13,6 +13,12 @@ window.errorMessage = (message) -> ehtml.html(message) ehtml +window.split = (val) -> + return val.split( /,\s*/ ) + +window.extractLast = (term) -> + return split( term ).pop() + # Disable button if text field is empty window.disableButtonIfEmptyField = (field_selector, button_selector) -> field = $(field_selector) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 6b21f463..264b764f 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -30,4 +30,10 @@ module IssuesHelper open: "open" } end + + def labels_autocomplete_source + labels = @project.issues_labels.order('count DESC') + labels = labels.map{ |l| { label: l.name, value: l.name } } + labels.to_json + end end diff --git a/app/views/issues/_form.html.haml b/app/views/issues/_form.html.haml index 670b4e05..8a9a91a3 100644 --- a/app/views/issues/_form.html.haml +++ b/app/views/issues/_form.html.haml @@ -55,3 +55,36 @@ = link_to "Cancel", project_issues_path(@project), class: cancel_class - else = link_to "Cancel", project_issue_path(@project, @issue), class: cancel_class + + + + +:javascript + $(function(){ + $("#issue_label_list") + .bind( "keydown", function( event ) { + if ( event.keyCode === $.ui.keyCode.TAB && + $( this ).data( "autocomplete" ).menu.active ) { + event.preventDefault(); + } + }) + .autocomplete({ + minLength: 0, + source: function( request, response ) { + response( $.ui.autocomplete.filter( + #{raw labels_autocomplete_source}, extractLast( request.term ) ) ); + }, + focus: function() { + return false; + }, + select: function(event, ui) { + var terms = split( this.value ); + terms.pop(); + terms.push( ui.item.value ); + terms.push( "" ); + this.value = terms.join( ", " ); + return false; + } + }); + }); +