diff --git a/dist/query-builder.min.js b/dist/query-builder.min.js index 3edc4b2a..1dcf21dc 100644 --- a/dist/query-builder.min.js +++ b/dist/query-builder.min.js @@ -3,4 +3,4 @@ * Copyright 2014 Damien "Mistic" Sorel (http://www.strangeplanet.fr) * Licensed under MIT (http://opensource.org/licenses/MIT) */ -!function(a){"use strict";var b=["string","integer","double","date","time","datetime"],c=["text","radio","checkbox","select"],d=function(b,c){var e=this;this.$el=b,this.settings=a.extend(!0,{},d.DEFAULTS,c),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||this.filters.length<1)&&a.error("Missing filters list"),this.checkFilters(),this.$el.on("change.queryBuilder",".rules-group-header input[name$=_cond]",function(){var b=a(this);b.is(":checked")&&(b.parent().addClass("active"),b.parent().siblings().removeClass("active"))}),this.$el.on("change.queryBuilder",".rule-filter-container select[name$=_filter]",function(){var b=a(this),c=b.closest("li");e.createRuleOperators(c,b.val()),e.createRuleInput(c,b.val())}),this.$el.on("change.queryBuilder",".rule-operator-container select[name$=_operator]",function(){var b=a(this),c=b.closest("li");e.updateRuleOperator(c,b.val())}),this.$el.on("click.queryBuilder","[data-add=rule]",function(){var b=a(this),c=b.closest("dl").find(">dd>ul");e.addRule(c)}),this.$el.on("click.queryBuilder","[data-add=group]",function(){var b=a(this),c=b.closest("dl").find(">dd>ul");e.addGroup(c)}),this.$el.on("click.queryBuilder","[data-delete=rule]",function(){var b=a(this),c=b.closest("li");c.remove()}),this.$el.on("click.queryBuilder","[data-delete=group]",function(){var b=a(this),c=b.closest("dl");c.remove()}),this.settings.sortable&&this.initSortable(),this.$el.addClass("query-builder"),this.addGroup(this.$el)};d.DEFAULTS={onValidationError:null,onAfterAddGroup:null,onAfterAddRule:null,sortable:!1,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(b){a.extend(!0,d.DEFAULTS,b)}},d.prototype.destroy=function(){this.status.generatedId&&this.$el.removeAttr("id"),this.$el.empty().off("click.queryBuilder change.queryBuilder").removeClass("query-builder").removeData("queryBuilder")},d.prototype.reset=function(){this.status.group_id=1,this.status.rule_id=0,this.addRule(this.$el.find(">dl>dd>ul").empty())},d.prototype.clear=function(){this.status.group_id=0,this.status.rule_id=0,this.$el.empty()},d.prototype.getRules=function(){this.markRuleAsError(this.$el.find("li"),!1);var b=this.$el.find(">dl"),c=this;return function d(b){var e={},f=b.find(">dd>ul>*");e.condition=b.find(">dt input[name$=_cond]:checked").val(),e.rules=[];for(var g=0,h=f.length;h>g;g++){var i=f.eq(g);if(i.hasClass("rule-container")){var j=c.getRuleFilter(i);if("-1"==j)continue;var k=c.getFilterById(j),l=c.getOperatorByType(c.getRuleOperator(i)),m=null;if(l.accept_values){m=c.getRuleValue(i,k),k.valueParser&&(m=k.valueParser.call(this,i,m,k,l));var n=c.validateValue(m,k);if(n!==!0)return c.markRuleAsError(i,!0),c.triggerValidationError(n,i,m,k,l),{}}var o={id:k.id,field:k.field,type:k.type,input:k.input,operator:l.type,value:m};e.rules.push(o)}else{var o=d(i);if(a.isEmptyObject(o))return{};e.rules.push(o)}}return 0==e.rules.length?{}:e}(b)},d.prototype.setRules=function(b){this.clear();var c=this.$el,d=this;!function e(b,c){var f=d.addGroup(c,!1),g=f.find(">dd>ul"),h=f.find(">dt input[name$=_cond]");b.condition||(b.condition="AND"),h.filter("[value=AND]").prop("checked","AND"==b.condition.toUpperCase()),h.filter("[value=OR]").prop("checked","OR"==b.condition.toUpperCase()),h.trigger("change"),a.each(b.rules,function(b,c){if(c.rules&&c.rules.length>0)e(c,g);else{c.id||a.error("Missing rule field id"),c.value||(c.value=""),c.operator||(c.operator="equal");var f=d.addRule(g),h=d.getFilterById(c.id),i=d.getOperatorByType(c.operator),j=f.find(".rule-value-container");if(f.find(".rule-filter-container select[name$=_filter]").val(c.id).trigger("change"),f.find(".rule-operator-container select[name$=_operator]").val(c.operator).trigger("change"),i.accept_values)switch(h.input){case"radio":j.find('input[name$=_value][value="'+c.value+'"]').prop("checked",!0).trigger("change");break;case"checkbox":a.isArray(c.value)||(c.value=[c.value]),a.each(c.value,function(a,b){j.find('input[name$=_value][value="'+b+'"]').prop("checked",!0).trigger("change")});break;case"select":j.find("select[name$=_value]").val(c.value).trigger("change");break;case"text":default:j.find("input[name$=_value]").val(c.value).trigger("change")}h.onAfterSetValue&&h.onAfterSetValue.call(this,f,c.value,h,i)}})}(b,c)},d.prototype.checkFilters=function(){var d=[];a.each(this.filters,function(e,f){switch(f.id||a.error("Missing filter id: "+e),-1!=d.indexOf(f.id)&&a.error("Filter already defined: "+f.id),d.push(f.id),f.type||a.error("Missing filter type: "+f.id),-1==b.indexOf(f.type)&&a.error("Invalid type: "+f.type),f.input?"function"!=typeof f.input&&-1==c.indexOf(f.input)&&a.error("Invalid input: "+f.input):f.input="text",f.field||(f.field=f.id),f.label||(f.label=f.field),f.type){case"string":f.internalType="string";break;case"integer":case"double":f.internalType="number";break;case"date":case"time":case"datetime":f.internalType="datetime"}switch(f.input){case"radio":case"checkbox":(!f.values||f.values.length<1)&&a.error("Missing values for filter: "+f.id)}})},d.prototype.addGroup=function(a,b){b=null==b?!0:b;var c=this.nextGroupId();a.append(this.getGroupTemplate(c));var d=a.find("#"+c);return this.settings.onAfterAddGroup&&this.settings.onAfterAddGroup.call(this,d),b&&this.addRule(d.find(">dd>ul")),d},d.prototype.addRule=function(a){var b=this.nextRuleId();a.append(this.getRuleTemplate(b));var c=a.find("#"+b);return this.settings.onAfterAddRule&&this.settings.onAfterAddRule.call(this,c),c},d.prototype.createRuleOperators=function(a,b){var c=a.find(".rule-operator-container").empty(),d=a.attr("id");if("-1"!=b){var e=this.getOperators(b),f=this.getRuleOperatorSelect(d,e);c.html(f)}},d.prototype.createRuleInput=function(a,b){if("-1"!=b){var c=a.find(".rule-value-container").empty(),d=a.attr("id"),e=this.getOperatorByType(this.getRuleOperator(a));if(e.accept_values){var f=this.getFilterById(b),g=this.getRuleInput(d,f);c.html(g).show(),f.onAfterCreateRuleInput&&f.onAfterCreateRuleInput.call(this,a,f),f.plugin&&c.find("select"==f.input?"select":"input")[f.plugin](f.plugin_config||{})}}},d.prototype.updateRuleOperator=function(a,b){var c=a.find(".rule-value-container"),d=this.getFilterById(this.getRuleFilter(a)),e=this.getOperatorByType(b);e.accept_values?(c.show(),c.is(":empty")&&this.createRuleInput(a,d.id)):c.hide(),d.onAfterChangeOperator&&d.onAfterChangeOperator.call(this,a,d,e)},d.prototype.validateValue=function(a,b){var c=b.validation||{};if(c.callback)return c.callback.call(this,a,b);switch(b.input){case"radio":if(void 0==a)return"radio_empty";break;case"checkbox":if(0==a.length)return"checkbox_empty";break;case"select":if(b.multiple){if(0==a.length)return"select_empty"}else if(void 0==a)return"select_empty";break;case"text":default:switch(b.internalType){case"string":if(void 0!=c.min){if(a.lengthc.max)return"string_exceed_max_length";if(c.format&&!c.format.test(a))return"string_invalid_format";break;case"number":if(isNaN(a))return"number_nan";if("integer"==b.type){if(parseInt(a)!=a)return"number_not_integer"}else if(parseFloat(a)!=a)return"number_not_double";if(void 0!=c.min&&ac.max)return"number_exceed_max";if(c.step){var d=a/c.step;if(parseInt(d)!=d)return"number_wrong_step"}break;case"datetime":if(window.moment&&c.format){var e=moment(a,c.format);if(!e.isValid())return"datetime_invalid";if(c.min&&emoment(c.max,c.format))return"datetime_exceed_max"}}}return!0},d.prototype.markRuleAsError=function(a,b){b?a.addClass("has-error"):a.removeClass("has-error")},d.prototype.triggerValidationError=function(a,b,c,d,e){d.onValidationError&&d.onValidationError.call(this,b,a,c,d,e),this.settings.onValidationError&&this.settings.onValidationError.call(this,b,a,c,d,e);var f=jQuery.Event("validationError.queryBuilder",{error:a,filter:d,operator:e,value:c,targetRule:b[0],builder:this});this.$el.trigger(f)},d.prototype.initSortable=function(){a.event.props.push("dataTransfer");var b,c=!1;this.$el.on("mousedown",".drag-handle",function(){c=!0}),this.$el.on("mouseup",".drag-handle",function(){c=!1}),this.$el.on("dragstart","[draggable]",function(d){d.stopPropagation(),c?(d.dataTransfer.setData("id",d.target.id),b=a('
 
'),b.height(a(d.target).height()),b.insertAfter(d.target),a(d.target).hide(),c=!1):d.preventDefault()}),this.$el.on("dragenter","[draggable]",function(c){c.stopPropagation();var d,e=a(c.target);return d=e.closest(".rule-container"),d.length?void b.detach().insertAfter(d):(d=e.closest(".rules-group-container"),d.length?void b.detach().appendTo(d.find(".rules-list").eq(0)):void 0)}),this.$el.on("dragover","[draggable]",function(a){a.preventDefault(),a.stopPropagation()}),this.$el.on("drop",function(b){b.stopPropagation();var c,d=a("#"+b.dataTransfer.getData("id")),e=a(b.target);return c=e.closest(".rule-container"),c.length?void d.detach().insertAfter(c):(c=e.closest(".rules-group-container"),c.length?void d.detach().appendTo(c.find(".rules-list").eq(0)):void 0)}),this.$el.on("dragend","[draggable]",function(c){c.stopPropagation();var d=a("#"+c.dataTransfer.getData("id"));d.show(),b.remove()})},d.prototype.nextGroupId=function(){return this.$el_id+"_group_"+this.status.group_id++},d.prototype.nextRuleId=function(){return this.$el_id+"_rule_"+this.status.rule_id++},d.prototype.getOperators=function(a){"string"==typeof a&&(a=this.getFilterById(a));for(var b=[],c=0,d=this.operators.length;d>c;c++)-1!=this.operators[c].apply_to.indexOf(a.internalType)&&(a.operators&&-1==a.operators.indexOf(this.operators[c].type)||b.push({type:this.operators[c].type,label:this.lang["operator_"+this.operators[c].type]}));return b},d.prototype.getFilterById=function(a){for(var b=0,c=this.filters.length;c>b;b++)if(this.filters[b].id==a)return this.filters[b];throw"Undefined filter: "+a},d.prototype.getOperatorByType=function(a){for(var b=0,c=this.operators.length;c>b;b++)if(this.operators[b].type==a)return this.operators[b];throw"Undefined operator: "+a},d.prototype.getRuleFilter=function(a){return a.find(".rule-filter-container select[name$=_filter]").val()},d.prototype.getRuleOperator=function(a){return a.find(".rule-operator-container select[name$=_operator]").val()},d.prototype.getRuleValue=function(b,c){c=c||this.getFilterByType(this.getRulefilter(b));var d,e=b.find(".rule-value-container");switch(c.input){case"radio":d=e.find("input[name$=_value]:checked").val();break;case"checkbox":d=[],e.find("input[name$=_value]:checked").each(function(){d.push(a(this).val())});break;case"select":c.multiple?(d=[],e.find("select[name$=_value] option:selected").each(function(){d.push(a(this).val())})):d=e.find("select[name$=_value] option:selected").val();break;case"text":default:d=e.find("input[name$=_value]").val()}return d},d.prototype.getGroupTemplate=function(a){return"
"+(this.settings.sortable?'
':"")+"
    "},d.prototype.getRuleTemplate=function(a){return"
  • "+(this.settings.sortable?'
    ':"")+'
    '+this.getRuleFilterSelect(a)+'
  • '},d.prototype.getRuleFilterSelect=function(b){var c='"},d.prototype.getRuleOperatorSelect=function(a,b){for(var c='"},d.prototype.getRuleInput=function(b,c){if("function"==typeof c.input){var d=this.$el.find("#"+b);return c.input.call(this,d,c)}var e=c.validation||{},f="";switch(c.input){case"radio":var g=c.vertical?" class=block":"";a.each(c.values,function(a,c){f+=" '+c+" "});break;case"checkbox":var g=c.vertical?" class=block":"";a.each(c.values,function(a,c){f+=" '+c+" "});break;case"select":f+='";break;case"text":default:switch(c.internalType){case"number":f+='1&&a.error("Unable to initialize on multiple target");var c=this.data("queryBuilder"),e="object"==typeof b&&b||{};return c||"destroy"!=b?(c||this.data("queryBuilder",new d(this,e)),"string"==typeof b?c[b].apply(c,Array.prototype.slice.call(arguments,1)):this):this}}(jQuery); \ No newline at end of file +!function(a){"use strict";var b=["string","integer","double","date","time","datetime"],c=["text","radio","checkbox","select"],d=function(b,c){var e=this;this.$el=b,this.settings=a.extend(!0,{},d.DEFAULTS,c),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||this.filters.length<1)&&a.error("Missing filters list"),this.checkFilters(),this.$el.on("change.queryBuilder",".rules-group-header input[name$=_cond]",function(){var b=a(this);b.is(":checked")&&(b.parent().addClass("active"),b.parent().siblings().removeClass("active"))}),this.$el.on("change.queryBuilder",".rule-filter-container select[name$=_filter]",function(){var b=a(this),c=b.closest("li");e.createRuleOperators(c,b.val()),e.createRuleInput(c,b.val())}),this.$el.on("change.queryBuilder",".rule-operator-container select[name$=_operator]",function(){var b=a(this),c=b.closest("li");e.updateRuleOperator(c,b.val())}),this.$el.on("click.queryBuilder","[data-add=rule]",function(){var b=a(this),c=b.closest("dl").find(">dd>ul");e.addRule(c)}),this.$el.on("click.queryBuilder","[data-add=group]",function(){var b=a(this),c=b.closest("dl").find(">dd>ul");e.addGroup(c)}),this.$el.on("click.queryBuilder","[data-delete=rule]",function(){var b=a(this),c=b.closest("li");c.remove()}),this.$el.on("click.queryBuilder","[data-delete=group]",function(){var b=a(this),c=b.closest("dl");c.remove()}),this.settings.sortable&&this.initSortable(),this.$el.addClass("query-builder"),this.addGroup(this.$el)};d.DEFAULTS={onValidationError:null,onAfterAddGroup:null,onAfterAddRule:null,sortable:!1,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(b){a.extend(!0,d.DEFAULTS,b)}},d.prototype.destroy=function(){this.status.generatedId&&this.$el.removeAttr("id"),this.$el.empty().off("click.queryBuilder change.queryBuilder").removeClass("query-builder").removeData("queryBuilder")},d.prototype.reset=function(){this.status.group_id=1,this.status.rule_id=0,this.addRule(this.$el.find(">dl>dd>ul").empty())},d.prototype.clear=function(){this.status.group_id=0,this.status.rule_id=0,this.$el.empty()},d.prototype.getRules=function(){this.markRuleAsError(this.$el.find("li"),!1);var b=this.$el.find(">dl"),c=this;return function d(b){var e={},f=b.find(">dd>ul>*");e.condition=b.find(">dt input[name$=_cond]:checked").val(),e.rules=[];for(var g=0,h=f.length;h>g;g++){var i=f.eq(g);if(i.hasClass("rule-container")){var j=c.getRuleFilter(i);if("-1"==j)continue;var k=c.getFilterById(j),l=c.getOperatorByType(c.getRuleOperator(i)),m=null;if(l.accept_values){m=c.getRuleValue(i,k),k.valueParser&&(m=k.valueParser.call(this,i,m,k,l));var n=c.validateValue(m,k);if(n!==!0)return c.markRuleAsError(i,!0),c.triggerValidationError(n,i,m,k,l),{}}var o={id:k.id,field:k.field,type:k.type,input:k.input,operator:l.type,value:m};e.rules.push(o)}else{var o=d(i);if(a.isEmptyObject(o))return{};e.rules.push(o)}}return 0==e.rules.length?{}:e}(b)},d.prototype.setRules=function(b){this.clear();var c=this.$el,d=this;!function e(b,c){var f=d.addGroup(c,!1),g=f.find(">dd>ul"),h=f.find(">dt input[name$=_cond]");b.condition||(b.condition="AND"),h.filter("[value=AND]").prop("checked","AND"==b.condition.toUpperCase()),h.filter("[value=OR]").prop("checked","OR"==b.condition.toUpperCase()),h.trigger("change"),a.each(b.rules,function(b,c){if(c.rules&&c.rules.length>0)e(c,g);else{c.id||a.error("Missing rule field id"),c.value||(c.value=""),c.operator||(c.operator="equal");var f=d.addRule(g),h=d.getFilterById(c.id),i=d.getOperatorByType(c.operator),j=f.find(".rule-value-container");if(f.find(".rule-filter-container select[name$=_filter]").val(c.id).trigger("change"),f.find(".rule-operator-container select[name$=_operator]").val(c.operator).trigger("change"),i.accept_values)switch(h.input){case"radio":j.find('input[name$=_value][value="'+c.value+'"]').prop("checked",!0).trigger("change");break;case"checkbox":a.isArray(c.value)||(c.value=[c.value]),a.each(c.value,function(a,b){j.find('input[name$=_value][value="'+b+'"]').prop("checked",!0).trigger("change")});break;case"select":j.find("select[name$=_value]").val(c.value).trigger("change");break;case"text":default:j.find("input[name$=_value]").val(c.value).trigger("change")}h.onAfterSetValue&&h.onAfterSetValue.call(this,f,c.value,h,i)}})}(b,c)},d.prototype.checkFilters=function(){var d=[];a.each(this.filters,function(e,f){switch(f.id||a.error("Missing filter id: "+e),-1!=d.indexOf(f.id)&&a.error("Filter already defined: "+f.id),d.push(f.id),f.type||a.error("Missing filter type: "+f.id),-1==b.indexOf(f.type)&&a.error("Invalid type: "+f.type),f.input?"function"!=typeof f.input&&-1==c.indexOf(f.input)&&a.error("Invalid input: "+f.input):f.input="text",f.field||(f.field=f.id),f.label||(f.label=f.field),f.type){case"string":f.internalType="string";break;case"integer":case"double":f.internalType="number";break;case"date":case"time":case"datetime":f.internalType="datetime"}switch(f.input){case"radio":case"checkbox":(!f.values||f.values.length<1)&&a.error("Missing values for filter: "+f.id)}})},d.prototype.addGroup=function(a,b){b=null==b?!0:b;var c=this.nextGroupId();a.append(this.getGroupTemplate(c));var d=a.find("#"+c);return this.settings.onAfterAddGroup&&this.settings.onAfterAddGroup.call(this,d),b&&this.addRule(d.find(">dd>ul")),d},d.prototype.addRule=function(a){var b=this.nextRuleId();a.append(this.getRuleTemplate(b));var c=a.find("#"+b);return this.settings.onAfterAddRule&&this.settings.onAfterAddRule.call(this,c),c},d.prototype.createRuleOperators=function(a,b){var c=a.find(".rule-operator-container").empty(),d=a.attr("id");if("-1"!=b){var e=this.getOperators(b),f=this.getRuleOperatorSelect(d,e);c.html(f)}},d.prototype.createRuleInput=function(a,b){if("-1"!=b){var c=a.find(".rule-value-container").empty(),d=a.attr("id"),e=this.getOperatorByType(this.getRuleOperator(a));if(e.accept_values){var f=this.getFilterById(b),g=this.getRuleInput(d,f);c.html(g).show(),f.onAfterCreateRuleInput&&f.onAfterCreateRuleInput.call(this,a,f),f.plugin&&c.find("select"==f.input?"select":"input")[f.plugin](f.plugin_config||{})}}},d.prototype.updateRuleOperator=function(a,b){var c=a.find(".rule-value-container"),d=this.getFilterById(this.getRuleFilter(a)),e=this.getOperatorByType(b);e.accept_values?(c.show(),c.is(":empty")&&this.createRuleInput(a,d.id)):c.hide(),d.onAfterChangeOperator&&d.onAfterChangeOperator.call(this,a,d,e)},d.prototype.validateValue=function(a,b){var c=b.validation||{};if(c.callback)return c.callback.call(this,a,b);switch(b.input){case"radio":if(void 0==a)return"radio_empty";break;case"checkbox":if(0==a.length)return"checkbox_empty";break;case"select":if(b.multiple){if(0==a.length)return"select_empty"}else if(void 0==a)return"select_empty";break;case"text":default:switch(b.internalType){case"string":if(void 0!=c.min){if(a.lengthc.max)return"string_exceed_max_length";if(c.format&&!c.format.test(a))return"string_invalid_format";break;case"number":if(isNaN(a))return"number_nan";if("integer"==b.type){if(parseInt(a)!=a)return"number_not_integer"}else if(parseFloat(a)!=a)return"number_not_double";if(void 0!=c.min&&ac.max)return"number_exceed_max";if(c.step){var d=a/c.step;if(parseInt(d)!=d)return"number_wrong_step"}break;case"datetime":if(window.moment&&c.format){var e=moment(a,c.format);if(!e.isValid())return"datetime_invalid";if(c.min&&emoment(c.max,c.format))return"datetime_exceed_max"}}}return!0},d.prototype.markRuleAsError=function(a,b){b?a.addClass("has-error"):a.removeClass("has-error")},d.prototype.triggerValidationError=function(a,b,c,d,e){d.onValidationError&&d.onValidationError.call(this,b,a,c,d,e),this.settings.onValidationError&&this.settings.onValidationError.call(this,b,a,c,d,e);var f=jQuery.Event("validationError.queryBuilder",{error:a,filter:d,operator:e,value:c,targetRule:b[0],builder:this});this.$el.trigger(f)},d.prototype.initSortable=function(){a.event.props.push("dataTransfer");var b,c,d=!1;this.$el.on("mousedown",".drag-handle",function(){d=!0}),this.$el.on("mouseup",".drag-handle",function(){d=!1}),this.$el.on("dragstart","[draggable]",function(e){e.stopPropagation(),d?(e.dataTransfer.setData("drag",!0),c=a(e.target),b=a('
     
    '),b.css("min-height",c.height()),b.insertAfter(c),setTimeout(function(){c.hide()},0),d=!1):e.preventDefault()}),this.$el.on("dragenter","[draggable]",function(c){c.stopPropagation();var d,e=a(c.target);return d=e.closest(".rule-container"),d.length?void b.detach().insertAfter(d):(d=e.closest(".rules-group-container"),d.length?void b.detach().appendTo(d.find(".rules-list").eq(0)):void 0)}),this.$el.on("dragover","[draggable]",function(a){a.preventDefault(),a.stopPropagation()}),this.$el.on("drop",function(b){b.stopPropagation();var d,e=a(b.target);return d=e.closest(".rule-container"),d.length?void c.detach().insertAfter(d):(d=e.closest(".rules-group-container"),d.length?void c.detach().appendTo(d.find(".rules-list").eq(0)):void 0)}),this.$el.on("dragend","[draggable]",function(a){a.stopPropagation(),c.show(),b.remove()})},d.prototype.nextGroupId=function(){return this.$el_id+"_group_"+this.status.group_id++},d.prototype.nextRuleId=function(){return this.$el_id+"_rule_"+this.status.rule_id++},d.prototype.getOperators=function(a){"string"==typeof a&&(a=this.getFilterById(a));for(var b=[],c=0,d=this.operators.length;d>c;c++)-1!=this.operators[c].apply_to.indexOf(a.internalType)&&(a.operators&&-1==a.operators.indexOf(this.operators[c].type)||b.push({type:this.operators[c].type,label:this.lang["operator_"+this.operators[c].type]}));return b},d.prototype.getFilterById=function(a){for(var b=0,c=this.filters.length;c>b;b++)if(this.filters[b].id==a)return this.filters[b];throw"Undefined filter: "+a},d.prototype.getOperatorByType=function(a){for(var b=0,c=this.operators.length;c>b;b++)if(this.operators[b].type==a)return this.operators[b];throw"Undefined operator: "+a},d.prototype.getRuleFilter=function(a){return a.find(".rule-filter-container select[name$=_filter]").val()},d.prototype.getRuleOperator=function(a){return a.find(".rule-operator-container select[name$=_operator]").val()},d.prototype.getRuleValue=function(b,c){c=c||this.getFilterByType(this.getRulefilter(b));var d,e=b.find(".rule-value-container");switch(c.input){case"radio":d=e.find("input[name$=_value]:checked").val();break;case"checkbox":d=[],e.find("input[name$=_value]:checked").each(function(){d.push(a(this).val())});break;case"select":c.multiple?(d=[],e.find("select[name$=_value] option:selected").each(function(){d.push(a(this).val())})):d=e.find("select[name$=_value] option:selected").val();break;case"text":default:d=e.find("input[name$=_value]").val()}return d},d.prototype.getGroupTemplate=function(a){return"
    "+(this.settings.sortable?'
    ':"")+"
      "},d.prototype.getRuleTemplate=function(a){return"
    • "+(this.settings.sortable?'
      ':"")+'
      '+this.getRuleFilterSelect(a)+'
    • '},d.prototype.getRuleFilterSelect=function(b){var c='"},d.prototype.getRuleOperatorSelect=function(a,b){for(var c='"},d.prototype.getRuleInput=function(b,c){if("function"==typeof c.input){var d=this.$el.find("#"+b);return c.input.call(this,d,c)}var e=c.validation||{},f="";switch(c.input){case"radio":var g=c.vertical?" class=block":"";a.each(c.values,function(a,c){f+=" '+c+" "});break;case"checkbox":var g=c.vertical?" class=block":"";a.each(c.values,function(a,c){f+=" '+c+" "});break;case"select":f+='";break;case"text":default:switch(c.internalType){case"number":f+='1&&a.error("Unable to initialize on multiple target");var c=this.data("queryBuilder"),e="object"==typeof b&&b||{};return c||"destroy"!=b?(c||this.data("queryBuilder",new d(this,e)),"string"==typeof b?c[b].apply(c,Array.prototype.slice.call(arguments,1)):this):this}}(jQuery); \ No newline at end of file diff --git a/src/query-builder.js b/src/query-builder.js index 888763c8..e41e186d 100644 --- a/src/query-builder.js +++ b/src/query-builder.js @@ -735,9 +735,10 @@ * Init HTML5 drag and drop */ QueryBuilder.prototype.initSortable = function() { + // configure jQuery to use dataTransfer $.event.props.push('dataTransfer'); - var placeholder, isHandle = false; + var placeholder, src, isHandle = false; this.$el.on('mousedown', '.drag-handle', function(e) { isHandle = true; @@ -750,13 +751,19 @@ e.stopPropagation(); if (isHandle) { - e.dataTransfer.setData('id', e.target.id); + // notify drag and drop + e.dataTransfer.setData('drag', true); + + src = $(e.target); placeholder = $('
       
      '); - placeholder.height($(e.target).height()); - placeholder.insertAfter(e.target); + placeholder.css('min-height', src.height()); + placeholder.insertAfter(src); - $(e.target).hide(); + // Chrome glitch (helper invisible if hidden immediately) + setTimeout(function() { + src.hide(); + }, 0); isHandle = false; } @@ -791,8 +798,7 @@ this.$el.on('drop', function(e) { e.stopPropagation(); - var src = $('#'+ e.dataTransfer.getData('id')), - target = $(e.target), parent; + var target = $(e.target), parent; parent = target.closest('.rule-container'); if (parent.length) { @@ -810,8 +816,6 @@ this.$el.on('dragend', '[draggable]', function(e) { e.stopPropagation(); - var src = $('#'+ e.dataTransfer.getData('id')); - src.show(); placeholder.remove(); });