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