-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.min.js
1 lines (1 loc) · 9.14 KB
/
helper.min.js
1
const versioningCdnUrl="V2.0.4",confirmSweetFinishDefault={text:"Click the ? button to return to the previous page",cancelButtonFirst:"Continue",cancelButtonSecond:"Edit",buttonLabel:"Done",buttonConfirmColor:"#6c5ce7"},confirmSweetDeleteDefault={title:"Are you sure?",body:"The data will be deleted and cannot be recovered!",buttonLabel:"Delete",buttonConfirmColor:"#6c5ce7"};function visiblePassword(e,t){$(document).on("click",e,function(){var e=$(t);"password"===e.attr("type")?e.attr("type","text"):e.attr("type","password")})}$.ajaxSetup({headers:{"X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr("content")}}),$(".modal").on("hidden.bs.modal",function(){$(this).find("form")[0].reset(),$("#id").val("")}),$(".required").append(' <span style="color:red">*</span>');const assets=location.protocol+"//"+location.host+"/";var loadingSpiner='<img src="https://cdn.jsdelivr.net/gh/yaza-putu/helpers@'+versioningCdnUrl+'/img/loadingCircle.gif" style="width: 22px;">',paperPlaneImg='<img src="https://cdn.jsdelivr.net/gh/yaza-putu/helpers@'+versioningCdnUrl+'/img/paper_plane.png" />';function sweetError(e){Swal.fire({title:"Error",text:e,type:"error"})}function sweetInfo(e){Swal.fire({title:"Info",text:e,type:"info"})}function sweetSuccess(e,t=null,o=null,n=!1){if(null!==t){let r=null==o?confirmSweetFinishDefault.text:o.text,a=null==o?confirmSweetFinishDefault.cancelButtonFirst:o.cancelButtonFirst,s=null==o?confirmSweetFinishDefault.cancelButtonSecond:o.cancelButtonSecond,i=null==o?confirmSweetFinishDefault.buttonLabel:o.buttonLabel,l=null==o?confirmSweetFinishDefault.buttonConfirmColor:o.buttonConfirmColor;Swal.fire({title:e,type:"success",text:r.replace("?",i),showCancelButton:!0,cancelButtonText:n?s:a,confirmButtonColor:l,confirmButtonText:i}).then(e=>{e.value?window.location.href=t:!0==n&&location.reload()})}else!0==n&&location.reload(),Swal.fire({title:"Success!",text:e,type:"success"})}function toastSuccess(e,t="top-right"){$.toast({heading:"Success",text:e,position:t,stack:!1,icon:"success",bgColor:"#10ac84",textColor:"white",loaderBg:"#1dd1a1"})}function toastError(e,t="top-right"){$.toast({heading:"Error",text:e,position:t,stack:!1,icon:"error",bgColor:"#ee5253",textColor:"white",loaderBg:"#ff6b6b"})}function toastWarning(e,t="top-right"){$.toast({heading:"Warning",text:e,position:t,stack:!1,icon:"warning",bgColor:"#cc8e35",textColor:"white",loaderBg:"#ffb142"})}function snackbar(e,t="bottom-center"){Snackbar.show({text:e,actionTextColor:"#FFFFFF",pos:t,actionText:"Close"})}function snackbarSuccess(e,t="bottom-center"){Snackbar.show({text:e,actionTextColor:"#155724",pos:t,actionText:"Close",backgroundColor:"#d4edda",textColor:"#155724"})}function snackbarWarning(e,t="bottom-center"){Snackbar.show({text:e,actionTextColor:"#856404",pos:t,actionText:"Close",backgroundColor:"#fff3cd",textColor:"#856404"})}function snackbarError(e,t="bottom-center"){Snackbar.show({text:e,actionTextColor:"#721c24",pos:t,actionText:"Close",backgroundColor:"#f8d7da",textColor:"#721c24"})}function notifySuccess(e,t="right",o="top"){notify({type:"success",title:"Success",position:{x:t,y:o},icon:paperPlaneImg,message:e,autoHide:!0,delay:2500})}function notifyWarning(e,t="right",o="top"){notify({type:"warning",title:"Warning",theme:"dark-theme",position:{x:t,y:o},icon:paperPlaneImg,message:e,autoHide:!0,delay:2500})}function notifyError(e,t="right",o="top"){notify({type:"error",title:"Error",theme:"dark-theme",position:{x:t,y:o},icon:paperPlaneImg,message:e,autoHide:!0,delay:2500})}function handleValidation(e){for(let t in $(".invalid-feedback").remove(),e){for(let o in e[t])$("[name="+t+"]").addClass("is-invalid").after('<div class="text-left invalid-feedback">'+e[t][o]+"</div>");$("[name="+t+"]").keypress(function(){$("[name="+t+"]").removeClass("is-invalid")}),$("[name="+t+"]").change(function(){$("[name="+t+"]").removeClass("is-invalid")})}}function reloadTable(e){var t=$(e).DataTable();t.cleanData,t.ajax.reload()}function ajaxPost(e,t,o=null,n="sweetError"){if(null!==o)var r=$(o).html();return $.ajax({type:"post",url:e,data:t,dataType:"json",contentType:!1,processData:!1,beforeSend:function(){null!==o&&$(o).empty().append(loadingSpiner).prop("disabled",!0).css("cursor","wait")},complete:function(){}}).done(function(e){}).fail(function(e){let t=e.responseJSON;t.errors||t.invalid?new handleValidation(t.errors||t.invalid):void 0!==t.message?("sweetError"==n&&sweetError(t.message),"toastError"==n&&toastError(t.message),"snackbarError"==n&&snackbarError(t.message),"snackbar"==n&&snackbar(t.message)):("sweetError"==n&&sweetError("There is an error"),"toastError"==n&&toastError("There is an error"),"snackbarError"==n&&snackbarError("There is an error"),"snackbar"==n&&snackbar("There is an error"))}).always(function(){null!==o&&$(o).empty().append(r).prop("disabled",!1).css("cursor","auto")})}function ajaxPostFile(e,t,o=null,n="#loading-body"){if($(n).empty().html('<div class="progress" style="display:none;">\n<div class="progress-bar bg-danger progress-bar-striped\nactive" role="progressbar"\naria-valuemin="0" aria-valuemax="100" style="width:0%">\n0%\n</div>\n</div>'),null!==o)var r=$(o).html();return $.ajax({type:"post",url:e,data:t,dataType:"json",contentType:!1,processData:!1,beforeSend:function(){null!==o&&$(o).empty().append(loadingSpiner).prop("disabled",!0).css("cursor","wait"),$(".progress").fadeIn()},complete:function(){},xhr:function(){var e=new window.XMLHttpRequest;return e.upload.addEventListener("progress",function(e){if(e.lengthComputable){var t=e.loaded/e.total;t=parseInt(100*t),$(".progress-bar").width(t+"%"),$(".progress-bar").html(t+"%")}},!1),e}}).done(function(e){}).fail(function(e){let t=e.responseJSON;t.errors||t.invalid?new handleValidation(t.errors||t.invalid):new sweetError(void 0!==t.message?t.message:"There is an error")}).always(function(){null!==o&&$(o).empty().append(r).prop("disabled",!1).css("cursor","auto"),$(".progress").fadeOut()})}function ajaxGet(e,t=!1){return $.ajax({type:"get",url:e,dataType:"json",contentType:!1,processData:!1,beforeSend:function(){!0==t&&new showBlockUI}}).done(function(e){}).fail(function(e){new sweetError("There is an error")}).always(function(){!0==t&&new hideBlockUI})}function ajaxDel(e,t,o=!1,n="sweetSuccess",r=null,a=null){let s=null==a?confirmSweetDeleteDefault.title:a.title,i=null==a?confirmSweetDeleteDefault.body:a.body,l=null==a?confirmSweetDeleteDefault.buttonLabel:a.buttonLabel,c=null==a?confirmSweetDeleteDefault.buttonConfirmColor:a.buttonConfirmColor;Swal.fire({title:s,text:i,type:"warning",showCancelButton:!0,confirmButtonColor:c,confirmButtonText:l}).then(a=>{a.value&&$.ajax({type:"POST",url:e,data:{id:t},dataType:"json",success:function(e){!0==e.success?("sweetSuccess"==n&&sweetSuccess(e.message),"notifySuccess"==n&¬ifySuccess(e.message),"toastSuccess"==n&&toastSuccess(e.message),"snackbarSuccess"==n&&snackbarSuccess(e.message),null!==r&&reloadTable(r),!0==o&&setTimeout(function(){location.reload()},1500)):new sweetError(e.message)},error:function(e){new sweetError("There is an error")}})})}function ajaxDelete(e,t,o=!1,n="sweetSuccess",r=null,a=null){let s=null==a?confirmSweetDeleteDefault.title:a.title,i=null==a?confirmSweetDeleteDefault.body:a.body,l=null==a?confirmSweetDeleteDefault.buttonLabel:a.buttonLabel,c=null==a?confirmSweetDeleteDefault.buttonConfirmColor:a.buttonConfirmColor;Swal.fire({title:s,text:i,type:"warning",showCancelButton:!0,confirmButtonColor:c,confirmButtonText:l}).then(a=>{a.value&&$.ajax({type:"POST",url:e,data:{id:t},dataType:"json",success:function(e){!0==e.success?("sweetSuccess"==n&&sweetSuccess(e.message),"notifySuccess"==n&¬ifySuccess(e.message),"toastSuccess"==n&&toastSuccess(e.message),"snackbarSuccess"==n&&snackbarSuccess(e.message),null!==r&&reloadTable(r),!0==o&&setTimeout(function(){location.reload()},1500)):new sweetError(e.message)},error:function(e){new sweetError("There is an error")}})})}function formatRupiah(e,t){var o=e.toString().replace(/[^,\d]/g,"").split(","),n=o[0].length%3,r=o[0].substr(0,n),a=o[0].substr(n).match(/\d{3}/gi);return a&&(r+=(separator=n?".":"")+a.join(".")),r=void 0!=o[1]?r+","+o[1]:r,void 0==t?r:r?"Rp. "+r:""}function setTimeZone(){var e=Intl.DateTimeFormat().resolvedOptions().timeZone;document.cookie="timezone ="+e+";secure"}function setParam(e,t,o){var n="([&?]|&)"+(t=encodeURIComponent(t))+"\\b(?:=(?:[^&#]*))*",r=document.createElement("a"),a=RegExp(n),s=t+(o?"="+encodeURIComponent(o):"");r.href=e;var i=r.search.replace(a,"$1"+s);return i===r.search?r.search+=(r.search?"&":"")+s:r.search=i,r.href}function datatable(e,t,o=[],n=[],r,a=!0){r=r||function(){},$(e).DataTable({ordering:!0,serverSide:!0,processing:!0,autoWidth:!a,responsive:a,oLanguage:{sProcessing:loadingSpiner},ajax:{url:t,data:r},drawCallback:function(e){$('[data-toggle="tooltip"]').tooltip(),[].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')).map(function(e){return new bootstrap.Tooltip(e)})},columns:o,columnDefs:n})}function showBlockUI(e="Please wait a moment ..."){$.blockUI({message:e,css:{"z-index":10002,border:"none",padding:"15px",backgroundColor:"#000","-webkit-border-radius":"10px","-moz-border-radius":"10px",opacity:.5,color:"#fff"}})}function hideBlockUI(){$.unblockUI()}$(document).on("keyup",".convert-currency",function(){$(this).val(formatRupiah(this.value,"Rp. "))});