ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

customizer-custom-popup.js (5130B)


      1 (function (root, CP_Customizer, $) {
      2 
      3     CP_Customizer.__customPopupControls = {};
      4 
      5     CP_Customizer.registerCustomPopupControls = function (tag, controls) {
      6         CP_Customizer.__customPopupControls[tag] = controls;
      7     };
      8 
      9 
     10     CP_Customizer.getCustomPopupFields = function (tag, $node) {
     11         var controls = CP_Customizer.hooks.applyFilters('filter_custom_popup_controls', CP_Customizer.utils.deepClone(CP_Customizer.__customPopupControls));
     12         var attrs = controls[tag] || [];
     13         var controls = [];
     14         for (var attrName in attrs) {
     15             if (attrs.hasOwnProperty(attrName)) {
     16                 var control = CP_Customizer.utils.deepClone(attrs[attrName].control);
     17 
     18 
     19                 if (_.isFunction(control.active)) {
     20                     if (!control.active($node)) {
     21                         continue;
     22                     }
     23                 }
     24 
     25                 if (control.attr) {
     26                     control.value = $node.attr(control.attr);
     27                 }
     28 
     29                 if (control.getValue) {
     30                     control.value = control.getValue(attrName, $node, control.default);
     31                 }
     32 
     33                 if (!control.value && control.default) {
     34                     control.value = control.default;
     35                 }
     36 
     37                 control.name = attrName;
     38                 control.id = attrName;
     39                 if (control.getParse) {
     40                     control.value = control.getParse(control.value);
     41                 }
     42                 controls.push(control);
     43             }
     44         }
     45         return controls;
     46     };
     47 
     48     CP_Customizer.openCustomPopupEditor = function ($node, tag, callback) {
     49         var popupContainer = $('#cp-container-editor');
     50 
     51         var fields = CP_Customizer.getCustomPopupFields(tag, $node);
     52 
     53 
     54         function setContent() {
     55             var values = {};
     56             for (var i = 0; i < fields.length; i++) {
     57                 var field = fields[i],
     58                     value = field.val();
     59 
     60                 values[field.id] = {
     61                     oldValue: field.value,
     62                     value: value
     63                 };
     64 
     65                 if (field.attr) {
     66                     $node.attr(field.attr, value);
     67                 }
     68 
     69             }
     70 
     71             if (callback) {
     72                 callback(values, $node);
     73             }
     74 
     75             CP_Customizer.closePopUps();
     76             CP_Customizer.updateState();
     77         }
     78 
     79 
     80         popupContainer.find('[id="cp-item-ok"]').off().click(setContent);
     81         popupContainer.find('[id="cp-item-cancel"]').off().click(function () {
     82             CP_Customizer.closePopUps();
     83         });
     84 
     85         popupContainer.find('#cp-items').empty();
     86         for (var i = 0; i < fields.length; i++) {
     87             var field = fields[i],
     88                 type = field.type || 'text',
     89                 content = (CP_Customizer.jsTPL[type] ? CP_Customizer.jsTPL[type](field) : '');
     90 
     91             var $fieldContent = $(content);
     92 
     93             $fieldContent.attr('data-field', field.name);
     94 
     95 
     96             field.$wrapper = $fieldContent;
     97             field.$panel = popupContainer.find('#cp-items');
     98             field.$node = $node;
     99 
    100             field.val = function () {
    101                 var value = {};
    102                 var field = this;
    103                 var _values = this.$wrapper.find('[id^="' + field.id + '"]').filter('input,textarea,select').map(function (index, elem) {
    104                     return {
    105                         key: $(this).attr('id').replace(field.id + "__", ''),
    106                         value: $(this).is('[type=checkbox]') ? this.checked : $(this).val()
    107                     };
    108                 }).toArray();
    109 
    110                 _(_values).each(function (v) {
    111                     value[v.key] = v.value;
    112                 });
    113 
    114                 if (_values.length === 1 && value.hasOwnProperty(field.id)) {
    115                     value = value[field.id];
    116                 }
    117 
    118                 return value;
    119             };
    120 
    121             if (field.ready && _.isFunction(field.ready)) {
    122                 $fieldContent.data('field', field);
    123                 $fieldContent.bind('shortcode-popup-ready', function () {
    124                     var $fieldContent = $(this);
    125                     var field = $fieldContent.data('field');
    126                     field.ready($fieldContent, $fieldContent.closest('#cp-items'));
    127                 });
    128 
    129 
    130             }
    131 
    132             popupContainer.find('#cp-items').append($fieldContent);
    133 
    134         }
    135 
    136         popupContainer.find('#cp-items').children().trigger('shortcode-popup-ready');
    137         CP_Customizer.popUp(window.CP_Customizer.translateCompanionString('Manage Options'), "cp-container-editor", {
    138             width: "600",
    139             class: "data-edit-popup"
    140         });
    141     };
    142 
    143 
    144     function htmlEscape(str) {
    145         return str
    146             .replace(/&/g, '&amp;')
    147             .replace(/"/g, '&quot;')
    148             .replace(/'/g, '&#39;')
    149             .replace(/</g, '&lt;')
    150             .replace(/>/g, '&gt;');
    151     }
    152 })(window, CP_Customizer, jQuery);