/** * Javascript code for AnsPress fontend * @since 2.0 * @package AnsPress * @author Rahul Aryan * @license GPL 2+ */ (function($){ /* on start */ $(function() { /* create document */ AnsPress.site = new AnsPress.site(); /* need to call init manually with $ */ AnsPress.site.initialize(); }); /* namespace */ window.AnsPress = {}; AnsPress.site = function() {}; AnsPress.site.prototype = { /** Initalize the class */ initialize: function(){ ApSite = this; this.ajax_id = new Object(); this.loading = new Object(); this.errors; this.ajaxData; this.afterAjaxComplete(); this.appendFormError(); this.appendMessageBox(); this.ap_comment_form(); }, doAjax: function(query, success, context, before, abort){ /** Shorthand method for calling ajax */ context = typeof context !== 'undefined' ? context : false; success = typeof success !== 'undefined' ? success : false; before = typeof before !== 'undefined' ? before : false; abort = typeof abort !== 'undefined' ? abort : false; var action = apGetValueFromStr(query, 'ap_ajax_action'); if(abort && (typeof ApSite.ajax_id[action] !== 'undefined')){ ApSite.ajax_id[action].abort(); } ApSite.showLoading(); var req = $.ajax({ type: 'POST', url: ajaxurl, data: query, beforeSend: before, success: success, dataType: 'json', context:context, }); ApSite.ajax_id[action] = req; return req; }, doAction: function(action){ var self = this; var action = typeof action !== 'undefined' ? '[data-action="'+action+'"]' : '[data-action]'; var actions = new Object(); $(action).each(function(i){ var action = $(this).attr('data-action'); if(typeof actions[action] !== 'undefined') return; actions[action] = '1'; if (typeof self[action] === 'function') self[action]('[data-action="'+action+'"]'); /*else console.log('No "'+action+'" method found in AnsPress.site{}');*/ }); }, /** * Process to run after completing an ajax request * @return {void} * @since 2.0 */ afterAjaxComplete:function(){ $( document ).ajaxComplete(function( event, data, settings ) { ApSite.hideLoading(); if(typeof data !== 'undefined' && typeof data.responseJSON !== 'undefined' && typeof data.responseJSON.ap_responce !== 'undefined'){ if(typeof data.responseJSON.message !== 'undefined'){ var type = typeof data.responseJSON.message_type === 'undefined' ? 'success' : data.responseJSON.message_type; ApSite.addMessage(data.responseJSON.message, type); } $(document).trigger('ap_after_ajax', data.responseJSON); if (data.responseJSON.do !== 'undefined' && typeof ApSite[data.responseJSON.do] === 'function') ApSite[data.responseJSON.do](data.responseJSON); } }); }, uniqueId: function(){ return $('.ap-uid').length; }, showLoading: function(){ var uid = this.uniqueId(); var el = $('
'); $('body').append(el); return '#apuid-'+ uid; }, hideLoading: function(){ $('.ap-loading-icon').hide(); }, suggest_similar_questions: function(elm){ var uid = this.uniqueId(); $(elm).on('keyup', function(){ ApSite.doAjax( apAjaxData('ap_ajax_action=suggest_similar_questions&value=' + $(elm).val()), function(data){ if(typeof data['html'] !== 'undefined') $('#similar_suggestions').html(data['html']); }, elm, false, true ); }); }, ap_ajax_form:function(form){ $('body').delegate(form,'submit', function(){ if(typeof tinyMCE !== 'undefined') tinyMCE.triggerSave(); ApSite.doAjax( apAjaxData($(form).formSerialize()), function(data){ console.log(data); }, form ); return false; }) }, appendFormError: function(){ $(document).on('ap_after_ajax', function(e, data){ if(typeof data.errors !== 'undefined'){ ApSite.clearFormErrors(data.form); $.each(data.errors, function(i, message) { var parent = $('#' + data.form ).find('#'+i).closest('.ap-form-fields'); parent.addClass('ap-have-error'); ApSite.helpBlock(parent, message); }); } }); }, helpBlock:function(elm, message){ /* remove existing help block */ if($(elm).find('.ap-form-error-message').length > 0) $(elm).find('.ap-form-error-message').remove(); $(elm).append(''); }, clearFormErrors: function(form){ var elm = $('#' + form ).find('.ap-have-error'); elm.find('.ap-form-error-message').remove(); elm.removeClass('ap-have-error'); }, appendMessageBox: function(){ if($('#ap-notify').length == '0') $('body').append(''); }, addMessage: function(message, type){ var icon = aplang[type]; $('