diff --git a/wire/modules/Inputfield/InputfieldPage/InputfieldPage.js b/wire/modules/Inputfield/InputfieldPage/InputfieldPage.js index ee90ecc6..1a69e3ed 100644 --- a/wire/modules/Inputfield/InputfieldPage/InputfieldPage.js +++ b/wire/modules/Inputfield/InputfieldPage/InputfieldPage.js @@ -1,5 +1,11 @@ +/** + * Initialize InputfieldPage element + * + * @param $this + * + */ function initInputfieldPage($this) { - + $this.find("p.InputfieldPageAddButton a").click(function() { var $input = $(this).parent('p').next('.InputfieldPageAddItems'); if($input.is(":visible")) $input.slideUp('fast').find(":input").val(''); @@ -7,80 +13,162 @@ function initInputfieldPage($this) { return false; }); - // support for dependent selects - $this.find(".findPagesSelector").each(function() { + initInputfieldPageDependentSelects($this); +} - var $t = $(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.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, '&'); + + 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 "); + if(selected) $option.attr('selected', 'selected'); + + // add the "); - if(selected) $option.attr('selected', 'selected'); - // add the