| 1 |
( |
| 2 |
function( $, exports, wp ) { |
| 3 |
var api = wp.customize; |
| 4 |
var $window = $( window ); |
| 5 |
|
| 6 |
wp.customize.bind( 'ready', function() { |
| 7 |
|
| 8 |
// Handle the Style Manager user feedback logic. |
| 9 |
var $styleManagerUserFeedbackModal = $('#style-manager-user-feedback-modal'); |
| 10 |
if ( $styleManagerUserFeedbackModal.length ) { |
| 11 |
var $styleManagerUserFeedbackForm = $styleManagerUserFeedbackModal.find('form'), |
| 12 |
$styleManagerUserFeedbackCloseBtn = $styleManagerUserFeedbackModal.find('.close'), |
| 13 |
$styleManagerUserFeedbackFirstStep = $styleManagerUserFeedbackModal.find('.first-step'), |
| 14 |
$styleManagerUserFeedbackSecondStep = $styleManagerUserFeedbackModal.find('.second-step'), |
| 15 |
$styleManagerUserFeedbackThanksStep = $styleManagerUserFeedbackModal.find('.thanks-step'), |
| 16 |
$styleManagerUserFeedbackErrorStep = $styleManagerUserFeedbackModal.find('.error-step'), |
| 17 |
styleManagerUserFeedbackModalShown = false, |
| 18 |
styleManagerColorPaletteChanged = false; |
| 19 |
|
| 20 |
// Handle when to open the modal. |
| 21 |
api.bind('saved', function () { |
| 22 |
// We will only show the modal once per Customizer session. |
| 23 |
if (!styleManagerUserFeedbackModalShown && styleManagerColorPaletteChanged) { |
| 24 |
$('body').addClass('modal-open'); |
| 25 |
styleManagerUserFeedbackModalShown = true; |
| 26 |
} |
| 27 |
}); |
| 28 |
|
| 29 |
// Handle the color palette changed info update. |
| 30 |
const colorPaletteSetting = api( 'sm_color_palette' ); |
| 31 |
if ( !_.isUndefined(colorPaletteSetting) ) { |
| 32 |
colorPaletteSetting.bind( function( new_value, old_value ) { |
| 33 |
if ( new_value != old_value ) { |
| 34 |
styleManagerColorPaletteChanged = true; |
| 35 |
} |
| 36 |
} ) |
| 37 |
} |
| 38 |
const colorPaletteVariationSetting = api( 'sm_color_palette_variation' ); |
| 39 |
if ( !_.isUndefined(colorPaletteVariationSetting) ) { |
| 40 |
colorPaletteVariationSetting.bind( function( new_value, old_value ) { |
| 41 |
if ( new_value != old_value ) { |
| 42 |
styleManagerColorPaletteChanged = true; |
| 43 |
} |
| 44 |
} ) |
| 45 |
} |
| 46 |
|
| 47 |
// Handle the modal submit. |
| 48 |
$styleManagerUserFeedbackForm.on('submit', function (event) { |
| 49 |
event.preventDefault(); |
| 50 |
|
| 51 |
let $form = $(event.target); |
| 52 |
|
| 53 |
let data = { |
| 54 |
action: 'customify_style_manager_user_feedback', |
| 55 |
nonce: customify_settings.style_manager_user_feedback_nonce, |
| 56 |
type: $form.find('input[name=type]').val(), |
| 57 |
rating: $form.find('input[name=rating]:checked').val(), |
| 58 |
message: $form.find('textarea[name=message]').val() |
| 59 |
}; |
| 60 |
|
| 61 |
$.post( |
| 62 |
customify_settings.ajax_url, |
| 63 |
data, |
| 64 |
function (response) { |
| 65 |
if (true === response.success) { |
| 66 |
$styleManagerUserFeedbackFirstStep.hide(); |
| 67 |
$styleManagerUserFeedbackSecondStep.hide(); |
| 68 |
$styleManagerUserFeedbackThanksStep.show(); |
| 69 |
$styleManagerUserFeedbackErrorStep.hide(); |
| 70 |
} else { |
| 71 |
$styleManagerUserFeedbackFirstStep.hide(); |
| 72 |
$styleManagerUserFeedbackSecondStep.hide(); |
| 73 |
$styleManagerUserFeedbackThanksStep.hide(); |
| 74 |
$styleManagerUserFeedbackErrorStep.show(); |
| 75 |
} |
| 76 |
} |
| 77 |
); |
| 78 |
}); |
| 79 |
|
| 80 |
$styleManagerUserFeedbackForm.find('input[name=rating]').on('change', function (event) { |
| 81 |
// Leave everything in working order |
| 82 |
setTimeout(function () { |
| 83 |
$styleManagerUserFeedbackSecondStep.show(); |
| 84 |
}, 300); |
| 85 |
|
| 86 |
let rating = $styleManagerUserFeedbackForm.find('input[name=rating]:checked').val(); |
| 87 |
|
| 88 |
$styleManagerUserFeedbackForm.find('.rating-placeholder').text(rating); |
| 89 |
}); |
| 90 |
|
| 91 |
$styleManagerUserFeedbackCloseBtn.on('click', function (event) { |
| 92 |
event.preventDefault(); |
| 93 |
|
| 94 |
$('body').removeClass('modal-open'); |
| 95 |
|
| 96 |
// Leave everything in working order |
| 97 |
setTimeout(function () { |
| 98 |
$styleManagerUserFeedbackFirstStep.show(); |
| 99 |
$styleManagerUserFeedbackSecondStep.hide(); |
| 100 |
$styleManagerUserFeedbackThanksStep.hide(); |
| 101 |
$styleManagerUserFeedbackErrorStep.hide(); |
| 102 |
}, 300); |
| 103 |
}); |
| 104 |
} |
| 105 |
} ); |
| 106 |
} |
| 107 |
)( jQuery, window, wp ); |
| 108 |
|
| 109 |
|
| 110 |
// Reverses a hex color to either black or white |
| 111 |
function customifyInverseHexColorToBlackOrWhite( hex ) { |
| 112 |
return customifyInverseHexColor( hex, true ); |
| 113 |
} |
| 114 |
|
| 115 |
// Taken from here: https://stackoverflow.com/a/35970186/6260836 |
| 116 |
function customifyInverseHexColor( hex, bw ) { |
| 117 |
if ( hex.indexOf( '#' ) === 0 ) { |
| 118 |
hex = hex.slice( 1 ); |
| 119 |
} |
| 120 |
// convert 3-digit hex to 6-digits. |
| 121 |
if ( hex.length === 3 ) { |
| 122 |
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; |
| 123 |
} |
| 124 |
if ( hex.length !== 6 ) { |
| 125 |
throw new Error( 'Invalid HEX color.' ); |
| 126 |
} |
| 127 |
var r = parseInt( hex.slice( 0, 2 ), 16 ), |
| 128 |
g = parseInt( hex.slice( 2, 4 ), 16 ), |
| 129 |
b = parseInt( hex.slice( 4, 6 ), 16 ); |
| 130 |
if ( bw ) { |
| 131 |
// http://stackoverflow.com/a/3943023/112731 |
| 132 |
return ( |
| 133 |
r * 0.299 + g * 0.587 + b * 0.114 |
| 134 |
) > 186 |
| 135 |
? '#000000' |
| 136 |
: '#FFFFFF'; |
| 137 |
} |
| 138 |
// invert color components |
| 139 |
r = ( |
| 140 |
255 - r |
| 141 |
).toString( 16 ); |
| 142 |
g = ( |
| 143 |
255 - g |
| 144 |
).toString( 16 ); |
| 145 |
b = ( |
| 146 |
255 - b |
| 147 |
).toString( 16 ); |
| 148 |
// pad each with zeros and return |
| 149 |
return "#" + customifyPadZero( r ) + customifyPadZero( g ) + customifyPadZero( b ); |
| 150 |
} |
| 151 |
|
| 152 |
function customifyPadZero( str, len ) { |
| 153 |
len = len || 2; |
| 154 |
var zeros = new Array( len ).join( '0' ); |
| 155 |
return ( |
| 156 |
zeros + str |
| 157 |
).slice( - len ); |
| 158 |
} |
| 159 |
|