/**
* Initialize InputfieldPage element
*
* @param $this
*
*/
function initInputfieldPage($this) {
$this.find("p.InputfieldPageAddButton a").on('click', function() {
var $input = $(this).parent('p').next('.InputfieldPageAddItems');
if($input.is(":visible")) $input.slideUp('fast').find(":input").val('');
else $input.slideDown('fast').parents('.ui-widget-content').slice(0,1).effect('highlight', {}, 500)
return false;
});
initInputfieldPageDependentSelects($this);
}
/**
* Initialize dependent selects in an .InputfieldPage
*
* @param $inputfieldPage
*
*/
function initInputfieldPageDependentSelects($inputfieldPage) {
/**
* Function to be called when a change is made to $select1
*
* @param $select1 Primary select
* @param $select2 Dependent select
* @param selector Selector string to find items
* @param formatName Name of format sent directly from InputfieldPage to ProcessPageSearch (server side)
* @param labelFieldName Name of field to use for labels
* @param part Page matching part of selector
* @param changed Is this due to a change in $select1? true or false
*
*/
function selectChanged($select1, $select2, selector, formatName, labelFieldName, part, changed) {
var v = $select1.val();
if(v == null) {
// no values selected
if($select2.children().length) {
$select2.children().remove();
$select2.trigger('change');
}
return;
}
v = v.toString();
v = v.replace(/,/g, '|'); // if multi-value field, convert commas to pipes
selector = selector.replace(part, '=' + v);
selector = selector.replace(/,\s*/g, '&');
selector = selector.replace(/\./g, '-');
if(selector.indexOf('_LPID')) selector = selector.replace(/_LPID[0-9]+/g, '');
var url = ProcessWire.config.urls.admin + 'page/search/for?' + selector + '&limit=9999&get=' + labelFieldName;
if(formatName.length) url += '&format_name=' + formatName;
$.getJSON(url, {}, function(data) {
var numSelected = 0;
$select2.children().addClass('option-tbd'); // mark existing options as to-be-deleted
for(var n = 0; n < data.matches.length; n++) {
var selected = false;
var page = data.matches[n];
var label = '';
// first see if we can find the existing option already present
var $option = $select2.children("[value=" + page.id + "]");
if($option.length > 0) selected = $option.is(':selected') || $option.is(':checked');
if(selected) numSelected++;
$option.remove();
// determine label
if(formatName.length) label = page[formatName];
if(!label.length) label = page[labelFieldName];
if(!label.length) label = page.name;
// create ").attr('value', page.id).text(label);
if(selected) $option.attr('selected', 'selected');
// add the