class-wp-meta-box-page.php
13 years ago
class-wp-option-forms.php
13 years ago
wp-option-forms.js
13 years ago
wp-option-forms.js
56 lines
| 1 | /* WP Options Form */ |
| 2 | /*global jQuery, ajaxurl*/ |
| 3 | jQuery(function ($) { |
| 4 | 'use strict'; |
| 5 | |
| 6 | // save function |
| 7 | var saveAjaxForm = function (target) { |
| 8 | var $this = $(target), |
| 9 | $form = $this.parents('form'), |
| 10 | // get ajax post values |
| 11 | vals = $form.serializeArray(); |
| 12 | |
| 13 | // disable button |
| 14 | $this.attr('disabled', true); |
| 15 | |
| 16 | // show ajax loader |
| 17 | $form.find('.ajax-feedback').css('visibility', 'visible'); |
| 18 | |
| 19 | // save option values |
| 20 | $.post(ajaxurl, vals, function (result) { |
| 21 | var $msg = $('<strong>').insertBefore($this); |
| 22 | |
| 23 | if (result === '1') { |
| 24 | $msg.html('Saved'); |
| 25 | } else { |
| 26 | // save options, non-ajax fallback |
| 27 | $form.find('[name="action"]').val('update'); |
| 28 | // normal submit |
| 29 | $form.submit(); |
| 30 | } |
| 31 | |
| 32 | $msg.css({ margin: '0 5px' }) |
| 33 | .delay(1000) |
| 34 | .fadeOut(function(){ |
| 35 | $(this).remove(); |
| 36 | }); |
| 37 | |
| 38 | // enable button |
| 39 | $this.attr('disabled', false); |
| 40 | |
| 41 | // hide ajax loader |
| 42 | $form.find('.ajax-feedback').css('visibility', 'hidden'); |
| 43 | |
| 44 | // trigger ajax_saved_options |
| 45 | $form.trigger('ajax_saved_options', [result]); |
| 46 | }); |
| 47 | }; |
| 48 | |
| 49 | // add ajax post |
| 50 | $('form.ajax-form input[type="submit"]').click(function(e) { |
| 51 | saveAjaxForm(this); |
| 52 | e.preventDefault(); |
| 53 | }); |
| 54 | |
| 55 | }); |
| 56 |