master_template/public/installer/froiden-helper/helper.js

452 lines
37 KiB
JavaScript
Raw Normal View History

2024-06-10 12:21:58 +00:00
(function($) {
'use strict';
$.easyAjax = function(options) {
var defaults = {
type: 'GET',
container: 'body',
blockUI: true,
disableButton: false,
buttonSelector: "[type='submit']",
dataType: "json",
messagePosition: "toastr",
errorPosition: "field",
hideElements: false,
redirect: true,
data: {},
file: false
};
var opt = defaults;
// Extend user-set options over defaults
if (options) {
opt = $.extend(defaults, options);
}
// Methods if not given in option
if (typeof opt.beforeSend != "function") {
opt.beforeSend = function() {
// Hide previous errors
$(opt.container).find(".has-error").each(function () {
$(this).find(".help-block").text("");
$(this).removeClass("has-error");
});
$(opt.container).find("#alert").html("");
if (opt.blockUI) {
$.easyBlockUI(opt.container);
}
if (opt.disableButton) {
loadingButton(opt.buttonSelector);
}
}
}
if (typeof opt.complete != "function") {
opt.complete = function (jqXHR, textStatus) {
if (opt.blockUI) {
$.easyUnblockUI(opt.container);
}
if (opt.disableButton) {
unloadingButton(opt.buttonSelector)
}
}
}
// Default error handler
if (typeof opt.error != "function") {
opt.error = function(jqXHR, textStatus, errorThrown) {
try {
var response = JSON.parse(jqXHR.responseText);
if (typeof response == "object") {
handleFail(response);
}
else {
var msg = "A server side error occurred. Please try again after sometime.";
if (textStatus == "timeout") {
msg = "Connection timed out! Please check your internet connection";
}
showResponseMessage(msg, "error");
}
}
catch (e) {
}
}
}
function showResponseMessage(msg, type, toastrOptions) {
var typeClasses = {
"error": "danger",
"success": "success",
"primary": "primary",
"warning": "warning",
"info": "info"
};
if (opt.messagePosition == "toastr") {
$.showToastr(msg, type, toastrOptions);
}
else {
var ele = $(opt.container).find("#alert");
var html = '<div class="alert alert-'+ typeClasses[type] +'">' + msg +'</div>';
if (ele.length == 0) {
$(opt.container).find(".form-group:first")
.before('<div id="alert">' + html + "</div>");
}
else {
ele.html(html);
}
}
}
// Execute ajax request
if (opt.file == true) {
var data = new FormData($(opt.container)[0]);
var keys = Object.keys(opt.data);
for(var i=0; i<keys.length;i++) {
data.append(keys[i], opt.data[keys[i]]);
}
opt.data = data;
}
$.ajax({
type: opt.type,
url: opt.url,
dataType: opt.dataType,
data: opt.data,
beforeSend: opt.beforeSend,
contentType: (opt.file)?false:"application/x-www-form-urlencoded; charset=UTF-8",
processData : !opt.file,
error: opt.error,
complete: opt.complete,
success: function (response) {
// Show success message
if (response.status == "success") {
if (response.action == "redirect") {
if (opt.redirect) {
var message = "";
if (typeof response.message != "undefined") {
message += response.message;
}
message += " Redirecting...";
showResponseMessage(message, "success", {
timeOut: 100000,
positionClass: "toast-top-right"
});
window.location.href = response.url;
}
}
else {
if (typeof response.message != "undefined") {
showResponseMessage(response.message, "success");
}
}
if (opt.removeElements == true) {
$(opt.container).find(".form-group, button, input").remove();
}
}
if (response.status == "fail") {
handleFail(response);
}
if (typeof opt.success == "function") {
opt.success(response);
}
}
});
function handleFail(response) {
if (typeof response.message != "undefined") {
showResponseMessage(response.message, "error");
}
if (typeof response.errors != "undefined") {
var keys = Object.keys(response.errors);
$(opt.container).find(".has-error").find(".help-block").remove();
$(opt.container).find(".has-error").removeClass("has-error");
if (opt.errorPosition == "field") {
for (var i = 0; i < keys.length; i++) {
// Escape dot that comes with error in array fields
var key = keys[i].replace(".", '\\.');
var formarray = keys[i];
// If the response has form array
if(formarray.indexOf('.') >0){
var array = formarray.split('.');
response.errors[keys[i]] = response.errors[keys[i]];
key = array[0]+'['+array[1]+']';
}
var ele = $(opt.container).find("[name='" + key + "']");
// If cannot find by name, then find by id
if (ele.length == 0) {
ele = $(opt.container).find("#" + key);
}
var grp = ele.closest(".form-group");
$(grp).find(".help-block").remove();
var helpBlockContainer = $(grp).find("div:first");
if($(ele).is(':radio')){
helpBlockContainer = $(grp).find("div:eq(2)");
}
if (helpBlockContainer.length == 0) {
helpBlockContainer = $(grp);
}
helpBlockContainer.append('<div class="help-block">' + response.errors[keys[i]] + '</div>');
$(grp).addClass("has-error");
}
if (keys.length > 0) {
var element = $("[name='" + keys[0] + "']");
if (element.length > 0) {
$("html, body").animate({scrollTop: element.offset().top - 150}, 200);
}
}
}
else {
var errorMsg = "<ul>";
for (var i = 0; i < keys.length; i++) {
errorMsg += "<li>" + response.errors[keys[i]] + "</li>";
}
errorMsg += "</ul>";
var errorElement = $(opt.container).find("#alert");
var html = '<div class="alert alert-danger">' + errorMsg +'</div>';
if (errorElement.length == 0) {
$(opt.container).find(".form-group:first")
.before('<div id="alert">' + html + "</div>");
}
else {
errorElement.html(html);
}
}
}
}
function loadingButton(selector) {
var button = $(opt.container).find(selector);
var text = "Submitting...";
if (button.width() < 20) {
text = "...";
}
if (!button.is("input")) {
button.attr("data-prev-text", button.html());
button.text(text);
button.prop("disabled", true);
}
else {
button.attr("data-prev-text", button.val());
button.val(text);
button.prop("disabled", true);
}
}
function unloadingButton(selector) {
var button = $(opt.container).find(selector);
if (!button.is("input")) {
button.html(button.attr("data-prev-text"));
button.prop("disabled", false);
}
else {
button.val(button.attr("data-prev-text"));
button.prop("disabled", false);
}
}
};
$.easyBlockUI = function (container, message) {
if (message == undefined) {
message = "Loading...";
}
var html = '<div class="loading-message"><div class="block-spinner-bar"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div></div>';
if (container != undefined) { // element blocking
var el = $(container);
var centerY = false;
if (el.height() <= ($(window).height())) {
centerY = true;
}
el.block({
message: html,
baseZ: 999999,
centerY: centerY,
css: {
top: '10%',
border: '0',
padding: '0',
backgroundColor: 'none'
},
overlayCSS: {
backgroundColor: 'transparent',
opacity: 0.05,
cursor: 'wait'
}
});
} else { // page blocking
$.blockUI({
message: html,
baseZ: 999999,
css: {
border: '0',
padding: '0',
backgroundColor: 'none'
},
overlayCSS: {
backgroundColor: '#555',
opacity: 0.05,
cursor: 'wait'
}
});
}
};
$.easyUnblockUI = function (container) {
if (container == undefined) {
$.unblockUI();
}
else {
$(container).unblock({
onUnblock: function () {
$(container).css('position', '');
$(container).css('zoom', '');
}
});
}
};
$.showToastr = function(toastrMessage, toastrType, options) {
var defaults = {
"closeButton": false,
"debug": false,
"positionClass": "toast-top-right",
"onclick": null,
"showDuration": "1000",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
var opt = defaults;
if (typeof options == "object") {
opt = $.extend(defaults, options);
}
toastr.options = opt;
toastrType = typeof toastrType !== 'undefined' ? toastrType : 'success';
toastr[toastrType](toastrMessage);
};
$.ajaxModal = function(selector, url, onLoad) {
$(selector).removeData('bs.modal').modal({
remote: url,
show: true
});
// Trigger to do stuff with form loaded in modal
$(document).trigger("ajaxPageLoad");
// Call onload method if it was passed in function call
if (typeof onLoad != "undefined") {
onLoad();
}
// Reset modal when it hides
$(selector).on('hidden.bs.modal', function () {
$(this).find('.modal-body').html('Loading...');
$(this).find('.modal-footer').html('<button type="button" data-dismiss="modal" class="btn dark btn-outline">Cancel</button>');
$(this).data('bs.modal', null);
});
};
$.showErrors = function(object) {
var keys = Object.keys(object);
$(".has-error").find(".help-block").remove();
$(".has-error").removeClass("has-error");
for (var i = 0; i < keys.length; i++) {
var ele = $("[name='" + keys[i] + "']");
if (ele.length == 0) {
ele = $("#" + keys[i]);
}
var grp = ele.closest(".form-group");
$(grp).find(".help-block").remove();
var helpBlockContainer = $(grp).find("div:first");
if (helpBlockContainer.length == 0) {
helpBlockContainer = $(grp);
}
helpBlockContainer.append('<div class="help-block">' + object[keys[i]] + '</div>');
$(grp).addClass("has-error");
}
}
})(jQuery);
// Prevent submit of ajax form
$(document).on("ready", function() {
$(".ajax-form").on("submit", function(e){
e.preventDefault();
})
});
$(document).on("ajaxPageLoad", function() {
$(".ajax-form").on("submit", function(e){
e.preventDefault();
})
});
/**
* bootbox.js v4.4.0
*
* http://bootboxjs.com/license.txt
*/
!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof exports?module.exports=b(require("jquery")):a.bootbox=b(a.jQuery)}(this,function a(b,c){"use strict";function d(a){var b=q[o.locale];return b?b[a]:q.en[a]}function e(a,c,d){a.stopPropagation(),a.preventDefault();var e=b.isFunction(d)&&d.call(c,a)===!1;e||c.modal("hide")}function f(a){var b,c=0;for(b in a)c++;return c}function g(a,c){var d=0;b.each(a,function(a,b){c(a,b,d++)})}function h(a){var c,d;if("object"!=typeof a)throw new Error("Please supply an object of options");if(!a.message)throw new Error("Please specify a message");return a=b.extend({},o,a),a.buttons||(a.buttons={}),c=a.buttons,d=f(c),g(c,function(a,e,f){if(b.isFunction(e)&&(e=c[a]={callback:e}),"object"!==b.type(e))throw new Error("button with key "+a+" must be an object");e.label||(e.label=a),e.className||(e.className=2>=d&&f===d-1?"btn-primary":"btn-default")}),a}function i(a,b){var c=a.length,d={};if(1>c||c>2)throw new Error("Invalid argument length");return 2===c||"string"==typeof a[0]?(d[b[0]]=a[0],d[b[1]]=a[1]):d=a[0],d}function j(a,c,d){return b.extend(!0,{},a,i(c,d))}function k(a,b,c,d){var e={className:"bootbox-"+a,buttons:l.apply(null,b)};return m(j(e,d,c),b)}function l(){for(var a={},b=0,c=arguments.length;c>b;b++){var e=arguments[b],f=e.toLowerCase(),g=e.toUpperCase();a[f]={label:d(g)}}return a}function m(a,b){var d={};return g(b,function(a,b){d[b]=!0}),g(a.buttons,function(a){if(d[a]===c)throw new Error("button key "+a+" is not allowed (options are "+b.join("\n")+")")}),a}var n={dialog:"<div class='bootbox modal' tabindex='-1' role='dialog'><div class='modal-dialog'><div class='modal-content'><div class='modal-body'><div class='bootbox-body'></div></div></div></div></div>",header:"<div class='modal-header'><h4 class='modal-title'></h4></div>",footer:"<div class='modal-footer'></div>",closeButton:"<button type='button' class='bootbox-close-button close' data-dismiss='modal' aria-hidden='true'>&times;</button>",form:"<form class='bootbox-form'></form>",inputs:{text:"<input class='bootbox-input bootbox-input-text form-control' autocomplete=off type=text />",textarea:"<textarea class='bootbox-input bootbox-input-textarea form-control'></textarea>",email:"<input class='bootbox-input bootbox-input-email form-control' autocomplete='off' type='email' />",select:"<select class='bootbox-input bootbox-input-select form-control'></select>",checkbox:"<div class='checkbox'><label><input class='bootbox-input bootbox-input-checkbox' type='checkbox' /></label></div>",date:"<input class='bootbox-input bootbox-input-date form-control' autocomplete=off type='date' />",time:"<input class='bootbox-input bootbox-input-time form-control' autocomplete=off type='time' />",number:"<input class='bootbox-input bootbox-input-number form-control' autocomplete=off type='number' />",password:"<input class='bootbox-input bootbox-input-password form-control' autocomplete='off' type='password' />"}},o={locale:"en",backdrop:"static",animate:!0,className:null,closeButton:!0,show:!0,container:"body"},p={};p.alert=function(){var a;if(a=k("alert",["ok"],["message","callback"],arguments),a.callback&&!b.isFunction(a.callback))throw new Error("alert requires callback property to be a function when provided");return a.buttons.ok.callback=a.onEscape=function(){return b.isFunction(a.callback)?a.callback.call(this):!0},p.dialog(a)},p.confirm=function(){var a;if(a=k("confirm",["cancel","confirm"],["message","callback"],arguments),a.buttons.cancel.callback=a.onEscape=function(){return a.callback.call(this,!1)},a.buttons.confirm.callback=function(){return a.callback.call(this,!0)},!b.isFunction(a.callback))throw new Error("confirm requires a callback");return p.dialog(a)},p.prompt=function(){var a,d,e,f,h,i,k;if(f=b(n.form),d={className:"bootbox-prompt",buttons:l("cancel","confirm"),value:"",inputType:"text"},a=m(j(d,arguments,["title","callback"]),["cancel","confirm"]),i=a.show===c?!0:a.show,a.message=f,a.buttons.cancel.callback=a.onEscape=function(){return a.callback.call(this,null)},a.bu
// Toastr
!function(a){a(["jquery"],function(a){return function(){function b(a,b,c){return o({type:u.error,iconClass:p().iconClasses.error,message:a,optionsOverride:c,title:b})}function c(b,c){return b||(b=p()),r=a("#"+b.containerId),r.length?r:(c&&(r=l(b)),r)}function d(a,b,c){return o({type:u.info,iconClass:p().iconClasses.info,message:a,optionsOverride:c,title:b})}function e(a){s=a}function f(a,b,c){return o({type:u.success,iconClass:p().iconClasses.success,message:a,optionsOverride:c,title:b})}function g(a,b,c){return o({type:u.warning,iconClass:p().iconClasses.warning,message:a,optionsOverride:c,title:b})}function h(a){var b=p();r||c(b),k(a,b)||j(b)}function i(b){var d=p();return r||c(d),b&&0===a(":focus",b).length?void q(b):void(r.children().length&&r.remove())}function j(b){for(var c=r.children(),d=c.length-1;d>=0;d--)k(a(c[d]),b)}function k(b,c){return b&&0===a(":focus",b).length?(b[c.hideMethod]({duration:c.hideDuration,easing:c.hideEasing,complete:function(){q(b)}}),!0):!1}function l(b){return r=a("<div/>").attr("id",b.containerId).addClass(b.positionClass).attr("aria-live","polite").attr("role","alert"),r.appendTo(a(b.target)),r}function m(){return{tapToDismiss:!0,toastClass:"toast",containerId:"toast-container",debug:!1,showMethod:"fadeIn",showDuration:300,showEasing:"swing",onShown:void 0,hideMethod:"fadeOut",hideDuration:1e3,hideEasing:"swing",onHidden:void 0,extendedTimeOut:1e3,iconClasses:{error:"toast-error",info:"toast-info",success:"toast-success",warning:"toast-warning"},iconClass:"toast-info",positionClass:"toast-top-right",timeOut:5e3,titleClass:"toast-title",messageClass:"toast-message",target:"body",closeHtml:"<button>&times;</button>",newestOnTop:!0}}function n(a){s&&s(a)}function o(b){function d(b){return!a(":focus",j).length||b?j[g.hideMethod]({duration:g.hideDuration,easing:g.hideEasing,complete:function(){q(j),g.onHidden&&"hidden"!==o.state&&g.onHidden(),o.state="hidden",o.endTime=new Date,n(o)}}):void 0}function e(){(g.timeOut>0||g.extendedTimeOut>0)&&(i=setTimeout(d,g.extendedTimeOut))}function f(){clearTimeout(i),j.stop(!0,!0)[g.showMethod]({duration:g.showDuration,easing:g.showEasing})}var g=p(),h=b.iconClass||g.iconClass;"undefined"!=typeof b.optionsOverride&&(g=a.extend(g,b.optionsOverride),h=b.optionsOverride.iconClass||h),t++,r=c(g,!0);var i=null,j=a("<div/>"),k=a("<div/>"),l=a("<div/>"),m=a(g.closeHtml),o={toastId:t,state:"visible",startTime:new Date,options:g,map:b};return b.iconClass&&j.addClass(g.toastClass).addClass(h),b.title&&(k.append(b.title).addClass(g.titleClass),j.append(k)),b.message&&(l.append(b.message).addClass(g.messageClass),j.append(l)),g.closeButton&&(m.addClass("toast-close-button").attr("role","button"),j.prepend(m)),j.hide(),g.newestOnTop?r.prepend(j):r.append(j),j[g.showMethod]({duration:g.showDuration,easing:g.showEasing,complete:g.onShown}),g.timeOut>0&&(i=setTimeout(d,g.timeOut)),j.hover(f,e),!g.onclick&&g.tapToDismiss&&j.click(d),g.closeButton&&m&&m.click(function(a){a.stopPropagation?a.stopPropagation():void 0!==a.cancelBubble&&a.cancelBubble!==!0&&(a.cancelBubble=!0),d(!0)}),g.onclick&&j.click(function(){g.onclick(),d()}),n(o),g.debug&&console&&console.log(o),j}function p(){return a.extend({},m(),v.options)}function q(a){r||(r=c()),a.is(":visible")||(a.remove(),a=null,0===r.children().length&&r.remove())}var r,s,t=0,u={error:"error",info:"info",success:"success",warning:"warning"},v={clear:h,remove:i,error:b,getContainer:c,info:d,options:{},subscribe:e,success:f,version:"2.0.3",warning:g};return v}()})}("function"==typeof define&&define.amd?define:function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("jquery")):window.toastr=b(window.jQuery)});
/*!
* jQuery blockUI plugin
* Version 2.70.0-2014.11.23
* Requires jQuery v1.7 or later
*
* Examples at: http://malsup.com/jquery/block/
* Copyright (c) 2007-2013 M. Alsup
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Thanks to Amir-Hossein Sobhi for some excellent contributions!
*/
!function(){"use strict";function e(e){function t(t,n){var s,h,k=t==window,y=n&&void 0!==n.message?n.message:void 0;if(n=e.extend({},e.blockUI.defaults,n||{}),!n.ignoreIfBlocked||!e(t).data("blockUI.isBlocked")){if(n.overlayCSS=e.extend({},e.blockUI.defaults.overlayCSS,n.overlayCSS||{}),s=e.extend({},e.blockUI.defaults.css,n.css||{}),n.onOverlayClick&&(n.overlayCSS.cursor="pointer"),h=e.extend({},e.blockUI.defaults.themedCSS,n.themedCSS||{}),y=void 0===y?n.message:y,k&&p&&o(window,{fadeOut:0}),y&&"string"!=typeof y&&(y.parentNode||y.jquery)){var m=y.jquery?y[0]:y,v={};e(t).data("blockUI.history",v),v.el=m,v.parent=m.parentNode,v.display=m.style.display,v.position=m.style.position,v.parent&&v.parent.removeChild(m)}e(t).data("blockUI.onUnblock",n.onUnblock);var g,I,w,U,x=n.baseZ;g=e(r||n.forceIframe?'<iframe class="blockUI" style="z-index:'+x++ +';display:none;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="'+n.iframeSrc+'"></iframe>':'<div class="blockUI" style="display:none"></div>'),I=e(n.theme?'<div class="blockUI blockOverlay ui-widget-overlay" style="z-index:'+x++ +';display:none"></div>':'<div class="blockUI blockOverlay" style="z-index:'+x++ +';display:none;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>'),n.theme&&k?(U='<div class="blockUI '+n.blockMsgClass+' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+(x+10)+';display:none;position:fixed">',n.title&&(U+='<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(n.title||"&nbsp;")+"</div>"),U+='<div class="ui-widget-content ui-dialog-content"></div>',U+="</div>"):n.theme?(U='<div class="blockUI '+n.blockMsgClass+' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+(x+10)+';display:none;position:absolute">',n.title&&(U+='<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(n.title||"&nbsp;")+"</div>"),U+='<div class="ui-widget-content ui-dialog-content"></div>',U+="</div>"):U=k?'<div class="blockUI '+n.blockMsgClass+' blockPage" style="z-index:'+(x+10)+';display:none;position:fixed"></div>':'<div class="blockUI '+n.blockMsgClass+' blockElement" style="z-index:'+(x+10)+';display:none;position:absolute"></div>',w=e(U),y&&(n.theme?(w.css(h),w.addClass("ui-widget-content")):w.css(s)),n.theme||I.css(n.overlayCSS),I.css("position",k?"fixed":"absolute"),(r||n.forceIframe)&&g.css("opacity",0);var C=[g,I,w],S=e(k?"body":t);e.each(C,function(){this.appendTo(S)}),n.theme&&n.draggable&&e.fn.draggable&&w.draggable({handle:".ui-dialog-titlebar",cancel:"li"});var O=f&&(!e.support.boxModel||e("object,embed",k?null:t).length>0);if(u||O){if(k&&n.allowBodyStretch&&e.support.boxModel&&e("html,body").css("height","100%"),(u||!e.support.boxModel)&&!k)var E=d(t,"borderTopWidth"),T=d(t,"borderLeftWidth"),M=E?"(0 - "+E+")":0,B=T?"(0 - "+T+")":0;e.each(C,function(e,t){var o=t[0].style;if(o.position="absolute",2>e)k?o.setExpression("height","Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.support.boxModel?0:"+n.quirksmodeOffsetHack+') + "px"'):o.setExpression("height",'this.parentNode.offsetHeight + "px"'),k?o.setExpression("width",'jQuery.support.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"'):o.setExpression("width",'this.parentNode.offsetWidth + "px"'),B&&o.setExpression("left",B),M&&o.setExpression("top",M);else if(n.centerY)k&&o.setExpression("top",'(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"'),o.marginTop=0;else if(!n.centerY&&k){var i=n.css&&n.css.top?parseInt(n.css.top,10):0,s="((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "+i+') + "px"';o.setExpression("top",s)}})}if(y&&(n.theme?w.find(".ui-widget-content").append(y):w.append(y),(y.jquery||y.nodeType)&&e(y).show()),(r||n.forceIframe)&&n.showOverlay&&g.show(),n.fadeIn){var j=n.onBlock?n.onBlock:c,H=n.