| 1 |
/** |
| 2 |
* Script run inside a Customizer control sidebar |
| 3 |
*/ |
| 4 |
(function($) { |
| 5 |
// An empty setting means "unset" — the control emits no CSS and the |
| 6 |
// stylesheet's own value stands. That state is not representable in an |
| 7 |
// <input type="range">: the HTML value-sanitization algorithm turns a |
| 8 |
// non-numeric value into min + (max-min)/2, so an unset control would park |
| 9 |
// its thumb on a meaningless midpoint. Controls that can be unset therefore |
| 10 |
// ship a `data-unset-val` holding the value the stylesheet actually paints. |
| 11 |
// Sliders with a numeric default have no such attribute and are untouched by |
| 12 |
// any of the branches below. |
| 13 |
var isUnset = function($range) { |
| 14 |
var id = $range.attr('data-customize-setting-link'); |
| 15 |
if (!id || !wp.customize.has(id)) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
var val = wp.customize(id).get(); |
| 19 |
return val === '' || val === null || typeof val === 'undefined'; |
| 20 |
}; |
| 21 |
|
| 22 |
// Park the thumb on the unset fallback without touching the setting. |
| 23 |
// Assigning .value directly fires no change event, so nothing is published |
| 24 |
// and the control keeps emitting zero CSS. |
| 25 |
var applyUnsetVal = function($range) { |
| 26 |
var unset = $range.attr('data-unset-val'); |
| 27 |
if (typeof unset !== 'undefined' && isUnset($range)) { |
| 28 |
$range.val(unset); |
| 29 |
return true; |
| 30 |
} |
| 31 |
return false; |
| 32 |
}; |
| 33 |
|
| 34 |
wp.customize.bind('ready', function() { |
| 35 |
rangeSlider(); |
| 36 |
|
| 37 |
var rangeValReset = jQuery('.betterdocs-customizer-reset.betterdocs-range-value'); |
| 38 |
rangeValReset.each(function() { |
| 39 |
$(rangeValReset).on( 'click', function (e) { |
| 40 |
e.preventDefault(); |
| 41 |
var nextRangeselector = $(this).next('.betterdcos-range-slider'); |
| 42 |
var nextRangeDefaultVal = $(this).next('.betterdcos-range-slider').data('default-val'); |
| 43 |
var $range = nextRangeselector.find('.betterdcos-range-slider__range'); |
| 44 |
var suffix = $range.attr('suffix') ? $range.attr('suffix') : ''; |
| 45 |
|
| 46 |
if (nextRangeDefaultVal === '' || nextRangeDefaultVal === null || typeof nextRangeDefaultVal === 'undefined') { |
| 47 |
// Round-tripping '' through the range input would coerce it to |
| 48 |
// the midpoint and then publish that number, so Reset would |
| 49 |
// apply styling instead of clearing it. Write the setting |
| 50 |
// directly and re-park the thumb on the stylesheet value. |
| 51 |
var id = $range.attr('data-customize-setting-link'); |
| 52 |
if (id && wp.customize.has(id)) { |
| 53 |
wp.customize(id).set(''); |
| 54 |
} |
| 55 |
var unset = $range.attr('data-unset-val'); |
| 56 |
if (typeof unset !== 'undefined') { |
| 57 |
$range.val(unset); |
| 58 |
} |
| 59 |
nextRangeselector.find('.betterdcos-range-slider__value').html($range.val() + suffix); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$range.val(nextRangeDefaultVal).trigger('change'); |
| 64 |
nextRangeselector.find('.betterdcos-range-slider__value').html(nextRangeDefaultVal + suffix); |
| 65 |
|
| 66 |
}); |
| 67 |
}); |
| 68 |
}); |
| 69 |
|
| 70 |
var rangeSlider = function() { |
| 71 |
var slider = $('.betterdcos-range-slider'), |
| 72 |
range = $('.betterdcos-range-slider__range'), |
| 73 |
value = $('.betterdcos-range-slider__value'); |
| 74 |
|
| 75 |
slider.each(function() { |
| 76 |
|
| 77 |
value.each(function() { |
| 78 |
var $range = $(this).prev(); |
| 79 |
// WP core's linkElements() has already pushed the saved setting |
| 80 |
// into the input by the time 'ready' fires, so read the live value |
| 81 |
// rather than the rendered attribute — otherwise an unset control |
| 82 |
// renders a bare suffix with no number in front of it. |
| 83 |
applyUnsetVal($range); |
| 84 |
var value = $range.val(); |
| 85 |
var suffix = ($range.attr('suffix')) ? $range.attr('suffix') : ''; |
| 86 |
$(this).html(value + suffix); |
| 87 |
}); |
| 88 |
|
| 89 |
range.on('input', function() { |
| 90 |
var suffix = ($(this).attr('suffix')) ? $(this).attr('suffix') : ''; |
| 91 |
$(this).next(value).html(this.value + suffix ); |
| 92 |
}); |
| 93 |
}); |
| 94 |
}; |
| 95 |
|
| 96 |
})(jQuery); |
| 97 |
|