artabro/wire/modules/Inputfield/InputfieldPage/InputfieldPage.min.js

1 line
3.5 KiB
JavaScript
Raw Permalink Normal View History

2024-08-27 11:35:37 +02:00
function initInputfieldPage($this){$this.find("p.InputfieldPageAddButton a").on("click",function(){var $input=$(this).parent("p").next(".InputfieldPageAddItems");if($input.is(":visible"))$input.slideUp("fast").find(":input").val("");else $input.slideDown("fast").parents(".ui-widget-content").slice(0,1).effect("highlight",{},500);return false});initInputfieldPageDependentSelects($this)}function initInputfieldPageDependentSelects($inputfieldPage){function selectChanged($select1,$select2,selector,formatName,labelFieldName,part,changed){var v=$select1.val();if(v==null){if($select2.children().length){$select2.children().remove();$select2.trigger("change")}return}v=v.toString();v=v.replace(/,/g,"|");selector=selector.replace(part,"="+v);selector=selector.replace(/,\s*/g,"&");selector=selector.replace(/\./g,"-");if(selector.indexOf("_LPID"))selector=selector.replace(/_LPID[0-9]+/g,"");var url=ProcessWire.config.urls.admin+"page/search/for?"+selector+"&limit=9999&get="+labelFieldName;if(formatName.length)url+="&format_name="+formatName;$.getJSON(url,{},function(data){var numSelected=0;$select2.children().addClass("option-tbd");for(var n=0;n<data.matches.length;n++){var selected=false;var page=data.matches[n];var label="";var $option=$select2.children("[value="+page.id+"]");if($option.length>0)selected=$option.is(":selected")||$option.is(":checked");if(selected)numSelected++;$option.remove();if(formatName.length)label=page[formatName];if(!label.length)label=page[labelFieldName];if(!label.length)label=page.name;$option=$("<option />").attr("value",page.id).text(label);if(selected)$option.attr("selected","selected");$select2.append($option)}if(!$select2.attr("multiple")){$blankOption=$("<option value=''>&nbsp;</option>");if(!numSelected)$blankOption.attr("selected","selected");$select2.prepend($blankOption)}$select2.children(".option-tbd").remove();if(changed||$select2.closest(".InputfieldAsmSelect").length){$select2.trigger("change")}})}function initSelector($t){var selector=$t.val();var selectorHasPage=selector.indexOf("=page.")>-1;var selectorHasItem=selector.indexOf("=item.")>-1;if(!selectorHasPage&&!selectorHasItem)return;if(selectorHasItem)selector=selector.replaceAll("=item.","=page.");var labelFieldName=$t.attr("data-label");var formatName=$t.attr("data-formatname");var $repeaterItems=$t.parents(".InputfieldRepeaterItem");if(!labelFieldName.length)$labelFieldName="name";var $wrap=$t.parents(".InputfieldPage");var $select2=$("select#"+$wrap.attr("id").replace(/^wrap_/,""));if($select2.length<1)return;var parts=selector.match(/(=page\.[_a-zA-Z0-9]+)/g);for(var n=0;n<parts.length;n++){var part=parts[n];var name=part.replace("=page.","");var $select1=$("#Inputfield_"+name);if((!$select1.length||selectorHasItem)&&$repeaterItems.length){var $selectInRepeater="";$repeaterItems.each(function(){if($selectInRepeater.length)return;$selectInRepeater=$("#Inputfield_"+name+"_repeater"+$(this).attr("data-page"))});if($selectInRepeater.length)$select1=$selectInRepeater}if($select1.length<1)continue;$select1.on("change",function(){selectChanged($select1,$select2,selector,formatName,labelFieldName,part,true)});if($select1.val()&&!$select2.val()&&$select2.children('option[value!=""]').length<1){setTimeout(function(){selectChanged($select1,$select2,selector,formatName,labelFieldName,part,false)},100)}}}$inputfieldPage.find(".findPagesSelector").each(function(){initSelector($(this))})}$(document).ready(function(){$(".InputfieldPage").each(function(){initInputfieldPage($(this))});$(document).on("reloaded",".InputfieldPage",function(){initInputfieldPage($(this))})});