PluginProbe
MaxButtons – Create buttons / 7.1.2
MaxButtons – Create buttons v7.1.2
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / js / maxajax.js

maxajax.js in MaxButtons – Create buttons 7.1.2, at js/maxajax.js

153 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1
2 /** New AJAX Call methods
3 /* Get the standard AJAX vars for this plugin */
4
5 function maxAjax() {
6 this.spinnerFunction = this.showSpinner;
7 this.successHandler = this.defaultSuccesHandler;
8 this.errorHandler = this.defaultErrorHandler;
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 $.ajax({
130 type: "POST",
131 url: maxajax.ajax_url,
132 data: data,
133 success: successHandler,
134 error: errorHandler,
135 });
136 }
137
138 maxAjax.prototype.defaultSuccessHandler = function (result, status, object, action)
139 {
140 this.removeSpinner();
141 $(document).trigger('maxajax_success_' + action, [result, status, object]);
142
143 }
144
145 maxAjax.prototype.defaultErrorHandler = function(jq,status,error, action)
146 {
147 this.removeSpinner();
148 $(document).trigger('maxajax_error_' + action, jq, status, error);
149 console.log(jq);
150 console.log(status);
151 console.log(error);
152 }
153