material.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 && evt.which != 9;
  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": false,
  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=checkbox-material><span class=check></span></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. if (!$(this).attr("data-hint") && !$this.hasClass("floating-label")) {
  70. return;
  71. }
  72. $this.wrap("<div class=form-control-wrapper></div>");
  73. $this.after("<span class=material-input></span>");
  74. // Add floating label if required
  75. if ($this.hasClass("floating-label")) {
  76. var placeholder = $this.attr("placeholder");
  77. $this.attr("placeholder", null).removeClass("floating-label");
  78. $this.after("<div class=floating-label>" + placeholder + "</div>");
  79. }
  80. // Add hint label if required
  81. if ($this.attr("data-hint")) {
  82. $this.after("<div class=hint>" + $this.attr("data-hint") + "</div>");
  83. }
  84. // Set as empty if is empty (damn I must improve this...)
  85. if ($this.val() === null || $this.val() == "undefined" || $this.val() === "") {
  86. $this.addClass("empty");
  87. }
  88. // Support for file input
  89. if ($this.parent().next().is("[type=file]")) {
  90. $this.parent().addClass("fileinput");
  91. var $input = $this.parent().next().detach();
  92. $this.after($input);
  93. }
  94. });
  95. $(document)
  96. .on("change", ".checkbox input[type=checkbox]", function() { $(this).blur(); })
  97. .on("keydown paste", ".form-control", function(e) {
  98. if(_isChar(e)) {
  99. $(this).removeClass("empty");
  100. }
  101. })
  102. .on("keyup change", ".form-control", function() {
  103. var $this = $(this);
  104. if ($this.val() === "" && (typeof $this[0].checkValidity != "undefined" && $this[0].checkValidity())) {
  105. $this.addClass("empty");
  106. } else {
  107. $this.removeClass("empty");
  108. }
  109. })
  110. .on("focus", ".form-control-wrapper.fileinput", function() {
  111. $(this).find("input").addClass("focus");
  112. })
  113. .on("blur", ".form-control-wrapper.fileinput", function() {
  114. $(this).find("input").removeClass("focus");
  115. })
  116. .on("change", ".form-control-wrapper.fileinput [type=file]", function() {
  117. var value = "";
  118. $.each($(this)[0].files, function(i, file) {
  119. value += file.name + ", ";
  120. });
  121. value = value.substring(0, value.length - 2);
  122. if (value) {
  123. $(this).prev().removeClass("empty");
  124. } else {
  125. $(this).prev().addClass("empty");
  126. }
  127. $(this).prev().val(value);
  128. });
  129. },
  130. "ripples": function(selector) {
  131. $((selector) ? selector : this.options.withRipples).ripples();
  132. },
  133. "autofill": function() {
  134. // This part of code will detect autofill when the page is loading (username and password inputs for example)
  135. var loading = setInterval(function() {
  136. $("input[type!=checkbox]").each(function() {
  137. if ($(this).val() && $(this).val() !== $(this).attr("value")) {
  138. $(this).trigger("change");
  139. }
  140. });
  141. }, 100);
  142. // After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
  143. setTimeout(function() {
  144. clearInterval(loading);
  145. }, 10000);
  146. // Now we just listen on inputs of the focused form (because user can select from the autofill dropdown only when the input has focus)
  147. var focused;
  148. $(document)
  149. .on("focus", "input", function() {
  150. var $inputs = $(this).parents("form").find("input").not("[type=file]");
  151. focused = setInterval(function() {
  152. $inputs.each(function() {
  153. if ($(this).val() !== $(this).attr("value")) {
  154. $(this).trigger("change");
  155. }
  156. });
  157. }, 100);
  158. })
  159. .on("blur", "input", function() {
  160. clearInterval(focused);
  161. });
  162. },
  163. "init": function() {
  164. if ($.fn.ripples && this.options.ripples) {
  165. this.ripples();
  166. }
  167. if (this.options.input) {
  168. this.input();
  169. }
  170. if (this.options.checkbox) {
  171. this.checkbox();
  172. }
  173. if (this.options.togglebutton) {
  174. this.togglebutton();
  175. }
  176. if (this.options.radio) {
  177. this.radio();
  178. }
  179. if (this.options.autofill) {
  180. this.autofill();
  181. }
  182. if (document.arrive && this.options.arrive) {
  183. if ($.fn.ripples && this.options.ripples) {
  184. $(document).arrive(this.options.withRipples, function() {
  185. $.material.ripples($(this));
  186. });
  187. }
  188. if (this.options.input) {
  189. $(document).arrive(this.options.inputElements, function() {
  190. $.material.input($(this));
  191. });
  192. }
  193. if (this.options.checkbox) {
  194. $(document).arrive(this.options.checkboxElements, function() {
  195. $.material.checkbox($(this));
  196. });
  197. }
  198. if (this.options.radio) {
  199. $(document).arrive(this.options.radioElements, function() {
  200. $.material.radio($(this));
  201. });
  202. }
  203. if (this.options.togglebutton) {
  204. $(document).arrive(this.options.togglebuttonElements, function() {
  205. $.material.togglebutton($(this));
  206. });
  207. }
  208. }
  209. }
  210. };
  211. })(jQuery);