]> git.immae.eu Git - github/wallabag/wallabag.git/blame - themes/_global/js/autoCompleteTags.js
implement #1122
[github/wallabag/wallabag.git] / themes / _global / js / autoCompleteTags.js
CommitLineData
fb26cc93
MR
1jQuery(function($) {
2
3 function split( val ) {
4 return val.split( /,\s*/ );
5 }
6 function extractLast( term ) {
7 return split( term ).pop();
8 }
9
10
11 $("#value").bind("keydown", function(event) {
12 if (event.keyCode === $.ui.keyCode.TAB && $(this).data("ui-autocomplete").menu.active) {
13 event.preventDefault();
14 }
15 }).autocomplete({
16 source : function(request, response) {
17 $.getJSON("./?view=tags", {
18 term : extractLast(request.term),
19 //id: $(':hidden#entry_id').val()
20 }, response);
21 },
22 search : function() {
23 // custom minLength
24 var term = extractLast(this.value);
25 if (term.length < 1) {
26 return false;
27 }
28 },
29 focus : function() {
30 // prevent value inserted on focus
31 return false;
32 },
33 select : function(event, ui) {
34 var terms = split(this.value);
35 // remove the current input
36 terms.pop();
37 // add the selected item
38 terms.push(ui.item.value);
39 // add placeholder to get the comma-and-space at the end
40 terms.push("");
41 this.value = terms.join(", ");
42 return false;
43 }
44 });
45
512ff180
TC
46 $('.suggestedtag').click(function(){
47 var input = $("#value");
48 var value = input.val();
49 var tag = $(this).text();
50 var terms = value.split(','); // tags into the <input>
51 if (jQuery.inArray(tag, terms) == -1 ) { // if the tag hasn't already been added
52 value += tag + ",";
53 input.val(value);
54 }
55 input.focus();
56 input[0].selectionStart = input[0].selectionEnd = input.val().length;
57
58 });
59
fb26cc93
MR
60
61});