| 1 |
/** |
| 2 |
* Copyright (c) Loft.Ocean |
| 3 |
* http://www.loftocean.com |
| 4 |
*/ |
| 5 |
|
| 6 |
(function(api, $){ |
| 7 |
$('head').append($('<style>', {'id': 'loftloader-hide-site-title', 'text': '.site-title { opacity: 0; }'})); |
| 8 |
// Main Switch section |
| 9 |
api.LoftLoaderSwitchSection = api.Section.extend({ |
| 10 |
initialize: function () { |
| 11 |
return api.Section.prototype.initialize.apply( this, arguments ); |
| 12 |
}, |
| 13 |
ready: function(){ |
| 14 |
var checked = this.container.find('input[name=loftloader-main-switch]').attr('checked') ? true : false; |
| 15 |
checked ? '' : $('#customize-theme-controls').addClass('loftloader-settings-disabled'); |
| 16 |
$('#customize-theme-controls').addClass('loftloader-controls-wrapper'); |
| 17 |
}, |
| 18 |
attachEvents: function () { |
| 19 |
var container = this.container; |
| 20 |
container.on('change', 'input[name=loftloader-main-switch]', function(e){ |
| 21 |
var checked = $(this).attr('checked') ? true : false, |
| 22 |
$element = container.find('#customize-control-loftloader_main_switch input[type=checkbox]'), |
| 23 |
controls_wrap = $('#customize-theme-controls'); |
| 24 |
|
| 25 |
checked ? $element.attr('checked', 'checked') : $element.removeAttr('checked'); |
| 26 |
$element.trigger('change'); |
| 27 |
checked ? controls_wrap.removeClass('loftloader-settings-disabled') : controls_wrap.addClass('loftloader-settings-disabled'); |
| 28 |
}); |
| 29 |
} |
| 30 |
}); |
| 31 |
$.extend(api.sectionConstructor, {loftloader_switch: api.LoftLoaderSwitchSection}); |
| 32 |
|
| 33 |
// Slider control |
| 34 |
api.controlConstructor.slider = api.Control.extend({ |
| 35 |
ready: function(){ |
| 36 |
var elem = this.container.find('.loader-ui-slider'), |
| 37 |
input = this.container.find('input[data-customize-setting-link]'); |
| 38 |
elem.slider({ |
| 39 |
'range': 'min', |
| 40 |
'min': elem.data('min'), |
| 41 |
'max': elem.data('max'), |
| 42 |
'value': elem.data('value'), |
| 43 |
'step': elem.data('step'), |
| 44 |
'slide': function(event, ui){ |
| 45 |
input.val(ui.value).trigger('change'); |
| 46 |
} |
| 47 |
}); |
| 48 |
} |
| 49 |
}); |
| 50 |
|
| 51 |
// Register event handler for hide controls/description |
| 52 |
api.bind('ready', function(e){ |
| 53 |
// Change the site title in string "You are customizing ..." |
| 54 |
loftloader_lite_i18n ? $('.site-title').text(loftloader_lite_i18n.name) : ''; |
| 55 |
$('#loftloader-hide-site-title').remove(); |
| 56 |
|
| 57 |
// Init for loader 2.0 customizer, when sync from lower version |
| 58 |
var settings = api.get(), |
| 59 |
$custom_img = $('#customize-control-loftloader_custom_img'); |
| 60 |
if($custom_img.length && !$custom_img.find('.attachment-thumb').length && settings && settings['loftloader_custom_img']){ |
| 61 |
var image = settings['loftloader_custom_img']; |
| 62 |
if(image){ |
| 63 |
var $container = $custom_img.find('.attachment-media-view').addClass('attachment-media-view-image'), |
| 64 |
$image = $('<div>', {'class': "thumbnail thumbnail-image"}).append($('<img>', {'class': "attachment-thumb", 'src': image})); |
| 65 |
|
| 66 |
$container.children('.placeholder').css('display', 'none').after($image).remove(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$('body') |
| 71 |
.on('change', 'input[type=number]', function(e){ |
| 72 |
var val = parseInt($(this).val()), |
| 73 |
min = $(this).attr('min') ? parseInt($(this).attr('min')) : 1; |
| 74 |
(val < min) ? $(this).val(min).trigger('change') : ''; |
| 75 |
}) |
| 76 |
.on('change', 'input.loftlader-checkbox', function(e){ |
| 77 |
var checked = $(this).attr('checked') ? true : false, |
| 78 |
$element = $(this).siblings('input'); |
| 79 |
if($element.length){ |
| 80 |
checked ? $element.attr('checked', 'checked') : $element.removeAttr('checked'); |
| 81 |
$element.trigger('change'); |
| 82 |
} |
| 83 |
}) |
| 84 |
.on('click', '.customize-more-toggle', function(e){ |
| 85 |
e.preventDefault(); |
| 86 |
var self = $(this), |
| 87 |
description = $(this).siblings('.customize-control-description'); |
| 88 |
|
| 89 |
if(description.length){ |
| 90 |
self.hasClass('expanded') ? description.slideUp('slow') : description.slideDown('slow', function(){ $(this).css('display', 'block'); }); |
| 91 |
self.toggleClass('expanded'); |
| 92 |
} |
| 93 |
}) |
| 94 |
.on('click', '.loftloader-any-page-generate', function(e){ |
| 95 |
e.preventDefault(); |
| 96 |
var shortcode = api.loftloader_generate_parameters(); |
| 97 |
$(this).siblings('.loftloader-any-page-shortcode').val('[loftloader ' + shortcode + ']').select(); |
| 98 |
}); |
| 99 |
}); |
| 100 |
|
| 101 |
/** |
| 102 |
* Convert to string 'on' if current value is boolean true (not other value equals to true) |
| 103 |
* @param value mix, the value to check |
| 104 |
* @return mix, string 'on' if current value equals to boolean true, otherwise return the original value. |
| 105 |
*/ |
| 106 |
function loftloader_check_boolean(value){ |
| 107 |
return (value === true) ? 'on' : value; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Get customize setting value |
| 111 |
* @param id string, the setting id |
| 112 |
* @return mix, return dirty value/setting value or false (the id not exists) |
| 113 |
**/ |
| 114 |
function loftloader_get_setting_value(id){ |
| 115 |
var settings = api.get(), //settings.settings, dirty_values = api.dirtyValues(), |
| 116 |
value = settings[id] ? settings[id] : false; //dirty_values[id] ? dirty_values[id] : (settings[id] ? settings[id]['value'] : false); |
| 117 |
if(typeof value === 'string'){ |
| 118 |
value = value.trim(); |
| 119 |
} |
| 120 |
return loftloader_check_boolean(value); |
| 121 |
} |
| 122 |
|
| 123 |
// Generate loftloader parameters |
| 124 |
api.loftloader_generate_parameters = function(){ |
| 125 |
var dependency = { |
| 126 |
'loftloader_bg_color': {}, |
| 127 |
'loftloader_bg_opacity': {}, |
| 128 |
'loftloader_bg_animation': {}, |
| 129 |
'loftloader_loader_type': { |
| 130 |
'sun': [ |
| 131 |
'loftloader_loader_color' |
| 132 |
], |
| 133 |
'circles': [ |
| 134 |
'loftloader_loader_color' |
| 135 |
], |
| 136 |
'wave': [ |
| 137 |
'loftloader_loader_color' |
| 138 |
], |
| 139 |
'square': [ |
| 140 |
'loftloader_loader_color' |
| 141 |
], |
| 142 |
'frame': [ |
| 143 |
'loftloader_loader_color', |
| 144 |
'loftloader_custom_img' |
| 145 |
], |
| 146 |
'imgloading': [ |
| 147 |
'loftloader_custom_img', |
| 148 |
'loftloader_img_width' |
| 149 |
] |
| 150 |
} |
| 151 |
}; |
| 152 |
var loftloader = type_value = loop = ''; |
| 153 |
if(loftloader_get_setting_value('loftloader_main_switch') === 'on'){ |
| 154 |
loftloader = 'loftloader_main_switch=on loftloader_show_range=sitewide'; |
| 155 |
for(var id in dependency){ |
| 156 |
type_value = loftloader_get_setting_value(id); |
| 157 |
switch(id){ |
| 158 |
case 'loftloader_bg_color': |
| 159 |
case 'loftloader_bg_opacity': |
| 160 |
case 'loftloader_bg_animation': |
| 161 |
loop = []; |
| 162 |
break; |
| 163 |
default: |
| 164 |
loop = dependency[id][type_value] ? dependency[id][type_value] : []; |
| 165 |
} |
| 166 |
loftloader += ' ' + id + '=' + type_value; |
| 167 |
if(loop){ |
| 168 |
for(var j in loop){ |
| 169 |
loftloader += ' ' + loop[j] + '=' + loftloader_get_setting_value(loop[j]); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
else{ |
| 175 |
loftloader = 'loftloader_main_switch=false'; |
| 176 |
} |
| 177 |
return loftloader; |
| 178 |
} |
| 179 |
})(wp.customize, jQuery); |
| 180 |
|