| 1 |
;(function ( $, window, document, undefined ) { |
| 2 |
|
| 3 |
$('body').on( 'change', '.cooked-widget-conditional', function() { |
| 4 |
var thisConditional = $(this); |
| 5 |
cooked_widget_conditional_init( thisConditional ); |
| 6 |
}); |
| 7 |
|
| 8 |
$('body').on( 'click', '.cooked-recipe-finder-show', function(e) { |
| 9 |
e.preventDefault(); |
| 10 |
var thisFinderShowLink = $(this), |
| 11 |
thisFinderShow = thisFinderShowLink.attr('id'); |
| 12 |
|
| 13 |
thisFinderShow = thisFinderShow.split( '-SHOW' ); |
| 14 |
thisFinderID = thisFinderShow[0]; |
| 15 |
thisFinder = $( '#' + thisFinderID ); |
| 16 |
|
| 17 |
thisFinderShowLink.hide(); |
| 18 |
|
| 19 |
thisFinder.selectize({ |
| 20 |
plugins: ['drag_drop'], |
| 21 |
valueField: 'id', |
| 22 |
labelField: 'title', |
| 23 |
searchField: 'title', |
| 24 |
maxItems: 25, |
| 25 |
maxOptions: 5, |
| 26 |
persist: false, |
| 27 |
hideSelected: true, |
| 28 |
closeAfterSelect: true, |
| 29 |
openOnFocus: false, |
| 30 |
options: [], |
| 31 |
onInitialize: function() { |
| 32 |
$( '#' + thisFinderID + '-selectized' ).focus(); |
| 33 |
}, |
| 34 |
onItemAdd: function() { |
| 35 |
thisFinder.trigger('change'); |
| 36 |
}, |
| 37 |
render: { |
| 38 |
option: function(item, escape) { |
| 39 |
return '<div>' + |
| 40 |
'<span class="title">' + |
| 41 |
'<span class="name">' + escape( item.title ) + '</span>' + |
| 42 |
'</span>' + |
| 43 |
'</div>'; |
| 44 |
} |
| 45 |
}, |
| 46 |
load: function (query, callback) { |
| 47 |
this.refreshItems(); |
| 48 |
if (!query.length) return callback(); |
| 49 |
$.ajax({ |
| 50 |
url: cooked_js_vars.rest_url + 'wp/v2/cooked_recipe/', |
| 51 |
type: 'GET', |
| 52 |
dataType: 'json', |
| 53 |
data: { |
| 54 |
search: query, |
| 55 |
per_page: 10 |
| 56 |
}, |
| 57 |
error: function() { |
| 58 |
callback(); |
| 59 |
}, |
| 60 |
success: function(res) { |
| 61 |
var item = {}, items = []; |
| 62 |
for (var key in res) { |
| 63 |
if (!res.hasOwnProperty(key)) continue; |
| 64 |
var obj = res[key]; |
| 65 |
for (var prop in obj) { |
| 66 |
if (!obj.hasOwnProperty(prop)) continue; |
| 67 |
if (prop == 'id') { |
| 68 |
var id = obj[prop]; |
| 69 |
} else if (prop == 'title') { |
| 70 |
var title = obj[prop].rendered; |
| 71 |
} |
| 72 |
} |
| 73 |
item = { id: id, title: title }; |
| 74 |
items.push( item ); |
| 75 |
} |
| 76 |
callback(items); |
| 77 |
} |
| 78 |
}); |
| 79 |
} |
| 80 |
}); |
| 81 |
}); |
| 82 |
|
| 83 |
})(jQuery, window, document); |
| 84 |
|
| 85 |
function cooked_widget_conditional_init(thisConditional) { |
| 86 |
thisConditional_ID = thisConditional.attr('id'); |
| 87 |
thisConditional_VAL = jQuery('#' + thisConditional_ID + ' option:selected').val(); |
| 88 |
|
| 89 |
jQuery( 'body' ).find( '[data-condition="' + thisConditional_ID + '"]' ).each(function(){ |
| 90 |
var thisValue = jQuery(this).data('value'); |
| 91 |
if ( thisValue == thisConditional_VAL ){ |
| 92 |
jQuery(this).fadeIn( 250 ); |
| 93 |
} else { |
| 94 |
jQuery(this).hide(); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
} |
| 99 |
|