form-submission.js
3 months ago
frontend-picker.js
1 week ago
item-select.js
2 months ago
listing.css
3 months ago
listing.js
3 months ago
quick-edit.js
3 months ago
table.css
3 months ago
quick-edit.js
63 lines
| 1 | import jQuery from 'jquery'; |
| 2 | import apiFetch from '@wordpress/api-fetch'; |
| 3 | import { addQueryArgs } from '@wordpress/url'; |
| 4 | |
| 5 | /** |
| 6 | * Fetch the placement data |
| 7 | * |
| 8 | * @param {Number} id the placement ID. |
| 9 | */ |
| 10 | const getPlacementData = (id) => { |
| 11 | apiFetch({ |
| 12 | path: addQueryArgs('/advanced-ads/v1/placement', { id: id }), |
| 13 | method: 'GET', |
| 14 | }).then((data) => { |
| 15 | if (data.error) { |
| 16 | return; |
| 17 | } |
| 18 | updateInputs(id, data); |
| 19 | }); |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Update quick edit fields with the fetched data |
| 24 | * |
| 25 | * @param {Number} id placement ID. |
| 26 | * @param {Object} data placement data. |
| 27 | */ |
| 28 | const updateInputs = (id, data) => { |
| 29 | const row = jQuery(`#edit-${id}`); |
| 30 | row.find('fieldset:disabled').prop('disabled', false); |
| 31 | row.find('select[name="status"]').val(data.status); |
| 32 | |
| 33 | // Add values to field required by the default quick edit functions. |
| 34 | row.find('[name="post_title"]').val(data.title); |
| 35 | row.find('[name="mm"]').val('01'); |
| 36 | |
| 37 | /** |
| 38 | * Allow add-ons to do field initialization |
| 39 | */ |
| 40 | wp.hooks.doAction( |
| 41 | 'advanced-ads-quick-edit-plaacement-fields-init', |
| 42 | id, |
| 43 | data |
| 44 | ); |
| 45 | }; |
| 46 | |
| 47 | export default () => { |
| 48 | /* eslint-disable no-undef */ |
| 49 | const editCopy = window.inlineEditPost.edit; |
| 50 | |
| 51 | // Replace the default WP function |
| 52 | window.inlineEditPost.edit = function (id) { |
| 53 | /* eslint-enable no-undef */ |
| 54 | // Call the original WP edit function. |
| 55 | editCopy.apply(this, arguments); |
| 56 | |
| 57 | // Now we do our stuff. |
| 58 | if ('object' === typeof id) { |
| 59 | getPlacementData(parseInt(this.getId(id), 10)); |
| 60 | } |
| 61 | }; |
| 62 | }; |
| 63 |