/* global ajaxurl, wp2sv_url*/ window.wp2sv=window.wp2sv||{}; (function(wp2sv,$){ "use strict"; var openedModal; var opening=0; var zIndex=9999; wp2sv.init=function(){ $(document).on('click','[data-wp2sv-modal]',function(e){ e.preventDefault(); wp2sv.openModal($(this).data('wp2sv-modal')); }); $(document).on('click','.wp2sv-modal-close',function(e){ e.preventDefault(); wp2sv.closeModal(this); }) }; wp2sv.openModal=function(modal){ modal=$(modal).closest('.wp2sv-modal'); if(!modal.find('.wp2sv-modal-content').length){ modal.wrapInner('
'); //console.log(modal); } modal.trigger('open'); $(document).trigger('wp2sv-modal-open'); if(!modal.find('.wp2sv-modal-close-icon').length){ modal.append('
') } opening++; modal.wrap('
'); modal.closest('.wp2sv-modal-mask').css('z-index',zIndex+opening); openedModal=modal; }; wp2sv.closeModal=function(modal){ modal=modal||openedModal; modal=$(modal); modal=modal.closest('.wp2sv-modal'); modal.trigger('close'); $(document).trigger('wp2sv-modal-close'); modal.unwrap().unwrap(); modal.trigger('closed'); opening--; }; var confirm_index=1; wp2sv.confirm=function(title,message,callback,is_alert){ var $confirm=$('#wp2sv-confirm'); $confirm.after($confirm=$confirm.clone()); $confirm.attr('id','wp2sv-confirm-'+confirm_index++).addClass('wp2sv-confirm-modal'); if(typeof message==='function'){ callback=message; message=title; title=''; } if(typeof callback!=='function'){ callback=function(){ return true; } } title&&$('.wp2sv-h1',$confirm).html(title); message&&$('.wp2sv-p',$confirm).html(message); if(is_alert){ $('.wp2sv-cancel-btn',$confirm).addClass('hidden'); } $confirm.on('click', '.wp2sv-confirm-btn', function () { var result; if ($(this).is('[data-btn-ok]')) { result=callback(true); } else { result=callback(false); } if(result!==false) { wp2sv.closeModal($confirm); } }); $confirm.on('closed',function(){ $confirm.remove(); }); wp2sv.openModal($confirm); }; wp2sv.alert=function(message,title,callback){ if(typeof title==='function'){ callback=title; title=''; } wp2sv.confirm(title,message,callback,true); }; wp2sv.post=function(action,data){ return wp2sv.ajax(action,data,'POST'); }; wp2sv.get=function(action,data){ return wp2sv.ajax(action,data); }; wp2sv.ajax=function(action,data,method){ if(typeof action==='object'){ if(!method) { method = data; } data=action; }else { if (typeof data === 'object') { data.action = action; }else{ data={action:action}; } } method=method||'GET'; data=data||{}; if(data.action){ data.wp2sv_action=data.action; data.wp2sv_nonce=wp2sv._nonce; } data.action='wp2sv'; return $.ajax({ type: method, dataType: "json", url: ajaxurl, data:data }); }; wp2sv.print=function(e){ var data=$(e).html(); var mywindow = window.open('', 'backup codes', 'height=400,width=600'); mywindow.document.write(''); /*optional stylesheet*/ mywindow.document.write(''); mywindow.document.write('
'); mywindow.document.write(data); mywindow.document.write('
'); mywindow.document.close(); // necessary for IE >= 10 mywindow.focus(); // necessary for IE >= 10 mywindow.onload=function(){ //mywindow.print(); //mywindow.close(); }; }; wp2sv.toast={ show:function(text,style){ this.el().find('.wp2sv-toast-message').html(text); var classes=style?'wp2sv-toast wp2sv-toast-' + style:'wp2sv-toast'; this.el().find('>div').attr('class', classes); this.el().show(); }, info:function(message){ this.show(message,'info'); }, error:function(message){ this.show(message,'error'); }, success:function(message){ this.show(message,'success'); }, warning:function(message){ this.show(message,'warning'); }, hide:function(){ this.el().hide(); }, el:function(){ if(!this.$el){ this.$el=$('#wp2sv-toast'); } if(!this.$el.length){ console.log('no toast'); let template=''; this.$el=$(template).appendTo('.wp2sv'); } return this.$el; } }; wp2sv.init(); })(window.wp2sv,jQuery);