| 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 |
|
| 9 |
// sanity check, are we on the correct page? |
| 10 |
if( $optionControls.length === 0 ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
var EventEmitter = require('wolfy87-eventemitter'); |
| 15 |
var events = new EventEmitter(); |
| 16 |
var Designer = require('./_designer.js')($, Option, events); |
| 17 |
var rowTemplate = wp.template('rule-row-template'); |
| 18 |
var i18n = boxzilla_i18n; |
| 19 |
|
| 20 |
// events |
| 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 |
|
| 26 |
$(window).load(function() { |
| 27 |
if( typeof(window.tinyMCE) === "undefined" ) { |
| 28 |
document.getElementById('notice-notinymce').style.display = ''; |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
// call contextual helper method for each row |
| 33 |
$('.boxzilla-rule-row').each(setContextualHelpers); |
| 34 |
|
| 35 |
function toggleTriggerOptions() { |
| 36 |
$optionControls.find('.boxzilla-trigger-options').toggle( this.value !== '' ); |
| 37 |
} |
| 38 |
|
| 39 |
function removeRule() { |
| 40 |
$(this).parents('tr').remove(); |
| 41 |
} |
| 42 |
|
| 43 |
function setContextualHelpers() { |
| 44 |
|
| 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() { valueInput.value = this.value; }); |
| 60 |
|
| 61 |
betterInput.style.display = ''; |
| 62 |
valueInput.style.display = 'none'; |
| 63 |
qualifierInput.style.display = ''; |
| 64 |
|
| 65 |
// change placeholder for textual help |
| 66 |
switch(condition) { |
| 67 |
default: |
| 68 |
betterInput.placeholder = i18n.enterCommaSeparatedValues; |
| 69 |
break; |
| 70 |
|
| 71 |
case '': |
| 72 |
case 'everywhere': |
| 73 |
qualifierInput.value = '1'; |
| 74 |
valueInput.value = ''; |
| 75 |
betterInput.style.display = 'none'; |
| 76 |
qualifierInput.style.display = 'none'; |
| 77 |
break; |
| 78 |
|
| 79 |
case 'is_single': |
| 80 |
case 'is_post': |
| 81 |
betterInput.placeholder = i18n.enterCommaSeparatedPosts; |
| 82 |
$betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=post", {multiple:true, multipleSep: ","}); |
| 83 |
break; |
| 84 |
|
| 85 |
case 'is_page': |
| 86 |
betterInput.placeholder = i18n.enterCommaSeparatedPages; |
| 87 |
$betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=page", {multiple:true, multipleSep: ","}); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'is_post_type': |
| 91 |
betterInput.placeholder = i18n.enterCommaSeparatedPostTypes; |
| 92 |
$betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=post_type", {multiple:true, multipleSep: ","}); |
| 93 |
break; |
| 94 |
|
| 95 |
case 'is_url': |
| 96 |
betterInput.placeholder = i18n.enterCommaSeparatedRelativeUrls; |
| 97 |
break; |
| 98 |
|
| 99 |
case 'is_post_in_category': |
| 100 |
$betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=category", {multiple:true, multipleSep: ","}); |
| 101 |
break; |
| 102 |
|
| 103 |
case 'is_post_with_tag': |
| 104 |
$betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=post_tag", {multiple:true, multipleSep: ","}); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function addRuleFields() { |
| 110 |
var data = { |
| 111 |
'key': optionControls.querySelectorAll('.boxzilla-rule-row').length |
| 112 |
}; |
| 113 |
var html = rowTemplate(data); |
| 114 |
$(document.getElementById('boxzilla-box-rules')).after(html); |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
module.exports = { |
| 119 |
'Designer': Designer, |
| 120 |
'Option': Option, |
| 121 |
'events': events |
| 122 |
}; |
| 123 |
})(); |