| 1 |
import { fireEvent } from "../public/utility"; |
| 2 |
|
| 3 |
if (!String.format) { |
| 4 |
String.format = function (format) { |
| 5 |
var args = Array.prototype.slice.call(arguments, 1); |
| 6 |
return format.replace(/{(\d+)}/g, function (match, number) { |
| 7 |
return typeof args[number] != 'undefined' |
| 8 |
? args[number] |
| 9 |
: match |
| 10 |
; |
| 11 |
}); |
| 12 |
}; |
| 13 |
} |
| 14 |
|
| 15 |
window.addEventListener('DOMContentLoaded', () => { |
| 16 |
|
| 17 |
const title = document.querySelector('.ag-post-metabox__title'); |
| 18 |
const icon = title ? title.querySelector('i') : document.createElement('i'); |
| 19 |
const text = title ? title.querySelector('span') : document.createElement('span'); |
| 20 |
const button = title ? title.querySelector('button') : document.createElement('button'); |
| 21 |
|
| 22 |
document.querySelectorAll('.ag-post-metabox__age-toggle').forEach(button => { |
| 23 |
button.addEventListener('click', () => document.body.classList.toggle('ag-show-age')); |
| 24 |
}); |
| 25 |
|
| 26 |
document.querySelectorAll('.ag-post-metabox__set').forEach(button => { |
| 27 |
button.addEventListener('click', (e) => { |
| 28 |
if (title) { |
| 29 |
title.dataset.age = e.currentTarget.parentNode.querySelector('input').value; |
| 30 |
|
| 31 |
const { |
| 32 |
dataset: { |
| 33 |
age, |
| 34 |
textRestrict, |
| 35 |
} |
| 36 |
} = title; |
| 37 |
|
| 38 |
text.textContent = String.format(textRestrict, age); |
| 39 |
fireEvent('AgeGateSetAge', age); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
document.body.classList.toggle('ag-show-age'); |
| 44 |
|
| 45 |
|
| 46 |
}); |
| 47 |
}); |
| 48 |
|
| 49 |
document.querySelectorAll('.ag_settings_switch').forEach(toggle => { |
| 50 |
toggle.addEventListener('change', (e) => { |
| 51 |
|
| 52 |
if (!title) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
const checked = e.target.checked; |
| 57 |
|
| 58 |
const { |
| 59 |
dataset: { |
| 60 |
age, |
| 61 |
textRestrict, |
| 62 |
textUnrestrict, |
| 63 |
} |
| 64 |
} = title; |
| 65 |
|
| 66 |
// TODO: DRY!!!!! |
| 67 |
if (e.target.name.match(/bypass/)) { |
| 68 |
console.log('bypass'); |
| 69 |
if (checked) { |
| 70 |
if (button) { |
| 71 |
button.style.display = 'none'; |
| 72 |
} |
| 73 |
|
| 74 |
document.body.classList.remove('ag-show-age'); |
| 75 |
icon.className = 'dashicons dashicons-unlock'; |
| 76 |
text.textContent = textUnrestrict; |
| 77 |
|
| 78 |
} else { |
| 79 |
if (button) { |
| 80 |
button.style.display = 'inline-block'; |
| 81 |
} |
| 82 |
|
| 83 |
icon.className = 'dashicons dashicons-lock'; |
| 84 |
text.textContent = String.format(textRestrict, age); |
| 85 |
} |
| 86 |
} else { |
| 87 |
console.log('no match'); |
| 88 |
if (checked) { |
| 89 |
if (button) { |
| 90 |
button.style.display = 'inline-block'; |
| 91 |
} |
| 92 |
|
| 93 |
icon.className = 'dashicons dashicons-lock'; |
| 94 |
text.textContent = String.format(textRestrict, age); |
| 95 |
} else { |
| 96 |
if (button) { |
| 97 |
button.style.display = 'none'; |
| 98 |
} |
| 99 |
|
| 100 |
document.body.classList.remove('ag-show-age'); |
| 101 |
icon.className = 'dashicons dashicons-unlock'; |
| 102 |
text.textContent = textUnrestrict; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
}); |
| 107 |
}); |
| 108 |
|
| 109 |
// handle language change dropdowns |
| 110 |
const languageSelector = document.querySelector('[name="post_lang_choice"], [name="icl_post_language"], [name="term_lang_choice"], [name="icl_tax_category_language"]'); |
| 111 |
|
| 112 |
console.log(languageSelector); |
| 113 |
window.addEventListener('AgeGateSetAge', (e) => { |
| 114 |
console.log(e); |
| 115 |
}) |
| 116 |
|
| 117 |
if (languageSelector) { |
| 118 |
const { agagemap } = window; |
| 119 |
|
| 120 |
const initial = languageSelector.value; |
| 121 |
const input = document.querySelector('[name="ag_settings[age]"]'); |
| 122 |
|
| 123 |
console.log(initial, agagemap); |
| 124 |
// if no input its not multi-age |
| 125 |
if (input) { |
| 126 |
const isCustom = agagemap[initial] != input.value; |
| 127 |
|
| 128 |
console.log(agagemap[initial], input.value) |
| 129 |
console.log(isCustom); |
| 130 |
|
| 131 |
languageSelector.addEventListener('change', (e) => { |
| 132 |
|
| 133 |
if (isCustom) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
const bypass = document.querySelector('.ag-post-metabox [name="ag_settings[bypass]"]'); |
| 138 |
const restrict = document.querySelector('.ag-post-metabox [name="ag_settings[restrict]"]'); |
| 139 |
|
| 140 |
input.value = agagemap[e.target.value]; |
| 141 |
title.dataset.age = agagemap[e.target.value]; |
| 142 |
|
| 143 |
const { |
| 144 |
dataset: { |
| 145 |
age, |
| 146 |
textRestrict, |
| 147 |
} |
| 148 |
} = title; |
| 149 |
|
| 150 |
if (restrict && !restrict.checked) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if (bypass && bypass.checked) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
text.textContent = String.format(textRestrict, age); |
| 159 |
// alert(agagemap[e.target.value]); |
| 160 |
}); |
| 161 |
} |
| 162 |
} |
| 163 |
// |
| 164 |
}); |
| 165 |
|