material.js 6.7 KB

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