/*jslint browser: true*/
/*global console, $ */
(function () {
if (typeof window.Utils !== "object") {
window.Utils = {
PATH_LOG_ERROR: "errors_logged.json?_action=logJSError",
isLoggingErrors: false,
onError: function (desc, page, line, chr) {
if (this.isLoggingErrors && $) {
$.post(this.PATH_LOG_ERROR, {
info: JSON.stringify(desc + (line ? ", line: " + line + (chr ? (" [" + chr + "]") : "") : "") + (page ? ", in: " + page : ""))
}, null, 'json');
}
this.trace();
this.error('JavaScript error:' + '\n\tError description: \t' + desc + '\n\tPage address: \t' + page + '\n\tLine number: \t' + line + (chr ? ('\n\tChar number: \t' + chr) : ''));
return true;
},
debug: function (data) {
this.isDebugging = data && (typeof console !== "undefined");
},
develope: function (data) {
this.isDeveloped = data;
},
logErrors: function (data, path) {
this.isLoggingErrors = data;
if (path) {
this.PATH_LOG_ERROR = path;
}
}
};
var method, el, els = ["log", "info", "warn", "error", "trace", "assert", "dir", "clear", "profile", "profileEnd", "group", "groupEnd"];
for (el in els) {
method = els[el];
window.Utils[method] = (function (method) {
return function () {
if (this.isDebugging) {
if (typeof console[method] === "function") {
console[method].apply(console, arguments);
} else if (typeof console[method] === "object") {
console[method](Array.prototype.slice.call(arguments, 0));
}
}
};
}(method));
}
}
}());/*jslint browser: true*/
/*global $, jQuery, WP, Utils, alert*/
(function ($) {
var AFTER = "after",
BEFORE = "before",
OK = "OK",
ERROR = "error",
SUCCESS = "success",
VALIDATING = "validating",
EVENT_FOCUS = "focus",
EVENT_BLUR = "blur",
EVENT_CLICK = "click",
EVENT_CHANGE = "change",
EVENT_KEYUP = "keyup",
EVENT_KEYPRESS = "keypress",
EVENT_UPDATE = "update",
EVENT_MODIFY = "modify",
EVENT_CLEAR = "clear",
//EVENT_RESET = "reset",
EVENT_DESTROY = "destroy",
EVENT_VALIDATE = "validate",
EVENT_VALIDATE_RELATED = "validateRelated",
TYPE_TEXT = "textarea,:password,:text",
TYPE_SELECT = "select",
TYPE_BOX = ":radio, :checkbox",
TYPE_FILE = ":file",
STRING = "string",
DISABLED = "disabled",
BLACK = "black",
ALPHA = "alpha",
NUMERIC = "numeric",
ORIGINAL = "original",
PLUGIN_CLASS = "hasValidator",
CLASSES = [ERROR, SUCCESS, VALIDATING].join(" "),
validations = {
isKeyNumeric: function (which) {
return (
(which >= 48 && which <= 57) // 0-9
);
},
isKeyAlpha: function (which) {
return (
which === 32 || // space
(which >= 65 && which <= 90) || // a-z
(which >= 97 && which <= 122) // A-Z
);
},
isKeyDelete: function (which) {
return (
which === 0 || // del
which === 8 || // backspace
which === 9 || // tab
which === 46 // dot
);
},
isCommaDot: function (which) {
return (
which === 44 || // przecinek
which === 46 // dot
);
},
isKeyChar: function (which) {
////Utils.log("isKeyVisible", which);
return (
(which >= 48 && which <= 64) || // znaki
(which >= 65 && which <= 90) || // a-z
(which >= 91 && which <= 96) || // znaki
(which >= 97 && which <= 122) || // A-Z
(which >= 123 && which <= 126) || // znaki
(which >= 160 && which <= 222) // znaki
);
},
isKeyBlack: function (which) {
////Utils.log("isKeyVisible", which);
return (
which === 8 || // backspace
this.isKeyChar(which));
},
isKeyPrintable: function (which) {
////Utils.log("isKeyPrintable", which);
return (
which === 32 || // space
this.isKeyBlack(which));
},
isKeyNavigation: function (which) {
////Utils.log("isKeyNavigation", which);
return (
which >= 33 && which <= 40 // strzałki
);
},
isChecked: function (el) {
return el.is(":checked");
},
isAnyChecked: function (el) {
return el.filter(":checked").length !== 0;
},
isAnySelected: function (el) {
var value = $.trim(el.find("option:selected").val());
//Utils.log("isAnySelected", el, value);
return (value !== "-1" && value !== "");
},
isEmpty: function (el) {
return $.trim(el.val()) === "";
},
isNotEmpty: function (el) {
//Utils.log("isNotEmpty", el, el.val());
return $.trim(el.val()) !== "";
},
isValidEmail: function (ev) {
return (/^[_a-z0-9\-]+(\.[_a-z0-9\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})$/i).test($.trim(ev.val()));
},
hasDigits: function (ev) {
return (/[0-9]+/).test($.trim(ev.val()));
},
hasCapitals: function (ev) {
return (/[A-Z]+/).test($.trim(ev.val()));
},
hasValidLength: function (el, data) {
return ((!data.min || (data.min && $.trim(el.val()).length >= data.min)) && (!data.max || (data.max && $.trim(el.val()).length <= data.max)));
}
},
restrictions = {
onlyBlack: function (e) {
//Utils.log("onlyBlack", e.which);
if (!validations.isKeyBlack(e.which)) {
//if (!validations.isKeyAlpha(e.which) && !validations.isKeyDelete(e.which)) {
e.preventDefault();
//}
}
},
onlyAlpha: function (e) {
////Utils.log("onlyAlpha", e.which);
if (validations.isKeyPrintable(e.which)) {
if (!validations.isKeyAlpha(e.which) && !validations.isKeyDelete(e.which)) {
e.preventDefault();
}
}
},
onlyNumeric: function (e) {
////Utils.log("onlyNumeric", e.which);
if (validations.isKeyPrintable(e.which)) {
if (!validations.isKeyNumeric(e.which) && !validations.isKeyDelete(e.which)) {
e.preventDefault();
}
}
}
},
defaults = {
debug: false,
useCSS: false,
useInfoCSS: false,
useDecoCSS: false,
decoPlace: AFTER,
infoPlace: AFTER,
infoTemplate: '',
decoTemplate: '',
decoContent: {
info: "",
warning: "",
error: "",
success: "",
validating: ""
},
textValidations: [{
test: validations.isNotEmpty,
errorText: "Pole wymagane"
}],
boxValidations: [{
test: validations.isAnyChecked,
errorText: "Pole wymagane"
}],
selectValidations: [{
test: validations.isAnySelected,
errorText: "Pole wymagane"
}],
btnReset: null,
btnSubmit: null,
onReset: null,
onSubmit: null
},
validatorItemMethods = {
init: function () {
this.info = $(this.form.infoTemplate).hide();
this.deco = $(this.form.decoTemplate).hide();
this.holder = this.parents(this.holderSelector).eq(0);
if (this.infoPlace === BEFORE) {
this.holder.prepend(this.info);
} else {
this.holder.append(this.info);
}
if (this.decoPlace === BEFORE) {
this.holder.prepend(this.deco);
} else {
this.holder.append(this.deco);
}
this.on(EVENT_CLEAR, this.clear.bind(this));
this.on(EVENT_DESTROY, this.destroy.bind(this));
this.on(EVENT_VALIDATE, this.validate.bind(this));
this.on(EVENT_VALIDATE_RELATED, this.validate.bind(this));
/* switch (this.type) {
case TYPE_BOX:
this.delegated.on(EVENT_CHANGE, this.selector, this.validateOnCheck.bind(this));
break;
case TYPE_TEXT:
this.delegated.on(EVENT_CHANGE, this.selector, this.validate.bind(this));
this.delegated.on(EVENT_KEYUP, this.selector, this.onTextKeyup.bind(this));
this.delegated.on(EVENT_KEYPRESS, this.selector, this.onTextKeypress.bind(this));
break;
case TYPE_FILE:
this.delegated.on(EVENT_CHANGE, this.selector, this.validate.bind(this));
case TYPE_SELECT:
this.delegated.on(EVENT_CHANGE, this.selector, this.validateOnSelect.bind(this));
this.delegated.on(EVENT_KEYUP, this.selector, this.onSelectKeyup.bind(this));
break;
}*/
switch (this.type) {
case TYPE_BOX:
this.on(EVENT_CHANGE, this.validateOnCheck.bind(this));
break;
case TYPE_TEXT:
if (this.delegated) {
//this.delegated.on(EVENT_CHANGE, this.selector, this.validate.bind(this));
this.delegated.on(EVENT_MODIFY, this.selector, this.onModify.bind(this));
this.delegated.on(EVENT_KEYUP, this.selector, this.onTextKeyup.bind(this));
this.delegated.on(EVENT_KEYPRESS, this.selector, this.onTextKeypress.bind(this));
} else {
this.on(EVENT_FOCUS, this.onFocus.bind(this));
this.on(EVENT_BLUR, this.onBlur.bind(this));
this.on(EVENT_CHANGE, this.validate.bind(this));
this.on(EVENT_MODIFY, this.onModify.bind(this));
this.on(EVENT_KEYUP, this.onTextKeyup.bind(this));
this.on(EVENT_KEYPRESS, this.onTextKeypress.bind(this));
}
break;
case TYPE_FILE:
this.on(EVENT_CHANGE, this.validate.bind(this));
break;
case TYPE_SELECT:
this.on(EVENT_CHANGE, this.validateOnSelect.bind(this));
this.on(EVENT_KEYUP, this.onSelectKeyup.bind(this));
break;
}
},
validationNext: function (e) {
var i = this.validationStates.length,
validationObject = this.validations[i];
////Utils.log("validationNext", i, this.validations.length);
if (i === this.validations.length || this.aborted) {
this.validationDone(e);
return;
}
if (this.is(":visible:enabled") || this.hidden) {
if ($.isFunction(validationObject.test)) {
//Utils.log("LOCAL", validationObject);
this.validationStates[i] = validationObject.test(this, validationObject.testData || {});
this.validationAfter(e, i);
} else if ($.type(validationObject.test) === STRING) {
////Utils.log("REMOTE", validationObject);
this.form.btnSubmit.attr(DISABLED, true);
this.jqxhr = $.post(validationObject.test, $.extend({}, validationObject.testData, validationObject.onBeforeTest())).done(function (response) {
//Utils.log("RESPONSE OK", response);
if ($.isPlainObject(response)) {
if (response.status === OK) {
validationObject.successText = response.value || "";
this.validationStates[i] = true;
this.validationAfter(e, i);
this.form.btnSubmit.removeAttr(DISABLED);
if ($.isFunction(validationObject.onAfterSuccess)) {
validationObject.onAfterSuccess(response);
}
} else {
validationObject.errorText = response.value || "";
this.isValid = false;
this.validationStates[i] = false;
this.validationAfter(e, i);
this.form.btnSubmit.removeAttr(DISABLED);
if ($.isFunction(validationObject.onAfterError)) {
validationObject.onAfterError(response);
}
}
} else {
if ($.trim(response)) {
validationObject.successText = response;
}
this.validationStates[i] = true;
this.validationAfter(e, i);
this.form.btnSubmit.removeAttr(DISABLED);
}
}.bind(this)).fail(function (response) {
////Utils.log("RESPONSE ERROR", response);
if ($.trim(response.responseText)) {
validationObject.errorText = response.responseText;
}
this.isValid = false;
this.validationStates[i] = false;
this.validationAfter(e, i);
this.form.btnSubmit.removeAttr(DISABLED);
}.bind(this));
}
} else {
this.isValid = null;
this.modified = true;
this.validated = false;
this.validationStates[i] = null;
this.validationAfter(e, i);
this.form.btnSubmit.removeAttr(DISABLED);
}
},
onTextKeypress: function (e) {
//Utils.log("onTextKeypress", this.restrict);
if ($.isFunction(this.restrict)) {
this.restrict(e);
} else if ($.type(this.restrict) === STRING) {
switch (this.restrict) {
case BLACK:
restrictions.onlyBlack(e);
break;
case ALPHA:
restrictions.onlyAlpha(e);
break;
case NUMERIC:
restrictions.onlyNumeric(e);
break;
default:
break;
}
}
},
validationAfter: function (e, i) {
var validationObject = this.validations[i];
if (this.validationStates[i] === false) {
//Utils.warn("validationError".bind(this);
if ($.isFunction(validationObject.onError)) {
validationObject.onError.bind(this)();
}
if (validationObject.errorText) {
this.errorTextArray.push(validationObject.errorText);
}
this.isValid = false;
this.validationDone(e);
} else if (this.validationStates[i] === true) {
//Utils.warn("validationSuccess".bind(this);
if ($.isFunction(validationObject.onSuccess)) {
validationObject.onSuccess.bind(this)();
}
if (validationObject.rules) {
$.each(validationObject.rules, function (i, el) {
if (el.element) {
switch (this.type) {
case TYPE_TEXT:
case TYPE_SELECT:
el.element.toggle(this.val() === el.value);
break;
case TYPE_BOX:
el.element.toggle(this.filter(":checked").val() === el.value);
break;
}
}
}.bind(this));
}
if (validationObject.successText) {
this.successTextArray.push(validationObject.successText);
}
this.validationNext(e);
} else {
//Utils.warn("validationNone".bind(this);
this.validationNext(e);
}
},
validate: function (e) {
//Utils.log(EVENT_VALIDATE, e.type, this, this.modified, this.validated);
switch (true) {
case (e.type === EVENT_VALIDATE_RELATED && (this.validated || this.modified)):
case (e.type === EVENT_VALIDATE && !this.validated):
case (e.type === EVENT_KEYPRESS && this.modified):
case (e.type === EVENT_KEYUP && this.modified):
case (e.type === EVENT_CHANGE && this.modified):
case (this.is(":hidden, :disabled")):
this.validationStart(e);
break;
}
},
validationStart: function (e) {
clearTimeout(this.keyupTimeout);
this.isValid = true;
this.aborted = false;
this.validated = true;
this.validating = true;
this.validationStates = [];
this.errorTextArray = [];
this.successTextArray = [];
this.showDeco(VALIDATING);
this.form.btnSubmit.attr(DISABLED, true);
if (this.validatingText) {
this.showInfo(VALIDATING, [this.validatingText]);
} else {
this.hideInfo();
}
this.validationNext(e);
},
validationDone: function (e) {
//Utils.log("validationDone", e);
if (this.aborted || this.isValid === null) {
this.clear();
} else {
this.validating = false;
this.modified = false;
if (this.isValid === true) {
this.showDeco(SUCCESS);
if (this.successTextArray.length) {
this.showInfo(SUCCESS, this.successTextArray);
} else {
this.hideInfo();
}
//Utils.info("RELATED", this.related);
if (e.type !== EVENT_VALIDATE_RELATED && this.related && this.related.length) {
$.each(this.related, function (i, el) {
el.trigger(EVENT_VALIDATE_RELATED);
});
}
} else if (this.isValid === false) {
this.showDeco(ERROR);
if (this.errorTextArray.length) {
this.showInfo(ERROR, this.errorTextArray);
} else {
this.hideInfo();
}
}
this.form.trigger(EVENT_UPDATE);
this.form.btnSubmit.removeAttr(DISABLED);
}
},
validateOnCheck: function (e) {
this.modified = true;
this.validate(e);
},
validateOnSelect: function (e) {
this.modified = true;
if (this.emptyOptionRemove && !this.emptyOptionRemoved) {
this.removeEmptyOption();
}
this.validate(e);
},
onFocus: function () {
this.data(ORIGINAL, this.val());
},
onBlur: function () {
if (this.val() !== this.data(ORIGINAL)) {
if (!this.validating) {
this.change();
}
}
},
onModify: function () {
////Utils.log("onModify");
this.modified = true;
this.change();
},
onTextKeyup: function (e) {
////Utils.log("onTextKeyup", e);
clearTimeout(this.keyupTimeout);
if (validations.isKeyPrintable(e.which)) {
this.modified = true;
////Utils.log("................");
this.keyupTimeout = setTimeout(function () {
this.validate(e);
}.bind(this), this.keyupValidationDelay);
}
},
onSelectKeyup: function (e) {
////Utils.log("onSelectKeyup", e);
if (validations.isKeyNavigation(e.which)) {
this.validateOnSelect(e);
}
},
positionInfoDeco: function () {
var position, newPosition, holder;
if (!this.form.useCSS) {
if (this.type === TYPE_BOX) {
holder = this.last().parent();
position = holder.position();
newPosition = {
top: position.top + holder.outerHeight() / 2,
left: position.left + holder.outerWidth()
};
} else {
position = this.position();
newPosition = {
top: position.top + this.outerHeight() / 2,
left: position.left + this.outerWidth()
};
}
if (!this.form.useDecoCSS) {
this.deco.css(newPosition);
}
if (!this.form.useInfoCSS) {
this.info.css(newPosition);
}
}
},
showDeco: function (type) {
this.clearDeco();
this.addClass(type);
this.holder.addClass(type);
//Utils.log("showDeco", this.form);
this.deco.addClass(type).empty().append(this.form.decoContent[type]).show(200, this.positionInfoDeco.bind(this));
},
showInfo: function (type, data) {
this.clearInfo();
this.info.addClass(type);
if (data && data.length) {
this.info.html(data.join("
")).slideDown(200, this.positionInfoDeco.bind(this));
}
},
clearDeco: function () {
this.positionInfoDeco();
this.removeClass(CLASSES);
this.holder.removeClass(CLASSES);
this.deco.removeClass(CLASSES);
},
clearInfo: function () {
this.positionInfoDeco();
this.info.removeClass(CLASSES);
},
hideDeco: function () {
this.deco.hide();
this.clearDeco();
},
hideInfo: function () {
this.info.hide();
this.clearInfo();
},
removeEmptyOption: function () {
this.emptyOptionRemoved = this.find('option[value="-1"], option.placeholder').detach();
},
destroy: function () {
//Utils.log(EVENT_DESTROY.bind(this);
this.clearDeco();
this.clearInfo();
this.info.remove();
this.deco.remove();
this.off();
},
clear: function () {
////Utils.log(EVENT_CLEAR.bind(this);
clearTimeout(this.keyupTimeout);
if (this.jqxhr) {
this.jqxhr.abort();
}
this.validated = false;
this.modified = false;
this.aborted = false;
this.clearDeco();
this.clearInfo();
this.hideInfo();
this.hideDeco();
if (this.emptyOptionRemoved) {
this.prepend(this.emptyOptionRemoved);
this.emptyOptionRemoved = null;
}
if ($.isFunction(this.onClear)) {
this.onClear.apply(this, arguments);
}
}
},
validatorMethods = {
init: function () {
//Utils.log("VALIDATOR init");
this.addClass(PLUGIN_CLASS);
$.each(this.items, function (i, el) {
////Utils.warn(i, el, el.selector, $(el.selector));
if (el instanceof $) {
this.items[i] = el.validatorItem(this);
} else if ($.type(el) === STRING) {
this.items[i] = $(el).validatorItem(this);
} else if ($.isPlainObject(el)) {
this.items[i] = $(el.selector).validatorItem(this, el);
}
}.bind(this));
if (this.btnSubmit && this.btnSubmit.length) {
this.btnSubmit.on(EVENT_CLICK, this.onFormSubmit.bind(this));
}
if (this.btnReset && this.btnReset.length) {
this.btnReset.on(EVENT_CLICK, this.onFormReset.bind(this));
}
this.on(EVENT_UPDATE, this.checkValidity.bind(this));
},
destroy: function () {
//Utils.log("destroy".bind(this);
this.clear();
$.each(this.items, function (i, el) {
//Utils.log(EVENT_DESTROY, el);
el.destroy();
});
this.off();
this.removeClass(PLUGIN_CLASS);
},
clear: function () {
//Utils.log("clear");
//this.trigger(EVENT_RESET);
$.each(this.items, function (i, el) {
//Utils.log(EVENT_CLEAR, el);
el.eq(0).trigger(EVENT_CLEAR);
});
},
validate: function (e) {
//Utils.log(this.items);
$.each(this.items, function (i, el) {
el.validate({
type: EVENT_VALIDATE
});
});
this.checkValidity(e);
},
checkValidity: function (e) {
//Utils.log("checkValidity", e);
this.isValid = true;
var firstItem;
$.each(this.items, function (i, el) {
//Utils.log(el, el.isValid);
if (el.isValid === false) {
this.isValid = false;
if (!firstItem) {
firstItem = el;
}
}
}.bind(this));
if (e && e.type === EVENT_CLICK && firstItem) {
firstItem.eq(0).focus();
}
/*if (this.isValid) {
this.btnSubmit.removeAttr(DISABLED);
}*/
},
onFormReset: function (e) {
//Utils.info("onFormReset", this.isValid, this.debug);
this.clear();
if ($.isFunction(this.onReset)) {
this.onReset(e);
}
if (this.debug) {
e.preventDefault();
alert("RESET DEBUG");
return;
}
},
onFormSubmit: function (e) {
this.validate(e);
//Utils.info("onFormSubmit", this.isValid, this.debug);
if (!this.isValid) {
e.preventDefault();
} else {
if ($.isFunction(this.onSubmit)) {
this.onSubmit(e);
}
if (this.debug) {
e.preventDefault();
alert("SUBMIT DEBUG");
return;
}
}
}
};
function validatorItem(form, options) {
this.form = form;
this.jqxhr = null;
this.keyupTimeout = null;
this.validating = null;
this.emptyOptionRemoved = false;
this.delegated = false;
this.hidden = false;
this.isValid = null;
this.modified = false;
this.validated = false;
this.validationStates = [];
this.errorTextArray = [];
this.successTextArray = [];
this.keyupValidationDelay = 3000;
this.validatingText = null;
this.holderSelector = "fieldset, div";
this.decoPlace = AFTER;
switch (true) {
case this.is(TYPE_BOX):
this.type = TYPE_BOX;
this.infoPlace = this.form.infoPlace || AFTER;
this.validations = this.form.boxValidations;
break;
case this.is(TYPE_TEXT):
this.type = TYPE_TEXT;
this.infoPlace = this.form.infoPlace || BEFORE;
this.validations = this.form.textValidations;
break;
case this.is(TYPE_FILE):
this.type = TYPE_FILE;
this.infoPlace = this.form.infoPlace || BEFORE;
this.validations = this.form.textValidations;
break;
case this.is(TYPE_SELECT):
this.type = TYPE_SELECT;
this.infoPlace = this.form.infoPlace || BEFORE;
this.validations = this.form.selectValidations;
break;
}
$.extend(this, validatorItemMethods, options);
////Utils.warn("validatorItem", this, this.holder);
this.init();
return this;
}
function validator(options) {
this.isValid = false;
$.extend(this, validatorMethods, defaults, options);
//////Utils.log("validator");
//////Utils.log("validator.items", this.items);
this.init();
return this;
}
validator.setValidatorDefaults = function (data) {
defaults = $.extend(defaults, data);
};
//validator.defaults = defaults;
validator.validations = validations;
validator.restrictions = restrictions;
$.fn.validator = validator;
$.fn.validatorItem = validatorItem;
}((typeof WP !== "undefined" && WP.$) ? WP.$ : $));/*jslint browser: true*/
/*global WP, $ */
var initLoginForm = function () {
Utils.info("initLoginForm");
var loginForm = WP.$(".bxLoginForm form"),
loginInput = loginForm.find(".loginInput"),
passwordInput = loginForm.find(".passwordInput"),
sendButton = loginForm.find(".sendButton"),
options = {
btnSubmit: sendButton,
useCSS: true,
items: [{
selector: loginInput,
infoPlace: "before",
validations: [{
test: WP.$.fn.validator.validations.isNotEmpty,
errorText: 'Wpisz login'
}]
}, {
selector: passwordInput,
infoPlace: "before",
validations: [{
test: WP.$.fn.validator.validations.isNotEmpty,
errorText: "Wpisz hasło"
}]
}]
};
loginForm.validator(options);
};
WP.$(initLoginForm);