customizer-shortcodes-popup.js (6952B)
1 (function (root, CP_Customizer, $) { 2 3 CP_Customizer.__shortcodesPopupControls = {}; 4 5 CP_Customizer.registerShortcodePopupControls = function (tag, controls) { 6 CP_Customizer.__shortcodesPopupControls[tag] = controls; 7 }; 8 9 10 CP_Customizer.hooks.addAction('dynamic_columns_handle', function (cols, node) { 11 if (CP_Customizer.isShortcodeContent(node)) { 12 var shortcode = CP_Customizer.getNodeShortcode(node); 13 var device = root.CP_Customizer.preview.currentDevice(); 14 var prop = ( device === "tablet") ? "columns_tablet" : "columns"; 15 shortcode.attrs = shortcode.attrs || {}; 16 shortcode.attrs[prop] = cols; 17 CP_Customizer.updateNodeFromShortcodeObject(node, shortcode); 18 } 19 }); 20 CP_Customizer.hooks.addFilter('is_shortcode_editable', function (value, shortcode) { 21 var controls = CP_Customizer.hooks.applyFilters('filter_shortcode_popup_controls', CP_Customizer.utils.deepClone(CP_Customizer.__shortcodesPopupControls)); 22 23 // console.log(controls, shortcode); 24 value = value || _.has(controls, shortcode.tag); 25 26 return value; 27 }); 28 29 30 CP_Customizer.getShortcodePopupFields = function (shortcodeData) { 31 var controls = CP_Customizer.hooks.applyFilters('filter_shortcode_popup_controls', CP_Customizer.utils.deepClone(CP_Customizer.__shortcodesPopupControls)); 32 var tag = _.isObject(shortcodeData) ? shortcodeData.tag : shortcodeData; 33 var attrs = controls[tag] || []; 34 var controls = []; 35 for (var attrName in attrs) { 36 if (attrs.hasOwnProperty(attrName)) { 37 var control = CP_Customizer.utils.deepClone(attrs[attrName].control); 38 39 if (shortcodeData.attrs && shortcodeData.attrs.hasOwnProperty(attrName)) { 40 control.value = shortcodeData.attrs[attrName]; 41 } else { 42 control.value = !_.isUndefined(control.default) ? control.default : ""; 43 } 44 45 if (control.getValue) { 46 control.value = control.getValue(attrName, shortcodeData.tag); 47 } 48 49 control.name = attrName; 50 control.id = attrName; 51 if (control.getParse) { 52 control.value = control.getParse(control.value); 53 } 54 controls.push(control); 55 } 56 } 57 return controls; 58 }; 59 60 CP_Customizer.openShortcodePopupEditor = function (callback, $node, shortcode) { 61 var popupContainer = $('#cp-container-editor'); 62 63 var fields = CP_Customizer.getShortcodePopupFields(shortcode); 64 65 var fallback = CP_Customizer.shortcodesAttrs && CP_Customizer.shortcodesAttrs[shortcode]; 66 67 if (!fields.length || fallback) { 68 return; 69 } 70 71 shortcode = { 72 tag: _.isObject(shortcode) ? shortcode.tag : shortcode, 73 attrs: _.isObject(shortcode) ? shortcode.attrs : {}, 74 }; 75 76 function setContent() { 77 for (var i = 0; i < fields.length; i++) { 78 var field = fields[i], 79 node = field.node; 80 var value = field.val(); 81 82 if (field.setValue) { 83 field.setValue(field.id, value, shortcode.tag); 84 } else { 85 shortcode.attrs[field.id] = field.setParse ? field.setParse(value) : value; 86 } 87 } 88 89 callback(shortcode.attrs); 90 CP_Customizer.closePopUps(); 91 CP_Customizer.updateState(); 92 } 93 94 95 popupContainer.find('[id="cp-item-ok"]').off().click(setContent); 96 popupContainer.find('[id="cp-item-cancel"]').off().click(function () { 97 CP_Customizer.closePopUps(); 98 }); 99 100 popupContainer.find('#cp-items').empty(); 101 for (var i = 0; i < fields.length; i++) { 102 var field = fields[i], 103 type = field.type || 'text', 104 content = (CP_Customizer.jsTPL[type] ? CP_Customizer.jsTPL[type](field) : ''); 105 106 var $fieldContent = $(content); 107 108 $fieldContent.attr('data-field', field.name); 109 110 111 field.$wrapper = $fieldContent; 112 field.$panel = popupContainer.find('#cp-items'); 113 field.$node = $node; 114 115 field.val = function () { 116 var value = {}; 117 var field = this; 118 var _values = this.$wrapper.find('[id^="' + field.id + '"]').filter('input,textarea,select').map(function (index, elem) { 119 return { 120 key: $(this).attr('id').replace(field.id + "__", ''), 121 value: $(this).is('[type=checkbox]') ? this.checked : $(this).val() 122 }; 123 }).toArray(); 124 125 _(_values).each(function (v) { 126 value[v.key] = v.value; 127 }); 128 129 if (_values.length === 1 && value.hasOwnProperty(field.id)) { 130 value = value[field.id]; 131 } 132 133 return value; 134 }; 135 136 if (field.ready && _.isFunction(field.ready)) { 137 $fieldContent.data('field', field); 138 $fieldContent.bind('shortcode-popup-ready', function () { 139 var $fieldContent = $(this); 140 var field = $fieldContent.data('field'); 141 field.ready($fieldContent, $fieldContent.closest('#cp-items')); 142 }); 143 144 145 } 146 147 popupContainer.find('#cp-items').append($fieldContent); 148 149 } 150 151 popupContainer.find('#cp-items').children().trigger('shortcode-popup-ready'); 152 CP_Customizer.popUp( window.CP_Customizer.translateCompanionString('Manage Options'), "cp-container-editor", { 153 width: "600", 154 class: "data-edit-popup" 155 }); 156 }; 157 158 159 CP_Customizer.editEscapedShortcodeAtts = function ($node, shortcode) { 160 CP_Customizer.openShortcodePopupEditor(function (attrs) { 161 shortcode.attrs = attrs; 162 163 var shortcodeText = '[' + shortcode.tag; 164 var attrs = []; 165 for (var attr in shortcode.attrs) { 166 if ((shortcode.attrs[attr] + "").trim() != "") { 167 168 attrs.push(attr + '="' + htmlEscape(htmlEscape(shortcode.attrs[attr])) + '"'); 169 } 170 } 171 172 if (attrs.length) { 173 shortcodeText += ' ' + attrs.join(' '); 174 } 175 176 shortcodeText += ']'; 177 178 CP_Customizer.updateNodeShortcode($node, shortcodeText); 179 180 }, $node, shortcode); 181 }; 182 183 function htmlEscape(str) { 184 return str 185 .replace(/&/g, '&') 186 .replace(/"/g, '"') 187 .replace(/'/g, ''') 188 .replace(/</g, '<') 189 .replace(/>/g, '>'); 190 } 191 })(window, CP_Customizer, jQuery); 192