1/** 2 jQuery filterSelectOn plugin 3 see https://github.com/dogawaf/jQuery.filterSelectOn 4 require jQuery 1.7+ 5 @copyright © 2012 Rémy DANIEL 6 E-mail contact: [email protected] 7 Twitter account: #dogawaf 8 */ 9 10/* 11most simple example : 12 $('select[name="child"]').filterSelectOn({ 13 parentSelector: 'select[name=parent]' 14 }); 15full example : 16 $('select[name="child"]').filterSelectOn({ 17 parentSelector: 'select[name=parent]', 18 emptyValueMask: /^$/, 19 compareFunc: function(value, parentValue){ 20 return (value === parentValue); 21 }, 22 callback: function(element){ 23 console.log(element) 24 } 25 }); 26*/ 27(function($) { 28 29 var FilterSelectOn = function(element, options) { 30 this._init(element, options); 31 }; 32 33 var compareFunc = function(value, parentValue){ 34 return (value === parentValue); 35 }; 36 37 FilterSelectOn.prototype = { 38 39 constructor: FilterSelectOn, 40 41 _init: function(element, options) { 42 var o = this.options = $.extend({ 43 parentSelector: '', 44 emptyValueMask: /^$/, 45 compareFunc: compareFunc, 46 callback: $.noop 47 }, options); 48 49 this.$element = $(element); 50 this.$parentSelect = $(o.parentSelector); 51 52 this._create(); 53 }, 54 55 _create: function() { 56 var o = this.options; 57 58 this.initialOptions = this.$element.children(); 59 60 this.$parentSelect.on('change.filterSelectOn', $.proxy(this._refresh, this)); 61 }, 62 63 _refresh: function() { 64 65 var o = this.options, 66 $element = this.$element, 67 selectedValue = $element.val(), 68 parentValue = this.$parentSelect.val(), 69 emptyValueMask = o.emptyValueMask; 70 71 // not any parent selected, all options are restored 72 if (emptyValueMask.test(parentValue)) { 73 74 $element.html(this.initialOptions); 75 76 // reset selected item 77 $element.val(selectedValue); 78 79 // a parent is selected, do filtering 80 } else { 81 82 // remove all options 83 $element.empty(); 84 85 // reset selected item 86 selectValue = ''; 87 88 // rebuild option list 89 $(this.initialOptions).filter(function(i) { 90 91 var $this = $(this), 92 value = $this.attr('value'); 93 94 // an option with no value is always shown 95 if (emptyValueMask.test(value)) { 96 return true; 97 } 98 99 // does this option should be kept ? 100 if (o.compareFunc($this.attr('data-parent'), parentValue)) { 101 102 // it should be a better experience 103 // if we can keep the selected item 104 if (selectedValue === value) { 105 selectValue = value; 106 } 107 108 return true; 109 } 110 111 return false; 112 113 }).appendTo($element); 114 115 $element.val(selectValue); 116 } 117 118 // callback 119 o.callback.call($element.get(0)); 120 }, 121 122 refresh: function() { 123 124 this._refresh(); 125 126 return this.$element; 127 }, 128 129 destroy: function() { 130 this.$parentSelect.off('.filterSelectOn'); 131 } 132 133 }; 134 135 $.fn.filterSelectOn = function(option) { 136 return this.each(function() { 137 var $this = $(this), 138 data = $this.data('filterSelectOn'), 139 options = typeof option === 'object' && option; 140 141 if (!data) { 142 $this.data('filterSelectOn', (data = new FilterSelectOn(this, options))); 143 } 144 145 if (typeof option === 'string') { 146 data[option](); 147 } 148 }); 149 }; 150 151})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.