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