| @@ -1,123 +1,155 @@ | ||
| 1 | -(function() { | |
| 2 | - 'use strict'; | |
| 1 | +(function () { | |
| 2 | + 'use strict'; | |
| 3 | 3 | |
| 4 | - var $ = window.jQuery; | |
| 5 | - var Option = require('./_option.js'); | |
| 6 | - var optionControls = document.getElementById('boxzilla-box-options-controls'); | |
| 7 | - var $optionControls = $(optionControls); | |
| 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'); | |
| 8 | 9 | |
| 9 | - // sanity check, are we on the correct page? | |
| 10 | - if( $optionControls.length === 0 ) { | |
| 11 | - return; | |
| 12 | - } | |
| 10 | + // sanity check, are we on the correct page? | |
| 11 | + if ($optionControls.length === 0) { | |
| 12 | + return; | |
| 13 | + } | |
| 13 | 14 | |
| 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; | |
| 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; | |
| 19 | 20 | |
| 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 ); | |
| 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); | |
| 25 | 26 | |
| 26 | - $(window).load(function() { | |
| 27 | - if( typeof(window.tinyMCE) === "undefined" ) { | |
| 28 | - document.getElementById('notice-notinymce').style.display = ''; | |
| 29 | - } | |
| 30 | - }); | |
| 27 | + $(window).load(function () { | |
| 28 | + if (typeof(window.tinyMCE) === "undefined") { | |
| 29 | + document.getElementById('notice-notinymce').style.display = ''; | |
| 30 | + } | |
| 31 | + }); | |
| 31 | 32 | |
| 32 | - // call contextual helper method for each row | |
| 33 | - $('.boxzilla-rule-row').each(setContextualHelpers); | |
| 33 | + // call contextual helper method for each row | |
| 34 | + $('.boxzilla-rule-row').each(setContextualHelpers); | |
| 34 | 35 | |
| 35 | - function toggleTriggerOptions() { | |
| 36 | - $optionControls.find('.boxzilla-trigger-options').toggle( this.value !== '' ); | |
| 37 | - } | |
| 36 | + function toggleTriggerOptions() { | |
| 37 | + $optionControls.find('.boxzilla-trigger-options').toggle(this.value !== ''); | |
| 38 | + } | |
| 38 | 39 | |
| 39 | - function removeRule() { | |
| 40 | - $(this).parents('tr').remove(); | |
| 41 | - } | |
| 40 | + function removeRule() { | |
| 41 | + $(this).parents('tr').remove(); | |
| 42 | + } | |
| 42 | 43 | |
| 43 | - function setContextualHelpers() { | |
| 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); | |
| 44 | 51 | |
| 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); | |
| 52 | + // remove previously added helpers | |
| 53 | + $(context.querySelectorAll('.boxzilla-helper')).remove(); | |
| 51 | 54 | |
| 52 | - // remove previously added helpers | |
| 53 | - $(context.querySelectorAll('.boxzilla-helper')).remove(); | |
| 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 | + }); | |
| 54 | 62 | |
| 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; }); | |
| 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 | + } | |
| 60 | 70 | |
| 61 | - betterInput.style.display = ''; | |
| 62 | - valueInput.style.display = 'none'; | |
| 63 | - qualifierInput.style.display = ''; | |
| 71 | + // change placeholder for textual help | |
| 72 | + switch (condition) { | |
| 73 | + default: | |
| 74 | + betterInput.placeholder = i18n.enterCommaSeparatedValues; | |
| 75 | + break; | |
| 64 | 76 | |
| 65 | - // change placeholder for textual help | |
| 66 | - switch(condition) { | |
| 67 | - default: | |
| 68 | - betterInput.placeholder = i18n.enterCommaSeparatedValues; | |
| 69 | - break; | |
| 77 | + case '': | |
| 78 | + case 'everywhere': | |
| 79 | + qualifierInput.value = '1'; | |
| 80 | + valueInput.value = ''; | |
| 81 | + betterInput.style.display = 'none'; | |
| 82 | + qualifierInput.style.display = 'none'; | |
| 83 | + break; | |
| 70 | 84 | |
| 71 | - case '': | |
| 72 | - case 'everywhere': | |
| 73 | - qualifierInput.value = '1'; | |
| 74 | - valueInput.value = ''; | |
| 75 | - betterInput.style.display = 'none'; | |
| 76 | - qualifierInput.style.display = 'none'; | |
| 77 | - break; | |
| 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; | |
| 78 | 93 | |
| 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; | |
| 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; | |
| 84 | 101 | |
| 85 | - case 'is_page': | |
| 86 | - betterInput.placeholder = i18n.enterCommaSeparatedPages; | |
| 87 | - $betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=page", {multiple:true, multipleSep: ","}); | |
| 88 | - break; | |
| 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; | |
| 89 | 109 | |
| 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; | |
| 110 | + case 'is_url': | |
| 111 | + qualifierInput.querySelector('option[value="contains"]').style.display = ''; | |
| 112 | + betterInput.placeholder = i18n.enterCommaSeparatedRelativeUrls; | |
| 113 | + break; | |
| 94 | 114 | |
| 95 | - case 'is_url': | |
| 96 | - betterInput.placeholder = i18n.enterCommaSeparatedRelativeUrls; | |
| 97 | - break; | |
| 115 | + case 'is_post_in_category': | |
| 116 | + $betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=category", { | |
| 117 | + multiple: true, | |
| 118 | + multipleSep: "," | |
| 119 | + }); | |
| 120 | + break; | |
| 98 | 121 | |
| 99 | - case 'is_post_in_category': | |
| 100 | - $betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=category", {multiple:true, multipleSep: ","}); | |
| 101 | - break; | |
| 122 | + case 'is_post_with_tag': | |
| 123 | + $betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=post_tag", { | |
| 124 | + multiple: true, | |
| 125 | + multipleSep: "," | |
| 126 | + }); | |
| 127 | + break; | |
| 102 | 128 | |
| 103 | - case 'is_post_with_tag': | |
| 104 | - $betterInput.suggest(ajaxurl + "?action=boxzilla_autocomplete&type=post_tag", {multiple:true, multipleSep: ","}); | |
| 105 | - break; | |
| 106 | - } | |
| 107 | - } | |
| 129 | + case 'is_user_logged_in': | |
| 130 | + betterInput.style.display = 'none'; | |
| 131 | + valueInput.parentNode.insertBefore(tnLoggedIn, valueInput.nextSibling); | |
| 132 | + break; | |
| 108 | 133 | |
| 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 | - } | |
| 134 | + case 'is_referer': | |
| 135 | + qualifierInput.querySelector('option[value="contains"]').style.display = ''; | |
| 136 | + break; | |
| 117 | 137 | |
| 118 | - module.exports = { | |
| 119 | - 'Designer': Designer, | |
| 120 | - 'Option': Option, | |
| 121 | - 'events': events | |
| 122 | - }; | |
| 123 | -})(); | |
| 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 | +})(); | |