2014-11-26 14:42:41 +03:00
|
|
|
/* globals jQuery */
|
2014-08-25 15:00:49 +04:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
(function ($) {
|
2014-11-26 14:42:41 +03:00
|
|
|
// Selector to select only not already processed elements
|
2015-12-14 04:38:09 +03:00
|
|
|
$.expr[":"].notmdproc = function (obj) {
|
2014-11-26 14:42:41 +03:00
|
|
|
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-12-14 04:38:09 +03:00
|
|
|
return (
|
|
|
|
!evt.ctrlKey
|
2015-12-14 04:28:39 +03:00
|
|
|
&& !evt.metaKey
|
|
|
|
&& !evt.altKey
|
|
|
|
&& evt.which != 8 // backspace
|
|
|
|
&& evt.which != 9 // tab
|
|
|
|
&& evt.which != 13 // enter
|
|
|
|
&& evt.which != 16 // shift
|
|
|
|
&& evt.which != 17 // ctrl
|
|
|
|
&& evt.which != 20 // caps lock
|
2015-12-14 04:38:09 +03:00
|
|
|
&& evt.which != 27 // escape
|
|
|
|
);
|
2014-10-18 18:05:05 +04:00
|
|
|
}
|
2014-11-26 14:42:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-09 20:24:09 +03:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
function _addFormGroupFocus(element) {
|
2015-12-09 21:57:24 +03:00
|
|
|
var $element = $(element);
|
|
|
|
if (!$element.prop('disabled')) { // this is showing as undefined on chrome but works fine on firefox??
|
|
|
|
$element.closest(".form-group").addClass("is-focused");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 13:28:36 +03:00
|
|
|
function _toggleDisabledState($element, state) {
|
|
|
|
var $target;
|
|
|
|
if ($element.hasClass('checkbox-inline') || $element.hasClass('radio-inline')) {
|
|
|
|
$target = $element;
|
|
|
|
} else {
|
|
|
|
$target = $element.closest('.checkbox').length ? $element.closest('.checkbox') : $element.closest('.radio');
|
|
|
|
}
|
|
|
|
return $target.toggleClass('disabled', state);
|
|
|
|
}
|
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
function _toggleTypeFocus($input) {
|
2016-05-22 13:28:36 +03:00
|
|
|
var disabledToggleType = false;
|
|
|
|
if ($input.is($.material.options.checkboxElements) || $input.is($.material.options.radioElements)) {
|
|
|
|
disabledToggleType = true;
|
|
|
|
}
|
2015-12-14 04:38:09 +03:00
|
|
|
$input.closest('label').hover(function () {
|
2016-05-22 13:28:36 +03:00
|
|
|
var $i = $(this).find('input');
|
|
|
|
var isDisabled = $i.prop('disabled'); // hack because the _addFormGroupFocus() wasn't identifying the property on chrome
|
|
|
|
if (disabledToggleType) {
|
|
|
|
_toggleDisabledState($(this), isDisabled);
|
|
|
|
}
|
|
|
|
if (!isDisabled) {
|
|
|
|
_addFormGroupFocus($i); // need to find the input so we can check disablement
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
_removeFormGroupFocus($(this).find('input'));
|
|
|
|
});
|
2015-11-20 19:17:12 +03:00
|
|
|
}
|
2015-12-09 20:24:09 +03:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
function _removeFormGroupFocus(element) {
|
2015-11-20 19:17:12 +03:00
|
|
|
$(element).closest(".form-group").removeClass("is-focused"); // remove class from form-group
|
|
|
|
}
|
2014-10-18 18:05:05 +04:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
$.material = {
|
2014-11-26 14:42:41 +03:00
|
|
|
"options": {
|
2014-12-04 13:34:13 +03:00
|
|
|
// These options set what will be started by $.material.init()
|
2015-12-08 02:19:38 +03:00
|
|
|
"validate": true,
|
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",
|
2016-04-26 08:42:39 +03:00
|
|
|
"checkboxElements": ".checkbox > label > input[type=checkbox], label.checkbox-inline > input[type=checkbox]",
|
2014-12-01 15:29:34 +03:00
|
|
|
"togglebuttonElements": ".togglebutton > label > input[type=checkbox]",
|
2016-04-26 09:08:51 +03:00
|
|
|
"radioElements": ".radio > label > input[type=radio], label.radio-inline > input[type=radio]"
|
2014-11-26 14:42:41 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"checkbox": function (selector) {
|
2014-11-26 14:42:41 +03:00
|
|
|
// Add fake-checkbox to material checkboxes
|
2015-12-09 21:57:24 +03:00
|
|
|
var $input = $((selector) ? selector : this.options.checkboxElements)
|
2015-12-14 04:38:09 +03:00
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class='checkbox-material'><span class='check'></span></span>");
|
2015-12-09 21:57:24 +03:00
|
|
|
|
|
|
|
_toggleTypeFocus($input);
|
2014-11-26 14:42:41 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"togglebutton": function (selector) {
|
2014-12-01 15:29:34 +03:00
|
|
|
// Add fake-checkbox to material checkboxes
|
2015-12-09 20:24:09 +03:00
|
|
|
var $input = $((selector) ? selector : this.options.togglebuttonElements)
|
2015-12-14 04:38:09 +03:00
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class='toggle'></span>");
|
2015-12-09 20:24:09 +03:00
|
|
|
|
2015-12-09 21:57:24 +03:00
|
|
|
_toggleTypeFocus($input);
|
2014-12-01 15:29:34 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"radio": function (selector) {
|
2014-11-26 14:42:41 +03:00
|
|
|
// Add fake-radio to material radios
|
2015-12-09 20:24:09 +03:00
|
|
|
var $input = $((selector) ? selector : this.options.radioElements)
|
2015-12-14 04:38:09 +03:00
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.after("<span class='circle'></span><span class='check'></span>");
|
2015-12-09 20:24:09 +03:00
|
|
|
|
2015-12-09 21:57:24 +03:00
|
|
|
_toggleTypeFocus($input);
|
2014-11-26 14:42:41 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"input": function (selector) {
|
2014-11-26 14:42:41 +03:00
|
|
|
$((selector) ? selector : this.options.inputElements)
|
2015-12-14 04:38:09 +03:00
|
|
|
.filter(":notmdproc")
|
|
|
|
.data("mdproc", true)
|
|
|
|
.each(function () {
|
|
|
|
var $input = $(this);
|
2015-01-26 20:05:46 +03:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
// Requires form-group standard markup (will add it if necessary)
|
|
|
|
var $formGroup = $input.closest(".form-group"); // note that form-group may be grandparent in the case of an input-group
|
2016-02-27 00:22:32 +03:00
|
|
|
if ($formGroup.length === 0 && $input.attr('type') !== "hidden" && !$input.attr('hidden')) {
|
2015-12-14 04:38:09 +03:00
|
|
|
$input.wrap("<div class='form-group'></div>");
|
|
|
|
$formGroup = $input.closest(".form-group"); // find node after attached (otherwise additional attachments don't work)
|
|
|
|
}
|
2015-11-06 19:30:05 +03:00
|
|
|
|
2015-12-14 04:38:09 +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'>" + $input.attr("data-hint") + "</p>");
|
|
|
|
$input.removeAttr("data-hint");
|
2015-11-10 23:45:08 +03:00
|
|
|
}
|
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
// Legacy - Change input-sm/lg to form-group-sm/lg instead (preferred standard and simpler css/less variants)
|
|
|
|
var legacySizes = {
|
|
|
|
"input-lg": "form-group-lg",
|
|
|
|
"input-sm": "form-group-sm"
|
|
|
|
};
|
|
|
|
$.each(legacySizes, function (legacySize, standardSize) {
|
|
|
|
if ($input.hasClass(legacySize)) {
|
|
|
|
$input.removeClass(legacySize);
|
|
|
|
$formGroup.addClass(standardSize);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Legacy - Add label-floating 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 + "'";
|
|
|
|
}
|
|
|
|
$formGroup.addClass("label-floating");
|
|
|
|
$input.after("<label " + forAttribute + "class='control-label'>" + placeholder + "</label>");
|
2015-11-06 20:04:07 +03:00
|
|
|
}
|
2014-11-12 15:22:17 +03:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
// Set as empty if is empty (damn I must improve this...)
|
|
|
|
if ($input.val() === null || $input.val() == "undefined" || $input.val() === "") {
|
|
|
|
$formGroup.addClass("is-empty");
|
|
|
|
}
|
2014-11-12 15:22:17 +03:00
|
|
|
|
2015-12-14 04:38:09 +03:00
|
|
|
// Support for file input
|
|
|
|
if ($formGroup.find("input[type=file]").length > 0) {
|
|
|
|
$formGroup.addClass("is-fileinput");
|
|
|
|
}
|
|
|
|
});
|
2015-08-22 16:59:08 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"attachInputEventHandlers": function () {
|
2015-12-08 02:19:38 +03:00
|
|
|
var validate = this.options.validate;
|
2015-11-20 19:17:12 +03:00
|
|
|
|
2014-11-26 14:42:41 +03:00
|
|
|
$(document)
|
2015-12-14 04:38:09 +03:00
|
|
|
.on("keydown paste", ".form-control", function (e) {
|
|
|
|
if (_isChar(e)) {
|
|
|
|
$(this).closest(".form-group").removeClass("is-empty");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("keyup change", ".form-control", function () {
|
|
|
|
var $input = $(this);
|
|
|
|
var $formGroup = $input.closest(".form-group");
|
|
|
|
var isValid = (typeof $input[0].checkValidity === "undefined" || $input[0].checkValidity());
|
2015-11-10 19:26:00 +03:00
|
|
|
|
2015-12-14 04:51:57 +03:00
|
|
|
if ($input.val() === "") {
|
2015-12-14 04:38:09 +03:00
|
|
|
$formGroup.addClass("is-empty");
|
2015-12-08 02:19:38 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-12-14 04:38:09 +03:00
|
|
|
$formGroup.removeClass("is-empty");
|
2015-12-08 02:19:38 +03:00
|
|
|
}
|
2015-11-19 05:21:55 +03:00
|
|
|
|
2015-12-14 04:38:09 +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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
if (validate) {
|
|
|
|
if (isValid) {
|
|
|
|
$formGroup.removeClass("has-error");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$formGroup.addClass("has-error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("focus", ".form-control, .form-group.is-fileinput", function () {
|
|
|
|
_addFormGroupFocus(this);
|
|
|
|
})
|
|
|
|
.on("blur", ".form-control, .form-group.is-fileinput", function () {
|
|
|
|
_removeFormGroupFocus(this);
|
|
|
|
})
|
|
|
|
// make sure empty is added back when there is a programmatic value change.
|
|
|
|
// NOTE: programmatic changing of value using $.val() must trigger the change event i.e. $.val('x').trigger('change')
|
|
|
|
.on("change", ".form-group input", function () {
|
|
|
|
var $input = $(this);
|
|
|
|
if ($input.attr("type") == "file") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var $formGroup = $input.closest(".form-group");
|
|
|
|
var value = $input.val();
|
|
|
|
if (value) {
|
|
|
|
$formGroup.removeClass("is-empty");
|
|
|
|
} else {
|
|
|
|
$formGroup.addClass("is-empty");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// set the fileinput readonly field with the name of the file
|
|
|
|
.on("change", ".form-group.is-fileinput input[type='file']", function () {
|
|
|
|
var $input = $(this);
|
|
|
|
var $formGroup = $input.closest(".form-group");
|
|
|
|
var value = "";
|
|
|
|
$.each(this.files, function (i, file) {
|
|
|
|
value += file.name + ", ";
|
|
|
|
});
|
|
|
|
value = value.substring(0, value.length - 2);
|
|
|
|
if (value) {
|
|
|
|
$formGroup.removeClass("is-empty");
|
|
|
|
} else {
|
|
|
|
$formGroup.addClass("is-empty");
|
|
|
|
}
|
|
|
|
$formGroup.find("input.form-control[readonly]").val(value);
|
2014-11-26 14:42:41 +03:00
|
|
|
});
|
|
|
|
},
|
2015-12-14 04:38:09 +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
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"autofill": function () {
|
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)
|
2015-12-14 04:38:09 +03:00
|
|
|
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
|
2015-12-14 04:38:09 +03:00
|
|
|
setTimeout(function () {
|
2014-12-04 13:34:13 +03:00
|
|
|
clearInterval(loading);
|
|
|
|
}, 10000);
|
2015-08-22 16:59:08 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"attachAutofillEventHandlers": function () {
|
2015-08-22 16:59:08 +03:00
|
|
|
// 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)
|
2015-12-14 04:38:09 +03:00
|
|
|
.on("focus", "input", function () {
|
|
|
|
var $inputs = $(this).parents("form").find("input").not("[type=file]");
|
|
|
|
focused = setInterval(function () {
|
|
|
|
$inputs.each(function () {
|
|
|
|
var $this = $(this);
|
|
|
|
if ($this.val() !== $this.attr("value")) {
|
|
|
|
$this.trigger("change");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 100);
|
|
|
|
})
|
|
|
|
.on("blur", ".form-group input", function () {
|
|
|
|
clearInterval(focused);
|
|
|
|
});
|
2014-12-04 13:34:13 +03:00
|
|
|
},
|
2015-12-14 04:38:09 +03:00
|
|
|
"init": function (options) {
|
2015-12-08 02:36:35 +03:00
|
|
|
this.options = $.extend({}, this.options, options);
|
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) {
|
2017-02-04 00:14:32 +03:00
|
|
|
document.arrive(this.options.withRipples, function () {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.ripples($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.input) {
|
2017-02-04 00:14:32 +03:00
|
|
|
document.arrive(this.options.inputElements, function () {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.input($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.checkbox) {
|
2017-02-04 00:14:32 +03:00
|
|
|
document.arrive(this.options.checkboxElements, function () {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.checkbox($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.radio) {
|
2017-02-04 00:14:32 +03:00
|
|
|
document.arrive(this.options.radioElements, function () {
|
2015-01-16 19:37:26 +03:00
|
|
|
$.material.radio($(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.options.togglebutton) {
|
2017-02-04 00:14:32 +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);
|