artabro/wire/modules/Inputfield/InputfieldToggle/InputfieldToggle.min.js
2024-08-27 11:35:37 +02:00

1 line
No EOL
3.5 KiB
JavaScript

function InputfieldToggleInit(){var isClick=false;var inputCheckedClass="InputfieldToggleChecked";var labelCheckedClass="InputfieldToggleCurrent";function getLabelFromInput($input){var $label=$input.next("label");if(!$label.length)$label=$input.parent("label");if(!$label.length)$label=$("label[for="+$input.attr("id")+"]");return $label}function getInputFromLabel($label){var $input=$label.prev("input");if(!$input.length)$input=$label.find("input");if(!$input.length)$input=$("input[id="+$label.attr("for")+"]");return $input}function toggleInputEvent($input){var $label=getLabelFromInput($input);var $prevInput=$input.closest(".InputfieldToggle").find("input."+inputCheckedClass);var $prevLabel=$prevInput.length?getLabelFromInput($prevInput):null;if($prevInput.length&&$prevInput.attr("id")!=$input.attr("id")){$prevInput.removeClass(inputCheckedClass).prop("checked",false);if($prevLabel)$prevLabel.removeClass(labelCheckedClass)}if($input.hasClass(inputCheckedClass)&&$input.closest(".InputfieldToggleUseDeselect").length){$input.prop("checked",false).removeClass(inputCheckedClass);$label.removeClass(labelCheckedClass);setTimeout(function(){$input.prop("checked",false).trigger("change")},100)}else{$input.prop("checked",true);$input.addClass(inputCheckedClass);$label.addClass(labelCheckedClass);$input.trigger("change")}}function initEvents(){$(document).on("change",".InputfieldToggle input",function(){if(isClick)return false;toggleInputEvent($(this))}).on("click",".InputfieldToggle label:not(.InputfieldHeader)",function(event){if(isClick)return false;var $label=$(this);var $input=getInputFromLabel($label);if(!$input.length)return;isClick=true;toggleInputEvent($input);setTimeout(function(){isClick=false},200);if($input.closest(".InputfieldToggleGroup").length)return false})}function initColors(){var $button=$(".InputfieldToggleHelper > button");var $input=$(".InputfieldToggleHelper > input");if(!$button.length)$button=$(".InputfieldForm button.ui-priority-secondary").eq(0);if(!$button.length)$button=$(".InputfieldForm button.ui-button").eq(0);if(!$button.length)$button=$(".InputfieldForm button[type=submit]");if(!$input.length)$input=$(".InputfieldForm input[type=text]").eq(0);if(!$button.length||!$input.length)return;InputfieldToggleSetColors({onBg:$button.css("background-color"),on:$button.css("color"),offBg:$input.css("background-color"),off:$input.css("color"),border:$input.css("border-bottom-color")})}initEvents();initColors()}function InputfieldToggleSetColors(customColors){var colors={on:"",onBg:"",off:"",offBg:"",border:"",hoverBg:"",hover:""};$.extend(colors,customColors);if(!colors.hoverBg&&colors.onBg){colors.hoverBg=colors.onBg.replace("rgb(","rgba(").replace(")",",.2)");if(!colors.hover)colors.hover=colors.off}var style="<style type='text/css'>"+".InputfieldToggleGroup label { "+(colors.offBg?"background-color: "+colors.offBg+"; ":"")+(colors.off?"color: "+colors.off+";":"")+(colors.border?"border-color: "+colors.border+";":"")+"} "+".InputfieldToggleGroup label.InputfieldToggleCurrent, "+".InputfieldToggleGroup input:checked + label { "+(colors.onBg?"background-color: "+colors.onBg+"; ":"")+(colors.on?"color: "+colors.on+";":"")+(colors.onBg?"border-color: "+colors.onBg+"; ":"")+"} "+".InputfieldToggleGroup label:not(.InputfieldToggleCurrent):hover,"+".InputfieldToggleGroup input:not(:checked) + label:not(.InputfieldToggleCurrent):hover { "+(colors.hoverBg?"background-color: "+colors.hoverBg+"; ":"")+(colors.hover?"color: "+colors.hover+"; ":"")+"}"+"</style>";$("head").append(style)}jQuery(document).ready(function($){InputfieldToggleInit()});