2024-04-04 14:37:20 +02:00
|
|
|
var InputfieldPageAutocomplete={init:function(id,url,labelField,searchField,operator){var $value=$("#"+id);var $ol=$("#"+id+"_items");var $input=$("#"+id+"_input");var $icon=$input.parent().find(".InputfieldPageAutocompleteStatus");var $note=$input.parent().find(".InputfieldPageAutocompleteNote");var numAdded=0;var numFound=0;var disableChars=$input.attr("data-disablechars");var noList=$input.hasClass("no_list");function hasDisableChar(str){if(!disableChars||!disableChars.length)return false;var disable=false;for(var n=0;n<disableChars.length;n++){if(str.indexOf(disableChars[n])>-1){disable=true;break}}return disable}InputfieldPageAutocomplete.setIconPosition($icon,"left");if(noList){$input.attr("data-selectedLabel",$input.val());var $remove=$input.siblings(".InputfieldPageAutocompleteRemove");InputfieldPageAutocomplete.setIconPosition($remove,"right");$remove.on("click",function(){$value.val("").trigger("change");$input.val("").attr("placeholder","").attr("data-selectedLabel","").trigger("change").trigger("focus");$input.trigger("keydown")});$input.on("change",function(){if($(this).val().length==0){$remove.hide()}else{$remove.show()}});$input.on("focus",function(){var val=$value.val();if(!val.length)return;if(hasDisableChar(val))return;if($(this).hasClass("added_item"))return;$(this).attr("placeholder",$(this).attr("data-selectedLabel"));$(this).val("")}).on("blur",function(){setTimeout(function(){},200)})}$icon.on("click",function(){$input.trigger("focus")});$icon.attr("data-class",$icon.attr("class"));function isAddAllowed(){var allowed=$("#_"+id.replace("Inputfield_","")+"_add_items").length>0;return allowed}$input.one("focus",function(){InputfieldPageAutocomplete.updateIcons($input.closest(".InputfieldContent"));$input.autocomplete({minLength:2,source:function(request,response){var term=request.term;if(hasDisableChar(term)){response([]);return}$icon.attr("class","fa fa-fw fa-spin fa-spinner");if($input.hasClass("and_words")&&term.indexOf(" ")>0){term=term.replace(/\s+/,",")}term=encodeURIComponent(term);var ajaxURL=url+"&"+searchField+operator+term;$.getJSON(ajaxURL,function(data){$icon.attr("class",$icon.attr("data-class"));numFound=data.total;if(data.total>0){$icon.attr("class","fa fa-fw fa-angle-double-down")}else if(isAddAllowed()){$icon.attr("class","fa fa-fw fa-plus-circle");$note.show()}else{$icon.attr("class","fa fa-fw fa-frown-o")}response($.map(data.matches,function(item){return{label:item[labelField],value:item[labelField],page_id:item.id}}))})},select:function(event,ui){if(!ui.item)return;var $t=$(this);if($t.hasClass("no_list")){$t.val(ui.item.label).trigger("change");$t.attr("data-selectedLabel",ui.item.label);$t.closest(".InputfieldPageAutocomplete").find(".InputfieldPageAutocompleteData").val(ui.item.page_id).trigger("change");$t.trigger("blur")}else{InputfieldPageAutocomplete.pageSelected($ol,ui.item);$t.val("").trigger("focus")}event.stopPropagation();return false},open:function(event,ui){var $items=$(".ui-autocomplete.ui-front");if(!$items.find("a").length){$items.find("div").each(function(){$(this).parent().html("<a>"+$(this).html()+"</a>")})}}}).on("blur",function(){var $input=$(this);$icon.attr("class",$icon.attr("data-class"));$note.hide();if($input.hasClass("no_list")){if($value.val().length||$input.val().length){if($input.hasClass("allow_any")||$input.hasClass("added_item")){}else{$input.val($input.attr("data-selectedLabel")).attr("placeholder","")}}else{$input.val("").attr("placeholder","").attr("data-selectedLabel","")}}if($input.hasClass("focus-after-blur")){$input.removeClass("focus-after-blur");setTimeout(function(){$input.trigger("focus")},250)}}).on("keyup",function(){$icon.attr("class",$icon.attr("data-class"))}).on("keydown",function(event){var $addNote;if(event.keyCode==13){event.preventDefault();if(isAddAllowed()){if($input.val().trim().length<1){$input.trigger("blur");return false}numAdded++;var page={page_id:-1*numAdded,label:$input.val()};if(noList){$value.val(page.page_id);$("#_"+id.replace("Inputfield_","")+"_add_items").val(page.label);$input.addClass("added_item").trigger(
|