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

1 line
14 KiB
JavaScript
Raw Permalink Normal View History

2024-08-27 11:35:37 +02:00
var InputfieldSelector={selector:"",spinner:"<i class='fa fa-lg fa-spin fa-spinner'></i>",borderColor:"#eee",init:function(){$(document).on("change",".InputfieldSelector select.select-field",InputfieldSelector.changeField);$(document).on("change",".InputfieldSelector select.select-subfield",InputfieldSelector.changeField);$(document).on("change",".InputfieldSelector :input:not(.select-field):not(.input-value-autocomplete)",function(){InputfieldSelector.changeAny($(this))});$(document).on("opened",".InputfieldSelector",function(){InputfieldSelector.normalizeHeightRows($(this))});var timeout=null;$(document).on("keyup",".InputfieldSelector input.input-value",function(){var $t=$(this);clearTimeout(timeout);if($t.hasClass("input-value-subselect")&&InputfieldSelector.valueHasOperator($t.val())){var $preview=$t.closest(".InputfieldContent").find(".selector-preview");$preview.html('Subselect detected: when done <a href="#" onclick="return false">click here to commit your change</a>.');return}timeout=setTimeout(function(){InputfieldSelector.changeAny($t)},100)});$(document).on("click",".InputfieldSelector .selector-add",function(){InputfieldSelector.addRow($(this));return false});$(document).on("click",".InputfieldSelector a.delete-row",InputfieldSelector.deleteRow);$(".InputfieldSelector .selector-preview").hide();$(document).on("wiretabclick",function(event,$newTab,$oldTab){var $inputfield=$newTab.find(".InputfieldSelector");if($inputfield.length==0)return;InputfieldSelector.normalizeHeightRows($inputfield)});var $rows=$(".InputfieldSelector .selector-row");if($rows.length>0){$rows.eq(0).find(".select-field").each(function(){InputfieldSelector.changeAny($(this))});$rows.eq(1).find(".input-value").trigger("change");$rows.each(function(){var $row=$(this);$row.css("border-color",InputfieldSelector.borderColor);InputfieldSelector.normalizeHeightRow($row);var $ac=$row.find(".input-value-autocomplete");if($ac.length>0){var subfield=$row.find(".select-subfield");var field=subfield.length?subfield.val():$row.find(".select-field").val();var name=$row.parents(".InputfieldSelector").find("input.selector-value").attr("name");InputfieldSelector.setupAutocomplete($ac,field,name)}})}$(".InputfieldSelector").each(function(){if($(this).find(".selector-preview-disabled").length>0)return;$(this).find(".input-value").eq(0).trigger("change")})},disableOption:function($option){$option.prop("disabled",true).prop("hidden",true)},enableOption:function($option){$option.prop("disabled",false).prop("hidden",false)},valueHasOperator:function(value){var operators=["=","<",">"];var hasOperator=false;for(n=0;n<operators.length;n++){var pos=value.indexOf(operators[n]);if(pos>-1&&value.substring(pos-1,1)!="\\"){hasOperator=true;break}}return hasOperator},addRow:function($context){var $list=$context.parents(".InputfieldSelector").find(".selector-list");var $row=$list.find(".selector-template-row");var $newRow=$row.clone();$newRow.removeClass("selector-template-row");$newRow.find(".opval").html("");$newRow.find(".select-field").val("");$newRow.hide();$newRow.find("option:disabled").prop("hidden",true);$list.append($newRow);$newRow.slideDown("fast");InputfieldSelector.normalizeHeightRow($newRow)},deleteRow:function(){var $row=$(this).parents(".selector-row");var $selectField=$row.find(".select-field");if($selectField.val()=="template"){$row.parents(".InputfieldSelector").find("select.select-field").each(function(){$(this).find("option:disabled").each(function(){InputfieldSelector.enableOption($(this))})})}var $siblings=$row.siblings();$row.slideUp("fast",function(){$row.remove();InputfieldSelector.changeAny($siblings.eq(0))});return false},changeFieldToggle:function($select){var $rootParent=$select.parents(".InputfieldSelector");var currentSetting=$rootParent.hasClass("InputfieldSelector_names")?"names":"labels";var newSetting=currentSetting==="labels"?"names":"labels";$rootParent.find(".select-field, .select-subfield").each(function(){$(this).find("option").each(function(){var name=$(this).attr("data-name");if(!name){if($(this).attr("value")=="toggle-names-labe