balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

ajax-save.js (3940B)


      1 /* global redux, tinyMCE, ajaxurl */
      2 
      3 (function( $ ) {
      4 	'use strict';
      5 
      6 	$.redux = $.redux || {};
      7 
      8 	$.redux.ajax_save = function( button ) {
      9 		var $data;
     10 		var $nonce;
     11 
     12 		var overlay           = $( document.getElementById( 'redux_ajax_overlay' ) );
     13 		var $notification_bar = $( document.getElementById( 'redux_notification_bar' ) );
     14 		var $parent           = $( button ).parents( '.redux-wrap-div' ).find( 'form' ).first();
     15 
     16 		overlay.fadeIn();
     17 
     18 		// Add the loading mechanism.
     19 		$( '.redux-action_bar .spinner' ).addClass( 'is-active' );
     20 		$( '.redux-action_bar input' ).prop( 'disabled', true );
     21 
     22 		$notification_bar.slideUp();
     23 
     24 		$( '.redux-save-warn' ).slideUp();
     25 		$( '.redux_ajax_save_error' ).slideUp(
     26 			'medium',
     27 			function() {
     28 				$( this ).remove();
     29 			}
     30 		);
     31 
     32 		// Editor field doesn't auto save. Have to call it. Boo.
     33 		if ( redux.optName.hasOwnProperty( 'editor' ) ) {
     34 			$.each(
     35 				redux.optName.editor,
     36 				function( $key ) {
     37 					var editor;
     38 
     39 					if ( 'undefined' !== typeof ( tinyMCE ) ) {
     40 						editor = tinyMCE.get( $key );
     41 
     42 						if ( editor ) {
     43 							editor.save();
     44 						}
     45 					}
     46 				}
     47 			);
     48 		}
     49 
     50 		$data = $parent.serialize();
     51 
     52 		// Add values for checked and unchecked checkboxes fields.
     53 		$parent.find( 'input[type=checkbox]' ).each(
     54 			function() {
     55 				var chkVal;
     56 
     57 				if ( 'undefined' !== typeof $( this ).attr( 'name' ) ) {
     58 					chkVal = $( this ).is( ':checked' ) ? $( this ).val() : '0';
     59 
     60 					$data += '&' + $( this ).attr( 'name' ) + '=' + chkVal;
     61 				}
     62 			}
     63 		);
     64 
     65 		if ( 'redux_save' !== button.attr( 'name' ) ) {
     66 			$data += '&' + button.attr( 'name' ) + '=' + button.val();
     67 		}
     68 
     69 		$nonce = $parent.attr( 'data-nonce' );
     70 
     71 		$.ajax(
     72 			{ type: 'post',
     73 				dataType: 'json',
     74 				url: ajaxurl,
     75 				data: {
     76 					action:     redux.optName.args.opt_name + '_ajax_save',
     77 					nonce:      $nonce,
     78 					'opt_name': redux.optName.args.opt_name,
     79 					data:       $data
     80 				},
     81 				error: function( response ) {
     82 					$( '.redux-action_bar input' ).prop( 'disabled', false );
     83 
     84 					if ( true === redux.optName.args.dev_mode ) {
     85 						console.log( response.responseText );
     86 
     87 						overlay.fadeOut( 'fast' );
     88 						$( '.redux-action_bar .spinner' ).removeClass( 'is-active' );
     89 						alert( redux.optName.ajax.alert );
     90 					} else {
     91 						redux.optName.args.ajax_save = false;
     92 
     93 						$( button ).trigger( 'click' );
     94 						$( '.redux-action_bar input' ).prop( 'disabled', true );
     95 					}
     96 				},
     97 				success: function( response ) {
     98 					var $save_notice;
     99 
    100 					if ( response.action && 'reload' === response.action ) {
    101 						location.reload( true );
    102 					} else if ( 'success' === response.status ) {
    103 						$( '.redux-action_bar input' ).prop( 'disabled', false );
    104 						overlay.fadeOut( 'fast' );
    105 						$( '.redux-action_bar .spinner' ).removeClass( 'is-active' );
    106 						redux.optName.options  = response.options;
    107 						redux.optName.errors   = response.errors;
    108 						redux.optName.warnings = response.warnings;
    109 						redux.optName.sanitize = response.sanitize;
    110 
    111 						$notification_bar.html( response.notification_bar ).slideDown( 'fast' );
    112 						if ( null !== response.errors || null !== response.warnings ) {
    113 							$.redux.notices();
    114 						}
    115 
    116 						if ( null !== response.sanitize ) {
    117 							$.redux.sanitize();
    118 						}
    119 
    120 						$save_notice = $( document.getElementById( 'redux_notification_bar' ) ).find( '.saved_notice' );
    121 
    122 						$save_notice.slideDown();
    123 						$save_notice.delay( 4000 ).slideUp();
    124 					} else {
    125 						$( '.redux-action_bar input' ).prop( 'disabled', false );
    126 						$( '.redux-action_bar .spinner' ).removeClass( 'is-active' );
    127 						overlay.fadeOut( 'fast' );
    128 						$( '.wrap h2:first' ).parent().append( '<div class="error redux_ajax_save_error" style="display:none;"><p>' + response.status + '</p></div>' );
    129 						$( '.redux_ajax_save_error' ).slideDown();
    130 						$( 'html, body' ).animate(
    131 							{ scrollTop: 0 },
    132 							'slow'
    133 						);
    134 					}
    135 				}
    136 			}
    137 		);
    138 
    139 		return false;
    140 	};
    141 })( jQuery );