/** * Javascript code for AnsPress fontend * @since 2.0 * @package AnsPress * @author Rahul Aryan * @license GPL 2+ */ function apAjaxData(e){e=e+"&action=ap_ajax";return e}function apQueryStringToJSON(e){var t=e.split("&");var n={};t.forEach(function(e){e=e.split("=");n[e[0]]=encodeURIComponent(e[1]||"")});return JSON.parse(JSON.stringify(n))}function apGetValueFromStr(e,t){t=t.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var n=new RegExp("[\\?&]"+t+"=([^]*)"),r=n.exec(e);return r==null?false:decodeURIComponent(r[1].replace(/\+/g," "))}(function(e){e(function(){AnsPress.site=new AnsPress.site;AnsPress.site.initialize()});window.AnsPress={};AnsPress.site=function(){};AnsPress.site.prototype={initialize:function(){ApSite=this;this.ajax_id=new Object;this.loading=new Object;this.errors;this.ajaxData;this.doAction();this.afterAjaxComplete();this.appendFormError();this.appendMessageBox();this.load_comment_form()},doAjax:function(t,n,r,i,s){r=typeof r!=="undefined"?r:false;n=typeof n!=="undefined"?n:false;i=typeof i!=="undefined"?i:false;s=typeof s!=="undefined"?s:false;var o=apGetValueFromStr(t,"ap_ajax_action");if(s&&typeof ApSite.ajax_id[o]!=="undefined"){ApSite.ajax_id[o].abort()}if(typeof ApSite.loading[o]!=="undefined")ApSite.hideLoading(ApSite.loading[o]);var u=function(t){ApSite.hideLoading(ApSite.loading[o]);if(n!==false)n(t)};var a=function(){ApSite.loading[o]=ApSite.showLoading(r);if(i!==false)i()};var f=e.ajax({type:"POST",url:ajaxurl,data:t,beforeSend:a,success:u,dataType:"json",context:r});ApSite.ajax_id[o]=f;return f},doAction:function(){e("[data-action]").each(function(t){var n=e(this).data("action");if(typeof ApSite[n]==="function")ApSite[n](this);else console.log('No "'+n+'" method found in AnsPress.site{}')})},afterAjaxComplete:function(){e(document).ajaxComplete(function(t,n,r){if(typeof n!=="undefined"&&typeof n.responseJSON!=="undefined"&&typeof n.responseJSON.ap_responce!=="undefined"){var i=typeof n.responseJSON.message_type==="undefined"?"success":n.responseJSON.message_type;ApSite.addMessage(n.responseJSON.message,i);e(document).trigger("ap_after_ajax",n.responseJSON);console.log(n.responseJSON.do!=="undefined"&&typeof ApSite[n.responseJSON.do]==="function");if(n.responseJSON.do!=="undefined"&&typeof ApSite[n.responseJSON.do]==="function")ApSite[n.responseJSON.do](n.responseJSON)}})},uniqueId:function(){return e(".ap-uid").length},showLoading:function(t){var n=this.uniqueId();var r=e(t).offset();var i=r.top;var s=r.left;if(e(t).is("a")||e(t).is("button")||e(t).is('input[type="submit"]')){var o=e('
');e(t).append(o)}else if(e(t).is("form")){var t=e(t).find('[type="submit"]');var r=e(t).offset();var i=r.top;var s=r.left;var o=e('');e("body").append(o);e(o).css({top:i,left:s,width:e(t).outerWidth(),height:e(t).outerHeight()})}else if(e(t).is('input[type="text"]')){var i=i+e(t).outerHeight()/2;var s=s+e(t).outerWidth()-30;var o=e('');e("body").append(o);e(o).css({top:i,left:s})}else{var i=i+6;var s=s-15;var o=e('');e("body").append(o);e(o).css({top:i,left:s})}return"#apuid-"+n},hideLoading:function(t){e(t).remove()},suggest_similar_questions:function(t){var n=this.uniqueId();e(t).on("keyup",function(){ApSite.doAjax(apAjaxData("ap_ajax_action=suggest_similar_questions&value="+e(t).val()),function(t){if(typeof t["html"]!=="undefined")e("#similar_suggestions").html(t["html"])},t,false,true)})},ap_ajax_form:function(t){e(t).on("submit",function(){if(typeof tinyMCE!=="undefined")tinyMCE.triggerSave();ApSite.doAjax(apAjaxData(e(t).formSerialize()),function(e){console.log(e)},t);return false})},appendFormError:function(){e(document).on("ap_after_ajax",function(t,n){if(typeof n.errors!=="undefined"){ApSite.clearFormErrors(n.form);e.each(n.errors,function(t,r){var i=e("#"+n.form).find("#"+t).closest(".ap-form-fields");i.addClass("ap-have-error");ApSite.helpBlock(i,r)})}})},helpBlock:function(t,n){if(e(t).find(".ap-form-error-message").length>0)e(t).find(".ap-form-error-message").remove();e(t).append('")},clearFormErrors:function(t){var n=e("#"+t).find(".ap-have-error");n.find(".ap-form-error-message").remove();n.removeClass("ap-have-error")},appendMessageBox:function(){if(e("#ap-notify").length=="0")e("body").append('')},addMessage:function(t,n){var r=aplang[n];e('