@ -2847,6 +2847,7 @@ function initTopicbar() {
topicDropdown . dropdown ( {
topicDropdown . dropdown ( {
allowAdditions : true ,
allowAdditions : true ,
forceSelection : false ,
fields : { name : "description" , value : "data-value" } ,
fields : { name : "description" , value : "data-value" } ,
saveRemoteData : false ,
saveRemoteData : false ,
label : {
label : {
@ -2864,18 +2865,50 @@ function initTopicbar() {
throttle : 500 ,
throttle : 500 ,
cache : false ,
cache : false ,
onResponse : function ( res ) {
onResponse : function ( res ) {
var formattedResponse = {
let formattedResponse = {
success : false ,
success : false ,
results : [ ] ,
results : [ ] ,
} ;
} ;
const stripTags = function ( text ) {
return text . replace ( /<[^>]*>?/gm , "" ) ;
} ;
let query = stripTags ( this . urlData . query . trim ( ) ) ;
let found _query = false ;
let current _topics = [ ] ;
topicDropdown . find ( 'div.label.visible.topic,a.label.visible' ) . each ( function ( _ , e ) { current _topics . push ( e . dataset . value ) ; } ) ;
if ( res . topics ) {
if ( res . topics ) {
formattedResponse . success = true ;
let found = false ;
for ( var i = 0 ; i < res . topics . length ; i ++ ) {
for ( let i = 0 ; i < res . topics . length ; i ++ ) {
formattedResponse . results . push ( { "description" : res . topics [ i ] . Name , "data-value" : res . topics [ i ] . Name } )
// skip currently added tags
if ( current _topics . indexOf ( res . topics [ i ] . Name ) != - 1 ) {
continue ;
}
if ( res . topics [ i ] . Name . toLowerCase ( ) === query . toLowerCase ( ) ) {
found _query = true ;
}
formattedResponse . results . push ( { "description" : res . topics [ i ] . Name , "data-value" : res . topics [ i ] . Name } ) ;
found = true ;
}
}
formattedResponse . success = found ;
}
}
if ( query . length > 0 && ! found _query ) {
formattedResponse . success = true ;
formattedResponse . results . unshift ( { "description" : query , "data-value" : query } ) ;
} else if ( query . length > 0 && found _query ) {
formattedResponse . results . sort ( function ( a , b ) {
if ( a . description . toLowerCase ( ) === query . toLowerCase ( ) ) return - 1 ;
if ( b . description . toLowerCase ( ) === query . toLowerCase ( ) ) return 1 ;
if ( a . description > b . description ) return - 1 ;
if ( a . description < b . description ) return 1 ;
return 0 ;
} ) ;
}
return formattedResponse ;
return formattedResponse ;
} ,
} ,
} ,
} ,