| 1 |
/* |
| 2 |
Cooked Javascript Functions |
| 3 |
|
| 4 |
Contents: |
| 5 |
1. Variables |
| 6 |
2a. Cooked Gallery Callback |
| 7 |
2b. Cooked Gallery Init |
| 8 |
3. Ingredients |
| 9 |
4. Servings Switcher |
| 10 |
5. Browse Search Button |
| 11 |
6. Timers |
| 12 |
7. Full-Screen Mode |
| 13 |
*/ |
| 14 |
|
| 15 |
var cooked_loading = false; |
| 16 |
|
| 17 |
(function($) { |
| 18 |
|
| 19 |
/**** 1. Variables ****/ |
| 20 |
$_Cooked_Ingredient_Boxes = $('.cooked-ingredient-checkbox'); |
| 21 |
$_Cooked_Fotorama = $('.cooked-recipe-gallery'); |
| 22 |
$_Cooked_Ajax_List = $('.cooked-recipe-loader'); |
| 23 |
$_Cooked_Recipe_Search = $('.cooked-recipe-search'); |
| 24 |
$_Cooked_Timers = $('.cooked-timer > a'); |
| 25 |
$_Cooked_FSM_Button = $('.cooked-fsm-button'); |
| 26 |
|
| 27 |
/**** 2a. Cooked Gallery ****/ |
| 28 |
|
| 29 |
if ( $_Cooked_Fotorama.length ) { |
| 30 |
$_Cooked_Fotorama.on('fotorama:ready', function(e, fotorama){ |
| 31 |
setTimeout(function(){ |
| 32 |
$_Cooked_Fotorama.addClass('cooked-gallery-loaded'); |
| 33 |
},100); |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
$(document).ready(function() { |
| 38 |
|
| 39 |
/**** 2b. Cooked Gallery ****/ |
| 40 |
|
| 41 |
if ( $_Cooked_Fotorama.length ) { |
| 42 |
$_Cooked_Fotorama.fotorama(); |
| 43 |
} |
| 44 |
|
| 45 |
/**** 3. Ingredients ****/ |
| 46 |
|
| 47 |
if ( $_Cooked_Ingredient_Boxes.length ) { |
| 48 |
|
| 49 |
init_cooked_ingredients($_Cooked_Ingredient_Boxes); |
| 50 |
|
| 51 |
function init_cooked_ingredients( Cooked_Ingredient_Boxes ){ |
| 52 |
Cooked_Ingredient_Boxes.on( 'click',function(e){ |
| 53 |
var thisCheckbox = $(this); |
| 54 |
if ( thisCheckbox.hasClass( 'cooked-checked' ) ){ |
| 55 |
thisCheckbox.parent().removeClass( 'cooked-checked' ); |
| 56 |
thisCheckbox.removeClass( 'cooked-checked' ); |
| 57 |
} else { |
| 58 |
thisCheckbox.parent().addClass( 'cooked-checked' ); |
| 59 |
thisCheckbox.addClass( 'cooked-checked' ); |
| 60 |
} |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/**** 4. Servings Switcher ****/ |
| 67 |
|
| 68 |
if ( $('.cooked-servings').length ) { |
| 69 |
var servingsSelectField = $('.cooked-servings').find('select'); |
| 70 |
servingsSelectField.on('change', function(e) { |
| 71 |
e.preventDefault(); |
| 72 |
var servings = $(this).children("option:selected").val(); |
| 73 |
const url = new URL(window.location.href); |
| 74 |
url.searchParams.set('servings', servings); |
| 75 |
window.location.href = url.toString(); |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
/**** 5. Browse Search Button ****/ |
| 80 |
|
| 81 |
if ( $_Cooked_Recipe_Search.length ) { |
| 82 |
$('body').on( 'click',function(e) { |
| 83 |
var thisButton = false; |
| 84 |
|
| 85 |
// Did someone click the Browse Button in one way or another? |
| 86 |
if ( $('.cooked-browse-select').has(e.target).length > 0 ) { |
| 87 |
thisButton = $(e.target).parents('.cooked-browse-select'); |
| 88 |
} else if ( $(e.target).hasClass('cooked-browse-select') ) { |
| 89 |
thisButton = $(e.target); |
| 90 |
} |
| 91 |
|
| 92 |
// Yep, they clicked the button! |
| 93 |
if (thisButton) { |
| 94 |
if (thisButton.hasClass('cooked-active') && $(e.target).hasClass('cooked-browse-select') || thisButton.hasClass('cooked-active') && $(e.target).hasClass('cooked-field-title')) { |
| 95 |
thisButton.removeClass('cooked-active'); |
| 96 |
} else { |
| 97 |
thisButton.addClass('cooked-active'); |
| 98 |
} |
| 99 |
|
| 100 |
// Nope, they clicked something else. |
| 101 |
} else { |
| 102 |
$('.cooked-browse-select').removeClass('cooked-active'); |
| 103 |
} |
| 104 |
}); |
| 105 |
|
| 106 |
var browseSearchButton = $('.cooked-browse-search-button'); |
| 107 |
browseSearchButton.on('click', function(e) { |
| 108 |
e.preventDefault(); |
| 109 |
var thisButton = $(this); |
| 110 |
thisButton.parents('form').trigger('submit'); |
| 111 |
}); |
| 112 |
|
| 113 |
// Add form submit handler for the Browse Search Form |
| 114 |
if ( cooked_js_vars.permalink_structure ) { |
| 115 |
$('.cooked-recipe-search form').on('submit', function(e) { |
| 116 |
e.preventDefault(); |
| 117 |
|
| 118 |
// Get form values |
| 119 |
const formValues = { |
| 120 |
category: $(this).find('[name="cp_recipe_category"]').val() || '', |
| 121 |
method: $(this).find('[name="cp_recipe_cooking_method"]').val() || '', |
| 122 |
cuisine: $(this).find('[name="cp_recipe_cuisine"]').val() || '', |
| 123 |
tags: $(this).find('[name="cp_recipe_tags"]').val() || '', |
| 124 |
diet: $(this).find('[name="cp_recipe_diet"]').val() || '', |
| 125 |
search: $(this).find('[name="cooked_search_s"]').val() || '', |
| 126 |
sort: $(this).find('[name="cooked_browse_sort_by"]').val() || 'date_desc', |
| 127 |
}; |
| 128 |
|
| 129 |
// Create URL segments |
| 130 |
const urlSegments = []; |
| 131 |
urlSegments.push(cooked_js_vars.browse_recipes_slug); |
| 132 |
|
| 133 |
// Add taxonomy segments |
| 134 |
const taxonomyFields = [ |
| 135 |
{ value: formValues.category, prefix: cooked_js_vars.recipe_category_slug }, |
| 136 |
{ value: formValues.method, prefix: cooked_js_vars.recipe_cooking_method_slug }, |
| 137 |
{ value: formValues.cuisine, prefix: cooked_js_vars.recipe_cuisine_slug }, |
| 138 |
{ value: formValues.tags, prefix: cooked_js_vars.recipe_tags_slug }, |
| 139 |
{ value: formValues.diet, prefix: cooked_js_vars.recipe_diet_slug }, |
| 140 |
]; |
| 141 |
|
| 142 |
taxonomyFields.forEach(field => { |
| 143 |
if (field.value) { |
| 144 |
urlSegments.push(`${field.prefix}/${encodeURIComponent(field.value)}`); |
| 145 |
} |
| 146 |
}); |
| 147 |
|
| 148 |
// Add search segment |
| 149 |
if (formValues.search) { |
| 150 |
urlSegments.push(`search/${encodeURIComponent(formValues.search)}`); |
| 151 |
} |
| 152 |
|
| 153 |
// Add sort segment |
| 154 |
urlSegments.push(`sort/${encodeURIComponent(formValues.sort)}`); |
| 155 |
|
| 156 |
// Build and navigate to URL |
| 157 |
const prettyUrl = '/' + urlSegments.filter(Boolean).join('/'); |
| 158 |
|
| 159 |
window.location.href = prettyUrl; |
| 160 |
}); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/**** 6. Timers ****/ |
| 165 |
function init_cooked_timers(Cooked_Timers) { |
| 166 |
Cooked_Timers.on('click', function(e) { |
| 167 |
e.preventDefault(); |
| 168 |
|
| 169 |
var thisTimer = $(this), |
| 170 |
timerID = 'cookedTimer-' + thisTimer.data('timer-id'), |
| 171 |
totalTimers = $('#cooked-timers-wrap').find('.cooked-timer-block').length, |
| 172 |
visibleClass, newHeight; |
| 173 |
|
| 174 |
// This timer is already here, let's just flash it. |
| 175 |
if ($('div#' + timerID).length) { |
| 176 |
$('div#' + timerID).css({ 'background' : '#eeeeee' }); |
| 177 |
setTimeout(function() { |
| 178 |
$('div#' + timerID).css({ 'background' : '' }); |
| 179 |
}, 200); |
| 180 |
|
| 181 |
return; |
| 182 |
} else { |
| 183 |
// Only 4 timers allowed at a time. |
| 184 |
if (totalTimers == 4) { |
| 185 |
$('#cooked-timers-wrap').css({ 'transform' : 'translate3d(0,0.5em,0)' }); |
| 186 |
setTimeout(function() { |
| 187 |
$('#cooked-timers-wrap').css({ 'transform' : '' }); |
| 188 |
},200); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
// Okay we're good to go, let's add this timer! |
| 193 |
totalTimers = totalTimers + 1; |
| 194 |
newHeight = totalTimers * 7.5; |
| 195 |
if (thisTimer.parents('.cooked-single-direction').length) { |
| 196 |
var thisStep = thisTimer.parents('.cooked-single-direction').data('step'); |
| 197 |
} else { |
| 198 |
var thisStep = cooked_js_vars.i18n_timer; |
| 199 |
} |
| 200 |
|
| 201 |
var Timer = { |
| 202 |
id: timerID, |
| 203 |
seconds: thisTimer.data('seconds'), |
| 204 |
step: thisStep, |
| 205 |
desc: thisTimer.data('desc') |
| 206 |
}; |
| 207 |
|
| 208 |
// Timers wrap already there? |
| 209 |
if ($('#cooked-timers-wrap').length) { |
| 210 |
if (totalTimers == 1) { |
| 211 |
visibleClass = ' cooked-visible'; |
| 212 |
} else { |
| 213 |
visibleClass = ''; |
| 214 |
} |
| 215 |
|
| 216 |
if (totalTimers > multiplesTrigger) { |
| 217 |
$('#cooked-timers-wrap').addClass('cooked-multiples'); |
| 218 |
} else { |
| 219 |
$('#cooked-timers-wrap').removeClass('cooked-multiples'); |
| 220 |
} |
| 221 |
|
| 222 |
$('#cooked-timers-wrap').addClass('cooked-visible'); |
| 223 |
let timerBlock = cookedTimerBlock(Timer, visibleClass); |
| 224 |
$('#cooked-timers-wrap').append(timerBlock); |
| 225 |
|
| 226 |
var thisTimerObj = $('#' + Timer.id).find('.cooked-timer-obj'); |
| 227 |
|
| 228 |
cookedTimer(thisTimerObj, false); |
| 229 |
|
| 230 |
setTimeout(function() { |
| 231 |
$('#cooked-timers-wrap').css({ 'height' : newHeight + 'em' }); |
| 232 |
$('.cooked-timer-block').addClass('cooked-visible'); |
| 233 |
}, 50); |
| 234 |
} else { |
| 235 |
let timerWrap = $('<div>', { id: 'cooked-timers-wrap' }); |
| 236 |
let timerBlock = cookedTimerBlock(Timer); |
| 237 |
timerWrap.append(timerBlock); |
| 238 |
|
| 239 |
$('body').append(timerWrap); |
| 240 |
var thisTimerObj = $('#' + Timer.id).find('.cooked-timer-obj'); |
| 241 |
cookedTimer(thisTimerObj, false); |
| 242 |
|
| 243 |
setTimeout(function() { |
| 244 |
$('#cooked-timers-wrap').addClass('cooked-visible'); |
| 245 |
}, 50); |
| 246 |
} |
| 247 |
} |
| 248 |
}); |
| 249 |
} |
| 250 |
|
| 251 |
function cookedTimerBlock(Timer, visibleClass = 'cooked-visible') { |
| 252 |
// Create the second div with dynamic id and classes |
| 253 |
let timerBlock = $('<div>', { |
| 254 |
id: Timer.id, |
| 255 |
class: 'cooked-timer-block ' + visibleClass |
| 256 |
}); |
| 257 |
|
| 258 |
// Create and append the step span |
| 259 |
$('<span>', { |
| 260 |
class: 'cooked-timer-step', |
| 261 |
text: Timer.step |
| 262 |
}).appendTo(timerBlock); |
| 263 |
|
| 264 |
// Create and append the description span |
| 265 |
$('<span>', { |
| 266 |
class: 'cooked-timer-desc', |
| 267 |
text: Timer.desc |
| 268 |
}).appendTo(timerBlock); |
| 269 |
|
| 270 |
// Create and append the timer object div |
| 271 |
$('<div>', { |
| 272 |
class: 'cooked-timer-obj', |
| 273 |
'data-seconds-left': Timer.seconds |
| 274 |
}).appendTo(timerBlock); |
| 275 |
|
| 276 |
// Create and append the icon |
| 277 |
$('<i>', { |
| 278 |
class: 'cooked-icon cooked-icon-times' |
| 279 |
}).appendTo(timerBlock); |
| 280 |
|
| 281 |
// Create and append the progress bar |
| 282 |
let progress = $('<div>', { class: 'cooked-progress' }); |
| 283 |
$('<span>').appendTo(progress); |
| 284 |
progress.appendTo(timerBlock); |
| 285 |
|
| 286 |
return timerBlock; |
| 287 |
} |
| 288 |
|
| 289 |
function cookedTimer(timerObj, startPaused) { |
| 290 |
var timer_sound = cooked_js_vars.timer_sound; |
| 291 |
var audio = new Audio(timer_sound); |
| 292 |
|
| 293 |
var thisTimerID = timerObj.parents('.cooked-timer-block').attr('id'), |
| 294 |
secondsLeft = timerObj.data('seconds-left'), |
| 295 |
parentBlock = timerObj.parents('.cooked-timer-block'); |
| 296 |
|
| 297 |
timerObj.startTimer({ |
| 298 |
classNames: { |
| 299 |
hours: 'cooked-timer-hours', |
| 300 |
minutes: 'cooked-timer-minutes', |
| 301 |
seconds: 'cooked-timer-seconds', |
| 302 |
clearDiv: 'cooked-timer-clearDiv', |
| 303 |
timeout: 'cooked-timer-timeout' |
| 304 |
}, |
| 305 |
onComplete: function() { |
| 306 |
audio.play(); |
| 307 |
timerObj.addClass('cooked-timer-complete'); |
| 308 |
} |
| 309 |
}); |
| 310 |
|
| 311 |
timerObj.prepend( '<i class="cooked-icon cooked-icon-reverse"></i><i class="cooked-icon cooked-icon-pause"></i><i class="cooked-icon cooked-icon-play"></i>' ); |
| 312 |
|
| 313 |
if (startPaused){ |
| 314 |
timerObj.trigger('pause'); |
| 315 |
parentBlock.addClass('cooked-paused'); |
| 316 |
parentBlock.find('i.cooked-icon-pause').hide(); |
| 317 |
parentBlock.find('i.cooked-icon-play').css({'display':'inline-block'}); |
| 318 |
parentBlock.addClass('cooked-paused'); |
| 319 |
$(this).parent().find('i.cooked-icon-play').css({'display':'inline-block'}); |
| 320 |
} |
| 321 |
|
| 322 |
cookedTimer_progress_bar( parentBlock, 10, 10 ); |
| 323 |
|
| 324 |
timerObj.on( 'update', function( e,timeLeft ){ |
| 325 |
cookedTimer_progress_bar( parentBlock,timeLeft,secondsLeft ); |
| 326 |
}); |
| 327 |
|
| 328 |
timerObj.on( 'complete', function( e,timeLeft ){ |
| 329 |
audio.play(); |
| 330 |
parentBlock.find('i.cooked-icon-pause').hide(); |
| 331 |
parentBlock.find('i.cooked-icon-play').hide(); |
| 332 |
parentBlock.find('.cooked-timer-seconds').html('00'); |
| 333 |
}); |
| 334 |
|
| 335 |
timerObj.on( 'click', 'i.cooked-icon-pause', function(e) { |
| 336 |
e.preventDefault(); |
| 337 |
$(this).hide(); |
| 338 |
parentBlock.addClass('cooked-paused'); |
| 339 |
$(this).parent().find('i.cooked-icon-play').css({'display':'inline-block'}); |
| 340 |
timerObj.trigger('pause'); |
| 341 |
}); |
| 342 |
|
| 343 |
timerObj.on( 'click', 'i.cooked-icon-play', function(e) { |
| 344 |
e.preventDefault(); |
| 345 |
$(this).hide(); |
| 346 |
parentBlock.removeClass('cooked-paused cooked-complete'); |
| 347 |
$(this).parent().find('i.cooked-icon-pause').css({'display':'inline-block'}); |
| 348 |
timerObj.trigger('resume'); |
| 349 |
}); |
| 350 |
|
| 351 |
timerObj.on( 'click', 'i.cooked-icon-reverse', function(e) { |
| 352 |
e.preventDefault(); |
| 353 |
parentBlock.removeClass('cooked-paused cooked-complete'); |
| 354 |
$(this).parent().find('i.cooked-icon-play').css({'display':'inline-block'}); |
| 355 |
$(this).parent().find('i.cooked-icon-pause').hide(); |
| 356 |
timerObj.remove(); |
| 357 |
$( '#' + thisTimerID ).append('<div class="cooked-timer-obj" data-seconds-left="' + secondsLeft + '"></div>'); |
| 358 |
var newTimer = $( '#' + thisTimerID ).find( '.cooked-timer-obj' ); |
| 359 |
cookedTimer( newTimer, true ); |
| 360 |
}); |
| 361 |
|
| 362 |
parentBlock.on( 'click', 'i.cooked-icon-times', function(e) { |
| 363 |
e.preventDefault(); |
| 364 |
|
| 365 |
if ( $('#cooked-timers-wrap').find('.cooked-timer-block').length == 1 ){ |
| 366 |
$('#cooked-timers-wrap').removeClass('cooked-visible'); |
| 367 |
} else { |
| 368 |
var totalTimers = $('#cooked-timers-wrap').find('.cooked-timer-block').length - 1; |
| 369 |
var newHeight = totalTimers * 7.5; |
| 370 |
$('#cooked-timers-wrap').css({ 'height' : newHeight + 'em' }); |
| 371 |
parentBlock.removeClass('cooked-visible'); |
| 372 |
if ( totalTimers == multiplesTrigger ){ |
| 373 |
$('#cooked-timers-wrap').removeClass('cooked-multiples'); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
setTimeout(function(){ |
| 378 |
parentBlock.remove(); |
| 379 |
},200); |
| 380 |
|
| 381 |
}); |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
function cookedTimer_progress_bar( container,remaining_time,total_time ){ |
| 386 |
var progressPercent = 100 - ( ( remaining_time / total_time ) * 100 ); |
| 387 |
container.find('.cooked-progress > span').css({ 'width' : progressPercent + '%' }); |
| 388 |
if ( progressPercent >= 100 ){ |
| 389 |
container.addClass('cooked-complete'); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if ($_Cooked_Timers.length){ |
| 394 |
// How many timers to show before it moves to the right side of the screen? |
| 395 |
var multiplesTrigger = 1; |
| 396 |
init_cooked_timers($_Cooked_Timers); |
| 397 |
} |
| 398 |
|
| 399 |
/**** 7. Full-Screen Mode ****/ |
| 400 |
|
| 401 |
if ($_Cooked_FSM_Button.length) { |
| 402 |
var noSleep = new NoSleep(); |
| 403 |
|
| 404 |
$_Cooked_FSM_Button.on('click', function(e) { |
| 405 |
e.preventDefault(); |
| 406 |
|
| 407 |
var recipe_id = $(this).data('recipe-id'), |
| 408 |
FSM_Container = $('.cooked-fsm[data-recipe-id="' + recipe_id + '"]'); |
| 409 |
|
| 410 |
$('body').addClass('cooked-noscroll cooked-fsm-active'); |
| 411 |
|
| 412 |
var New_FSM_Container = FSM_Container.clone().appendTo('body'); |
| 413 |
|
| 414 |
setTimeout(function() { |
| 415 |
New_FSM_Container.addClass('cooked-visible'); |
| 416 |
}, 10); |
| 417 |
|
| 418 |
setTimeout(function() { |
| 419 |
New_FSM_Container.addClass('cooked-active'); |
| 420 |
}, 50); |
| 421 |
|
| 422 |
var Cooked_Timers = New_FSM_Container.find('.cooked-timer > a'); |
| 423 |
var Cooked_Ingredient_Boxes = New_FSM_Container.find('.cooked-ingredient-checkbox'); |
| 424 |
init_cooked_timers(Cooked_Timers); |
| 425 |
init_cooked_ingredients(Cooked_Ingredient_Boxes); |
| 426 |
|
| 427 |
// Enable wake lock aka "Hands Free Cooking Mode" (Keeps the screen from going dark). |
| 428 |
noSleep.enable(); |
| 429 |
|
| 430 |
New_FSM_Container.on('click', '.cooked-close-fsm', function(e) { |
| 431 |
e.preventDefault(); |
| 432 |
New_FSM_Container.removeClass('cooked-active'); |
| 433 |
$('body').removeClass('cooked-noscroll cooked-fsm-active'); |
| 434 |
|
| 435 |
// Disable wake lock aka "Hands Free Cooking Mode". |
| 436 |
noSleep.disable(); |
| 437 |
|
| 438 |
setTimeout(function() { |
| 439 |
New_FSM_Container.remove(); |
| 440 |
}, 350); |
| 441 |
}); |
| 442 |
}); |
| 443 |
|
| 444 |
$('body').on('click', '.cooked-fsm-mobile-nav > a', function(e) { |
| 445 |
e.preventDefault(); |
| 446 |
|
| 447 |
var thisButton = $(this), |
| 448 |
nav_id = thisButton.data('nav-id'), |
| 449 |
FSM_Container = thisButton.parents('.cooked-fsm'); |
| 450 |
|
| 451 |
FSM_Container.find('.cooked-fsm-mobile-nav > a').removeClass('cooked-active'); |
| 452 |
FSM_Container.find('.cooked-fsm-content').removeClass('cooked-active'); |
| 453 |
|
| 454 |
thisButton.addClass('cooked-active'); |
| 455 |
|
| 456 |
if (nav_id == 'ingredients') { |
| 457 |
FSM_Container.find('.cooked-fsm-content.cooked-fsm-ingredients').addClass('cooked-active'); |
| 458 |
} else { |
| 459 |
FSM_Container.find('.cooked-fsm-content.cooked-fsm-directions-wrap').addClass('cooked-active'); |
| 460 |
FSM_Container.find('.cooked-fsm-content.cooked-fsm-directions').addClass('cooked-active'); |
| 461 |
FSM_Container.find('.cooked-fsm-content.cooked-fsm-notes').addClass('cooked-active'); |
| 462 |
} |
| 463 |
}); |
| 464 |
} |
| 465 |
}); |
| 466 |
|
| 467 |
})( jQuery ); |
| 468 |
|