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