2014-11-26 14:42:41 +03:00
|
|
|
/* globals jQuery */
|
2014-08-25 15:00:49 +04:00
|
|
|
|
2014-10-18 20:12:02 +04: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-08-18 18:25:33 +04: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) {
|
2015-01-20 05:16:59 +03:00
|
|
|
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8 && evt.which != 9;
|
2014-10-18 18:05:05 +04:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-18 18:05:05 +04: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)",
|
2015-05-14 12:59:32 +03:00
|
|
|
".withripple",
|
2015-08-23 00:09:49 +03:00
|
|
|
".pagination li:not(.active):not(.disabled) a:not(.withoutripple)"
|
2014-11-26 14:42:41 +03:00
|
|
|
].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)
|
2015-03-11 18:45:01 +03:00
|
|
|
.after("<span class=checkbox-material><span class=check></span></span>");
|
2014-11-26 14:42:41 +03:00
|
|
|
},
|
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() {
|
2015-11-06 20:04:07 +03:00
|
|
|
var $input = $(this);
|
2015-01-26 20:05:46 +03:00
|
|
|
|
2015-11-06 20:04:07 +03:00
|
|
|
// Now using/requiring form-group standard markup (instead of the old div.form-control-wrapper)
|
2015-11-10 19:26:00 +03:00
|
|
|
var $formGroup = $input.parent(".form-group");
|
|
|
|
if($formGroup.length === 0){
|
2015-11-06 20:04:07 +03:00
|
|
|
//console.debug("Generating form-group for input", $this);
|
2015-11-10 19:26:00 +03:00
|
|
|
$formGroup = $input.wrap("<div class='form-group'></div>");
|
2015-11-06 19:30:05 +03:00
|
|
|
}
|
|
|
|
|
2015-11-06 20:04:07 +03:00
|
|
|
// Legacy - Add floating label if using old shorthand <input class="floating-label" placeholder="foo">
|
|
|
|
if ($input.hasClass("floating-label")) {
|
|
|
|
var placeholder = $input.attr("placeholder");
|
|
|
|
$input.attr("placeholder", null).removeClass("floating-label");
|
|
|
|
var id = $input.attr("id");
|
|
|
|
var forAttribute = "";
|
|
|
|
if(id) {
|
|
|
|
forAttribute = "for='" + id + "'";
|
|
|
|
}
|
|
|
|
$input.after("<label " + forAttribute + "class='floating-label'>" + placeholder + "</label>");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
2015-11-06 23:20:23 +03:00
|
|
|
else {
|
|
|
|
// If it has a label, based on the way the css is written with the adjacent sibling selector `~`,
|
|
|
|
// we need the label to be *after* the input for it to work properly.
|
|
|
|
// See: http://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-selector
|
2015-11-10 19:26:00 +03:00
|
|
|
var $label = $formGroup.find("label.floating-label");
|
2015-11-06 23:20:23 +03:00
|
|
|
if($label.length > 0){
|
|
|
|
$label.detach();
|
|
|
|
$input.after($label);
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 15:22:17 +03:00
|
|
|
|
2015-11-06 20:04:07 +03:00
|
|
|
// Legacy - Add hint label if using the old shorthand data-hint attribute on the input
|
|
|
|
if ($input.attr("data-hint")) {
|
|
|
|
$input.after("<p class='help-block hint'>" + $input.attr("data-hint") + "</p>");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
2014-11-12 15:22:17 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
// Set as empty if is empty (damn I must improve this...)
|
2015-11-06 20:04:07 +03:00
|
|
|
if ($input.val() === null || $input.val() == "undefined" || $input.val() === "") {
|
2015-11-10 20:52:15 +03:00
|
|
|
$formGroup.addClass("is-empty");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
2014-11-12 15:22:17 +03:00
|
|
|
|
2015-11-06 23:20:23 +03:00
|
|
|
// Add at the end of the form-group
|
2015-11-10 19:26:00 +03:00
|
|
|
$formGroup.append("<span class='material-input'></span>");
|
2015-11-06 23:20:23 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
// Support for file input
|
2015-11-10 19:26:00 +03:00
|
|
|
if ($formGroup.next().is("[type=file]")) {
|
|
|
|
$formGroup.addClass("fileinput");
|
|
|
|
var $nextInput = $formGroup.next().detach();
|
2015-11-06 20:04:07 +03:00
|
|
|
$input.after($nextInput);
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
});
|
2015-08-22 16:59:08 +03:00
|
|
|
},
|
|
|
|
"attachInputEventHandlers": function() {
|
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)) {
|
2015-11-10 20:52:15 +03:00
|
|
|
$(this).parent(".form-group").removeClass("is-empty");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("keyup change", ".form-control", function() {
|
2015-11-10 19:26:00 +03:00
|
|
|
var $input = $(this);
|
|
|
|
var $formGroup = $input.parent(".form-group");
|
|
|
|
var isValid = (typeof $input[0].checkValidity === "undefined" || $input[0].checkValidity());
|
|
|
|
|
|
|
|
if ($input.val() === "" && isValid) {
|
2015-11-10 20:52:15 +03:00
|
|
|
$formGroup.addClass("is-empty");
|
2015-11-10 19:26:00 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-11-10 20:52:15 +03:00
|
|
|
$formGroup.removeClass("is-empty");
|
2015-11-10 19:26:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validation events do not bubble, so they must be attached directly to the input: http://jsfiddle.net/PEpRM/1/
|
|
|
|
// Further, even the bind method is being caught, but since we are already calling #checkValidity here, just alter
|
|
|
|
// the form-group on change.
|
2015-11-10 22:52:59 +03:00
|
|
|
//
|
|
|
|
// NOTE: I'm not sure we should be intervening regarding validation, this seems better as a README and snippet of code.
|
|
|
|
// BUT, I've left it here for backwards compatibility.
|
2015-11-10 19:26:00 +03:00
|
|
|
if(isValid){
|
|
|
|
$formGroup.removeClass("has-error");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$formGroup.addClass("has-error");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
})
|
2015-11-10 22:52:59 +03:00
|
|
|
.on("focus", ".form-control, .form-group.fileinput", function() {
|
2015-11-10 19:26:00 +03:00
|
|
|
$(this).parent().addClass("is-focused"); // add class to form-group
|
2014-11-26 14:42:41 +03:00
|
|
|
})
|
2015-11-10 22:52:59 +03:00
|
|
|
.on("blur", ".form-control, .form-group.fileinput", function() {
|
2015-11-10 19:26:00 +03:00
|
|
|
$(this).parent().removeClass("is-focused"); // remove class from form-group
|
2014-11-26 14:42:41 +03:00
|
|
|
})
|
2015-11-06 19:30:05 +03:00
|
|
|
.on("change", ".form-group.fileinput [type=file]", function() {
|
2015-08-03 23:16:46 +03:00
|
|
|
var $this = $(this);
|
2014-11-26 14:42:41 +03:00
|
|
|
var value = "";
|
2015-08-03 23:16:46 +03:00
|
|
|
$.each(this.files, function(i, file) {
|
2014-11-26 14:42:41 +03:00
|
|
|
value += file.name + ", ";
|
|
|
|
});
|
|
|
|
value = value.substring(0, value.length - 2);
|
2015-11-10 20:52:15 +03:00
|
|
|
var $formGroup = $this.parent(".form-group");
|
2014-11-26 14:42:41 +03:00
|
|
|
if (value) {
|
2015-11-10 20:52:15 +03:00
|
|
|
$formGroup.removeClass("is-empty");
|
2014-11-26 14:42:41 +03:00
|
|
|
} else {
|
2015-11-10 20:52:15 +03:00
|
|
|
$formGroup.addClass("is-empty");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
2015-08-03 23:16:46 +03:00
|
|
|
$this.prev().val(value);
|
2014-11-26 14:42:41 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
"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() {
|
|
|
|
// 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() {
|
2015-08-03 23:16:46 +03:00
|
|
|
var $this = $(this);
|
|
|
|
if ($this.val() && $this.val() !== $this.attr("value")) {
|
|
|
|
$this.trigger("change");
|
2014-12-04 13:34:13 +03:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
});
|
2014-12-04 13:34:13 +03:00
|
|
|
}, 100);
|
2014-10-28 10:45:33 +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);
|
2015-08-22 16:59:08 +03:00
|
|
|
},
|
|
|
|
"attachAutofillEventHandlers": function() {
|
|
|
|
// Listen on inputs of the focused form (because user can select from the autofill dropdown only when the input has focus)
|
2014-12-04 13:34:13 +03:00
|
|
|
var focused;
|
|
|
|
$(document)
|
|
|
|
.on("focus", "input", function() {
|
2015-11-07 01:46:05 +03:00
|
|
|
console.log($(this).parent());
|
2014-12-04 13:34:13 +03:00
|
|
|
var $inputs = $(this).parents("form").find("input").not("[type=file]");
|
|
|
|
focused = setInterval(function() {
|
|
|
|
$inputs.each(function() {
|
2015-08-03 23:16:46 +03:00
|
|
|
var $this = $(this);
|
|
|
|
if ($this.val() !== $this.attr("value")) {
|
|
|
|
$this.trigger("change");
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100);
|
2014-12-04 13:34:13 +03:00
|
|
|
})
|
2015-11-07 01:46:05 +03:00
|
|
|
.on("blur", ".form-group input", function() {
|
2014-12-04 13:34:13 +03:00
|
|
|
clearInterval(focused);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
"init": function() {
|
2015-08-03 23:26:33 +03:00
|
|
|
var $document = $(document);
|
2015-08-22 16:59:08 +03:00
|
|
|
|
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();
|
2015-08-22 16:59:08 +03:00
|
|
|
this.attachInputEventHandlers();
|
2014-12-04 13:34:13 +03:00
|
|
|
}
|
|
|
|
if (this.options.checkbox) {
|
|
|
|
this.checkbox();
|
|
|
|
}
|
|
|
|
if (this.options.togglebutton) {
|
|
|
|
this.togglebutton();
|
|
|
|
}
|
|
|
|
if (this.options.radio) {
|
|
|
|
this.radio();
|
|
|
|
}
|
|
|
|
if (this.options.autofill) {
|
|
|
|
this.autofill();
|
2015-08-22 16:59:08 +03:00
|
|
|
this.attachAutofillEventHandlers();
|
2014-12-04 13:34:13 +03:00
|
|
|
}
|
2014-10-28 10:45:33 +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) {
|
2015-08-03 23:26:33 +03:00
|
|
|
$document.arrive(this.options.withRipples, function() {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.ripples($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.input) {
|
2015-08-03 23:26:33 +03:00
|
|
|
$document.arrive(this.options.inputElements, function() {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.input($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.checkbox) {
|
2015-08-03 23:26:33 +03:00
|
|
|
$document.arrive(this.options.checkboxElements, function() {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.checkbox($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.radio) {
|
2015-08-03 23:26:33 +03:00
|
|
|
$document.arrive(this.options.radioElements, function() {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.radio($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.togglebutton) {
|
2015-08-03 23:26:33 +03:00
|
|
|
$document.arrive(this.options.togglebuttonElements, function() {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.togglebutton($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-04 13:34:13 +03:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
}
|
|
|
|
};
|
2014-10-19 21:38:31 +04:00
|
|
|
|
2014-10-18 20:12:02 +04:00
|
|
|
})(jQuery);
|