153 lines
5.2 KiB
JavaScript
153 lines
5.2 KiB
JavaScript
"use strict";
|
|
|
|
var Index;
|
|
|
|
(function() {
|
|
Index = (function() {
|
|
function Index(){
|
|
JsBase.jsModule = this;
|
|
this.init();
|
|
}
|
|
|
|
Index.prototype = {
|
|
dt : {},
|
|
validator : {},
|
|
validatorSubscribe : {},
|
|
validatorContactUs : {},
|
|
jsModule : false,
|
|
}
|
|
|
|
Index.prototype.init = function(){
|
|
this.bindFormValidations();
|
|
this.bindFormSubmits();
|
|
};
|
|
|
|
const formHandlers = {
|
|
"unsubscribe": "submitUnsubscribeForm",
|
|
"contact_us": "submitContactForm"
|
|
};
|
|
|
|
Index.prototype.bindFormSubmits = function(){
|
|
let _this = this;
|
|
let jqForms = $("#popup_subscribe, #footer_subscribe, #unsubscribe, #contact_us");
|
|
jqForms.each(function() {
|
|
let $form = $(this);
|
|
let formId = $form.attr("id");
|
|
let handlerName = formHandlers[formId];
|
|
|
|
if (!handlerName) return;
|
|
|
|
// Form input handling
|
|
$form.find("input").on({
|
|
focus: function() {
|
|
$(this).parent().addClass("active");
|
|
},
|
|
blur: function() {
|
|
$(this).parent().removeClass("active");
|
|
},
|
|
keydown: function(e) {
|
|
if (e.which === JsBase.iKeys.ENTER) {
|
|
JsBase.ajaxLoader.jqAjaxSaveCaller = $(this);
|
|
_this[handlerName]($form);
|
|
}
|
|
}
|
|
});
|
|
|
|
// Submit button
|
|
$form.find("button[type='submit']").click(function(e) {
|
|
e.preventDefault();
|
|
JsBase.ajaxLoader.jqAjaxSaveCaller = $(this);
|
|
_this[handlerName]($form);
|
|
return true;
|
|
});
|
|
});
|
|
};
|
|
|
|
Index.prototype.submitUnsubscribeForm = function (jqParent) {
|
|
let _this = this;
|
|
let jqField = jqParent.find('input[type="email"]');
|
|
if (!_this.validatorSubscribe.isFormValid(jqField)) return false;
|
|
|
|
let oData = {action: 'unsubscribe', data: {sub_email: jqField.val()}};
|
|
JsBase.ajax(oData, false, (data) => {
|
|
_this.handleFormResponse(jqParent, data);
|
|
});
|
|
|
|
return true;
|
|
};
|
|
|
|
Index.prototype.submitContactForm = function(jqParent){
|
|
var _this = this;
|
|
if (!_this.validatorContactUs.isFormValid(false)) return false;
|
|
|
|
let oData = {
|
|
action: 'contact_us',
|
|
data: $.toJSON({
|
|
name: jqParent.find("#name").val(),
|
|
email: jqParent.find("#email").val(),
|
|
phone: jqParent.find("#phone").val(),
|
|
message: jqParent.find("#message").val()
|
|
})
|
|
};
|
|
|
|
JsBase.ajax(oData, false, (data) => {
|
|
_this.handleFormResponse(jqParent, data);
|
|
});
|
|
|
|
return true;
|
|
};
|
|
|
|
Index.prototype.bindFormValidations = function(){
|
|
if (typeof Validator === "undefined") {
|
|
console.log("Validator is not defined, form validation will not work.");
|
|
return;
|
|
}
|
|
|
|
Validator.prototype.showError = function(caller, msg) {
|
|
if (caller && window.JsBase && $.isFunction(JsBase.showFieldError)) JsBase.showFieldError(caller, msg);
|
|
};
|
|
|
|
Validator.prototype.hideError = function(caller) {
|
|
if (caller && window.JsBase && $.isFunction(JsBase.hideFieldError)) JsBase.hideFieldError(caller);
|
|
};
|
|
|
|
if ($.isDefined(JsBase.dt.subCfg)) {
|
|
this.validatorSubscribe = new Validator(JsBase.dt.subCfg, "body", JsBase.gM);
|
|
if ($.isDefined(JsBase.dt.fData)) {
|
|
this.validatorSubscribe.showErrorsByObject(JsBase.dt.fData.err);
|
|
}
|
|
}
|
|
|
|
if ($.isDefined(JsBase.dt.valCfg)) {
|
|
this.validatorContactUs = new Validator(JsBase.dt.valCfg, "body", JsBase.gM);
|
|
if ($.isDefined(JsBase.dt.fData)) {
|
|
this.validatorContactUs.showErrorsByObject(JsBase.dt.fData.err);
|
|
}
|
|
}
|
|
};
|
|
|
|
Index.prototype.handleFormResponse = function(jqParent, data) {
|
|
let _this = this;
|
|
let isSuccess = data && data.jsonok;
|
|
if (!isSuccess && data.error && data.err) {
|
|
for(let i in data.err) {
|
|
if (data.err.hasOwnProperty(i)) {
|
|
let $field = jqParent.find("#" + i);
|
|
if ($field.length) {
|
|
_this.validatorContactUs.showError($field, data.err[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
setTimeout(() => {
|
|
jqParent.find("h3.tt, .v-ok").removeClass("ok err v-ok");
|
|
if (isSuccess) {
|
|
jqParent.find("input:not(.btn), textarea").val('');
|
|
}
|
|
}, 1400);
|
|
};
|
|
|
|
return Index;
|
|
})();
|
|
})(); |