1
|
|
|
/** |
2
|
|
|
* Inspired by http://jqueryui.com/demos/autocomplete/#multiple |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
(function( $ ) { |
6
|
|
|
$.widget('ui.multiple_autocomplete', { |
7
|
|
|
_create: function() { |
8
|
|
|
var self = this; |
9
|
|
|
function split( val ) { |
10
|
|
|
return val.split( /,\s*/ ); |
11
|
|
|
} |
12
|
|
|
function extractLast( term ) { |
13
|
|
|
return split( term ).pop(); |
14
|
|
|
} |
15
|
|
|
function showOptions() { |
16
|
|
|
if(!self.element.autocomplete('widget').is(':visible') && self.element.val().trim() == '') { |
17
|
|
|
self.element.autocomplete('search', ''); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
//console.log('_create: ' + this.options['id']); |
21
|
|
|
this.element.bind('click', function( event ) { |
22
|
|
|
showOptions(); |
23
|
|
|
}); |
24
|
|
|
this.element.bind('input', function( event ) { |
25
|
|
|
showOptions(); |
26
|
|
|
}); |
27
|
|
|
this.element.bind('blur', function( event ) { |
28
|
|
|
var tmp = self.element.val().trim(); |
29
|
|
|
if(tmp[tmp.length-1] == ',') { |
30
|
|
|
self.element.val(tmp.substring(0, tmp.length-1)); |
31
|
|
|
} else { |
32
|
|
|
self.element.val(tmp); |
33
|
|
|
} |
34
|
|
|
if(self.element.val().trim() != '') { |
35
|
|
|
self.element.trigger('change'); // Changes wasn't saved when only using the dropdown. |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
this.element.bind( "keydown", function( event ) { |
|
|
|
|
39
|
|
|
if ( event.keyCode === $.ui.keyCode.TAB && |
40
|
|
|
$( this ).data( "autocomplete" ).menu.active ) { |
|
|
|
|
41
|
|
|
event.preventDefault(); |
42
|
|
|
} |
43
|
|
|
}) |
44
|
|
|
.autocomplete({ |
45
|
|
|
minLength: 0, |
46
|
|
|
source: function( request, response ) { |
47
|
|
|
// delegate back to autocomplete, but extract the last term |
48
|
|
|
response( $.ui.autocomplete.filter( |
49
|
|
|
self.options.source, extractLast( request.term ) ) ); |
50
|
|
|
}, |
51
|
|
|
focus: function() { |
52
|
|
|
// prevent value inserted on focus |
53
|
|
|
return false; |
54
|
|
|
}, |
55
|
|
|
select: function( event, ui ) { |
56
|
|
|
var terms = split( this.value ); |
57
|
|
|
// remove the current input |
58
|
|
|
terms.pop(); |
59
|
|
|
// add the selected item |
60
|
|
|
terms.push( ui.item.value ); |
61
|
|
|
// add placeholder to get the comma-and-space at the end |
62
|
|
|
terms.push( "" ); |
|
|
|
|
63
|
|
|
this.value = terms.join( ", " ); |
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
}); |
67
|
|
|
/*this.button = $( "<button type='button'> </button>" ) |
68
|
|
|
.attr( "tabIndex", -1 ) |
69
|
|
|
.attr( "title", "Show All Items" ) |
70
|
|
|
.insertAfter( this.element ) |
71
|
|
|
.addClass('svg') |
72
|
|
|
.addClass('action') |
73
|
|
|
.addClass('combo-button') |
74
|
|
|
.click(function() { |
75
|
|
|
// close if already visible |
76
|
|
|
if ( self.element.autocomplete( "widget" ).is( ":visible" ) ) { |
77
|
|
|
self.element.autocomplete( "close" ); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// work around a bug (likely same cause as #5265) |
82
|
|
|
$( this ).blur(); |
83
|
|
|
|
84
|
|
|
var tmp = self.element.val().trim(); |
85
|
|
|
if(tmp[tmp.length-1] != ',') { |
86
|
|
|
self.element.val(tmp+', '); |
87
|
|
|
} |
88
|
|
|
// pass empty string as value to search for, displaying all results |
89
|
|
|
self.element.autocomplete( "search", "" ); |
90
|
|
|
self.element.focus(); |
91
|
|
|
});*/ |
92
|
|
|
}, |
93
|
|
|
}); |
94
|
|
|
})( jQuery ); |
95
|
|
|
|