Projects : mp-wp : mp-wp_genesis
1 | wpAjax = jQuery.extend( { |
2 | unserialize: function( s ) { |
3 | var r = {}; if ( !s ) { return r; } |
4 | var q = s.split('?'); if ( q[1] ) { s = q[1]; } |
5 | var pp = s.split('&'); |
6 | for ( var i in pp ) { |
7 | if ( jQuery.isFunction(pp.hasOwnProperty) && !pp.hasOwnProperty(i) ) { continue; } |
8 | var p = pp[i].split('='); |
9 | r[p[0]] = p[1]; |
10 | } |
11 | return r; |
12 | }, |
13 | parseAjaxResponse: function( x, r, e ) { // 1 = good, 0 = strange (bad data?), -1 = you lack permission |
14 | var parsed = {}; |
15 | var re = jQuery('#' + r).html(''); |
16 | if ( x && typeof x == 'object' && x.getElementsByTagName('wp_ajax') ) { |
17 | parsed.responses = []; |
18 | parsed.errors = false; |
19 | var err = ''; |
20 | jQuery('response', x).each( function() { |
21 | var th = jQuery(this); |
22 | var child = jQuery(this.firstChild); |
23 | var response = { action: th.attr('action'), what: child.get(0).nodeName, id: child.attr('id'), oldId: child.attr('old_id'), position: child.attr('position') }; |
24 | response.data = jQuery( 'response_data', child ).text(); |
25 | response.supplemental = {}; |
26 | if ( !jQuery( 'supplemental', child ).children().each( function() { |
27 | response.supplemental[this.nodeName] = jQuery(this).text(); |
28 | } ).size() ) { response.supplemental = false } |
29 | response.errors = []; |
30 | if ( !jQuery('wp_error', child).each( function() { |
31 | var code = jQuery(this).attr('code'); |
32 | var anError = { code: code, message: this.firstChild.nodeValue, data: false }; |
33 | var errorData = jQuery('wp_error_data[code="' + code + '"]', x); |
34 | if ( errorData ) { anError.data = errorData.get(); } |
35 | var formField = jQuery( 'form-field', errorData ).text(); |
36 | if ( formField ) { code = formField; } |
37 | if ( e ) { wpAjax.invalidateForm( jQuery('#' + e + ' :input[name="' + code + '"]' ).parents('.form-field:first') ); } |
38 | err += '<p>' + anError.message + '</p>'; |
39 | response.errors.push( anError ); |
40 | parsed.errors = true; |
41 | } ).size() ) { response.errors = false; } |
42 | parsed.responses.push( response ); |
43 | } ); |
44 | if ( err.length ) { re.html( '<div class="error">' + err + '</div>' ); } |
45 | return parsed; |
46 | } |
47 | if ( isNaN(x) ) { return !re.html('<div class="error"><p>' + x + '</p></div>'); } |
48 | x = parseInt(x,10); |
49 | if ( -1 == x ) { return !re.html('<div class="error"><p>' + wpAjax.noPerm + '</p></div>'); } |
50 | else if ( 0 === x ) { return !re.html('<div class="error"><p>' + wpAjax.broken + '</p></div>'); } |
51 | return true; |
52 | }, |
53 | invalidateForm: function ( selector ) { |
54 | return jQuery( selector ).addClass( 'form-invalid' ).change( function() { jQuery(this).removeClass( 'form-invalid' ); } ); |
55 | }, |
56 | validateForm: function( selector ) { |
57 | selector = jQuery( selector ); |
58 | return !wpAjax.invalidateForm( selector.find('.form-required').andSelf().filter('.form-required:has(:input[value=""]), .form-required:input[value=""]') ).size(); |
59 | } |
60 | }, wpAjax || { noPerm: 'You do not have permission to do that.', broken: 'An unidentified error has occurred.' } ); |