| 1 |
;(function ($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Show/hide the delete (close) button for user role conditions |
| 6 |
* depending on whether there's more than one rule. |
| 7 |
*/ |
| 8 |
function updateCloseButtonVisibility($wrapper) { |
| 9 |
const $rules = $wrapper.find('.king-addons-el-hf-user-role-condition'); |
| 10 |
const showClose = $rules.length > 1; |
| 11 |
|
| 12 |
$rules.each(function () { |
| 13 |
$(this) |
| 14 |
.find('.user_role-condition-delete') |
| 15 |
.toggleClass('king-addons-el-hf-hidden', !showClose); |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Handler for adding a new rule. |
| 21 |
*/ |
| 22 |
function handleAddRuleClick(e) { |
| 23 |
e.preventDefault(); |
| 24 |
e.stopPropagation(); |
| 25 |
|
| 26 |
const $this = $(this); |
| 27 |
const currentId = parseInt($this.attr('data-rule-id'), 10); |
| 28 |
const newId = currentId + 1; |
| 29 |
|
| 30 |
const $ruleWrap = $this |
| 31 |
.closest('.king-addons-el-hf-user-role-selector-wrapper') |
| 32 |
.find('.user_role-builder-wrap'); |
| 33 |
|
| 34 |
// `wp.template` uses the script template with id: #tmpl-king-addons-el-hf-user-role-condition |
| 35 |
const template = wp.template('king-addons-el-hf-user-role-condition'); |
| 36 |
|
| 37 |
// Append the newly generated HTML |
| 38 |
$ruleWrap.append(template({ id: newId })); |
| 39 |
|
| 40 |
// Update the data-rule-id so the next added rule increments properly |
| 41 |
$this.attr('data-rule-id', newId); |
| 42 |
|
| 43 |
// Update delete-button visibility |
| 44 |
const $fieldWrap = $this.closest('.king-addons-el-hf-user-role-wrapper'); |
| 45 |
updateCloseButtonVisibility($fieldWrap); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Handler for deleting a rule. |
| 50 |
*/ |
| 51 |
function handleDeleteRuleClick(e) { |
| 52 |
e.preventDefault(); |
| 53 |
e.stopPropagation(); |
| 54 |
|
| 55 |
const $this = $(this); |
| 56 |
const $ruleCondition = $this.closest('.king-addons-el-hf-user-role-condition'); |
| 57 |
const $fieldWrap = $this.closest('.king-addons-el-hf-user-role-wrapper'); |
| 58 |
|
| 59 |
// Remove the rule |
| 60 |
$ruleCondition.remove(); |
| 61 |
|
| 62 |
// Re-index existing rules so their data-rule and names are in sequence |
| 63 |
let updatedRuleId = 0; |
| 64 |
$fieldWrap.find('.king-addons-el-hf-user-role-condition').each(function (index) { |
| 65 |
const $condition = $(this); |
| 66 |
const oldRuleId = $condition.attr('data-rule'); |
| 67 |
const $selectLocation = $condition.find('.user_role-condition'); |
| 68 |
const originalName = $selectLocation.attr('name'); |
| 69 |
|
| 70 |
$condition.attr('data-rule', index); |
| 71 |
$selectLocation.attr( |
| 72 |
'name', |
| 73 |
originalName.replace(`[${oldRuleId}]`, `[${index}]`) |
| 74 |
); |
| 75 |
|
| 76 |
// Update the old class with the new index |
| 77 |
$condition |
| 78 |
.removeClass(`king-addons-el-hf-user-role-${oldRuleId}`) |
| 79 |
.addClass(`king-addons-el-hf-user-role-${index}`); |
| 80 |
|
| 81 |
updatedRuleId = index; |
| 82 |
}); |
| 83 |
|
| 84 |
// Update the add-rule link so it knows the last rule ID |
| 85 |
$fieldWrap |
| 86 |
.find('.user_role-add-rule-wrap a') |
| 87 |
.attr('data-rule-id', updatedRuleId); |
| 88 |
|
| 89 |
// Update delete-button visibility |
| 90 |
updateCloseButtonVisibility($fieldWrap); |
| 91 |
} |
| 92 |
|
| 93 |
$(document).ready(function () { |
| 94 |
|
| 95 |
const selectorWrapper = $('.king-addons-el-hf-user-role-selector-wrapper'); |
| 96 |
|
| 97 |
// Initialize visibility of delete buttons on page load |
| 98 |
selectorWrapper.each(function () { |
| 99 |
updateCloseButtonVisibility($(this)); |
| 100 |
}); |
| 101 |
|
| 102 |
// Event delegation for adding rules |
| 103 |
selectorWrapper.on( |
| 104 |
'click', |
| 105 |
'.user_role-add-rule-wrap a', |
| 106 |
handleAddRuleClick |
| 107 |
); |
| 108 |
|
| 109 |
// Event delegation for deleting rules |
| 110 |
selectorWrapper.on( |
| 111 |
'click', |
| 112 |
'.user_role-condition-delete', |
| 113 |
handleDeleteRuleClick |
| 114 |
); |
| 115 |
}); |
| 116 |
}(jQuery, window)); |