@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-License-Identifier: AGPL-3.0-or-later
$ . onmount ( "[data-js-autocomplete-input]" , function ( ) {
$ . onmount ( "[data-js-autocomplete-input]" , function ( ) {
function addChip ( $input ) {
function addChip ( $input , userTypedComma ) {
var $autocompleteContainer = $input
var $autocompleteContainer = $input
. parents ( "[data-js-autocomplete-container]" )
. parents ( "[data-js-autocomplete-container]" )
. first ( ) ;
. first ( ) ;
@ -11,7 +11,8 @@ $.onmount("[data-js-autocomplete-input]", function() {
. first ( ) ;
. first ( ) ;
var $tagsHiddenInput = $ ( "[data-js-autocomplete-hidden-input]" ) ;
var $tagsHiddenInput = $ ( "[data-js-autocomplete-hidden-input]" ) ;
$input
var remaining = "" ;
var inputTags = $input
. val ( )
. val ( )
. split ( "," )
. split ( "," )
. map ( function ( tag ) {
. map ( function ( tag ) {
@ -19,8 +20,15 @@ $.onmount("[data-js-autocomplete-input]", function() {
} )
} )
. filter ( function ( tag ) {
. filter ( function ( tag ) {
return tag !== "" ;
return tag !== "" ;
} )
. forEach ( function ( tag ) {
} ) ;
// process only first tag, to left of comma, if user typed comma
if ( userTypedComma && $input . val ( ) . indexOf ( "," ) !== - 1 ) {
remaining = inputTags . slice ( 1 ) . join ( ", " ) ;
inputTags = inputTags . slice ( 0 , 1 ) ;
}
inputTags . forEach ( function ( tag ) {
if ( ! $tagsHiddenInput . val ( ) . match ( new RegExp ( "(^|,)" + tag + "," ) ) ) {
if ( ! $tagsHiddenInput . val ( ) . match ( new RegExp ( "(^|,)" + tag + "," ) ) ) {
var clearIcon = document . createElement ( "a" ) ;
var clearIcon = document . createElement ( "a" ) ;
clearIcon . classList . add ( "btn" ) ;
clearIcon . classList . add ( "btn" ) ;
@ -48,7 +56,7 @@ $.onmount("[data-js-autocomplete-input]", function() {
$tagsHiddenInput . val ( $tagsHiddenInput . val ( ) + tag + "," ) ;
$tagsHiddenInput . val ( $tagsHiddenInput . val ( ) + tag + "," ) ;
}
}
} ) ;
} ) ;
$autocompleteContainer . find ( "[data-js-autocomplete-input]" ) . val ( "" ) ;
$autocompleteContainer . find ( "[data-js-autocomplete-input]" ) . val ( remaining ) ;
$autocompleteContainer . find ( "[data-js-autocomplete-suggestions]" ) . html ( "" ) ;
$autocompleteContainer . find ( "[data-js-autocomplete-suggestions]" ) . html ( "" ) ;
$ . onmount ( ) ;
$ . onmount ( ) ;
@ -102,6 +110,7 @@ $.onmount("[data-js-autocomplete-input]", function() {
} ) ;
} ) ;
$ ( this ) . keydown ( function ( event ) {
$ ( this ) . keydown ( function ( event ) {
var $this = $ ( this ) ;
var $autocompleteMenu = $ ( "[data-js-autocomplete-menu]" ) . first ( ) ;
var $autocompleteMenu = $ ( "[data-js-autocomplete-menu]" ) . first ( ) ;
var $nextActiveItem = null ;
var $nextActiveItem = null ;
@ -111,6 +120,11 @@ $.onmount("[data-js-autocomplete-input]", function() {
$ ( this ) . blur ( ) ;
$ ( this ) . blur ( ) ;
break ;
break ;
case "," :
case "," :
// wait for comma to be added to text so addChip() sees it
setTimeout ( function ( ) {
addChip ( $this , true ) ;
} , 100 ) ;
break ;
case "Enter" :
case "Enter" :
event . preventDefault ( ) ;
event . preventDefault ( ) ;
addChip ( $ ( this ) ) ;
addChip ( $ ( this ) ) ;