author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
var wpAjax = jQuery.extend( { |
2 |
unserialize: function( s ) { |
|
3 |
var r = {}, q, pp, i, p; |
|
4 |
if ( !s ) { return r; } |
|
5 |
q = s.split('?'); if ( q[1] ) { s = q[1]; } |
|
6 |
pp = s.split('&'); |
|
7 |
for ( i in pp ) { |
|
8 |
if ( jQuery.isFunction(pp.hasOwnProperty) && !pp.hasOwnProperty(i) ) { continue; } |
|
9 |
p = pp[i].split('='); |
|
10 |
r[p[0]] = p[1]; |
|
11 |
} |
|
12 |
return r; |
|
13 |
}, |
|
14 |
parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission |
|
5 | 15 |
var parsed = {}, re = jQuery('#' + r).empty(), err = ''; |
0 | 16 |
|
17 |
if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) { |
|
18 |
parsed.responses = []; |
|
19 |
parsed.errors = false; |
|
20 |
jQuery('response', x).each( function() { |
|
21 |
var th = jQuery(this), child = jQuery(this.firstChild), response; |
|
22 |
response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') }; |
|
23 |
response.data = jQuery( 'response_data', child ).text(); |
|
24 |
response.supplemental = {}; |
|
25 |
if ( !jQuery( 'supplemental', child ).children().each( function() { |
|
26 |
response.supplemental[this.nodeName] = jQuery(this).text(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
} ).length ) { response.supplemental = false; } |
0 | 28 |
response.errors = []; |
29 |
if ( !jQuery('wp_error', child).each( function() { |
|
30 |
var code = jQuery(this).attr('code'), anError, errorData, formField; |
|
31 |
anError = { code: code, message: this.firstChild.nodeValue, data: false }; |
|
32 |
errorData = jQuery('wp_error_data[code="' + code + '"]', x); |
|
33 |
if ( errorData ) { anError.data = errorData.get(); } |
|
34 |
formField = jQuery( 'form-field', errorData ).text(); |
|
35 |
if ( formField ) { code = formField; } |
|
36 |
if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); } |
|
37 |
err += '<p>' + anError.message + '</p>'; |
|
38 |
response.errors.push( anError ); |
|
39 |
parsed.errors = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
} ).length ) { response.errors = false; } |
0 | 41 |
parsed.responses.push( response ); |
42 |
} ); |
|
43 |
if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); } |
|
44 |
return parsed; |
|
45 |
} |
|
46 |
if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); } |
|
47 |
x = parseInt(x,10); |
|
48 |
if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); } |
|
49 |
else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken + '</p></div>'); } |
|
50 |
return true; |
|
51 |
}, |
|
52 |
invalidateForm: function ( selector ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
return jQuery( selector ).addClass( 'form-invalid' ).find('input').one( 'change wp-check-valid-field', function() { jQuery(this).closest('.form-invalid').removeClass( 'form-invalid' ); } ); |
0 | 54 |
}, |
55 |
validateForm: function( selector ) { |
|
56 |
selector = jQuery( selector ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
return !wpAjax.invalidateForm( selector.find('.form-required').filter( function() { return jQuery('input:visible', this).val() === ''; } ) ).length; |
0 | 58 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
}, wpAjax || { noPerm: 'Sorry, you are not allowed to do that.', broken: 'Something went wrong.' } ); |
0 | 60 |
|
61 |
// Basic form validation |
|
62 |
jQuery(document).ready( function($){ |
|
63 |
$('form.validate').submit( function() { return wpAjax.validateForm( $(this) ); } ); |
|
64 |
}); |