| 1 |
const $ = window.jQuery |
| 2 |
const Option = require('./_option.js') |
| 3 |
const optionControls = document.getElementById('boxzilla-box-options-controls') |
| 4 |
const $optionControls = $(optionControls) |
| 5 |
const tnLoggedIn = document.createTextNode(' logged in') |
| 6 |
const EventEmitter = require('wolfy87-eventemitter') |
| 7 |
const events = new EventEmitter() |
| 8 |
const Designer = require('./_designer.js')($, Option, events) |
| 9 |
const rowTemplate = window.wp.template('rule-row-template') |
| 10 |
const i18n = window.boxzilla_i18n |
| 11 |
const ruleComparisonEl = document.getElementById('boxzilla-rule-comparison') |
| 12 |
const rulesContainerEl = document.getElementById('boxzilla-box-rules') |
| 13 |
const ajaxurl = window.ajaxurl |
| 14 |
|
| 15 |
// events |
| 16 |
$(window).on('load', function () { |
| 17 |
if (typeof (window.tinyMCE) === 'undefined') { |
| 18 |
document.getElementById('notice-notinymce').style.display = '' |
| 19 |
} |
| 20 |
|
| 21 |
$optionControls.on('click', '.boxzilla-add-rule', addRuleFields) |
| 22 |
$optionControls.on('click', '.boxzilla-remove-rule', removeRule) |
| 23 |
$optionControls.on('change', '.boxzilla-rule-condition', setContextualHelpers) |
| 24 |
$optionControls.find('.boxzilla-auto-show-trigger').on('change', toggleTriggerOptions) |
| 25 |
$(ruleComparisonEl).change(toggleAndOrTexts) |
| 26 |
$('.boxzilla-rule-row').each(setContextualHelpers) |
| 27 |
}) |
| 28 |
|
| 29 |
function toggleAndOrTexts () { |
| 30 |
var newText = ruleComparisonEl.value === 'any' ? i18n.or : i18n.and |
| 31 |
$('.boxzilla-andor').text(newText) |
| 32 |
} |
| 33 |
|
| 34 |
function toggleTriggerOptions () { |
| 35 |
$optionControls.find('.boxzilla-trigger-options').toggle(this.value !== '') |
| 36 |
} |
| 37 |
|
| 38 |
function removeRule () { |
| 39 |
var row = $(this).parents('tr') |
| 40 |
|
| 41 |
// delete andor row |
| 42 |
row.prev().remove() |
| 43 |
|
| 44 |
// delete rule row |
| 45 |
row.remove() |
| 46 |
} |
| 47 |
|
| 48 |
function setContextualHelpers () { |
| 49 |
var context = (this.tagName.toLowerCase() === 'tr') ? this : $(this).parents('tr').get(0) |
| 50 |
var condition = context.querySelector('.boxzilla-rule-condition').value |
| 51 |
var valueInput = context.querySelector('.boxzilla-rule-value') |
| 52 |
var qualifierInput = context.querySelector('.boxzilla-rule-qualifier') |
| 53 |
var betterInput = valueInput.cloneNode(true) |
| 54 |
var $betterInput = $(betterInput) |
| 55 |
|
| 56 |
// remove previously added helpers |
| 57 |
$(context.querySelectorAll('.boxzilla-helper')).remove() |
| 58 |
|
| 59 |
// prepare better input |
| 60 |
betterInput.removeAttribute('name') |
| 61 |
betterInput.className = betterInput.className + ' boxzilla-helper' |
| 62 |
valueInput.parentNode.insertBefore(betterInput, valueInput.nextSibling) |
| 63 |
$betterInput.change(function () { |
| 64 |
valueInput.value = this.value |
| 65 |
}) |
| 66 |
|
| 67 |
betterInput.style.display = '' |
| 68 |
valueInput.style.display = 'none' |
| 69 |
qualifierInput.style.display = '' |
| 70 |
qualifierInput.querySelector('option[value="not_contains"]').style.display = 'none' |
| 71 |
qualifierInput.querySelector('option[value="contains"]').style.display = 'none' |
| 72 |
if (tnLoggedIn.parentNode) { |
| 73 |
tnLoggedIn.parentNode.removeChild(tnLoggedIn) |
| 74 |
} |
| 75 |
|
| 76 |
// change placeholder for textual help |
| 77 |
switch (condition) { |
| 78 |
default: |
| 79 |
betterInput.placeholder = i18n.enterCommaSeparatedValues |
| 80 |
break |
| 81 |
|
| 82 |
case '': |
| 83 |
case 'everywhere': |
| 84 |
qualifierInput.value = '1' |
| 85 |
valueInput.value = '' |
| 86 |
betterInput.style.display = 'none' |
| 87 |
qualifierInput.style.display = 'none' |
| 88 |
break |
| 89 |
|
| 90 |
case 'is_single': |
| 91 |
case 'is_post': |
| 92 |
betterInput.placeholder = i18n.enterCommaSeparatedPosts |
| 93 |
$betterInput.suggest(ajaxurl + '?action=boxzilla_autocomplete&type=post', { |
| 94 |
multiple: true, |
| 95 |
multipleSep: ',' |
| 96 |
}) |
| 97 |
break |
| 98 |
|
| 99 |
case 'is_page': |
| 100 |
betterInput.placeholder = i18n.enterCommaSeparatedPages |
| 101 |
$betterInput.suggest(ajaxurl + '?action=boxzilla_autocomplete&type=page', { |
| 102 |
multiple: true, |
| 103 |
multipleSep: ',' |
| 104 |
}) |
| 105 |
break |
| 106 |
|
| 107 |
case 'is_post_type': |
| 108 |
betterInput.placeholder = i18n.enterCommaSeparatedPostTypes |
| 109 |
$betterInput.suggest(ajaxurl + '?action=boxzilla_autocomplete&type=post_type', { |
| 110 |
multiple: true, |
| 111 |
multipleSep: ',' |
| 112 |
}) |
| 113 |
break |
| 114 |
|
| 115 |
case 'is_url': |
| 116 |
qualifierInput.querySelector('option[value="contains"]').style.display = '' |
| 117 |
qualifierInput.querySelector('option[value="not_contains"]').style.display = '' |
| 118 |
betterInput.placeholder = i18n.enterCommaSeparatedRelativeUrls |
| 119 |
break |
| 120 |
|
| 121 |
case 'is_post_in_category': |
| 122 |
$betterInput.suggest(ajaxurl + '?action=boxzilla_autocomplete&type=category', { |
| 123 |
multiple: true, |
| 124 |
multipleSep: ',' |
| 125 |
}) |
| 126 |
break |
| 127 |
|
| 128 |
case 'is_post_with_tag': |
| 129 |
$betterInput.suggest(ajaxurl + '?action=boxzilla_autocomplete&type=post_tag', { |
| 130 |
multiple: true, |
| 131 |
multipleSep: ',' |
| 132 |
}) |
| 133 |
break |
| 134 |
|
| 135 |
case 'is_user_logged_in': |
| 136 |
betterInput.style.display = 'none' |
| 137 |
valueInput.parentNode.insertBefore(tnLoggedIn, valueInput.nextSibling) |
| 138 |
break |
| 139 |
|
| 140 |
case 'is_referer': |
| 141 |
qualifierInput.querySelector('option[value="contains"]').style.display = '' |
| 142 |
qualifierInput.querySelector('option[value="not_contains"]').style.display = '' |
| 143 |
break |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
function addRuleFields () { |
| 148 |
var data = { |
| 149 |
key: optionControls.querySelectorAll('.boxzilla-rule-row').length, |
| 150 |
andor: ruleComparisonEl.value === 'any' ? i18n.or : i18n.and |
| 151 |
} |
| 152 |
var html = rowTemplate(data) |
| 153 |
$(rulesContainerEl).append(html) |
| 154 |
return false |
| 155 |
} |
| 156 |
|
| 157 |
module.exports = { |
| 158 |
Designer: Designer, |
| 159 |
Option: Option, |
| 160 |
events: events |
| 161 |
} |
| 162 |
|