praiadeseselle/wire/modules/Inputfield/InputfieldPageAutocomplete/InputfieldPageAutocomplete.min.js

1 line
8.2 KiB
JavaScript
Raw Permalink Normal View History

2022-03-08 15:55:41 +01: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.click(function(){$value.val("").change();$input.val("").attr("placeholder","").attr("data-selectedLabel","").change().focus();$input.trigger("keydown")});$input.change(function(){if($(this).val().length==0){$remove.hide()}else{$remove.show()}});$input.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("")}).blur(function(){setTimeout(function(){},200)})}$icon.click(function(){$input.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).change();$t.attr("data-selectedLabel",ui.item.label);$t.closest(".InputfieldPageAutocomplete").find(".InputfieldPageAutocompleteData").val(ui.item.page_id).change();$t.blur()}else{InputfieldPageAutocomplete.pageSelected($ol,ui.item);$t.val("").focus()}event.stopPropagation();return false}}).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.focus()},250)}}).keyup(function(){$icon.attr("class",$icon.attr("data-class"))}).keydown(function(event){var $addNote;if(event.keyCode==13){event.preventDefault();if(isAddAllowed()){if($.trim($input.val()).length<1){$input.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").blur();$addNote=$note.siblings(".InputfieldPageAutocompleteNoteAdd");if(!$addNote.length){$addNote=$("<div class='notes InputfieldPageAutocompleteNote InputfieldPageAutocompleteNoteAdd'></div>");$note.after($addNote)}$addNote.text($note.attr("data-adding")+" "+page.label);$addNote.show()}else{InputfieldPageAutocomplete.p