diff --git a/dist/js/query-builder.js b/dist/js/query-builder.js index 61d44e19..ae26acc7 100644 --- a/dist/js/query-builder.js +++ b/dist/js/query-builder.js @@ -8,7 +8,7 @@ // Plugins: bt-checkbox, bt-selectpicker, bt-tooltip-errors, change-filters, filter-description, invert, mongodb-support, not-group, sortable, sql-support, unique-filter (function(root, factory) { if (typeof define == 'function' && define.amd) { - define(['jquery', 'doT', 'jQuery.extendext'], factory); + define(['jquery', 'dot', 'jquery-extendext'], factory); } else { factory(root.jQuery, root.doT); diff --git a/dist/js/query-builder.min.js b/dist/js/query-builder.min.js index 40e32f61..4c5191a2 100644 --- a/dist/js/query-builder.min.js +++ b/dist/js/query-builder.min.js @@ -4,6 +4,6 @@ * Licensed under MIT (http://opensource.org/licenses/MIT) */ -!function(a,b){"function"==typeof define&&define.amd?define(["jquery","doT","jQuery.extendext"],b):b(a.jQuery,a.doT)}(this,function($,a){"use strict";function b(a){return this instanceof b?(this.root=null,void(this.$=$(this))):b.getModel(a)}function c(a,b){if(null!==a&&"object"==typeof a){var c=Object.keys(a);return 1===c.length?c[0]:void 0!==a.$gte&&void 0!==a.$lte?"between":void 0!==a.$lt&&void 0!==a.$gt?"not_between":void 0!==a.$regex?"$regex":void 0}return"eq"}function d(a){for(var b=Object.keys(a),c=0,d=b.length;c.rules-list",group_condition:".rules-group-header [name$=_cond]",rule_filter:".rule-filter-container [name$=_filter]",rule_operator:".rule-operator-container [name$=_operator]",rule_value:".rule-value-container [name*=_value_]",add_rule:"[data-add=rule]",delete_rule:"[data-delete=rule]",add_group:"[data-add=group]",delete_group:"[data-delete=group]"};g.templates={},g.regional={},g.OPERATORS={equal:{type:"equal",nb_inputs:1,multiple:!1,apply_to:["string","number","datetime","boolean"]},not_equal:{type:"not_equal",nb_inputs:1,multiple:!1,apply_to:["string","number","datetime","boolean"]},"in":{type:"in",nb_inputs:1,multiple:!0,apply_to:["string","number","datetime"]},not_in:{type:"not_in",nb_inputs:1,multiple:!0,apply_to:["string","number","datetime"]},less:{type:"less",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},less_or_equal:{type:"less_or_equal",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},greater:{type:"greater",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},greater_or_equal:{type:"greater_or_equal",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},between:{type:"between",nb_inputs:2,multiple:!1,apply_to:["number","datetime"]},not_between:{type:"not_between",nb_inputs:2,multiple:!1,apply_to:["number","datetime"]},begins_with:{type:"begins_with",nb_inputs:1,multiple:!1,apply_to:["string"]},not_begins_with:{type:"not_begins_with",nb_inputs:1,multiple:!1,apply_to:["string"]},contains:{type:"contains",nb_inputs:1,multiple:!1,apply_to:["string"]},not_contains:{type:"not_contains",nb_inputs:1,multiple:!1,apply_to:["string"]},ends_with:{type:"ends_with",nb_inputs:1,multiple:!1,apply_to:["string"]},not_ends_with:{type:"not_ends_with",nb_inputs:1,multiple:!1,apply_to:["string"]},is_empty:{type:"is_empty",nb_inputs:0,multiple:!1,apply_to:["string"]},is_not_empty:{type:"is_not_empty",nb_inputs:0,multiple:!1,apply_to:["string"]},is_null:{type:"is_null",nb_inputs:0,multiple:!1,apply_to:["string","number","datetime","boolean"]},is_not_null:{type:"is_not_null",nb_inputs:0,multiple:!1,apply_to:["string","number","datetime","boolean"]}},g.DEFAULTS={filters:[],plugins:[],sort_filters:!1,display_errors:!0,allow_groups:-1,allow_empty:!1,conditions:["AND","OR"],default_condition:"AND",inputs_separator:" , ",select_placeholder:"------",display_empty_filter:!0,default_filter:null,optgroups:{},default_rule_flags:{filter_readonly:!1,operator_readonly:!1,value_readonly:!1,no_delete:!1},default_group_flags:{condition_readonly:!1,no_add_rule:!1,no_add_group:!1,no_delete:!1},templates:{group:null,rule:null,filterSelect:null,operatorSelect:null},lang_code:"en",lang:{},operators:["equal","not_equal","in","not_in","less","less_or_equal","greater","greater_or_equal","between","not_between","begins_with","not_begins_with","contains","not_contains","ends_with","not_ends_with","is_empty","is_not_empty","is_null","is_not_null"],icons:{add_group:"glyphicon glyphicon-plus-sign",add_rule:"glyphicon glyphicon-plus",remove_group:"glyphicon glyphicon-remove",remove_rule:"glyphicon glyphicon-remove",error:"glyphicon glyphicon-warning-sign"}},g.prototype.init=function(c,d){c[0].queryBuilder=this,this.$el=c,this.settings=$.extendext(!0,"replace",{},g.DEFAULTS,d),this.model=new b,this.status={group_id:0,rule_id:0,generated_id:!1,has_optgroup:!1,has_operator_oprgroup:!1,id:null},this.settings.allow_groups===!1?this.settings.allow_groups=0:this.settings.allow_groups===!0&&(this.settings.allow_groups=-1),this.filters=this.settings.filters,this.icons=this.settings.icons,this.operators=this.settings.operators,this.templates=this.settings.templates,this.plugins=this.settings.plugins,void 0===g.regional.en&&l.error("Config",'"i18n/en.js" not loaded.'),this.lang=$.extendext(!0,"replace",{},g.regional.en,g.regional[this.settings.lang_code],this.settings.lang),Object.keys(this.templates).forEach(function(b){this.templates[b]||(this.templates[b]=g.templates[b]),"string"==typeof this.templates[b]&&(this.templates[b]=a.template(this.templates[b]))},this),this.$el.attr("id")||(this.$el.attr("id","qb_"+Math.floor(99999*Math.random())),this.status.generated_id=!0),this.status.id=this.$el.attr("id"),this.$el.addClass("query-builder form-inline"),this.filters=this.checkFilters(this.filters),this.operators=this.checkOperators(this.operators),this.bindEvents(),this.initPlugins(),this.trigger("afterInit"),d.rules?(this.setRules(d.rules),delete this.settings.rules):this.setRoot(!0)},g.prototype.checkFilters=function(a){var b=[];if(a&&0!==a.length||l.error("Config","Missing filters list"),a.forEach(function(a,c){switch(a.id||l.error("Config","Missing filter {0} id",c),b.indexOf(a.id)!=-1&&l.error("Config",'Filter "{0}" already defined',a.id),b.push(a.id),a.type?g.types[a.type]||l.error("Config",'Invalid type "{0}"',a.type):a.type="string",a.input?"function"!=typeof a.input&&g.inputs.indexOf(a.input)==-1&&l.error("Config",'Invalid input "{0}"',a.input):a.input="text",a.operators&&a.operators.forEach(function(a){"string"!=typeof a&&l.error("Config","Filter operators must be global operators types (string)")}),a.field||(a.field=a.id),a.label||(a.label=a.field),a.optgroup?(this.status.has_optgroup=!0,this.settings.optgroups[a.optgroup]||(this.settings.optgroups[a.optgroup]=a.optgroup)):a.optgroup=null,a.input){case"radio":case"checkbox":(!a.values||a.values.length<1)&&l.error("Config",'Missing filter "{0}" values',a.id);break;case"select":a.placeholder&&(void 0===a.placeholder_value&&(a.placeholder_value=-1),l.iterateOptions(a.values,function(b){b==a.placeholder_value&&l.error("Config",'Placeholder of filter "{0}" overlaps with one of its values',a.id)}))}},this),this.settings.sort_filters)if("function"==typeof this.settings.sort_filters)a.sort(this.settings.sort_filters);else{var c=this;a.sort(function(a,b){return c.translateLabel(a.label).localeCompare(c.translateLabel(b.label))})}return this.status.has_optgroup&&(a=l.groupSort(a,"optgroup")),a},g.prototype.checkOperators=function(a){var b=[];return a.forEach(function(c,d){"string"==typeof c?(g.OPERATORS[c]||l.error("Config",'Unknown operator "{0}"',c),a[d]=c=$.extendext(!0,"replace",{},g.OPERATORS[c])):(c.type||l.error("Config",'Missing "type" for operator {0}',d),g.OPERATORS[c.type]&&(a[d]=c=$.extendext(!0,"replace",{},g.OPERATORS[c.type],c)),void 0!==c.nb_inputs&&void 0!==c.apply_to||l.error("Config",'Missing "nb_inputs" and/or "apply_to" for operator "{0}"',c.type)),b.indexOf(c.type)!=-1&&l.error("Config",'Operator "{0}" already defined',c.type),b.push(c.type),c.optgroup?(this.status.has_operator_optgroup=!0,this.settings.optgroups[c.optgroup]||(this.settings.optgroups[c.optgroup]=c.optgroup)):c.optgroup=null},this),this.status.has_operator_optgroup&&(a=l.groupSort(a,"optgroup")),a},g.prototype.bindEvents=function(){var a=this;this.$el.on("change.queryBuilder",h.group_condition,function(){if($(this).is(":checked")){var a=$(this).closest(h.group_container);b(a).condition=$(this).val()}}),this.$el.on("change.queryBuilder",h.rule_filter,function(){var c=$(this).closest(h.rule_container);b(c).filter=a.getFilterById($(this).val())}),this.$el.on("change.queryBuilder",h.rule_operator,function(){var c=$(this).closest(h.rule_container);b(c).operator=a.getOperatorByType($(this).val())}),this.$el.on("click.queryBuilder",h.add_rule,function(){var c=$(this).closest(h.group_container);a.addRule(b(c))}),this.$el.on("click.queryBuilder",h.delete_rule,function(){var c=$(this).closest(h.rule_container);a.deleteRule(b(c))}),0!==this.settings.allow_groups&&(this.$el.on("click.queryBuilder",h.add_group,function(){var c=$(this).closest(h.group_container);a.addGroup(b(c))}),this.$el.on("click.queryBuilder",h.delete_group,function(){var c=$(this).closest(h.group_container);a.deleteGroup(b(c))})),this.model.on({drop:function(b,c){c.$el.remove(),a.refreshGroupsConditions()},add:function(b,c,d){0===d?c.$el.prependTo(c.parent.$el.find(">"+h.rules_list)):c.$el.insertAfter(c.parent.rules[d-1].$el),a.refreshGroupsConditions()},move:function(b,c,d,e){c.$el.detach(),0===e?c.$el.prependTo(d.$el.find(">"+h.rules_list)):c.$el.insertAfter(d.rules[e-1].$el),a.refreshGroupsConditions()},update:function(b,c,d,e,f){if(c instanceof k)switch(d){case"error":a.displayError(c);break;case"flags":a.applyRuleFlags(c);break;case"filter":a.updateRuleFilter(c,f);break;case"operator":a.updateRuleOperator(c,f);break;case"value":a.updateRuleValue(c)}else switch(d){case"error":a.displayError(c);break;case"flags":a.applyGroupFlags(c);break;case"condition":a.updateGroupCondition(c)}}})},g.prototype.setRoot=function(a,b,c){a=void 0===a||a===!0;var d=this.nextGroupId(),e=$(this.getGroupTemplate(d,1));return this.$el.append(e),this.model.root=new j(null,e),this.model.root.model=this.model,this.model.root.data=b,this.model.root.__.flags=$.extend({},this.settings.default_group_flags,c),this.trigger("afterAddGroup",this.model.root),this.model.root.condition=this.settings.default_condition,a&&this.addRule(this.model.root),this.model.root},g.prototype.addGroup=function(a,b,c,d){b=void 0===b||b===!0;var e=a.level+1,f=this.trigger("beforeAddGroup",a,b,e);if(f.isDefaultPrevented())return null;var g=this.nextGroupId(),h=$(this.getGroupTemplate(g,e)),i=a.addGroup(h);return i.data=c,i.__.flags=$.extend({},this.settings.default_group_flags,d),this.trigger("afterAddGroup",i),i.condition=this.settings.default_condition,b&&this.addRule(i),i},g.prototype.deleteGroup=function(a){if(a.isRoot())return!1;var b=this.trigger("beforeDeleteGroup",a);if(b.isDefaultPrevented())return!1;var c=!0;return a.each("reverse",function(a){c&=this.deleteRule(a)},function(a){c&=this.deleteGroup(a)},this),c&&(a.drop(),this.trigger("afterDeleteGroup")),c},g.prototype.updateGroupCondition=function(a){a.$el.find(">"+h.group_condition).each(function(){var b=$(this);b.prop("checked",b.val()===a.condition),b.parent().toggleClass("active",b.val()===a.condition)}),this.trigger("afterUpdateGroupCondition",a)},g.prototype.refreshGroupsConditions=function(){!function a(b){(!b.flags||b.flags&&!b.flags.condition_readonly)&&b.$el.find(">"+h.group_condition).prop("disabled",b.rules.length<=1).parent().toggleClass("disabled",b.rules.length<=1),b.each(function(a){},function(b){a(b)},this)}(this.model.root)},g.prototype.addRule=function(a,b,c){var d=this.trigger("beforeAddRule",a);if(d.isDefaultPrevented())return null;var e=this.nextRuleId(),f=$(this.getRuleTemplate(e)),g=a.addRule(f);return void 0!==b&&(g.data=b),g.__.flags=$.extend({},this.settings.default_rule_flags,c),this.trigger("afterAddRule",g),this.createRuleFilters(g),!this.settings.default_filter&&this.settings.display_empty_filter||(g.filter=this.change("getDefaultFilter",this.getFilterById(this.settings.default_filter||this.filters[0].id),g)),g},g.prototype.deleteRule=function(a){if(a.flags.no_delete)return!1;var b=this.trigger("beforeDeleteRule",a);return!b.isDefaultPrevented()&&(a.drop(),this.trigger("afterDeleteRule"),!0)},g.prototype.createRuleFilters=function(a){var b=this.change("getRuleFilters",this.filters,a),c=$(this.getRuleFilterSelect(a,b));a.$el.find(h.filter_container).html(c),this.trigger("afterCreateRuleFilters",a)},g.prototype.createRuleOperators=function(a){var b=a.$el.find(h.operator_container).empty();if(a.filter){var c=this.getOperators(a.filter),d=$(this.getRuleOperatorSelect(a,c));b.html(d),a.__.operator=c[0],this.trigger("afterCreateRuleOperators",a,c)}},g.prototype.createRuleInput=function(a){var b=a.$el.find(h.value_container).empty();if(a.__.value=void 0,a.filter&&a.operator&&0!==a.operator.nb_inputs){for(var c=this,d=$(),e=a.filter,f=0;f0&&b.append(this.settings.inputs_separator),b.append(g),d=d.add(g)}b.show(),d.on("change "+(e.input_event||""),function(){this._updating_input||(a._updating_value=!0,a.value=c.getRuleInputValue(a),a._updating_value=!1)}),e.plugin&&d[e.plugin](e.plugin_config||{}),this.trigger("afterCreateRuleInput",a),void 0!==e.default_value?a.value=e.default_value:(a._updating_value=!0,a.value=c.getRuleInputValue(a),a._updating_value=!1)}},g.prototype.updateRuleFilter=function(a,b){this.createRuleOperators(a),this.createRuleInput(a),a.$el.find(h.rule_filter).val(a.filter?a.filter.id:"-1"),b&&a.filter&&b.id!==a.filter.id&&(a.data=void 0),this.trigger("afterUpdateRuleFilter",a)},g.prototype.updateRuleOperator=function(a,b){var c=a.$el.find(h.value_container);a.operator&&0!==a.operator.nb_inputs?(c.show(),!c.is(":empty")&&b&&a.operator.nb_inputs===b.nb_inputs||this.createRuleInput(a)):(c.hide(),a.__.value=void 0),a.operator&&a.$el.find(h.rule_operator).val(a.operator.type),this.trigger("afterUpdateRuleOperator",a),this.updateRuleValue(a)},g.prototype.updateRuleValue=function(a){a._updating_value||this.setRuleInputValue(a,a.value),this.trigger("afterUpdateRuleValue",a)},g.prototype.applyRuleFlags=function(a){var b=a.flags;b.filter_readonly&&a.$el.find(h.rule_filter).prop("disabled",!0),b.operator_readonly&&a.$el.find(h.rule_operator).prop("disabled",!0),b.value_readonly&&a.$el.find(h.rule_value).prop("disabled",!0),b.no_delete&&a.$el.find(h.delete_rule).remove(),this.trigger("afterApplyRuleFlags",a)},g.prototype.applyGroupFlags=function(a){var b=a.flags;b.condition_readonly&&a.$el.find(">"+h.group_condition).prop("disabled",!0).parent().addClass("readonly"),b.no_add_rule&&a.$el.find(h.add_rule).remove(),b.no_add_group&&a.$el.find(h.add_group).remove(),b.no_delete&&a.$el.find(h.delete_group).remove(),this.trigger("afterApplyGroupFlags",a)},g.prototype.clearErrors=function(a){a=a||this.model.root,a&&(a.error=null,a instanceof j&&a.each(function(a){a.error=null},function(a){this.clearErrors(a)},this))},g.prototype.displayError=function(a){if(this.settings.display_errors)if(null===a.error)a.$el.removeClass("has-error");else{var b=this.lang.errors[a.error[0]]||a.error[0];b=l.fmt(b,a.error.slice(1)),b=this.change("displayError",b,a.error,a),a.$el.addClass("has-error").find(h.error_container).eq(0).attr("title",b)}},g.prototype.triggerValidationError=function(a,b,c){$.isArray(b)||(b=[b]);var d=this.trigger("validationError",a,b,c);d.isDefaultPrevented()||(a.error=b)},g.prototype.destroy=function(){this.trigger("beforeDestroy"),this.status.generated_id&&this.$el.removeAttr("id"),this.clear(),this.model=null,this.$el.off(".queryBuilder").removeClass("query-builder").removeData("queryBuilder"),delete this.$el[0].queryBuilder},g.prototype.reset=function(){this.status.group_id=1,this.status.rule_id=0,this.model.root.empty(),this.addRule(this.model.root),this.trigger("afterReset")},g.prototype.clear=function(){this.status.group_id=0,this.status.rule_id=0,this.model.root&&(this.model.root.drop(),this.model.root=null),this.trigger("afterClear")},g.prototype.setOptions=function(a){$.each(a,function(a,b){g.modifiable_options.indexOf(a)!==-1&&(this.settings[a]=b)}.bind(this))},g.prototype.getModel=function(a){return a?b(a):this.model.root},g.prototype.validate=function(){this.clearErrors();var a=this,b=function c(b){var d=0,e=0;return b.each(function(b){if(!b.filter)return a.triggerValidationError(b,"no_filter",null),void e++;if(!b.operator)return a.triggerValidationError(b,"no_operator",null),void e++;if(0!==b.operator.nb_inputs){var c=a.validateValue(b,b.value);if(c!==!0)return a.triggerValidationError(b,c,b.value),void e++}d++},function(a){c(a)?d++:e++}),!(e>0)&&(!!(0!==d||a.settings.allow_empty&&b.isRoot())||(a.triggerValidationError(b,"empty_group",null),!1))}(this.model.root);return this.change("validate",b)},g.prototype.getRules=function(a){a=$.extend({get_flags:!1,allow_invalid:!1},a);var b=this.validate();if(!b&&!a.allow_invalid)return null;var c=this,d=function e(b){var d={condition:b.condition,rules:[]};if(b.data&&(d.data=$.extendext(!0,"replace",{},b.data)),a.get_flags){var f=c.getGroupFlags(b.flags,"all"===a.get_flags);$.isEmptyObject(f)||(d.flags=f)}return b.each(function(b){var e=null;b.operator&&0===b.operator.nb_inputs||(e=b.value);var f={id:b.filter?b.filter.id:null,field:b.filter?b.filter.field:null,type:b.filter?b.filter.type:null,input:b.filter?b.filter.input:null,operator:b.operator?b.operator.type:null,value:e};if((b.filter&&b.filter.data||b.data)&&(f.data=$.extendext(!0,"replace",{},b.filter.data,b.data)),a.get_flags){var g=c.getRuleFlags(b.flags,"all"===a.get_flags);$.isEmptyObject(g)||(f.flags=g)}d.rules.push(c.change("ruleToJson",f,b))},function(a){d.rules.push(e(a))},this),c.change("groupToJson",d,b)}(this.model.root);return d.valid=b,this.change("getRules",d)},g.prototype.setRules=function(a,b){b=$.extend({allow_invalid:!1},b),$.isArray(a)&&(a={condition:this.settings.default_condition,rules:a}),a&&a.rules&&(0!==a.rules.length||this.settings.allow_empty)||l.error("RulesParse","Incorrect data object passed"),this.clear(),this.setRoot(!1,a.data,this.parseGroupFlags(a)),this.applyGroupFlags(this.model.root),a=this.change("setRules",a);var c=this;!function d(a,e){null!==e&&(void 0===a.condition?a.condition=c.settings.default_condition:c.settings.conditions.indexOf(a.condition)==-1&&(l.error(!b.allow_invalid,"UndefinedCondition",'Invalid condition "{0}"',a.condition),a.condition=c.settings.default_condition),e.condition=a.condition,a.rules.forEach(function(a){var f;if(void 0!==a.rules)if(c.settings.allow_groups!==-1&&c.settings.allow_groups1){h=["operator_not_multiple",e.type];break}break;case"select":if(void 0===b[i]||0===b[i].length||d.placeholder&&b[i]==d.placeholder_value){f.allow_empty_value||(h=["select_empty"]);break}if(d.multiple&&!e.multiple&&b[i].length>1){h=["operator_not_multiple",e.type];break}break;default:switch(g.types[d.type]){case"string":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["string_empty"]);break}if(void 0!==f.min&&b[i].lengthparseInt(f.max)){h=[this.getValidationMessage(f,"max","string_exceed_max_length"),f.max];break}if(f.format&&("string"==typeof f.format&&(f.format=new RegExp(f.format)),!f.format.test(b[i]))){h=[this.getValidationMessage(f,"format","string_invalid_format"),f.format];break}break;case"number":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["number_nan"]);break}if(isNaN(b[i])){h=["number_nan"];break}if("integer"==d.type){if(parseInt(b[i])!=b[i]){h=["number_not_integer"];break}}else if(parseFloat(b[i])!=b[i]){h=["number_not_double"];break}if(void 0!==f.min&&b[i]parseFloat(f.max)){h=[this.getValidationMessage(f,"max","number_exceed_max"),f.max];break}if(void 0!==f.step&&"any"!==f.step){var j=(b[i]/f.step).toPrecision(14);if(parseInt(j)!=j){h=[this.getValidationMessage(f,"step","number_wrong_step"),f.step];break}}break;case"datetime":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["datetime_empty"]);break}if(f.format){"moment"in window||l.error("MissingLibrary","MomentJS is required for Date/Time validation. Get it here http://momentjs.com");var k=moment(b[i],f.format);if(!k.isValid()){h=[this.getValidationMessage(f,"format","datetime_invalid"),f.format];break}if(f.min&&kmoment(f.max,f.format)){h=[this.getValidationMessage(f,"max","datetime_exceed_max"),f.max];break}}break;case"boolean":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["boolean_not_valid"]);break}if(c=(""+b[i]).trim().toLowerCase(),"true"!==c&&"false"!==c&&"1"!==c&&"0"!==c&&1!==b[i]&&0!==b[i]){h=["boolean_not_valid"];break}}}if(h!==!0)break}return h},g.prototype.nextGroupId=function(){return this.status.id+"_group_"+this.status.group_id++},g.prototype.nextRuleId=function(){return this.status.id+"_rule_"+this.status.rule_id++},g.prototype.getOperators=function(a){"string"==typeof a&&(a=this.getFilterById(a));for(var b=[],c=0,d=this.operators.length;c.rules-list",group_condition:".rules-group-header [name$=_cond]",rule_filter:".rule-filter-container [name$=_filter]",rule_operator:".rule-operator-container [name$=_operator]",rule_value:".rule-value-container [name*=_value_]",add_rule:"[data-add=rule]",delete_rule:"[data-delete=rule]",add_group:"[data-add=group]",delete_group:"[data-delete=group]"};g.templates={},g.regional={},g.OPERATORS={equal:{type:"equal",nb_inputs:1,multiple:!1,apply_to:["string","number","datetime","boolean"]},not_equal:{type:"not_equal",nb_inputs:1,multiple:!1,apply_to:["string","number","datetime","boolean"]},"in":{type:"in",nb_inputs:1,multiple:!0,apply_to:["string","number","datetime"]},not_in:{type:"not_in",nb_inputs:1,multiple:!0,apply_to:["string","number","datetime"]},less:{type:"less",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},less_or_equal:{type:"less_or_equal",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},greater:{type:"greater",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},greater_or_equal:{type:"greater_or_equal",nb_inputs:1,multiple:!1,apply_to:["number","datetime"]},between:{type:"between",nb_inputs:2,multiple:!1,apply_to:["number","datetime"]},not_between:{type:"not_between",nb_inputs:2,multiple:!1,apply_to:["number","datetime"]},begins_with:{type:"begins_with",nb_inputs:1,multiple:!1,apply_to:["string"]},not_begins_with:{type:"not_begins_with",nb_inputs:1,multiple:!1,apply_to:["string"]},contains:{type:"contains",nb_inputs:1,multiple:!1,apply_to:["string"]},not_contains:{type:"not_contains",nb_inputs:1,multiple:!1,apply_to:["string"]},ends_with:{type:"ends_with",nb_inputs:1,multiple:!1,apply_to:["string"]},not_ends_with:{type:"not_ends_with",nb_inputs:1,multiple:!1,apply_to:["string"]},is_empty:{type:"is_empty",nb_inputs:0,multiple:!1,apply_to:["string"]},is_not_empty:{type:"is_not_empty",nb_inputs:0,multiple:!1,apply_to:["string"]},is_null:{type:"is_null",nb_inputs:0,multiple:!1,apply_to:["string","number","datetime","boolean"]},is_not_null:{type:"is_not_null",nb_inputs:0,multiple:!1,apply_to:["string","number","datetime","boolean"]}},g.DEFAULTS={filters:[],plugins:[],sort_filters:!1,display_errors:!0,allow_groups:-1,allow_empty:!1,conditions:["AND","OR"],default_condition:"AND",inputs_separator:" , ",select_placeholder:"------",display_empty_filter:!0,default_filter:null,optgroups:{},default_rule_flags:{filter_readonly:!1,operator_readonly:!1,value_readonly:!1,no_delete:!1},default_group_flags:{condition_readonly:!1,no_add_rule:!1,no_add_group:!1,no_delete:!1},templates:{group:null,rule:null,filterSelect:null,operatorSelect:null},lang_code:"en",lang:{},operators:["equal","not_equal","in","not_in","less","less_or_equal","greater","greater_or_equal","between","not_between","begins_with","not_begins_with","contains","not_contains","ends_with","not_ends_with","is_empty","is_not_empty","is_null","is_not_null"],icons:{add_group:"glyphicon glyphicon-plus-sign",add_rule:"glyphicon glyphicon-plus",remove_group:"glyphicon glyphicon-remove",remove_rule:"glyphicon glyphicon-remove",error:"glyphicon glyphicon-warning-sign"}},g.prototype.init=function(c,d){c[0].queryBuilder=this,this.$el=c,this.settings=$.extendext(!0,"replace",{},g.DEFAULTS,d),this.model=new b,this.status={group_id:0,rule_id:0,generated_id:!1,has_optgroup:!1,has_operator_oprgroup:!1,id:null},this.settings.allow_groups===!1?this.settings.allow_groups=0:this.settings.allow_groups===!0&&(this.settings.allow_groups=-1),this.filters=this.settings.filters,this.icons=this.settings.icons,this.operators=this.settings.operators,this.templates=this.settings.templates,this.plugins=this.settings.plugins,void 0===g.regional.en&&l.error("Config",'"i18n/en.js" not loaded.'),this.lang=$.extendext(!0,"replace",{},g.regional.en,g.regional[this.settings.lang_code],this.settings.lang),Object.keys(this.templates).forEach(function(b){this.templates[b]||(this.templates[b]=g.templates[b]),"string"==typeof this.templates[b]&&(this.templates[b]=a.template(this.templates[b]))},this),this.$el.attr("id")||(this.$el.attr("id","qb_"+Math.floor(99999*Math.random())),this.status.generated_id=!0),this.status.id=this.$el.attr("id"),this.$el.addClass("query-builder form-inline"),this.filters=this.checkFilters(this.filters),this.operators=this.checkOperators(this.operators),this.bindEvents(),this.initPlugins(),this.trigger("afterInit"),d.rules?(this.setRules(d.rules),delete this.settings.rules):this.setRoot(!0)},g.prototype.checkFilters=function(a){var b=[];if(a&&0!==a.length||l.error("Config","Missing filters list"),a.forEach(function(a,c){switch(a.id||l.error("Config","Missing filter {0} id",c),b.indexOf(a.id)!=-1&&l.error("Config",'Filter "{0}" already defined',a.id),b.push(a.id),a.type?g.types[a.type]||l.error("Config",'Invalid type "{0}"',a.type):a.type="string",a.input?"function"!=typeof a.input&&g.inputs.indexOf(a.input)==-1&&l.error("Config",'Invalid input "{0}"',a.input):a.input="text",a.operators&&a.operators.forEach(function(a){"string"!=typeof a&&l.error("Config","Filter operators must be global operators types (string)")}),a.field||(a.field=a.id),a.label||(a.label=a.field),a.optgroup?(this.status.has_optgroup=!0,this.settings.optgroups[a.optgroup]||(this.settings.optgroups[a.optgroup]=a.optgroup)):a.optgroup=null,a.input){case"radio":case"checkbox":(!a.values||a.values.length<1)&&l.error("Config",'Missing filter "{0}" values',a.id);break;case"select":a.placeholder&&(void 0===a.placeholder_value&&(a.placeholder_value=-1),l.iterateOptions(a.values,function(b){b==a.placeholder_value&&l.error("Config",'Placeholder of filter "{0}" overlaps with one of its values',a.id)}))}},this),this.settings.sort_filters)if("function"==typeof this.settings.sort_filters)a.sort(this.settings.sort_filters);else{var c=this;a.sort(function(a,b){return c.translateLabel(a.label).localeCompare(c.translateLabel(b.label))})}return this.status.has_optgroup&&(a=l.groupSort(a,"optgroup")),a},g.prototype.checkOperators=function(a){var b=[];return a.forEach(function(c,d){"string"==typeof c?(g.OPERATORS[c]||l.error("Config",'Unknown operator "{0}"',c),a[d]=c=$.extendext(!0,"replace",{},g.OPERATORS[c])):(c.type||l.error("Config",'Missing "type" for operator {0}',d),g.OPERATORS[c.type]&&(a[d]=c=$.extendext(!0,"replace",{},g.OPERATORS[c.type],c)),void 0!==c.nb_inputs&&void 0!==c.apply_to||l.error("Config",'Missing "nb_inputs" and/or "apply_to" for operator "{0}"',c.type)),b.indexOf(c.type)!=-1&&l.error("Config",'Operator "{0}" already defined',c.type),b.push(c.type),c.optgroup?(this.status.has_operator_optgroup=!0,this.settings.optgroups[c.optgroup]||(this.settings.optgroups[c.optgroup]=c.optgroup)):c.optgroup=null},this),this.status.has_operator_optgroup&&(a=l.groupSort(a,"optgroup")),a},g.prototype.bindEvents=function(){var a=this;this.$el.on("change.queryBuilder",h.group_condition,function(){if($(this).is(":checked")){var a=$(this).closest(h.group_container);b(a).condition=$(this).val()}}),this.$el.on("change.queryBuilder",h.rule_filter,function(){var c=$(this).closest(h.rule_container);b(c).filter=a.getFilterById($(this).val())}),this.$el.on("change.queryBuilder",h.rule_operator,function(){var c=$(this).closest(h.rule_container);b(c).operator=a.getOperatorByType($(this).val())}),this.$el.on("click.queryBuilder",h.add_rule,function(){var c=$(this).closest(h.group_container);a.addRule(b(c))}),this.$el.on("click.queryBuilder",h.delete_rule,function(){var c=$(this).closest(h.rule_container);a.deleteRule(b(c))}),0!==this.settings.allow_groups&&(this.$el.on("click.queryBuilder",h.add_group,function(){var c=$(this).closest(h.group_container);a.addGroup(b(c))}),this.$el.on("click.queryBuilder",h.delete_group,function(){var c=$(this).closest(h.group_container);a.deleteGroup(b(c))})),this.model.on({drop:function(b,c){c.$el.remove(),a.refreshGroupsConditions()},add:function(b,c,d){0===d?c.$el.prependTo(c.parent.$el.find(">"+h.rules_list)):c.$el.insertAfter(c.parent.rules[d-1].$el),a.refreshGroupsConditions()},move:function(b,c,d,e){c.$el.detach(),0===e?c.$el.prependTo(d.$el.find(">"+h.rules_list)):c.$el.insertAfter(d.rules[e-1].$el),a.refreshGroupsConditions()},update:function(b,c,d,e,f){if(c instanceof k)switch(d){case"error":a.displayError(c);break;case"flags":a.applyRuleFlags(c);break;case"filter":a.updateRuleFilter(c,f);break;case"operator":a.updateRuleOperator(c,f);break;case"value":a.updateRuleValue(c)}else switch(d){case"error":a.displayError(c);break;case"flags":a.applyGroupFlags(c);break;case"condition":a.updateGroupCondition(c)}}})},g.prototype.setRoot=function(a,b,c){a=void 0===a||a===!0;var d=this.nextGroupId(),e=$(this.getGroupTemplate(d,1));return this.$el.append(e),this.model.root=new j(null,e),this.model.root.model=this.model,this.model.root.data=b,this.model.root.__.flags=$.extend({},this.settings.default_group_flags,c),this.trigger("afterAddGroup",this.model.root),this.model.root.condition=this.settings.default_condition,a&&this.addRule(this.model.root),this.model.root},g.prototype.addGroup=function(a,b,c,d){b=void 0===b||b===!0;var e=a.level+1,f=this.trigger("beforeAddGroup",a,b,e);if(f.isDefaultPrevented())return null;var g=this.nextGroupId(),h=$(this.getGroupTemplate(g,e)),i=a.addGroup(h);return i.data=c,i.__.flags=$.extend({},this.settings.default_group_flags,d),this.trigger("afterAddGroup",i),i.condition=this.settings.default_condition,b&&this.addRule(i),i},g.prototype.deleteGroup=function(a){if(a.isRoot())return!1;var b=this.trigger("beforeDeleteGroup",a);if(b.isDefaultPrevented())return!1;var c=!0;return a.each("reverse",function(a){c&=this.deleteRule(a)},function(a){c&=this.deleteGroup(a)},this),c&&(a.drop(),this.trigger("afterDeleteGroup")),c},g.prototype.updateGroupCondition=function(a){a.$el.find(">"+h.group_condition).each(function(){var b=$(this);b.prop("checked",b.val()===a.condition),b.parent().toggleClass("active",b.val()===a.condition)}),this.trigger("afterUpdateGroupCondition",a)},g.prototype.refreshGroupsConditions=function(){!function a(b){(!b.flags||b.flags&&!b.flags.condition_readonly)&&b.$el.find(">"+h.group_condition).prop("disabled",b.rules.length<=1).parent().toggleClass("disabled",b.rules.length<=1),b.each(function(a){},function(b){a(b)},this)}(this.model.root)},g.prototype.addRule=function(a,b,c){var d=this.trigger("beforeAddRule",a);if(d.isDefaultPrevented())return null;var e=this.nextRuleId(),f=$(this.getRuleTemplate(e)),g=a.addRule(f);return void 0!==b&&(g.data=b),g.__.flags=$.extend({},this.settings.default_rule_flags,c),this.trigger("afterAddRule",g),this.createRuleFilters(g),!this.settings.default_filter&&this.settings.display_empty_filter||(g.filter=this.change("getDefaultFilter",this.getFilterById(this.settings.default_filter||this.filters[0].id),g)),g},g.prototype.deleteRule=function(a){if(a.flags.no_delete)return!1;var b=this.trigger("beforeDeleteRule",a);return!b.isDefaultPrevented()&&(a.drop(),this.trigger("afterDeleteRule"),!0)},g.prototype.createRuleFilters=function(a){var b=this.change("getRuleFilters",this.filters,a),c=$(this.getRuleFilterSelect(a,b));a.$el.find(h.filter_container).html(c),this.trigger("afterCreateRuleFilters",a)},g.prototype.createRuleOperators=function(a){var b=a.$el.find(h.operator_container).empty();if(a.filter){var c=this.getOperators(a.filter),d=$(this.getRuleOperatorSelect(a,c));b.html(d),a.__.operator=c[0],this.trigger("afterCreateRuleOperators",a,c)}},g.prototype.createRuleInput=function(a){var b=a.$el.find(h.value_container).empty();if(a.__.value=void 0,a.filter&&a.operator&&0!==a.operator.nb_inputs){for(var c=this,d=$(),e=a.filter,f=0;f0&&b.append(this.settings.inputs_separator),b.append(g),d=d.add(g)}b.show(),d.on("change "+(e.input_event||""),function(){this._updating_input||(a._updating_value=!0,a.value=c.getRuleInputValue(a),a._updating_value=!1)}),e.plugin&&d[e.plugin](e.plugin_config||{}),this.trigger("afterCreateRuleInput",a),void 0!==e.default_value?a.value=e.default_value:(a._updating_value=!0,a.value=c.getRuleInputValue(a),a._updating_value=!1)}},g.prototype.updateRuleFilter=function(a,b){this.createRuleOperators(a),this.createRuleInput(a),a.$el.find(h.rule_filter).val(a.filter?a.filter.id:"-1"),b&&a.filter&&b.id!==a.filter.id&&(a.data=void 0),this.trigger("afterUpdateRuleFilter",a)},g.prototype.updateRuleOperator=function(a,b){var c=a.$el.find(h.value_container);a.operator&&0!==a.operator.nb_inputs?(c.show(),!c.is(":empty")&&b&&a.operator.nb_inputs===b.nb_inputs||this.createRuleInput(a)):(c.hide(),a.__.value=void 0),a.operator&&a.$el.find(h.rule_operator).val(a.operator.type),this.trigger("afterUpdateRuleOperator",a),this.updateRuleValue(a)},g.prototype.updateRuleValue=function(a){a._updating_value||this.setRuleInputValue(a,a.value),this.trigger("afterUpdateRuleValue",a)},g.prototype.applyRuleFlags=function(a){var b=a.flags;b.filter_readonly&&a.$el.find(h.rule_filter).prop("disabled",!0),b.operator_readonly&&a.$el.find(h.rule_operator).prop("disabled",!0),b.value_readonly&&a.$el.find(h.rule_value).prop("disabled",!0),b.no_delete&&a.$el.find(h.delete_rule).remove(),this.trigger("afterApplyRuleFlags",a)},g.prototype.applyGroupFlags=function(a){var b=a.flags;b.condition_readonly&&a.$el.find(">"+h.group_condition).prop("disabled",!0).parent().addClass("readonly"),b.no_add_rule&&a.$el.find(h.add_rule).remove(),b.no_add_group&&a.$el.find(h.add_group).remove(),b.no_delete&&a.$el.find(h.delete_group).remove(),this.trigger("afterApplyGroupFlags",a)},g.prototype.clearErrors=function(a){a=a||this.model.root,a&&(a.error=null,a instanceof j&&a.each(function(a){a.error=null},function(a){this.clearErrors(a)},this))},g.prototype.displayError=function(a){if(this.settings.display_errors)if(null===a.error)a.$el.removeClass("has-error");else{var b=this.lang.errors[a.error[0]]||a.error[0];b=l.fmt(b,a.error.slice(1)),b=this.change("displayError",b,a.error,a),a.$el.addClass("has-error").find(h.error_container).eq(0).attr("title",b)}},g.prototype.triggerValidationError=function(a,b,c){$.isArray(b)||(b=[b]);var d=this.trigger("validationError",a,b,c);d.isDefaultPrevented()||(a.error=b)},g.prototype.destroy=function(){this.trigger("beforeDestroy"),this.status.generated_id&&this.$el.removeAttr("id"),this.clear(),this.model=null,this.$el.off(".queryBuilder").removeClass("query-builder").removeData("queryBuilder"),delete this.$el[0].queryBuilder},g.prototype.reset=function(){this.status.group_id=1,this.status.rule_id=0,this.model.root.empty(),this.addRule(this.model.root),this.trigger("afterReset")},g.prototype.clear=function(){this.status.group_id=0,this.status.rule_id=0,this.model.root&&(this.model.root.drop(),this.model.root=null),this.trigger("afterClear")},g.prototype.setOptions=function(a){$.each(a,function(a,b){g.modifiable_options.indexOf(a)!==-1&&(this.settings[a]=b)}.bind(this))},g.prototype.getModel=function(a){return a?b(a):this.model.root},g.prototype.validate=function(){this.clearErrors();var a=this,b=function c(b){var d=0,e=0;return b.each(function(b){if(!b.filter)return a.triggerValidationError(b,"no_filter",null),void e++;if(!b.operator)return a.triggerValidationError(b,"no_operator",null),void e++;if(0!==b.operator.nb_inputs){var c=a.validateValue(b,b.value);if(c!==!0)return a.triggerValidationError(b,c,b.value),void e++}d++},function(a){c(a)?d++:e++}),!(e>0)&&(!!(0!==d||a.settings.allow_empty&&b.isRoot())||(a.triggerValidationError(b,"empty_group",null),!1))}(this.model.root);return this.change("validate",b)},g.prototype.getRules=function(a){a=$.extend({get_flags:!1,allow_invalid:!1},a);var b=this.validate();if(!b&&!a.allow_invalid)return null;var c=this,d=function e(b){var d={condition:b.condition,rules:[]};if(b.data&&(d.data=$.extendext(!0,"replace",{},b.data)),a.get_flags){var f=c.getGroupFlags(b.flags,"all"===a.get_flags);$.isEmptyObject(f)||(d.flags=f)}return b.each(function(b){var e=null;b.operator&&0===b.operator.nb_inputs||(e=b.value);var f={id:b.filter?b.filter.id:null,field:b.filter?b.filter.field:null,type:b.filter?b.filter.type:null,input:b.filter?b.filter.input:null,operator:b.operator?b.operator.type:null,value:e};if((b.filter&&b.filter.data||b.data)&&(f.data=$.extendext(!0,"replace",{},b.filter.data,b.data)),a.get_flags){var g=c.getRuleFlags(b.flags,"all"===a.get_flags);$.isEmptyObject(g)||(f.flags=g)}d.rules.push(c.change("ruleToJson",f,b))},function(a){d.rules.push(e(a))},this),c.change("groupToJson",d,b)}(this.model.root);return d.valid=b,this.change("getRules",d)},g.prototype.setRules=function(a,b){b=$.extend({allow_invalid:!1},b),$.isArray(a)&&(a={condition:this.settings.default_condition,rules:a}),a&&a.rules&&(0!==a.rules.length||this.settings.allow_empty)||l.error("RulesParse","Incorrect data object passed"),this.clear(),this.setRoot(!1,a.data,this.parseGroupFlags(a)),this.applyGroupFlags(this.model.root),a=this.change("setRules",a);var c=this;!function d(a,e){null!==e&&(void 0===a.condition?a.condition=c.settings.default_condition:c.settings.conditions.indexOf(a.condition)==-1&&(l.error(!b.allow_invalid,"UndefinedCondition",'Invalid condition "{0}"',a.condition),a.condition=c.settings.default_condition),e.condition=a.condition,a.rules.forEach(function(a){var f;if(void 0!==a.rules)if(c.settings.allow_groups!==-1&&c.settings.allow_groups1){h=["operator_not_multiple",e.type];break}break;case"select":if(void 0===b[i]||0===b[i].length||d.placeholder&&b[i]==d.placeholder_value){f.allow_empty_value||(h=["select_empty"]);break}if(d.multiple&&!e.multiple&&b[i].length>1){h=["operator_not_multiple",e.type];break}break;default:switch(g.types[d.type]){case"string":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["string_empty"]);break}if(void 0!==f.min&&b[i].lengthparseInt(f.max)){h=[this.getValidationMessage(f,"max","string_exceed_max_length"),f.max];break}if(f.format&&("string"==typeof f.format&&(f.format=new RegExp(f.format)),!f.format.test(b[i]))){h=[this.getValidationMessage(f,"format","string_invalid_format"),f.format];break}break;case"number":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["number_nan"]);break}if(isNaN(b[i])){h=["number_nan"];break}if("integer"==d.type){if(parseInt(b[i])!=b[i]){h=["number_not_integer"];break}}else if(parseFloat(b[i])!=b[i]){h=["number_not_double"];break}if(void 0!==f.min&&b[i]parseFloat(f.max)){h=[this.getValidationMessage(f,"max","number_exceed_max"),f.max];break}if(void 0!==f.step&&"any"!==f.step){var j=(b[i]/f.step).toPrecision(14);if(parseInt(j)!=j){h=[this.getValidationMessage(f,"step","number_wrong_step"),f.step];break}}break;case"datetime":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["datetime_empty"]);break}if(f.format){"moment"in window||l.error("MissingLibrary","MomentJS is required for Date/Time validation. Get it here http://momentjs.com");var k=moment(b[i],f.format);if(!k.isValid()){h=[this.getValidationMessage(f,"format","datetime_invalid"),f.format];break}if(f.min&&kmoment(f.max,f.format)){h=[this.getValidationMessage(f,"max","datetime_exceed_max"),f.max];break}}break;case"boolean":if(void 0===b[i]||0===b[i].length){f.allow_empty_value||(h=["boolean_not_valid"]);break}if(c=(""+b[i]).trim().toLowerCase(),"true"!==c&&"false"!==c&&"1"!==c&&"0"!==c&&1!==b[i]&&0!==b[i]){h=["boolean_not_valid"];break}}}if(h!==!0)break}return h},g.prototype.nextGroupId=function(){return this.status.id+"_group_"+this.status.group_id++},g.prototype.nextRuleId=function(){return this.status.id+"_rule_"+this.status.rule_id++},g.prototype.getOperators=function(a){"string"==typeof a&&(a=this.getFilterById(a));for(var b=[],c=0,d=this.operators.length;c '+b+" "});break;case"select":h+='";break;case"textarea":h+='