var InputfieldSelector={selector:"",spinner:"",borderColor:"#eee",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)});var $rows=$(".InputfieldSelector .selector-row");if($rows.length>0){$rows.eq(0).find(".select-field").each(function(){InputfieldSelector.changeAny($(this))});$rows.eq(1).find(".input-value").trigger("change");$rows.each(function(){var $row=$(this);$row.css("border-color",InputfieldSelector.borderColor);InputfieldSelector.normalizeHeightRow($row);var $ac=$row.find(".input-value-autocomplete");if($ac.length>0){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");InputfieldSelector.setupAutocomplete($ac,field,name)}})}$(".InputfieldSelector").each(function(){if($(this).find(".selector-preview-disabled").length>0)return;$(this).find(".input-value").eq(0).trigger("change")})},disableOption:function($option){$option.prop("disabled",true).prop("hidden",true)},enableOption:function($option){$option.prop("disabled",false).prop("hidden",false)},valueHasOperator:function(value){var operators=["=","<",">"];var hasOperator=false;for(n=0;n-1&&value.substring(pos-1,1)!="\\"){hasOperator=true;break}}return hasOperator},addRow:function($context){var $list=$context.parents(".InputfieldSelector").find(".selector-list");var $row=$list.find(".selector-template-row");var $newRow=$row.clone();$newRow.removeClass("selector-template-row");$newRow.find(".opval").html("");$newRow.find(".select-field").val("");$newRow.hide();$newRow.find("option:disabled").prop("hidden",true);$list.append($newRow);$newRow.slideDown("fast");InputfieldSelector.normalizeHeightRow($newRow)},deleteRow:function(){var $row=$(this).parents(".selector-row");var $selectField=$row.find(".select-field");if($selectField.val()=="template"){$row.parents(".InputfieldSelector").find("select.select-field").each(function(){$(this).find("option:disabled").each(function(){InputfieldSelector.enableOption($(this))})})}var $siblings=$row.siblings();$row.slideUp("fast",function(){$row.remove();InputfieldSelector.changeAny($siblings.eq(0))});return false},changeFieldToggle:function($select){var $rootParent=$select.parents(".InputfieldSelector");var currentSetting=$rootParent.hasClass("InputfieldSelector_names")?"names":"labels";var newSetting=currentSetting==="labels"?"names":"labels";$rootParent.find(".select-field, .select-subfield").each(function(){$(this).find("option").each(function(){var name=$(this).attr("data-name");if(!name){if($(this).attr("value")=="toggle-names-labels"){$(this).html($(this).attr("data-"+currentSetting))}return}if(currentSetting=="labels"){$(this).html(name)}else{$(this).html($(this).attr("data-label"))}})});$rootParent.removeClass("InputfieldSelector_"+currentSetting).addClass("InputfieldSelector_"+newSetting);$select.val($select.attr("data-selected"));return false},changeField:function(){var $select=$(this);var $option=$select.children("option:selected");var field=$select.val();if(!field||field.length==0)return;if(field=="toggle-names-labels")return InputfieldSelector.changeFieldToggle($select);var $row=$select.parents(".selector-row");var action="opval";$row.children(".opval").html("");$select.attr("data-selected",field);var $hiddenInput=$select.parents(".InputfieldSelector").find(".selector-value");var name=$hiddenInput.attr("name");var type=$select.attr("data-type");if(field.match(/\.$/)){action="subfield-opval";if(field.indexOf("@")>-1)field=field.substring(1,field.length-1);else field=field.substring(0,field.length-1);$row.addClass("has-subfield")}else if(field.match(/\.id$/)){field="id";action="opval";type="selector"}else if($select.is(".select-field")){$row.children(".subfield").html("");$row.removeClass("has-subfield")}var url="./?InputfieldSelector="+action+"&field="+field+"&type="+type+"&name="+name;var $spinner=$(InputfieldSelector.spinner);$row.append($spinner);$.get(url,function(data){$spinner.remove();function actionOpval($data){$data.hide();var $opval=$row.children(".opval");$opval.html("").append($data);$opval.children(":not(.input-or)").fadeIn("fast");InputfieldSelector.changeAny($select);var $ac=$opval.find(".input-value-autocomplete");if($ac.length>0)InputfieldSelector.setupAutocomplete($ac,field,name)}function actionSubfield($data){$data.hide();var $subfield=$row.children(".subfield");$subfield.html("").append($data);$data.fadeIn("fast",function(){})}if(action=="subfield-opval"){data=data.split("");actionSubfield($(data[0]));actionOpval($(data[1]))}else if(action=="opval"){actionOpval($(data))}else{actionSubfield($(data))}InputfieldSelector.normalizeHeightRow($row);$row.closest(".InputfieldContent").find(".hasDatepicker").datepicker("destroy").removeAttr("id").removeClass("hasDatepicker")})},normalizeHeightRow:function($row){InputfieldSelector.normalizeHeight($row.find(":input, i.fa"))},normalizeHeightRows:function($parent){$parent.find(".selector-row").each(function(){InputfieldSelector.normalizeHeightRow($(this))})},normalizeHeight:function($items){var maxHeight=0;$items.each(function(){$(this).css("margin-top",0);var h=$(this).outerHeight();if(h>maxHeight)maxHeight=h});$items.each(function(){var h=$(this).outerHeight();if(h0)templateIDs.push(templateID)}if($row.hasClass("has-subfield")){var $selectSubfield=$row.find(".select-subfield");var $selectOption=$selectSubfield.children("option:selected");var subfield=$selectSubfield.val();if(subfield.length>0){if(subfield.indexOf(".")>0){if(fieldName.indexOf("@")>-1)fieldName="@"+subfield;else fieldName=subfield}else if($selectOption.hasClass("select-subfield-default")){fieldName=subfield}else{fieldName+=subfield}if(fieldName.indexOf(".data")>0)fieldName=fieldName.replace(/\.data$/,"")}}var $op=$select.siblings(".opval").children(".select-operator");var op=$op.val();var $value=$op.next(".input-value");var value=$value.val();var fieldPrefix="";if(op&&op.indexOf('"')>-1){value=" ";$value.prop("disabled",true)}else if($value.is(":disabled")){$value.prop("disabled",false)}if(op&&op.indexOf("!")===0&&op!=="!="){fieldPrefix="!";op=op.substring(1)}if(typeof value!="undefined")if(value.length){if($value.hasClass("input-value-subselect")&&InputfieldSelector.valueHasOperator(value)){value="["+value+"]"}else if(fieldName!="_custom"&&op.indexOf('"')<0){if(value.indexOf('"')>-1){if(value.indexOf("'")===-1){value="'"+value+"'"}else{value='"'+value.replace(/"/g,"")+'"'}}else if(!value.match(/^[-_a-zA-Z0-9]*$/)){value='"'+value+'"'}}}var testField=","+fieldPrefix+fieldName+"~"+op+"~";var testValue="~"+op+"~"+value+",";var mayOrValue=value&&value.length>0&&test.indexOf(testField)>-1;var mayOrField=value&&value.length>0&&test.indexOf(testValue)>-1;var $orCheckbox=$row.find(".input-or");var useOrValue=mayOrValue&&$orCheckbox.is(":checked");var useOrField=mayOrField&&$orCheckbox.is(":checked");var isOrGroup=(useOrField||useOrValue)&&fieldName=="_custom";if(useOrValue){$row.addClass("has-or-value");$row.find(".select-field, .select-operator, .select-subfield").prop("disabled",true)}else if($row.is(".has-or-value")){$row.removeClass("has-or-value");$row.find(".select-field, .select-operator, .select-subfield").prop("disabled",false)}if(useOrField){$row.addClass("has-or-field");$row.find(".input-value, .select-operator").prop("disabled",true)}else if($row.is(".has-or-field")){$row.removeClass("has-or-field");$row.find(".input-value, .select-operator").prop("disabled",false)}selectors[n++]={not:fieldPrefix==="!",field:fieldName,operator:op,value:value,mayOrValue:mayOrValue,mayOrField:mayOrField,useOrValue:useOrValue,useOrField:useOrField,isOrGroup:isOrGroup,checkbox:$orCheckbox};if(mayOrField||mayOrValue)showOrNotes=true;test+=","+fieldPrefix+fieldName+"~"+op+"~"+value+",";selector+=","+fieldPrefix+fieldName+op+value});if(templateIDs.length>0){var $masterSelect=null;$selectFields.each(function(){var $select=$(this);var numDisabledOptions=0;$select.find("option").each(function(){var $option=$(this);var templates=$option.attr("data-templates");if(typeof templates!="undefined"&&templates!="*"){InputfieldSelector.enableOption($option);var numFound=0;for(i=0;i-1)numFound++}if(numFound){InputfieldSelector.enableOption($option)}else{if(!$option.is(":selected"))InputfieldSelector.disableOption($option);numDisabledOptions++}}});if(numDisabledOptions>0&&!$select.parent().is(".selector-template-row")){$select.find("option:disabled").prop("hidden",true)}})}selector="";for(n=0;n0)selector+=", ";for(var i=0;i0&&selector!=InputfieldSelector.selector){if(!$preview.is(".selector-preview-disabled")){$preview.html(""+initValue+selector+"
");$preview.fadeIn()}var $counter=$preview.siblings(".selector-counter");if($counter.length>0&&!$counter.is(".selector-counter-disabled")){$counter.html(InputfieldSelector.spinner).fadeIn("fast");InputfieldSelector.selector=selector;$.post("./?InputfieldSelector=test&name="+$hiddenInput.attr("name"),{selector:selector},function(data){$counter.hide();$counter.html(data);$counter.show()})}}if($hiddenInput.val()!=selector){$hiddenInput.val(selector);if(selector.length==0){$preview.hide();$preview.siblings(".selector-counter").html("")}$hiddenInput.trigger("change")}InputfieldSelector.selector=selector;var $orNotes=$inputfield.find(".or-notes");if(showOrNotes)$orNotes.fadeIn();else $orNotes.hide()}};$(document).ready(function(){InputfieldSelector.init()});