/**
* ProcessWire Selector Inputfield JS
*
* Concept by Antti Peisa
* Code by Ryan Cramer
* Sponsored by Avoine
*
* ProcessWire 3.x (development), Copyright 2015 by Ryan Cramer
* https://processwire.com
*
*/
var InputfieldSelector = {
/**
* Saved value of the preview selector shown to users
*
*/
selector: '',
/**
* Markup for the spinner that appears when ajax events are running
*
*/
spinner: "",
/**
* Default border-color value, which we later attempt to modify and auto determine from the admin theme
*
*/
borderColor: '#eee',
/**
* Initialize InputfieldSelector and attach all needed events
*
*/
init: function() {
$(document).on('change', '.InputfieldSelector select.select-field', InputfieldSelector.changeField);
$(document).on('change', '.InputfieldSelector select.select-subfield', InputfieldSelector.changeField);
$(document).on('change', '.InputfieldSelector :input:not(.select-field):not(.input-value-autocomplete)', function() {
InputfieldSelector.changeAny($(this));
});
$(document).on('opened', '.InputfieldSelector', function() {
InputfieldSelector.normalizeHeightRows($(this));
});
var timeout = null;
$(document).on('keyup', '.InputfieldSelector input.input-value', function() {
var $t = $(this);
clearTimeout(timeout);
if($t.hasClass("input-value-subselect") && InputfieldSelector.valueHasOperator($t.val())) {
var $preview = $t.closest('.InputfieldContent').find('.selector-preview');
$preview.html('Subselect detected: when done click here to commit your change.');
return;
}
timeout = setTimeout(function() { InputfieldSelector.changeAny($t); }, 100);
});
$(document).on('click', '.InputfieldSelector .selector-add', function() {
InputfieldSelector.addRow($(this));
return false;
});
$(document).on('click', '.InputfieldSelector a.delete-row', InputfieldSelector.deleteRow);
$(".InputfieldSelector .selector-preview").hide();
$(document).on('wiretabclick', function(event, $newTab, $oldTab) {
var $inputfield = $newTab.find(".InputfieldSelector");
if($inputfield.length == 0) return;
InputfieldSelector.normalizeHeightRows($inputfield);
});
/*
$(".InputfieldSelector .InputfieldContent").eq(0).each(function() {
InputfieldSelector.borderColor = $(this).css('border-bottom-color');
});
*/
// trigger change any event for first item, in case we have one already populated
//var $rows = $(".InputfieldSelector .selector-row:not(.selector-template-row)");
var $rows = $(".InputfieldSelector .selector-row");
if($rows.length > 0) {
$rows.eq(0).find(".select-field").each(function() {
// this ensures any data-template-ids attributes affect the field disabled state at the template-row level
InputfieldSelector.changeAny($(this));
});
$rows.eq(1).find(".input-value").trigger('change'); // first visible row, if present
$rows.each(function() {
var $row = $(this);
$row.css('border-color', InputfieldSelector.borderColor); // match border color to current admin theme
InputfieldSelector.normalizeHeightRow($row);
var $ac = $row.find(".input-value-autocomplete");
if($ac.length > 0) {
// setup autocomplete
var subfield = $row.find(".select-subfield");
var field = subfield.length ? subfield.val() : $row.find(".select-field").val();
var name = $row.parents(".InputfieldSelector").find("input.selector-value").attr('name'); // selector-value intentional!
InputfieldSelector.setupAutocomplete($ac, field, name);
}
});
}
$(".InputfieldSelector").each(function() {
if($(this).find(".selector-preview-disabled").length > 0) return;
// force items to populate previews
$(this).find(".input-value").eq(0).trigger('change');
});
},
/**
* Disable a