/* on start */ jQuery(function() { /* create document */ APjs.site = new APjs.site(); /* need to call init manually with jQuery */ APjs.site.initialize(); }); /* namespace */ window.APjs = {}; APjs.site = function() {}; APjs.site.prototype = { /* automatically called */ initialize: function() { if(jQuery('.for-non-logged-in #password').length > 0){ jQuery('.for-non-logged-in #password1').on('click chnage keyup', jQuery.proxy(this.checkPasswordMatch, this, '.for-non-logged-in #password', '.for-non-logged-in #password1')); jQuery('.for-non-logged-in #password').on('click chnage keyup', jQuery.proxy(this.checkPasswordLength, this, '#password')); } if(jQuery('.for-non-logged-in #email').length > 0) jQuery('.for-non-logged-in #email').on('click chnage keyup', jQuery.proxy(this.checkEmail, this, '.for-non-logged-in #email')); if(jQuery('.for-non-logged-in #username').length > 0) jQuery('.for-non-logged-in #username').on('click chnage keyup', jQuery.proxy(this.checkUsernameLength, this, '.for-non-logged-in #username')); if(jQuery('.for-non-logged-in #username').length > 0) jQuery('.for-non-logged-in #username').blur( jQuery.proxy(this.checkUsernameAvilable, this, '.for-non-logged-in #username')); if(jQuery('.for-non-logged-in #email').length > 0) jQuery('.for-non-logged-in #email').blur( jQuery.proxy(this.checkEmailAvilable, this, '.for-non-logged-in #email')); this.castVote(); this.favourite(); this.appendMessageBox(); this.close(); this.loadFlagModal(); this.flagModalSubmit(); this.dropdown(); jQuery('body').delegate('.ap-modal-bg, .ap-modal-close', 'click', function () { jQuery('.ap-modal').toggleClass('active'); }); }, appendMessageBox: function(){ if(jQuery('#ap-messagebox').length == '0') jQuery('body').append('
'); }, showLoading: function(message){ /* set the default message if no message passed */ if(typeof message == 'undefined')message = aplang.loading; if(jQuery('#ap-ajax-loading').length == '0') jQuery('#ap-messagebox').append(''); jQuery('#ap-ajax-loading').slideDown(200); }, /* chnage the message of loading */ addMessage: function(message, type){ jQuery('').appendTo('#ap-messagebox').delay(5000).slideUp(200); }, hideLoading:function(){ jQuery('#ap-ajax-loading').delay(500).slideUp(200); }, /* add bootstrap validation class */ fieldValidationClass: function(field, vclass){ jQuery(field).addClass(vclass); }, /* add help block below field */ helpBlock:function(elm, message){ /* remove existing help block */ if(jQuery(elm).find('.help-block').length > 0) jQuery(elm).find('.help-block').remove(); jQuery(elm).append(''+message+''); }, toggleFormSubmission:function(form, toggle){ //jQuery(form).submit(toggle); if(toggle && jQuery(form).find('.has-error').length == 0) jQuery(form).find('button[type="submit"]').removeAttr('disabled'); else jQuery(form).find('button[type="submit"]').attr('disabled', 'disabled'); }, isEmail : function (email) { var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; return regex.test(email); }, /* check for matching password between to fields */ checkPasswordMatch : function (field1, field2) { var password = jQuery(field1).val(); var confirmPassword = jQuery(field2).val(); var parent = jQuery(field2).closest('.form-group'); if (password != confirmPassword){ this.fieldValidationClass(parent, 'has-error'); if(parent.find('.help-block').length == 0) this.helpBlock(parent, aplang.password_field_not_macthing); this.toggleFormSubmission(jQuery(field2).closest('form'), false); } else{ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); if(parent.find('.help-block').length >0) parent.find('.help-block').remove(); this.toggleFormSubmission(jQuery(field2).closest('form'), true); } }, /* check for password length */ checkPasswordLength:function(field){ var password = jQuery(field).val(), parent = jQuery(field).closest('.form-group'); if(password.length < 6){ this.fieldValidationClass(parent, 'has-error'); this.helpBlock(parent, aplang.password_length_less); this.toggleFormSubmission(jQuery(field).closest('form'), false); }else{ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); parent.find('.help-block').remove(); this.toggleFormSubmission(jQuery(field).closest('form'), true); } }, /* check for valid email */ checkEmail: function(field){ var field = jQuery(field); var parent = jQuery(field).closest('.form-group'); if (!this.isEmail(field.val())){ this.fieldValidationClass(parent, 'has-error'); if(parent.find('.help-block').length == 0) this.helpBlock(parent, aplang.not_valid_email); this.toggleFormSubmission(jQuery(field).closest('form'), false); } else{ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); if(parent.find('.help-block').length >0) parent.find('.help-block').remove(); this.toggleFormSubmission(jQuery(field).closest('form'), true); } }, /* check length of username */ checkUsernameLength: function(field){ var field = jQuery(field); var parent = field .closest('.form-group'); if (field.val().length <= 4){ this.fieldValidationClass(parent, 'has-error'); if(parent.find('.help-block').length == 0) this.helpBlock(parent, aplang.username_less); this.toggleFormSubmission(field.closest('form'), false); } else{ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); if(parent.find('.help-block').length >0) parent.find('.help-block').remove(); this.toggleFormSubmission(field.closest('form'), true); } }, /* check if username is available */ checkUsernameAvilable: function(field){ var field = jQuery(field); var parent = field .closest('.form-group'); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_check_username', username: jQuery(field).val(), }, context:this, success: function(data){ if(data == 'true'){ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); if(parent.find('.help-block').length >0) parent.find('.help-block').remove(); this.toggleFormSubmission(field.closest('form'), true); }else{ this.fieldValidationClass(parent, 'has-error'); if(parent.find('.help-block').length == 0) this.helpBlock(parent, aplang.username_not_avilable); this.toggleFormSubmission(field.closest('form'), false); } } }); }, /* check if email is available */ checkEmailAvilable: function(field){ var field = jQuery(field); var parent = field .closest('.form-group'); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_check_email', email: jQuery(field).val(), }, context:this, success: function(data){ if(data == 'true'){ parent.removeClass('has-error'); this.fieldValidationClass(parent, 'has-feedback'); if(parent.find('.help-block').length >0) parent.find('.help-block').remove(); this.toggleFormSubmission(field.closest('form'), true); }else{ this.fieldValidationClass(parent, 'has-error'); if(parent.find('.help-block').length == 0) this.helpBlock(parent, aplang.email_already_in_use); this.toggleFormSubmission(field.closest('form'), false); } } }); }, castVote:function() { var self = this; jQuery('[data-action="vote"] a').click(function(e){ e.preventDefault(); var args = jQuery(this).data('args'); self.showLoading(aplang.voting_on_post); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_vote_on_post', args: args, }, dataType: 'json', context:this, success: function(result, textStatus, XMLHttpRequest){ self.hideLoading(); if(result['action'] == 'voted' || result['action'] == 'undo'){ if(result['action'] == 'voted'){ jQuery(this).addClass('voted'); if(result['type'] == 'up') jQuery(this).parent().find('.vote-down').addClass('disable'); if(result['type'] == 'down') jQuery(this).parent().find('.vote-up').addClass('disable'); } else if(result['action'] == 'undo'){ jQuery(this).removeClass('voted'); if(result['type'] == 'up') jQuery(this).parent().find('.vote-down').removeClass('disable'); if(result['type'] == 'down') jQuery(this).parent().find('.vote-up').removeClass('disable'); } jQuery(this).parent().find('.net-vote-count').text(result['count']); self.addMessage(result['message'], 'success'); }else{ self.addMessage(result['message'], 'error'); } } }); }); }, favourite:function (){ var self = this; jQuery('a.favourite-btn').click(function(e){ e.preventDefault(); /* show loading message */ self.showLoading(aplang.adding_to_fav); var args = jQuery(this).data('args'); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_add_to_favourite', args: args, }, dataType: 'json', context:this, success: function(data){ /* hide loading message */ self.hideLoading(); if(data['action'] == 'added' || data['action'] == 'removed'){ if(data['action'] == 'added') jQuery(this).addClass('added'); else if(data['action'] == 'removed') jQuery(this).removeClass('added'); jQuery(this).html(data['count']); jQuery(this).attr('title',data['title']); jQuery(this).parent().find('span').html(data['text']); self.addMessage(data['message'], 'success'); }else{ self.addMessage(data['message'], 'error'); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); }); }, close:function(){ var self = this; jQuery('[data-action="close-question"]').click(function(e){ e.preventDefault(); var args = jQuery(this).data('args'); self.showLoading(aplang.requesting_for_closing); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_vote_for_close', args: args, }, dataType: 'json', context:this, success: function(result){ /* hide loading message */ self.hideLoading(); if(result['row']){ if(result['action'] == 'added') jQuery(this).addClass('closed'); else if(result['action'] == 'removed') jQuery(this).removeClass('closed'); jQuery(this).text(result['text']); jQuery(this).attr('title', result['title']); self.addMessage(result['message'], 'success'); }else{ self.addMessage(result['message'], 'error'); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); }); }, modalShow:function(id){ jQuery('.ap-modal').addClass('active'); }, loadFlagModal:function (){ var self = this; jQuery('[data-action="flag-modal"]').click(function(e){ e.preventDefault(); if(!jQuery(this).is('.loaded')){ var args = jQuery(this).data('args'); self.showLoading(); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_flag_note_modal', args: args, }, context:this, success: function(data){ self.hideLoading(); jQuery(data).appendTo('body'); self.modalShow(jQuery(this).attr('href')); jQuery(this).addClass('loaded'); }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); }else{ self.modalShow(jQuery(this).attr('href')); } }); jQuery('body').delegate(':radio[name="note_id"]', 'click', function () { jQuery(':radio[name="note_id"]').closest('.note').removeClass('active'); jQuery(this).closest('.note').addClass('active'); }); }, flagModalSubmit: function(){ var self = this; jQuery('body').delegate('#submit-flag-question', 'click', function(e){ e.preventDefault(); var args = jQuery(this).data('args'); var note_id = jQuery(this).closest('.flag-note').find(':radio[name="note_id"]:checked').val(); var other_note = jQuery(this).closest('.flag-note').find('#other-note').val(); self.showLoading(aplang.sending_request); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_submit_flag_note', args: args, note_id: note_id, other_note: other_note, }, dataType:'json', context:this, success: function(result){ self.hideLoading(); var id = jQuery(this).closest('.flag-note').attr('id'); var to_update = jQuery('#flag_' + jQuery(this).data('update')); jQuery('#'+id).removeClass('active'); //jQuery('#'+id).remove(); if(result['row'] == '1'){ if(result['action'] == 'flagged') to_update.addClass('flagged'); to_update.text(result['text']); to_update.attr('title',result['title']); self.addMessage(result['message'], 'success'); }else{ self.addMessage(result['message'], 'error'); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); }); }, dropdown:function(){ jQuery('.ap-dropdown-toggle').on('click', function(e){ e.preventDefault(); jQuery(this).closest('ul').toggleClass('open'); }); } }; function ap_load_comment_form(){ jQuery('[data-form="comment"]').click(function(e){ e.preventDefault(); var args = jQuery(this).data('args'), elem = jQuery(this).attr('href'); if(jQuery(this).is('.ajax-done')){ jQuery(elem+' #respond').slideToggle(); }else{ jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_load_comment_form', args: args, }, context:this, success: function(data, textStatus, XMLHttpRequest){ if(jQuery(data).selector == 'not_logged_in'){ ap_not_logged_in(); }else{ jQuery(data).appendTo(elem); jQuery(this).addClass('ajax-done'); jQuery('html, body').animate({ scrollTop: (jQuery('#respond').offset().top) - 50 }, 500); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); } }); } function ap_edit_comment_form(){ jQuery('body').delegate('[data-action="edit-comment"]', 'click', function(e){ e.preventDefault(); var args = jQuery(this).data('args'); jQuery.ajax({ url: ajaxurl, data: { action: 'ap_edit_comment_form', args: args, }, context:this, success: function(data, textStatus, XMLHttpRequest){ if(jQuery(data).selector == 'not_logged_in'){ ap_not_logged_in(); }else{ jQuery(this).closest('.comment-content').html(data); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); }); } function ap_save_comment(){ // this is the id of the form jQuery('body').delegate('[data-action="save-inline-comment"]', 'click', function(e) { e.preventDefault(); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_save_comment_form', args: jQuery(jQuery(this).data('elem')).serialize(), }, context:this, success: function(data){ jQuery(this).closest('.comment-content').html(data); } }); }); } function ap_delete_comment(){ jQuery('body').delegate('[data-action="delete-comment"]', 'click', function(e){ e.preventDefault(); if(confirm(jQuery(this).data('confirm'))){ var args = jQuery(this).data('args'); jQuery.ajax({ url: ajaxurl, data: { action: 'ap_delete_comment', args: args, }, context:this, success: function(data, textStatus, XMLHttpRequest){ if(jQuery(data).selector == 'not_logged_in'){ ap_not_logged_in(); }else{ jQuery(this).closest('li.comment').remove(); } }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); } }); } function ap_not_logged_in(){ if(jQuery('#please-login').length){ jQuery('#please-login').slideToggle(); }else{ jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_not_logged_in_messgae' }, context:this, success: function(data, textStatus, XMLHttpRequest){ jQuery(data).appendTo('body'); }, error: function(MLHttpRequest, textStatus, errorThrown){ alert(errorThrown); } }); } } function ap_change_status(){ jQuery('[data-action="change-status"]').click(function(e){ e.preventDefault(); jQuery(this).closest('#change-status').toggleClass('active'); var args = jQuery(this).data('args'); }); jQuery('[data-action="set-status"]').click(function(e){ e.preventDefault(); jQuery.ajax({ type: 'POST', url: ajaxurl, data: { action: 'ap_set_status', args: jQuery(this).data('args') }, context:this, success: function(data, textStatus, XMLHttpRequest){ //jQuery('#change-status').replaceWith(data); location.reload(); }, error: function(MLHttpRequest, textStatus, errorThrown){ //alert(errorThrown); } }); }); } function ap_toggle_sub_cat(){ jQuery('.sub-cat-count').click(function(e){ e.preventDefault(); jQuery(this).closest('.taxo-footer').find('.child').slideToggle(200); }); } jQuery(document).ready(function (){ ap_load_comment_form(); ap_edit_comment_form(); ap_save_comment(); ap_delete_comment(); ap_change_status(); ap_toggle_sub_cat(); jQuery('body').delegate('#please-login button', 'click' ,function(){ jQuery(this).parent().slideToggle(); }); });