text: /* QueryBuilder v1.0.0-SNAPSHOT / Copyright 2014 Damien "Mistic" Sorel (http://www.strangeplanet.fr) / Licensed under MIT (http://opensource.org/licenses/MIT) */
  (function(e){var r="string integer double date time datetime".split(" "),s=["text","radio","checkbox","select"],c=function(a,d){var b=this;this.$el=a;this.settings=e.extend(!0,{},c.DEFAULTS,d);this.filters=this.settings.filters;this.lang=this.settings.lang;this.operators=this.settings.operators;this.status={group_id:0,rule_id:0,generatedId:!1};this.$el.attr("id")||(this.$el.attr("id","qb_"+Math.floor(99999*Math.random())),this.status.generatedId=!0);this.$el_id=this.$el.attr("id");(!this.filters||
  1>this.filters.length)&&e.error("Missing filters list");this.checkFilters();this.$el.on("change.queryBuilder",".rules-group-header input[name$=_cond]",function(){var a=e(this);a.is(":checked")&&(a.parent().addClass("active"),a.parent().siblings().removeClass("active"))});this.$el.on("change.queryBuilder",".rule-filter-container select[name$=_filter]",function(){var a=e(this),d=a.closest("li");b.createRuleOperators(d,a.val());b.createRuleInput(d,a.val())});this.$el.on("change.queryBuilder",".rule-operator-container select[name$=_operator]",
  function(){var a=e(this),d=a.closest("li");b.updateRuleOperator(d,a.val())});this.$el.on("click.queryBuilder","[data-add=rule]",function(){var a=e(this).closest("dl").find(">dd>ul");b.addRule(a)});this.$el.on("click.queryBuilder","[data-add=group]",function(){var a=e(this).closest("dl").find(">dd>ul");b.addGroup(a)});this.$el.on("click.queryBuilder","[data-delete=rule]",function(){e(this).closest("li").remove()});this.$el.on("click.queryBuilder","[data-delete=group]",function(){e(this).closest("dl").remove()});
  this.$el.addClass("query-builder");this.addGroup(this.$el)};c.DEFAULTS={onValidationError:null,onAfterAddGroup:null,onAfterAddRule:null,filters:[],lang:{add_rule:"Add rule",add_group:"Add group",delete_rule:"Delete",delete_group:"Delete",and_condition:"AND",or_condition:"OR",filter_select_placeholder:"------",operator_equal:"equal",operator_not_equal:"not equal",operator_in:"in",operator_not_in:"not in",operator_less:"less",operator_less_or_equal:"less or equal",operator_greater:"greater",operator_greater_or_equal:"greater or equal",
  operator_begins_with:"begins with",operator_not_begins_with:"doesn't begin with",operator_contains:"contains",operator_not_contains:"doesn't contain",operator_ends_with:"ends with",operator_not_ends_with:"doesn't end with",operator_is_empty:"is empty",operator_is_not_empty:"is not empty",operator_is_null:"is null",operator_is_not_null:"is not null"},operators:[{type:"equal",accept_values:!0,apply_to:["string","number","datetime"]},{type:"not_equal",accept_values:!0,apply_to:["string","number","datetime"]},
  {type:"in",accept_values:!0,apply_to:["string","number","datetime"]},{type:"not_in",accept_values:!0,apply_to:["string","number","datetime"]},{type:"less",accept_values:!0,apply_to:["number","datetime"]},{type:"less_or_equal",accept_values:!0,apply_to:["number","datetime"]},{type:"greater",accept_values:!0,apply_to:["number","datetime"]},{type:"greater_or_equal",accept_values:!0,apply_to:["number","datetime"]},{type:"begins_with",accept_values:!0,apply_to:["string"]},{type:"not_begins_with",accept_values:!0,
  apply_to:["string"]},{type:"contains",accept_values:!0,apply_to:["string"]},{type:"not_contains",accept_values:!0,apply_to:["string"]},{type:"ends_with",accept_values:!0,apply_to:["string"]},{type:"not_ends_with",accept_values:!0,apply_to:["string"]},{type:"is_empty",accept_values:!1,apply_to:["string"]},{type:"is_not_empty",accept_values:!1,apply_to:["string"]},{type:"is_null",accept_values:!1,apply_to:["string","number","datetime"]},{type:"is_not_null",accept_values:!1,apply_to:["string","number",
  "datetime"]}]};window.QueryBuilder={setDefaults:function(a){e.extend(!0,c.DEFAULTS,a)}};c.prototype.destroy=function(){this.status.generatedId&&this.$el.removeAttr("id");this.$el.empty().off("click.queryBuilder change.queryBuilder").removeClass("query-builder").removeData("queryBuilder")};c.prototype.reset=function(){this.status.group_id=1;this.status.rule_id=0;this.addRule(this.$el.find(">dl>dd>ul").empty())};c.prototype.clear=function(){this.status.group_id=0;this.status.rule_id=0;this.$el.empty()};
  c.prototype.getRules=function(){this.markRuleAsError(this.$el.find("li"),!1);var a=this;return function b(f){var g={},c=f.find(">dd>ul>*");g.condition=f.find(">dt input[name$=_cond]:checked").val();g.rules=[];f=0;for(var k=c.length;f<k;f++){var h=c.eq(f);if(h.hasClass("rule-container")){var l=a.getRuleFilter(h);if("-1"!=l){var l=a.getFilterById(l),p=a.getOperator(a.getRuleOperator(h)),m=null;if(p.accept_values){var m=a.getRuleValue(h,l),q=a.validateValue(m,l);if(!0!==q)return a.markRuleAsError(h,
  !0),a.triggerValidationError(q,l,p,m,h),{};l.valueParser&&(m=l.valueParser.call(this,m,l,p))}h={id:l.id,field:l.field,type:l.type,input:l.input,operator:p.type,value:m};g.rules.push(h)}}else{h=b(h);if(e.isEmptyObject(h))return{};g.rules.push(h)}}return 0==g.rules.length?{}:g}(this.$el.find(">dl"))};c.prototype.setRules=function(a){this.clear();var d=this;(function f(a,c){var k=d.addGroup(c,!1),h=k.find(">dd>ul"),k=k.find(">dt input[name$=_cond]");a.condition||(a.condition="AND");k.filter("[value=AND]").prop("checked",
  "AND"==a.condition.toUpperCase());k.filter("[value=OR]").prop("checked","OR"==a.condition.toUpperCase());k.trigger("change");e.each(a.rules,function(a,c){if(c.rules&&0<c.rules.length)f(c,h);else{c.id||e.error("Missing rule field id");c.value||(c.value="");c.operator||(c.operator="equal");var g=d.addRule(h),k=d.getFilterById(c.id),n=g.find(".rule-value-container");g.find(".rule-filter-container select[name$=_filter]").val(c.id).trigger("change");g.find(".rule-operator-container select[name$=_operator]").val(c.operator).trigger("change");
  switch(k.input){case "radio":n.find('input[name$=_value][value="'+c.value+'"]').prop("checked",!0).trigger("change");break;case "checkbox":e.isArray(c.value)||(c.value=[c.value]);e.each(c.value,function(a,d){n.find('input[name$=_value][value="'+d+'"]').prop("checked",!0).trigger("change")});break;case "select":n.find("select[name$=_value]").val(c.value).trigger("change");break;default:n.find("input[name$=_value]").val(c.value).trigger("change")}k.onAfterSetValue&&k.onAfterSetValue.call(this,g,c.value,
  k,c.operator)}})})(a,this.$el)};c.prototype.checkFilters=function(){var a=[];e.each(this.filters,function(d,b){b.id||e.error("Missing filter id: "+d);-1!=a.indexOf(b.id)&&e.error("Filter already defined: "+b.id);a.push(b.id);b.type||e.error("Missing filter type: "+b.id);-1==r.indexOf(b.type)&&e.error("Invalid type: "+b.type);b.input?-1==s.indexOf(b.input)&&e.error("Invalid input: "+b.input):b.input="text";b.field||(b.field=b.id);b.label||(b.label=b.field);switch(b.type){case "string":b.internalType=
  "string";break;case "integer":case "double":b.internalType="number";break;case "date":case "time":case "datetime":b.internalType="datetime"}switch(b.input){case "radio":case "checkbox":(!b.values||1>b.values.length)&&e.error("Missing values for filter: "+b.id)}})};c.prototype.addGroup=function(a,d){d=null==d?!0:d;var b=this.nextGroupId();a.append(this.getGroupTemplate(b));b=a.find("#"+b);this.settings.onAfterAddGroup&&this.settings.onAfterAddGroup.call(this,b);d&&this.addRule(b.find(">dd>ul"));return b};
  c.prototype.addRule=function(a){var d=this.nextRuleId();a.append(this.getRuleTemplate(d));a=a.find("#"+d);this.settings.onAfterAddRule&&this.settings.onAfterAddRule.call(this,a);return a};c.prototype.createRuleOperators=function(a,d){a.find(".rule-operator-container").empty();var b=a.attr("id");if("-1"!=d){var f=this.getOperators(d),b=this.getRuleOperatorSelect(b,f);a.find(".rule-operator-container").html(b)}};c.prototype.createRuleInput=function(a,d){if("-1"!=d){var b=a.find(".rule-value-container").empty(),
  f=a.attr("id");if(this.getOperator(this.getRuleOperator(a)).accept_values){var c=this.getFilterById(d),f=this.getRuleInput(f,c);b.html(f).show();c.onAfterCreateRuleInput&&c.onAfterCreateRuleInput.call(this,a,c);if(c.plugin)b.find("select"==c.input?"select":"input")[c.plugin](c.plugin_config||{})}}};c.prototype.updateRuleOperator=function(a,d){var b=a.find(".rule-value-container"),f=this.getFilterById(this.getRuleFilter(a)),c=this.getOperator(d);c.accept_values?(b.show(),b.is(":empty")&&this.createRuleInput(a,
  this.getRulefilter(a))):b.hide();f.onAfterChangeOperator&&f.onAfterChangeOperator.call(this,a,f,c.type)};c.prototype.validateValue=function(a,d){var b=d.validation||{};if(b.callback)return b.callback.call(this,a,d);switch(d.input){case "radio":if(void 0==a)return"radio_empty";break;case "checkbox":if(0==a.length)return"checkbox_empty";break;case "select":if(d.multiple){if(0==a.length)return"select_empty"}else if(void 0==a)return"select_empty";break;default:switch(d.internalType){case "string":if(void 0!=
  b.min){if(a.length<b.min)return"string_exceed_min_length"}else if(0==a.length)return"string_empty";if(void 0!=b.max&&a.length>b.max)return"string_exceed_max_length";if(b.format&&!b.format.test(a))return"string_invalid_format";break;case "number":if(isNaN(a))return"number_nan";if("integer"==d.type){if(parseInt(a)!=a)return"number_not_integer"}else if(parseFloat(a)!=a)return"number_not_double";if(void 0!=b.min&&a<b.min)return"number_exceed_min";if(void 0!=b.max&&a>b.max)return"number_exceed_max";if(b.step&&
  (b=a/b.step,parseInt(b)!=b))return"number_wrong_step";break;case "datetime":if(window.moment&&b.format){var f=moment(a,b.format);if(f.isValid()){if(b.min&&f<moment(b.min,b.format))return"datetime_exceed_min";if(b.max&&f>moment(b.max,b.format))return"datetime_exceed_max"}else return"datetime_invalid"}}}return!0};c.prototype.markRuleAsError=function(a,d){d?a.addClass("has-error"):a.removeClass("has-error")};c.prototype.triggerValidationError=function(a,d,b,f,c){d.onValidationError&&d.onValidationError.call(this,
  c,a,f,d,b);this.settings.onValidationError&&this.settings.onValidationError.call(this,c,a,f,d,b);a=jQuery.Event("validationError.queryBuilder",{error:a,filter:d,operator:b,value:f,targetRule:c[0],builder:this});this.$el.trigger(a)};c.prototype.nextGroupId=function(){var a=this.$el_id+"_group_"+this.status.group_id;this.status.group_id++;return a};c.prototype.nextRuleId=function(){var a=this.$el_id+"_rule_"+this.status.rule_id;this.status.rule_id++;return a};c.prototype.getOperators=function(a){"string"==
  typeof a&&(a=this.getFilterById(a));for(var d=[],b=0,c=this.operators.length;b<c;b++)-1==this.operators[b].apply_to.indexOf(a.internalType)||a.operators&&-1==a.operators.indexOf(this.operators[b].type)||d.push({type:this.operators[b].type,label:this.lang["operator_"+this.operators[b].type]});return d};c.prototype.getFilterById=function(a){for(var d=0,b=this.filters.length;d<b;d++)if(this.filters[d].id==a)return this.filters[d];throw"Undefined filter: "+a;};c.prototype.getOperator=function(a){for(var d=
  0,b=this.operators.length;d<b;d++)if(this.operators[d].type==a)return this.operators[d];throw"Undefined operator: "+a;};c.prototype.getRuleFilter=function(a){return a.find(".rule-filter-container select[name$=_filter]").val()};c.prototype.getRuleOperator=function(a){return a.find(".rule-operator-container select[name$=_operator]").val()};c.prototype.getRuleValue=function(a,d){d=d||this.getFilterByType(this.getRulefilter(a));var b,c=a.find(".rule-value-container");switch(d.input){case "radio":b=c.find("input[name$=_value]:checked").val();
  break;case "checkbox":b=[];c.find("input[name$=_value]:checked").each(function(){b.push(e(this).val())});break;case "select":d.multiple?(b=[],c.find("select[name$=_value] option:selected").each(function(){b.push(e(this).val())})):b=c.find("select[name$=_value] option:selected").val();break;default:b=c.find("input[name$=_value]").val()}return b};c.prototype.getGroupTemplate=function(a){return"<dl id="+a+' class="rules-group-container">   <dt class="rules-group-header">     <div class="btn-group">       <label class="btn btn-mini btn-xs btn-primary active"><input type="radio" name="'+
  a+'_cond" value="AND" checked> '+this.lang.and_condition+'</label>       <label class="btn btn-mini btn-xs btn-primary"><input type="radio" name="'+a+'_cond" value="OR"> '+this.lang.or_condition+'</label>     </div>     <div class="btn-group pull-right">       <button type="button" class="btn btn-mini btn-xs btn-success" data-add="rule"><i class="glyphicon glyphicon-plus"></i> '+this.lang.add_rule+'</button>       <button type="button" class="btn btn-mini btn-xs btn-success" data-add="group"><i class="glyphicon glyphicon-plus-sign"></i> '+
  this.lang.add_group+'</button>       <button type="button" class="btn btn-mini btn-xs btn-danger" data-delete="group"><i class="glyphicon glyphicon-remove"></i> '+this.lang.delete_group+"</button>     </div>   </dt>   <dd class=rules-group-body>     <ul class=rules-list></ul>   </dd> </dl>"};c.prototype.getRuleTemplate=function(a){return"<li id="+a+' class="rule-container">   <div class="rule-header">     <div class="btn-group pull-right">       <button type="button" class="btn btn-mini btn-xs btn-danger" data-delete="rule"><i class="glyphicon glyphicon-remove"></i> '+
  this.lang.delete_rule+'</button>     </div>   </div>   <div class="rule-filter-container">'+this.getRuleFilterSelect(a)+'</div>   <div class="rule-operator-container"></div>   <div class="rule-value-container"></div> </li>'};c.prototype.getRuleFilterSelect=function(a){var d='<select name="'+a+'_filter">',d=d+('<option value="-1">'+this.lang.filter_select_placeholder+"</option>");e.each(this.filters,function(a,c){d+='<option value="'+c.id+'">'+c.label+"</option>"});return d+="</select>"};c.prototype.getRuleOperatorSelect=
  function(a,d){for(var b='<select name="'+a+'_operator">',c=0,e=d.length;c<e;c++)b+='<option value="'+d[c].type+'">'+d[c].label+"</option>";return b+"</select>"};c.prototype.getRuleInput=function(a,d){var b=d.validation||{},c="";switch(d.input){case "radio":var g=d.vertical?" class=block":"";e.each(d.values,function(b,d){c+="<label"+g+'><input type="radio" name="'+a+'_value" value="'+b+'"> '+d+"</label> "});break;case "checkbox":g=d.vertical?" class=block":"";e.each(d.values,function(b,d){c+="<label"+
  g+'><input type="checkbox" name="'+a+'_value" value="'+b+'"> '+d+"</label> "});break;case "select":c+='<select name="'+a+'_value"'+(d.multiple?" multiple":"")+">";d.values&&e.each(d.values,function(a,b){c+='<option value="'+a+'"> '+b+"</option> "});c+="</select>";break;default:switch(d.internalType){case "number":c+='<input type="number" name="'+a+'_value"';b.step&&(c+=' step="'+b.step+'"');b.min&&(c+=' min="'+b.min+'"');b.max&&(c+=' max="'+b.max+'"');d.placeholder&&(c+=' placeholder="'+d.placeholder+
  '"');c+=">";break;default:c+='<input type="text" name="'+a+'_value"',d.placeholder&&(c+=' placeholder="'+d.placeholder+'"'),c+=">"}}return c};e.fn.queryBuilder=function(a){1<this.length&&e.error("Unable to initialize on multiple target");var d=this.data("queryBuilder"),b="object"==typeof a&&a||{};if(!d&&"destroy"==a)return this;d||this.data("queryBuilder",new c(this,b));return"string"==typeof a?d[a].apply(d,Array.prototype.slice.call(arguments,1)):this}})(jQuery);