-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-ajax-form-v5.js.html
More file actions
executable file
·186 lines (159 loc) · 5.95 KB
/
Copy pathsimple-ajax-form-v5.js.html
File metadata and controls
executable file
·186 lines (159 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: simple-ajax-form-v5.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: simple-ajax-form-v5.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @file Simple AJAX Form
* @description jQuery extension to make forms AJAX enabled. Mainly used in WordPress projects
*
* @author Gerkin, tcbarrett
* @copyright 2016
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
* @package iThoughts-toolbox
*
*/
'use strict';
( ithoughts => {
var $ = ithoughts.$;
$.fn.extend({
/**
* Send a form through ajax
* @function simpleAjaxForm
* @memberof external:jQuery
* @param {object} opts Options
* @param {boolean} [opts.validate=false] Options
* @param {function} [opts.callback=false] Options
* @returns {undefined}
*/
simpleAjaxForm( opts ) {
var defaults = {
validate: false,
};
var options = $.extend( defaults, opts );
this.each( function bindEach() {
var $form = $( this ),
formopts = $.extend({
target: $form.data( 'target' ),
callback: $form.data( 'callback' ),
}, options );
if ( formopts.target && $( `#${ formopts.target }` ).length ) {
$( `#${ formopts.target }` ).html( '' ).hide();
}
$form.find( 'button[name="actionB"]' ).click( function onClick() {
$form.find( '[name="action"]' ).val( this.value );
});
var postText = ( this.getAttribute( 'post_text' ) ? this.getAttribute( 'post_text' ) : 'Updating, please wait...' ),
loader;
$form.ajaxForm({
beforeSubmit() {
//if( !jqForm.valid() ) return false;
if ( formopts.target && $( `#${ formopts.target }` ).length ) {
$( `#${ formopts.target }` ).html( `<p>${ postText }</p>` ).removeClass().addClass( 'clear updating' ).fadeTo( 100, 1 );
}
loader = ithoughts.makeLoader();
return true;
},
error() {
loader.remove();
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-error' ).html( '<p>Form submission failed.</p>' );
},
success( responseText, statusText, xhr, jQForm ) {
loader.remove();
if ( 'undefined' === typeof( jQForm )) {
jQForm = xhr;
}
try {
var res;
if ( 'String' === responseText.constructor.name ) {
res = JSON.parse( responseText );
} else if ( 'Object' === responseText.constructor.name ) {
res = responseText;
} else {
throw `Unhandled type ${ typeof responseText }`;
}
if ( '0' === res || !res ) {
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-warning' ).html( '<p>Server did not respond anything</p>' );
} else {
if ( typeof res.success != 'undefined' && res.success != null && typeof res.data != 'undefined' && res.data != null ) { // handle wp_send_json_{success|error}
res.data.valid = res.success;
res = res.data;
}
// If a nonce refresh token is present, try to update it
if ( res.nonce_refresh ) {
$form.find( '[name="_wpnonce"]' ).val( res.nonce_refresh );
}
// Handle raw response
if ( !res.valid ) {
if ( formopts.target && $( `#${ formopts.target }` ).length ) {
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-error' ).html( res.text );
}
} else {
if ( res.reload ) {
window.location.href = `${ window.location.href }&json-res-txt=${ window.encodeURI( res.text ) }`;
}
if ( formopts.target && $( `#${ formopts.target }` ).length ) {
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-success' ).html( res.text );
}
if ( res.redirect ) {
if ( res.text ) {
setTimeout(() => {
window.location.href = res.redirect;
}, 2500 );
} else {
window.location.href = res.redirect;
}
}
}
try {
if ( 'function' == typeof $form[0].simple_ajax_callback ) {
$form[0].simple_ajax_callback( res );
}
if ( 'function' == typeof formopts.callback ) {
formopts.callback( res );
}
} catch ( e ) {
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-error' ).html( '<p>Error with received data</p>' );
console.error( e );
}
}
} catch ( e ) {
$( `#${ formopts.target }` ).removeClass().addClass( 'clear notice notice-error' ).html( '<p>Invalid server response</p>' );
}
},
});
});
},
});
ithoughts.$d.ready(() => {
$( '.simpleajaxform' ).simpleAjaxForm();
});
})( iThoughts.v5 );
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="iThoughts.html">iThoughts</a></li><li><a href="iThoughts.v5.html">v5</a></li></ul><h3>Global</h3><ul><li><a href="global.html#_go">_go</a></li><li><a href="global.html#_off">_off</a></li><li><a href="global.html#_on">_on</a></li><li><a href="global.html#isNA">isNA</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Wed Jul 05 2017 15:21:22 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>