2014-11-26 14:42:41 +03:00
|
|
|
/* globals jQuery */
|
2014-10-31 11:20:24 +03:00
|
|
|
|
|
|
|
(function($) {
|
2014-11-26 14:42:41 +03:00
|
|
|
// Selector to select only not already processed elements
|
|
|
|
$.expr[":"].notmdproc = function(obj){
|
|
|
|
if ($(obj).data("mdproc")) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
function _isChar(evt) {
|
|
|
|
if (typeof evt.which == "undefined") {
|
|
|
|
return true;
|
|
|
|
} else if (typeof evt.which == "number" && evt.which > 0) {
|
|
|
|
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;
|
2014-10-31 11:20:24 +03:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
$.material = {
|
|
|
|
"options": {
|
2014-12-04 13:34:13 +03:00
|
|
|
// These options set what will be started by $.material.init()
|
2014-12-04 16:47:42 +03:00
|
|
|
"input": true,
|
2014-12-04 13:34:13 +03:00
|
|
|
"ripples": true,
|
|
|
|
"checkbox": true,
|
|
|
|
"togglebutton": true,
|
|
|
|
"radio": true,
|
|
|
|
"arrive": true,
|
2015-01-06 12:28:40 +03:00
|
|
|
"autofill": false,
|
2014-12-04 13:34:13 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
"withRipples": [
|
|
|
|
".btn:not(.btn-link)",
|
|
|
|
".card-image",
|
|
|
|
".navbar a:not(.withoutripple)",
|
|
|
|
".dropdown-menu a",
|
|
|
|
".nav-tabs a:not(.withoutripple)",
|
|
|
|
".withripple"
|
|
|
|
].join(","),
|
|
|
|
"inputElements": "input.form-control, textarea.form-control, select.form-control",
|
|
|
|
"checkboxElements": ".checkbox > label > input[type=checkbox]",
|
2014-12-01 15:29:34 +03:00
|
|
|
"togglebuttonElements": ".togglebutton > label > input[type=checkbox]",
|
2014-11-26 14:42:41 +03:00
|
|
|
"radioElements": ".radio > label > input[type=radio]"
|
|
|
|
},
|
|
|
|
"checkbox": function(selector) {
|
|
|
|
// Add fake-checkbox to material checkboxes
|
|
|
|
$((selector) ? selector : this.options.checkboxElements)
|
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class=ripple></span><span class=check></span>");
|
|
|
|
},
|
2014-12-01 15:29:34 +03:00
|
|
|
"togglebutton": function(selector) {
|
|
|
|
// Add fake-checkbox to material checkboxes
|
|
|
|
$((selector) ? selector : this.options.togglebuttonElements)
|
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class=toggle></span>");
|
|
|
|
},
|
2014-11-26 14:42:41 +03:00
|
|
|
"radio": function(selector) {
|
|
|
|
// Add fake-radio to material radios
|
|
|
|
$((selector) ? selector : this.options.radioElements)
|
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class=circle></span><span class=check></span>");
|
|
|
|
},
|
|
|
|
"input": function(selector) {
|
|
|
|
$((selector) ? selector : this.options.inputElements)
|
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.each( function() {
|
|
|
|
var $this = $(this);
|
2015-01-26 20:05:46 +03:00
|
|
|
|
|
|
|
if (!$(this).attr("data-hint") && !$this.hasClass("floating-label")) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
$this.wrap("<div class=form-control-wrapper></div>");
|
|
|
|
$this.after("<span class=material-input></span>");
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
// Add floating label if required
|
|
|
|
if ($this.hasClass("floating-label")) {
|
|
|
|
var placeholder = $this.attr("placeholder");
|
|
|
|
$this.attr("placeholder", null).removeClass("floating-label");
|
|
|
|
$this.after("<div class=floating-label>" + placeholder + "</div>");
|
|
|
|
}
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
// Add hint label if required
|
|
|
|
if ($this.attr("data-hint")) {
|
|
|
|
$this.after("<div class=hint>" + $this.attr("data-hint") + "</div>");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set as empty if is empty (damn I must improve this...)
|
|
|
|
if ($this.val() === null || $this.val() == "undefined" || $this.val() === "") {
|
|
|
|
$this.addClass("empty");
|
|
|
|
}
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
// Support for file input
|
|
|
|
if ($this.parent().next().is("[type=file]")) {
|
|
|
|
$this.parent().addClass("fileinput");
|
|
|
|
var $input = $this.parent().next().detach();
|
|
|
|
$this.after($input);
|
|
|
|
}
|
|
|
|
});
|
2014-10-31 11:20:24 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
$(document)
|
|
|
|
.on("change", ".checkbox input[type=checkbox]", function() { $(this).blur(); })
|
|
|
|
.on("keydown paste", ".form-control", function(e) {
|
|
|
|
if(_isChar(e)) {
|
|
|
|
$(this).removeClass("empty");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("keyup change", ".form-control", function() {
|
|
|
|
var $this = $(this);
|
2014-12-12 19:59:20 +03:00
|
|
|
if($this.val() === "" && $this[0].checkValidity()) {
|
2014-11-26 14:42:41 +03:00
|
|
|
$this.addClass("empty");
|
|
|
|
} else {
|
|
|
|
$this.removeClass("empty");
|
2014-10-31 11:20:24 +03:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
})
|
|
|
|
.on("focus", ".form-control-wrapper.fileinput", function() {
|
|
|
|
$(this).find("input").addClass("focus");
|
|
|
|
})
|
|
|
|
.on("blur", ".form-control-wrapper.fileinput", function() {
|
|
|
|
$(this).find("input").removeClass("focus");
|
|
|
|
})
|
|
|
|
.on("change", ".form-control-wrapper.fileinput [type=file]", function() {
|
|
|
|
var value = "";
|
|
|
|
$.each($(this)[0].files, function(i, file) {
|
|
|
|
value += file.name + ", ";
|
|
|
|
});
|
|
|
|
value = value.substring(0, value.length - 2);
|
|
|
|
if (value) {
|
|
|
|
$(this).prev().removeClass("empty");
|
|
|
|
} else {
|
|
|
|
$(this).prev().addClass("empty");
|
|
|
|
}
|
|
|
|
$(this).prev().val(value);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"ripples": function(selector) {
|
2014-12-29 16:33:55 +03:00
|
|
|
$((selector) ? selector : this.options.withRipples).ripples();
|
2014-11-26 14:42:41 +03:00
|
|
|
},
|
2014-12-04 13:34:13 +03:00
|
|
|
"autofill": function() {
|
2014-11-26 14:42:41 +03:00
|
|
|
|
2014-12-04 13:34:13 +03:00
|
|
|
// This part of code will detect autofill when the page is loading (username and password inputs for example)
|
|
|
|
var loading = setInterval(function() {
|
|
|
|
$("input[type!=checkbox]").each(function() {
|
|
|
|
if ($(this).val() && $(this).val() !== $(this).attr("value")) {
|
|
|
|
$(this).trigger("change");
|
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
});
|
2014-12-04 13:34:13 +03:00
|
|
|
}, 100);
|
2014-11-26 14:42:41 +03:00
|
|
|
|
2014-12-04 13:34:13 +03:00
|
|
|
// After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
|
|
|
|
setTimeout(function() {
|
|
|
|
clearInterval(loading);
|
|
|
|
}, 10000);
|
|
|
|
// Now we just listen on inputs of the focused form (because user can select from the autofill dropdown only when the input has focus)
|
|
|
|
var focused;
|
|
|
|
$(document)
|
|
|
|
.on("focus", "input", function() {
|
|
|
|
var $inputs = $(this).parents("form").find("input").not("[type=file]");
|
|
|
|
focused = setInterval(function() {
|
|
|
|
$inputs.each(function() {
|
|
|
|
if ($(this).val() !== $(this).attr("value")) {
|
2014-11-26 14:42:41 +03:00
|
|
|
$(this).trigger("change");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100);
|
2014-12-04 13:34:13 +03:00
|
|
|
})
|
|
|
|
.on("blur", "input", function() {
|
|
|
|
clearInterval(focused);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"init": function() {
|
2015-01-06 12:25:09 +03:00
|
|
|
if ($.fn.ripples && this.options.ripples) {
|
2014-12-04 13:34:13 +03:00
|
|
|
this.ripples();
|
|
|
|
}
|
|
|
|
if (this.options.input) {
|
|
|
|
this.input();
|
|
|
|
}
|
|
|
|
if (this.options.checkbox) {
|
|
|
|
this.checkbox();
|
|
|
|
}
|
|
|
|
if (this.options.togglebutton) {
|
|
|
|
this.togglebutton();
|
|
|
|
}
|
|
|
|
if (this.options.radio) {
|
|
|
|
this.radio();
|
|
|
|
}
|
|
|
|
if (this.options.autofill) {
|
|
|
|
this.autofill();
|
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
|
2014-12-04 13:34:13 +03:00
|
|
|
if (document.arrive && this.options.arrive) {
|
2015-01-16 19:37:26 +03:00
|
|
|
if ($.fn.ripples && this.options.ripples) {
|
|
|
|
$(document).arrive(this.options.withRipples, function() {
|
|
|
|
$.material.ripples($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.input) {
|
|
|
|
$(document).arrive(this.options.inputElements, function() {
|
|
|
|
$.material.input($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.checkbox) {
|
|
|
|
$(document).arrive(this.options.checkboxElements, function() {
|
|
|
|
$.material.checkbox($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.radio) {
|
|
|
|
$(document).arrive(this.options.radioElements, function() {
|
|
|
|
$.material.radio($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.togglebutton) {
|
|
|
|
$(document).arrive(this.options.togglebuttonElements, function() {
|
|
|
|
$.material.togglebutton($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:34:13 +03:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
};
|
2014-10-31 11:20:24 +03:00
|
|
|
|
|
|
|
})(jQuery);
|