| 1 |
jQuery(function() { |
| 2 |
var is_saving = false, |
| 3 |
spinner = jQuery('<div class="fv-player-shortcode-editor-small-spinner" style="float: right; margin-top: 20px; margin-right: 10px"> </div>'), |
| 4 |
postbox_data_changed = []; |
| 5 |
|
| 6 |
function closeWarning(e) { |
| 7 |
(e || window.event).returnValue = true; //Gecko + IE |
| 8 |
return true; //Gecko + Webkit, Safari, Chrome etc. |
| 9 |
} |
| 10 |
|
| 11 |
function show_popup(message, error) { |
| 12 |
var popup = jQuery('#fv-player-settings-save-notice'), |
| 13 |
content = jQuery('<p>').html(message); |
| 14 |
|
| 15 |
popup.toggleClass( 'is-error', !!error ); |
| 16 |
popup.empty().append(content).fadeIn(); |
| 17 |
|
| 18 |
setTimeout(function() { |
| 19 |
jQuery('#fv-player-settings-save-notice').fadeOut(); |
| 20 |
}, 3000); |
| 21 |
} |
| 22 |
|
| 23 |
// use setTimeout to avoid conflicts with other plugins |
| 24 |
setTimeout(function() { |
| 25 |
// detect any change in postbox |
| 26 |
jQuery(document).on( 'input', '.postbox :input', function(e) { |
| 27 |
if( postbox_data_changed.length == 0 ) { |
| 28 |
window.addEventListener('beforeunload', closeWarning); |
| 29 |
} |
| 30 |
|
| 31 |
var postbox_id = jQuery(this).closest('.postbox').attr('id'); |
| 32 |
|
| 33 |
if( postbox_data_changed.indexOf( postbox_id ) == -1 ) { |
| 34 |
postbox_data_changed.push( postbox_id ); |
| 35 |
} |
| 36 |
}); |
| 37 |
},0); |
| 38 |
|
| 39 |
jQuery(document).on( 'click', '.fv-wordpress-flowplayer-save', function(e) { |
| 40 |
e.preventDefault(); |
| 41 |
|
| 42 |
if (is_saving) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
// save custom CSS |
| 47 |
if( window.fv_player_settings_custom_css_codeMirror ) { |
| 48 |
window.fv_player_settings_custom_css_codeMirror.codemirror.save(); |
| 49 |
} |
| 50 |
|
| 51 |
is_saving = true; |
| 52 |
|
| 53 |
jQuery( document ).trigger( 'fv-wordpress-flowplayer-save' ); |
| 54 |
|
| 55 |
var $this = jQuery(this), |
| 56 |
$postbox = $this.closest('.postbox'), |
| 57 |
serialized = jQuery($postbox).find(':input').serializeArray(), |
| 58 |
reload = $this.data('reload'), |
| 59 |
postbox_id = $postbox.attr('id'), |
| 60 |
postbox_name = $postbox.find('h2').first().text(); |
| 61 |
|
| 62 |
$this.closest('td').append(spinner); |
| 63 |
|
| 64 |
// add 'fv-wp-flowplayer-submit-ajax' to serialized data |
| 65 |
serialized.push({name: 'fv-wp-flowplayer-submit-ajax', value: 'fv-wp-flowplayer-submit-ajax'}); |
| 66 |
|
| 67 |
// add nonce 'fv_flowplayer_settings_ajax_nonce' to serialized data |
| 68 |
serialized.push({name: 'fv_flowplayer_settings_ajax_nonce', value: jQuery('#fv_flowplayer_settings_ajax_nonce').val()}); |
| 69 |
|
| 70 |
// add postbox id to serialized data |
| 71 |
serialized.push({name: 'postbox_id', value: postbox_id}); |
| 72 |
|
| 73 |
// use custom action if not reloading |
| 74 |
if( !reload ) { |
| 75 |
serialized.push({name: 'action', value: 'fv_flowplayer_settings_save'}); |
| 76 |
} |
| 77 |
|
| 78 |
var ajax_obj = { |
| 79 |
url: reload ? window.location.href : ajaxurl, |
| 80 |
type: 'POST', |
| 81 |
data: serialized, |
| 82 |
success: function(data) { |
| 83 |
// get new postbox |
| 84 |
var $new = jQuery(data).find('#' + postbox_id); |
| 85 |
|
| 86 |
// replace old postbox with new one |
| 87 |
if(reload) $postbox.replaceWith($new); |
| 88 |
show_popup('Settings saved for <strong>' + postbox_name + '</strong>' ); |
| 89 |
}, |
| 90 |
error: function(data) { |
| 91 |
show_popup('Error saving settings for <strong>' + postbox_name + '</strong>', true ); |
| 92 |
console.error(data); |
| 93 |
}, |
| 94 |
complete: function() { |
| 95 |
is_saving = false; |
| 96 |
spinner.remove(); |
| 97 |
|
| 98 |
// remove saved postbox from array |
| 99 |
postbox_data_changed.splice( postbox_data_changed.indexOf( postbox_id ), 1 ); |
| 100 |
|
| 101 |
// ceck if there are any unsaved postboxes |
| 102 |
if( postbox_data_changed.length == 0 ) { |
| 103 |
window.removeEventListener('beforeunload', closeWarning); |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
jQuery.ajax(ajax_obj); |
| 110 |
|
| 111 |
}); |
| 112 |
|
| 113 |
// remove beforeunload event when submit button is clicked |
| 114 |
jQuery(document).on( 'click', '[name="fv-wp-flowplayer-submit"], [name=fv-player-save-all]', function(e) { |
| 115 |
window.removeEventListener('beforeunload', closeWarning); |
| 116 |
}); |
| 117 |
|
| 118 |
}); |
| 119 |
|