material.js (7482B)
1 /* globals jQuery */ 2 3 (function($) { 4 // Selector to select only not already processed elements 5 $.expr[":"].notmdproc = function(obj){ 6 if ($(obj).data("mdproc")) { 7 return false; 8 } else { 9 return true; 10 } 11 }; 12 13 function _isChar(evt) { 14 if (typeof evt.which == "undefined") { 15 return true; 16 } else if (typeof evt.which == "number" && evt.which > 0) { 17 return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8 && evt.which != 9; 18 } 19 return false; 20 } 21 22 $.material = { 23 "options": { 24 // These options set what will be started by $.material.init() 25 "input": true, 26 "ripples": true, 27 "checkbox": true, 28 "togglebutton": true, 29 "radio": true, 30 "arrive": true, 31 "autofill": false, 32 33 "withRipples": [ 34 ".btn:not(.btn-link)", 35 ".card-image", 36 ".navbar a:not(.withoutripple)", 37 ".dropdown-menu a", 38 ".nav-tabs a:not(.withoutripple)", 39 ".withripple" 40 ].join(","), 41 "inputElements": "input.form-control, textarea.form-control, select.form-control", 42 "checkboxElements": ".checkbox > label > input[type=checkbox]", 43 "togglebuttonElements": ".togglebutton > label > input[type=checkbox]", 44 "radioElements": ".radio > label > input[type=radio]" 45 }, 46 "checkbox": function(selector) { 47 // Add fake-checkbox to material checkboxes 48 $((selector) ? selector : this.options.checkboxElements) 49 .filter(":notmdproc") 50 .data("mdproc", true) 51 .after("<span class=checkbox-material><span class=check></span></span>"); 52 }, 53 "togglebutton": function(selector) { 54 // Add fake-checkbox to material checkboxes 55 $((selector) ? selector : this.options.togglebuttonElements) 56 .filter(":notmdproc") 57 .data("mdproc", true) 58 .after("<span class=toggle></span>"); 59 }, 60 "radio": function(selector) { 61 // Add fake-radio to material radios 62 $((selector) ? selector : this.options.radioElements) 63 .filter(":notmdproc") 64 .data("mdproc", true) 65 .after("<span class=circle></span><span class=check></span>"); 66 }, 67 "input": function(selector) { 68 $((selector) ? selector : this.options.inputElements) 69 .filter(":notmdproc") 70 .data("mdproc", true) 71 .each( function() { 72 var $this = $(this); 73 74 if (!$(this).attr("data-hint") && !$this.hasClass("floating-label")) { 75 return; 76 } 77 $this.wrap("<div class=form-control-wrapper></div>"); 78 $this.after("<span class=material-input></span>"); 79 80 // Add floating label if required 81 if ($this.hasClass("floating-label")) { 82 var placeholder = $this.attr("placeholder"); 83 $this.attr("placeholder", null).removeClass("floating-label"); 84 $this.after("<div class=floating-label>" + placeholder + "</div>"); 85 } 86 87 // Add hint label if required 88 if ($this.attr("data-hint")) { 89 $this.after("<div class=hint>" + $this.attr("data-hint") + "</div>"); 90 } 91 92 // Set as empty if is empty (damn I must improve this...) 93 if ($this.val() === null || $this.val() == "undefined" || $this.val() === "") { 94 $this.addClass("empty"); 95 } 96 97 // Support for file input 98 if ($this.parent().next().is("[type=file]")) { 99 $this.parent().addClass("fileinput"); 100 var $input = $this.parent().next().detach(); 101 $this.after($input); 102 } 103 }); 104 105 $(document) 106 .on("change", ".checkbox input[type=checkbox]", function() { $(this).blur(); }) 107 .on("keydown paste", ".form-control", function(e) { 108 if(_isChar(e)) { 109 $(this).removeClass("empty"); 110 } 111 }) 112 .on("keyup change", ".form-control", function() { 113 var $this = $(this); 114 if ($this.val() === "" && (typeof $this[0].checkValidity != "undefined" && $this[0].checkValidity())) { 115 $this.addClass("empty"); 116 } else { 117 $this.removeClass("empty"); 118 } 119 }) 120 .on("focus", ".form-control-wrapper.fileinput", function() { 121 $(this).find("input").addClass("focus"); 122 }) 123 .on("blur", ".form-control-wrapper.fileinput", function() { 124 $(this).find("input").removeClass("focus"); 125 }) 126 .on("change", ".form-control-wrapper.fileinput [type=file]", function() { 127 var value = ""; 128 $.each($(this)[0].files, function(i, file) { 129 value += file.name + ", "; 130 }); 131 value = value.substring(0, value.length - 2); 132 if (value) { 133 $(this).prev().removeClass("empty"); 134 } else { 135 $(this).prev().addClass("empty"); 136 } 137 $(this).prev().val(value); 138 }); 139 }, 140 "ripples": function(selector) { 141 $((selector) ? selector : this.options.withRipples).ripples(); 142 }, 143 "autofill": function() { 144 145 // This part of code will detect autofill when the page is loading (username and password inputs for example) 146 var loading = setInterval(function() { 147 $("input[type!=checkbox]").each(function() { 148 if ($(this).val() && $(this).val() !== $(this).attr("value")) { 149 $(this).trigger("change"); 150 } 151 }); 152 }, 100); 153 154 // After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them 155 setTimeout(function() { 156 clearInterval(loading); 157 }, 10000); 158 // Now we just listen on inputs of the focused form (because user can select from the autofill dropdown only when the input has focus) 159 var focused; 160 $(document) 161 .on("focus", "input", function() { 162 var $inputs = $(this).parents("form").find("input").not("[type=file]"); 163 focused = setInterval(function() { 164 $inputs.each(function() { 165 if ($(this).val() !== $(this).attr("value")) { 166 $(this).trigger("change"); 167 } 168 }); 169 }, 100); 170 }) 171 .on("blur", "input", function() { 172 clearInterval(focused); 173 }); 174 }, 175 "init": function() { 176 if ($.fn.ripples && this.options.ripples) { 177 this.ripples(); 178 } 179 if (this.options.input) { 180 this.input(); 181 } 182 if (this.options.checkbox) { 183 this.checkbox(); 184 } 185 if (this.options.togglebutton) { 186 this.togglebutton(); 187 } 188 if (this.options.radio) { 189 this.radio(); 190 } 191 if (this.options.autofill) { 192 this.autofill(); 193 } 194 195 if (document.arrive && this.options.arrive) { 196 if ($.fn.ripples && this.options.ripples) { 197 $(document).arrive(this.options.withRipples, function() { 198 $.material.ripples($(this)); 199 }); 200 } 201 if (this.options.input) { 202 $(document).arrive(this.options.inputElements, function() { 203 $.material.input($(this)); 204 }); 205 } 206 if (this.options.checkbox) { 207 $(document).arrive(this.options.checkboxElements, function() { 208 $.material.checkbox($(this)); 209 }); 210 } 211 if (this.options.radio) { 212 $(document).arrive(this.options.radioElements, function() { 213 $.material.radio($(this)); 214 }); 215 } 216 if (this.options.togglebutton) { 217 $(document).arrive(this.options.togglebuttonElements, function() { 218 $.material.togglebutton($(this)); 219 }); 220 } 221 222 } 223 } 224 }; 225 226 })(jQuery);