delete-group.js
3 months ago
edit-group.js
1 month ago
form-submission.js
3 months ago
forms.css
3 months ago
icons.css
3 months ago
listShowMore.js
3 months ago
listing.css
3 months ago
listing.js
3 months ago
sort-ads.js
3 months ago
table.css
3 months ago
edit-group.js
94 lines
| 1 | import jQuery from 'jquery'; |
| 2 | |
| 3 | const $ = jQuery; |
| 4 | |
| 5 | function handleAddAdToGroup() { |
| 6 | const groupForm = $(this).closest('.advads-ad-group-form'), |
| 7 | $ad = groupForm.find('.advads-group-add-ad-list-ads option:selected'), |
| 8 | weightSelector = groupForm |
| 9 | .find('.advads-group-add-ad-list-weights') |
| 10 | .last(), |
| 11 | adTable = groupForm.find('.advads-group-ads tbody'); |
| 12 | |
| 13 | const adVal = $ad.val().match(/\d+/g); |
| 14 | let adUrl = ''; |
| 15 | |
| 16 | if (adVal) { |
| 17 | adUrl = advancedAds.endpoints.editAd + adVal.pop(); |
| 18 | } |
| 19 | |
| 20 | const statusType = $ad.data('status'); |
| 21 | const statusString = $ad.data('status-string'); |
| 22 | |
| 23 | // add new row if does not already exist |
| 24 | if ( |
| 25 | $ad.length && |
| 26 | weightSelector.length && |
| 27 | !adTable.find('[name="' + $ad.val() + '"]').length |
| 28 | ) { |
| 29 | adTable.append( |
| 30 | $('<tr></tr>').append( |
| 31 | $('<td></td>').html( |
| 32 | `<span class="advads-help advads-help-no-icon advads-ad-status-icon advads-ad-status-icon-${statusType}"> |
| 33 | <span class="advads-tooltip">${statusString}</span> |
| 34 | </span>` |
| 35 | ), |
| 36 | $('<td></td>').html( |
| 37 | `<a target="_blank" href="${adUrl}">${$ad.text()}</a>` |
| 38 | ), |
| 39 | $('<td></td>').append( |
| 40 | weightSelector |
| 41 | .clone() |
| 42 | .removeClass() |
| 43 | .val(weightSelector.val()) |
| 44 | .prop('name', $ad.val()) |
| 45 | ), |
| 46 | '<td><button type="button" class="advads-remove-ad-from-group button">x</button></td>' |
| 47 | ) |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function handleRemoveAdFromGroupClick() { |
| 53 | const adRow = $(this).closest('tr'); |
| 54 | adRow.remove(); |
| 55 | } |
| 56 | |
| 57 | function showTypeOptions(el) { |
| 58 | el.each(function () { |
| 59 | const _this = jQuery(this); |
| 60 | // first, hide all options except title and type |
| 61 | _this |
| 62 | .closest('.advads-ad-group-form') |
| 63 | .find('.advads-option:not(.static)') |
| 64 | .hide(); |
| 65 | const currentType = _this.val(); |
| 66 | |
| 67 | // now, show only the ones corresponding with the group type |
| 68 | _this |
| 69 | .parents('.advads-ad-group-form') |
| 70 | .find('.advads-group-type-' + currentType) |
| 71 | .show(); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | export default function () { |
| 76 | // add ad to group |
| 77 | $('.advads-group-add-ad button').on('click', handleAddAdToGroup); |
| 78 | |
| 79 | // remove ad from group |
| 80 | $('#advads-ad-group-list').on( |
| 81 | 'click', |
| 82 | '.advads-remove-ad-from-group', |
| 83 | handleRemoveAdFromGroupClick |
| 84 | ); |
| 85 | |
| 86 | // handle switching of group types based on a class derrived from that type |
| 87 | $('.advads-ad-group-type input').on('click', function () { |
| 88 | showTypeOptions($(this)); |
| 89 | }); |
| 90 | |
| 91 | // set default group options for each group |
| 92 | showTypeOptions($('.advads-ad-group-type input:checked')); |
| 93 | } |
| 94 |