1jQuery(function(){ 2 jQuery.fn.hasAttr = function(attribute){ 3 var attr = this.attr(attribute); 4 if (typeof attr !== typeof undefined && attr !== false) 5 return true; 6 return false; 7 } 8 9 jQuery.fn.materialForm = function() { 10 // Inputs 11 this.find('input, textarea').each(function(i){ 12 if(isValidType(jQuery(this))){ 13 var name = jQuery(this).attr('name'); 14 jQuery(this).attr('id', name); 15 var $wrap = jQuery(this).wrap("<div class='material-input'></div>").parent(); 16 $wrap.append("<span class='material-bar'></span>"); 17 18 var tagName = jQuery(this).prop("tagName").toLowerCase(); 19 $wrap.addClass(tagName); 20 21 var placeholder = jQuery(this).attr('placeholder'); 22 if(placeholder){ 23 $wrap.append("<label for='"+name+"'>"+placeholder+"</label>"); 24 jQuery(this).removeAttr('placeholder'); 25 } 26 27 addFilled(jQuery(this)); 28 } 29 30 if(isType(jQuery(this), 'radio') || isType(jQuery(this), 'checkbox')){ 31 32 var name = jQuery(this).attr('name').replace('[]',''); 33 var group_id = 'material-group-' + name; 34 var placeholder = jQuery(this).attr('placeholder'); 35 var item_id = name+ '-' + i; 36 var $label = jQuery('<label for="'+item_id+'">'+placeholder+'</label>'); 37 var $group_item = jQuery('<div class="material-group-item"></div>'); 38 jQuery(this).attr('id', item_id); 39 40 if(jQuery("#"+group_id).length){ 41 var $group = jQuery('#'+group_id); 42 $group.append(jQuery(this)); 43 } 44 else{ 45 var $group = jQuery(this).wrap("<div class='material-group' id='"+group_id+"'></div>").parent(); 46 } 47 48 if(isType(jQuery(this), 'radio')) 49 var $radio = jQuery('<div class="material-radio"></div>'); 50 else 51 var $radio = jQuery('<div class="material-checkbox"></div>'); 52 53 $group_item.append(jQuery(this)); 54 $group_item.append($label); 55 $group_item.append($radio); 56 57 $group.append($group_item); 58 } 59 }); 60 61 this.find('input, textarea').on('blur', function(){ 62 if(isValidType(jQuery(this))) 63 addFilled(jQuery(this)) 64 }); 65 66 // Radio 67 68 function isValidType($el){ 69 var type = $el.attr('type'); 70 return (type != 'hidden' && type != 'submit' && type != 'checkbox' && type != 'radio' && type != 'file' ? 1 : 0); 71 } 72 73 function isType($el, type){ 74 var el_type = $el.attr('type'); 75 return (el_type == type); 76 } 77 78 function addFilled($el){ 79 if($el.val()) 80 $el.addClass('filled'); 81 else 82 $el.removeClass('filled'); 83 } 84 85 // Selects 86 this.find('select').each(function(i){ 87 var placeholder = jQuery(this).attr('placeholder'); 88 var type = (jQuery(this).attr('multiple') ? 'checkbox' : 'radio'); 89 var name = id = jQuery(this).attr('name'); 90 var $wrap = jQuery(this).wrap("<div class='material-select'></div>").parent(); 91 if(type == 'checkbox'){ 92 name += '[]'; 93 var $bar = jQuery('<span class="material-bar"></span>'); 94 $wrap.append($bar).addClass('checkbox'); 95 } 96 else{ 97 var $title = jQuery('<span class="material-title">'+placeholder+'</span>'); 98 $wrap.prepend($title); 99 } 100 101 var $label = jQuery('<label for="select-'+i+'"><span>'+placeholder+'</span><strong></strong></label>'); 102 var $checkbox = jQuery('<input type="checkbox" id="select-'+i+'">'); 103 104 $wrap.prepend($checkbox); 105 $wrap.prepend($label); 106 107 var $list = jQuery('<ul class="'+type+'"></ul>'); 108 $wrap.append($list); 109 110 var selected_length = 0; 111 var length = jQuery(this).children('option').length; 112 var $selected; 113 jQuery(this).children('option').each(function(j){ 114 var title = jQuery(this).text(); 115 var value = jQuery(this).val(); 116 117 var selected = jQuery(this).hasAttr('selected'); 118 119 var $list_item = jQuery('<li></li>'); 120 $list.append($list_item); 121 122 var $label = jQuery('<label for="'+id+'-'+j+'">'+title+'</label>'); 123 var $input = jQuery('<input type="'+type+'" value="'+value+'" name="'+name+'" id="'+id+'-'+j+'">'); 124 if(selected){ 125 $selected = $input.prop('checked', true); 126 selected_length++; 127 } 128 129 $list_item.append($input); 130 $list_item.append($label); 131 }); 132 if($bar){ 133 var percentage = selected_length / length * 100; 134 $bar.width(percentage + '%'); 135 } else{ 136 if(selected_length){ 137 label.children('span').text($selected.next('label').text()); 138 $wrap.addClass('filled'); 139 } 140 } 141 jQuery(this).remove(); 142 }); 143 144 jQuery(document).on('click', function(e) { 145 if (jQuery(e.target).closest('.material-select').length === 0) { 146 // cancel highlighting 147 jQuery('.material-select > input').prop('checked', false); 148 } 149 }); 150 151 jQuery('.material-select > input').on('change', function(){ 152 var changed_id = jQuery(this).attr('id'); 153 jQuery('.material-select > input').each(function(){ 154 var this_id = jQuery(this).attr('id');
155 if(changed_id != this_id) 156 jQuery(this).prop('checked', false); 157 }); 158 }); 159 160 jQuery('.material-select ul input').on('change', function(){ 161 if(jQuery(this).closest('.material-select.checkbox').length){ 162 var $ul = jQuery(this).closest('ul') 163 var length = $ul.find('input').length; 164 var checked_length = $ul.find('input:checked').length; 165 var percentage = checked_length / length * 100; 166 jQuery(this).closest('.material-select').find('.material-bar').width(percentage + '%'); 167 } 168 else{ 169 var $material_select = jQuery(this).closest('.material-select') 170 var $label = $material_select.children('label').children('span'); 171 var $next = jQuery(this).next('label'); 172 $label.text($next.text()); 173 $material_select.children('input').prop('checked', false); 174 $material_select.addClass('filled'); 175 } 176 }); 177 178 }; 179}); 180 181jQuery(document).ready(function() { 182 jQuery('form.material').materialForm(); 183});
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.