inline-edit-group-ads.js
102 lines
| 1 | /* saving ad group ad settings */ |
| 2 | |
| 3 | var inlineEditAdGroupAds; |
| 4 | (function($) { |
| 5 | inlineEditAdGroupAds = { |
| 6 | init: function() { |
| 7 | var t = this; |
| 8 | $('#the-list').on('click', 'a.edit-ad-group-ads', function() { |
| 9 | inlineEditAdGroupAds.edit(this); |
| 10 | return false; |
| 11 | }); |
| 12 | $('#the-list').on('click', 'a.cancel', function() { |
| 13 | return inlineEditAdGroupAds.revert(); |
| 14 | }); |
| 15 | $('#the-list').on('click', 'a.save', function() { |
| 16 | return inlineEditAdGroupAds.save(this); |
| 17 | }); |
| 18 | $('#the-list').on('keydown', 'input, select', function() { |
| 19 | if (e.which === 13) { |
| 20 | return inlineEditAdGroupAds.save(this); |
| 21 | } |
| 22 | }); |
| 23 | $('#posts-filter input[type="submit"]').mousedown(function() { |
| 24 | t.revert(); |
| 25 | }); |
| 26 | }, |
| 27 | edit: function(link) { |
| 28 | var td, id, t = this; |
| 29 | // get group id |
| 30 | id = t.getId(link); |
| 31 | // get container |
| 32 | td = $(link).parents('td'); |
| 33 | // load form with information |
| 34 | params = { |
| 35 | action: 'advads-ad-group-ads-form', |
| 36 | group_id: id, |
| 37 | }; |
| 38 | $.post(ajaxurl, params, |
| 39 | // append return |
| 40 | function(r) { |
| 41 | if (r) { |
| 42 | // hide all child elements |
| 43 | td.children('*').hide(); |
| 44 | // display the form |
| 45 | $(r).appendTo(td); |
| 46 | } |
| 47 | } |
| 48 | ); |
| 49 | return false; |
| 50 | }, |
| 51 | save: function(link) { |
| 52 | var params, td, id, t = this; |
| 53 | // get group id |
| 54 | id = t.getId(link); |
| 55 | // get container |
| 56 | td = $(link).parents('td'); |
| 57 | td.find('.ad-group-ads-form .spinner').show(); |
| 58 | params = { |
| 59 | action: 'advads-ad-group-ads-form-save', |
| 60 | group_id: id, |
| 61 | fields: '' |
| 62 | }; |
| 63 | params.fields = td.find(':input').serialize(); |
| 64 | // make ajax request |
| 65 | $.post(ajaxurl, params, function(r) { |
| 66 | td.find('.ad-group-ads-form .spinner').hide(); |
| 67 | if (r) { |
| 68 | t.revert(); // show normal table |
| 69 | |
| 70 | $.each(r, function(key, value){ |
| 71 | // search the field with the ad weight and change the value |
| 72 | td.find('.ad-weight-' + key).html(value); |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | }, "json" |
| 77 | ); |
| 78 | return false; |
| 79 | }, |
| 80 | // remove edit form and display the other elements again |
| 81 | revert: function() { |
| 82 | var td = $('table.widefat .ad-group-ads-form').parents('td'); |
| 83 | if (td) { |
| 84 | $('table.widefat .spinner').hide(); |
| 85 | td.find('.ad-group-ads-form').remove(); |
| 86 | td.find('*').show(); |
| 87 | } |
| 88 | |
| 89 | return false; |
| 90 | }, |
| 91 | // get the id of the group from the link clicked |
| 92 | getId: function(link) { |
| 93 | groupid = $(link).parents('td').find('.ad-group-id').val(); |
| 94 | return parseInt(groupid); |
| 95 | ; |
| 96 | } |
| 97 | }; |
| 98 | $(document).ready(function() { |
| 99 | inlineEditAdGroupAds.init(); |
| 100 | }); |
| 101 | })(jQuery); |
| 102 |