// Stand 1.6.2012 (function ($, Modernizr, console) { // stellt sicher, dass forms.js nur einmal ausgeführt wird if (window.isFormsLoaded) return; window.isFormsLoaded = true; // Bricht ab, wenn die Abhängigkeiten nicht erfüllt sind $ || console.error("forms.js benötigt jQuery"); Modernizr || console.error("forms.js benötigt Modernizr"); if (!$ || !Modernizr) return; /** * Hängt Event Handler an eine Liste von Radio Buttons. * @param param Index des Radiobuttons, oder Index-Array * @param handler Funktion die ausgeführt wird, wenn der Radiobutton selektiert wird, oder beim Laden der Seite selektiert ist * @param elseHandler Funktion die ausgeführt wird, wenn ein anderer Radiobutton selektiert wird, oder beim Laden der Seite selektiert ist. * * Die Handler erhalten ein Boolean als Argument, das false ist, wenn der Handler beim Laden der Seite ausgeführt wird. Darüber kann geprüft werden * ob Animationen ausgeführt werden sollten. * * Beispiel: * 1. $(".my-radio-list").radioChecked(0, function(animated) { alert("hallo welt"); }); * 2. $(".my-radio-list").radioChecked([0, 2], function(animated) { * $(".my-div").slideDown(animated ? "fast" : 0); * }, function(animated) { * $(".my-div").slideUp(animated ? "fast" : 0); * } **/ $.fn.radioChecked = function (param, handler, elseHandler, nonSelectedHandler) { var hasSelection = !!(this.find(":radio:checked").length); return this.find(":radio").each(function (index) { var $this = $(this); var changeFunction = function (animated) { animated = animated !== false; if ($this.is(":checked")) { if (param == index || ($.isArray(param) && $.inArray(index, param) > -1) || param == $this.val() || ($.isArray(param) && $.inArray($this.val(), param) > -1)) { handler(animated, $this); } else { if (typeof elseHandler == "function") elseHandler(animated, $this); } } }; $this.change(changeFunction); if (hasSelection) changeFunction(false); if (!hasSelection && typeof (nonSelectedHandler) == "function") nonSelectedHandler(false); }); }; /** * Stellt sicher, dass nur asp.net Validatoren selektiert werden. * Beispiel: * var validators = $("span.failing").validators(); **/ $.fn.validators = function () { return this.filter("span").filter(function () { return !$(this).children().length; }); }; $.fn.checkboxChecked = function (handler, elseHandler) { return this.each(function () { var $this = $(this); if (!$this.is(":checkbox")) { $this.find(":checkbox").checkboxChecked(handler, elseHandler); return $this; } else { var changeFunction = function (animated) { animated = animated !== false; if ($this.is(":checked")) { handler(animated); } else if (typeof elseHandler == "function") { elseHandler(animated); } } $this.change(changeFunction); changeFunction(false); } }); }; /** * Analog zu Radio Checked für Dropdown Listen */ $.fn.optionSelected = function (param, handler, elseHandler) { var select = this; return this.find("option").each(function (index) { var $this = $(this); var changeFunction = function (animated) { animated = animated !== false; if ($this.is(":selected")) { if (param == $this.val() || param == index || ($.isArray(param) && $.inArray(index, param) > -1) || ($.isArray(param) && $.inArray($this.val(), param) > -1)) { handler(animated); } else { if (typeof elseHandler == "function") elseHandler(animated); } } }; changeFunction(false); select.change(changeFunction); }); } /** * Versucht automatisch zu erkennen ob ein Quicknavitem * zweispaltig ist oder nur einspaltig **/ /** * Passt die Darstellung eines file-Input-Elements an * Beispiel: * $("input:file").uploadControl(); **/ $.fn.uploadControl = function () { return this.filter("input:file").each(function () { var $this = $(this); var container = $this.wrap("").parent(); var value = $("", { "class": "value" }).prependTo(container).text("Keine ausgewählt"); var button = $("