diff --git a/jquery.chained.js b/jquery.chained.js index 53915e3..1c5ad33 100644 --- a/jquery.chained.js +++ b/jquery.chained.js @@ -18,83 +18,104 @@ $.fn.chained = function(parent_selector, options) { - return this.each(function() { - - /* Save this to child because this changes when scope changes. */ - var child = this; - var backup = $(child).clone(); + return this.each( + function( index, element ) { + + /* Save this to child because this changes when scope changes. */ + var child = element; + var backup = $(child).clone(); + + /* Handles maximum two parents now. */ + $(parent_selector).each( function( index, element ) { + $(element).bind("change", function() { + updateChildren(); + }); + + /* Force IE to see something selected on first page load, */ + /* unless something is already selected */ + if (!$("option:selected", element).length) { + $("option", element).first().attr("selected", "selected"); + } - /* Handles maximum two parents now. */ - $(parent_selector).each(function() { - $(this).bind("change", function() { + /* Force updating the children. */ updateChildren(); - }); - - /* Force IE to see something selected on first page load, */ - /* unless something is already selected */ - if (!$("option:selected", this).length) { - $("option", this).first().attr("selected", "selected"); - } - - /* Force updating the children. */ - updateChildren(); - }); - - function updateChildren() { - var trigger_change = true; - var currently_selected_value = $("option:selected", child).val(); - - $(child).html(backup.html()); - - /* If multiple parents build classname like foo\bar. */ - var selected = ""; - $(parent_selector).each(function() { - var selectedClass = $("option:selected", this).val(); - if (selectedClass) { - if (selected.length > 0) { - if (window.Zepto) { - /* Zepto class regexp dies with classes like foo\bar. */ - selected += "\\\\"; - } else { - selected += "\\"; + } ); + + function updateChildren() { + var trigger_change = true; + var currently_selected_value = $("option:selected", child).val(); + + $(child).html(backup.html()); + + /* If multiple parents build classname like foo\bar. */ + var selected = ""; + $(parent_selector).each(function() { + var selectedClass = $("option:selected", this).val(); + if (selectedClass) { + if (selected.length > 0) { + if (window.Zepto) { + /* Zepto class regexp dies with classes like foo\bar. */ + selected += "\\\\"; + } else { + selected += "\\"; + } } + selected += selectedClass; } - selected += selectedClass; - } - }); - - /* Also check for first parent without subclassing. */ - /* TODO: This should be dynamic and check for each parent */ - /* without subclassing. */ - var first; - if ($.isArray(parent_selector)) { - first = $(parent_selector[0]).first(); - } else { - first = $(parent_selector).first(); - } - var selected_first = $("option:selected", first).val(); - - $("option", child).each(function() { - /* Remove unneeded items but save the default value. */ - if ($(this).hasClass(selected) && $(this).val() === currently_selected_value) { - $(this).prop("selected", true); - trigger_change = false; - } else if (!$(this).hasClass(selected) && !$(this).hasClass(selected_first) && $(this).val() !== "") { - $(this).remove(); + }); + + /* Also check for first parent without subclassing. */ + /* TODO: This should be dynamic and check for each parent */ + /* without subclassing. */ + var first; + if ($.isArray(parent_selector)) { + first = $(parent_selector[0]).first(); + } else { + first = $(parent_selector).first(); } - }); + var selected_first = $("option:selected", first).val(); + + $("option", child).each( function( index, element ) { + var hasprop = false; + var isselfirst = false; + var e = $(element); + if ( e.attr('data-chained') ) { + var d = e.data( 'chained' ); + if ( d == selected ) { + hasprop = true; + } + if ( d == selected_first ) { + isselfirst = true; + } + } else { + if ( $(element).hasClass( selected ) ) { + hasprop = true; + } + if ( e.hasClass(selected_first) ) { + isselfirst = true; + } + } + /* Remove unneeded items but save the default value. */ + if ( hasprop && e.val() === currently_selected_value) { + e.prop("selected", true); + trigger_change = false; + } else if (!hasprop && !isselfirst && e.val() !== "") { + e.remove(); + } + } ); - /* If we have only the default value disable select. */ - if (1 === $("option", child).size() && $(child).val() === "") { - $(child).attr("disabled", "disabled"); - } else { - $(child).removeAttr("disabled"); - } - if (trigger_change) { - $(child).trigger("change"); + /* If we have only the default value disable select. */ + if (1 === $("option", child).size() && $(child).val() === "") { + $(child).attr("disabled", "disabled"); + } else { + $(child).removeAttr("disabled"); + } + if (trigger_change) { + $(child).trigger("change"); + } } } - }); + ); }; /* Alias for those who like to use more English like syntax. */ diff --git a/jquery.chained.min.js b/jquery.chained.min.js index 75b83cb..6da8796 100644 --- a/jquery.chained.min.js +++ b/jquery.chained.min.js @@ -1,2 +1,2 @@ /*! Chained 0.9.9 - MIT license - Copyright 2010-2013 Mika Tuupola */ -!function(a,b){"use strict";a.fn.chained=function(c){return this.each(function(){function d(){var d=!0,g=a("option:selected",e).val();a(e).html(f.html());var h="";a(c).each(function(){var c=a("option:selected",this).val();c&&(h.length>0&&(h+=b.Zepto?"\\\\":"\\"),h+=c)});var i;i=a.isArray(c)?a(c[0]).first():a(c).first();var j=a("option:selected",i).val();a("option",e).each(function(){a(this).hasClass(h)&&a(this).val()===g?(a(this).prop("selected",!0),d=!1):a(this).hasClass(h)||a(this).hasClass(j)||""===a(this).val()||a(this).remove()}),1===a("option",e).size()&&""===a(e).val()?a(e).attr("disabled","disabled"):a(e).removeAttr("disabled"),d&&a(e).trigger("change")}var e=this,f=a(e).clone();a(c).each(function(){a(this).bind("change",function(){d()}),a("option:selected",this).length||a("option",this).first().attr("selected","selected"),d()})})},a.fn.chainedTo=a.fn.chained,a.fn.chained.defaults={}}(window.jQuery||window.Zepto,window,document); \ No newline at end of file +(function($,window,document,undefined){"use strict";$.fn.chained=function(parent_selector,options){return this.each(function(index,element){var child=element;var backup=$(child).clone();$(parent_selector).each(function(index,element){$(element).bind("change",function(){updateChildren()});if(!$("option:selected",element).length){$("option",element).first().attr("selected","selected")}updateChildren()});function updateChildren(){var trigger_change=true;var currently_selected_value=$("option:selected",child).val();$(child).html(backup.html());var selected="";$(parent_selector).each(function(){var selectedClass=$("option:selected",this).val();if(selectedClass){if(selected.length>0){if(window.Zepto){selected+="\\\\"}else{selected+="\\"}}selected+=selectedClass}});var first;if($.isArray(parent_selector)){first=$(parent_selector[0]).first()}else{first=$(parent_selector).first()}var selected_first=$("option:selected",first).val();$("option",child).each(function(index,element){var hasprop=false;var isselfirst=false;var e=$(element);if(e.attr("data-chained")){var d=e.data("chained");if(d==selected){hasprop=true}if(d==selected_first){isselfirst=true}}else{if($(element).hasClass(selected)){hasprop=true}if(e.hasClass(selected_first)){isselfirst=true}}if(hasprop&&e.val()===currently_selected_value){e.prop("selected",true);trigger_change=false}else if(!hasprop&&!isselfirst&&e.val()!==""){e.remove()}});if(1===$("option",child).size()&&$(child).val()===""){$(child).attr("disabled","disabled")}else{$(child).removeAttr("disabled")}if(trigger_change){$(child).trigger("change")}}})};$.fn.chainedTo=$.fn.chained;$.fn.chained.defaults={}})(window.jQuery||window.Zepto,window,document); \ No newline at end of file