| 1 |
|
| 2 |
/** New AJAX Call methods |
| 3 |
/* Get the standard AJAX vars for this plugin */ |
| 4 |
function maxAjax() { |
| 5 |
this.spinnerFunction = this.showSpinner; |
| 6 |
this.successHandler = this.defaultSuccesHandler; |
| 7 |
this.errorHandler = this.defaultErrorHandler; |
| 8 |
|
| 9 |
} |
| 10 |
|
| 11 |
maxAjax.prototype.init = function() |
| 12 |
{ |
| 13 |
// default actions that trigger ajax action. |
| 14 |
$(document).on('submit', '.mb-ajax-form', function (e) { e.preventDefault(); return false; }); // don't submit ajax forms ( prevent enter submit ) |
| 15 |
$(document).on('click', '.mb-ajax-form .mb-ajax-submit', $.proxy(this.ajaxForm, this )); |
| 16 |
$(document).on('click', '.mb-ajax-action', $.proxy(this.ajaxCall, this )); |
| 17 |
$(document).on('change', '.mb-ajax-action-change', $.proxy(this.ajaxCall, this)); |
| 18 |
$(document).trigger('maxajax_init'); // for hanging in other actions. |
| 19 |
} |
| 20 |
|
| 21 |
maxAjax.prototype.ajaxInit = function() |
| 22 |
{ |
| 23 |
data = { |
| 24 |
action: maxajax.ajax_action, |
| 25 |
nonce: maxajax.nonce, |
| 26 |
} |
| 27 |
|
| 28 |
return data; |
| 29 |
} |
| 30 |
|
| 31 |
maxAjax.prototype.setFunction = function(type, new_function) |
| 32 |
{ |
| 33 |
if (type == 'spinner') |
| 34 |
{ |
| 35 |
this.spinnerFunction = new_function; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
maxAjax.prototype.ajaxForm = function (e) |
| 40 |
{ |
| 41 |
e.preventDefault(); |
| 42 |
|
| 43 |
var target = $(e.target); |
| 44 |
var form = $(target).parents('form'); |
| 45 |
var action = $(target).data('action'); |
| 46 |
|
| 47 |
var data = this.ajaxInit(); |
| 48 |
data['form'] = form.serialize(); |
| 49 |
data['plugin_action'] = action; |
| 50 |
// data['action'] = 'mb_button_action'; |
| 51 |
|
| 52 |
$(document).trigger('maxajax_formpost_' + action, [data,target]); |
| 53 |
|
| 54 |
this.spinnerFunction(target); |
| 55 |
|
| 56 |
this.ajaxPost(data); |
| 57 |
} |
| 58 |
|
| 59 |
/* Ajax call functionality */ |
| 60 |
maxAjax.prototype.ajaxCall = function (e) |
| 61 |
{ |
| 62 |
e.preventDefault(); |
| 63 |
var target = $(e.target); |
| 64 |
|
| 65 |
if (! $(target).hasClass('.mb-ajax-action') && ! $(target).hasClass('.mb-ajax-action-change') ) |
| 66 |
{ |
| 67 |
if ($(target).parents('.mb-ajax-action'). length > 0) |
| 68 |
target = $(e.target).parents('.mb-ajax-action'); |
| 69 |
|
| 70 |
if ($(target).parents('.mb-ajax-action-change'). length > 0) |
| 71 |
target = $(e.target).parents('.mb-ajax-action'); |
| 72 |
} |
| 73 |
|
| 74 |
var param = false; |
| 75 |
var plugin_action = $(target).data('action'); |
| 76 |
var check_param = $(target).data('param'); |
| 77 |
var param_input = $(target).data('param-input'); |
| 78 |
|
| 79 |
if (typeof check_param !== 'undefined') |
| 80 |
param = check_param; |
| 81 |
if (typeof param_input !== 'undefined') |
| 82 |
param = $(param_input).val(); |
| 83 |
|
| 84 |
data = this.ajaxInit(); |
| 85 |
|
| 86 |
data['plugin_action'] = plugin_action; |
| 87 |
data['param'] = param; |
| 88 |
data['post'] = $('form').serialize(); // send it all |
| 89 |
|
| 90 |
var spinnerFunc = this.spinnerFunction; |
| 91 |
|
| 92 |
this.spinnerFunction(target); |
| 93 |
|
| 94 |
this.ajaxPost(data); |
| 95 |
} |
| 96 |
|
| 97 |
maxAjax.prototype.showSpinner = function(target) |
| 98 |
{ |
| 99 |
// spinner styling in elements |
| 100 |
var spinner = '<div class="maxajax-load-spinner"></div>'; |
| 101 |
//$('.maxajax-load-spinner').remove(); |
| 102 |
$(target).after(spinner); |
| 103 |
//return spinner; |
| 104 |
} |
| 105 |
|
| 106 |
maxAjax.prototype.removeSpinner = function() |
| 107 |
{ |
| 108 |
$('.maxajax-load-spinner').remove(); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
maxAjax.prototype.ajaxPost = function(data, successHandler, errorHandler) |
| 113 |
{ |
| 114 |
var self = this; |
| 115 |
|
| 116 |
if (typeof successHandler == 'undefined') |
| 117 |
{ |
| 118 |
var action = data['plugin_action']; |
| 119 |
var successHandler = function (r,s,o,) { self.defaultSuccessHandler(r,s,o,action) } ; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
if (typeof errorHandler == 'undefined') |
| 124 |
{ |
| 125 |
var action = data['plugin_action']; |
| 126 |
var errorHandler = function (r,s,o,) { self.defaultErrorHandler(r,s,o,action) } ; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
$.ajax({ |
| 131 |
type: "POST", |
| 132 |
url: maxajax.ajax_url, |
| 133 |
data: data, |
| 134 |
success: successHandler, |
| 135 |
error: errorHandler, |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
maxAjax.prototype.defaultSuccessHandler = function (result, status, object, action) |
| 140 |
{ |
| 141 |
this.removeSpinner(); |
| 142 |
$(document).trigger('maxajax_success_' + action, [result, status, object]); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
maxAjax.prototype.defaultErrorHandler = function(jq,status,error, action) |
| 147 |
{ |
| 148 |
this.removeSpinner(); |
| 149 |
$(document).trigger('maxajax_error_' + action, jq, status, error); |
| 150 |
console.error(jq); |
| 151 |
console.error(status); |
| 152 |
console.error(error); |
| 153 |
console.error(action); |
| 154 |
} |
| 155 |
|