From 98a1b268c9d318d75a949f1f943b96d48d894c67 Mon Sep 17 00:00:00 2001 From: bnayal Date: Tue, 4 Apr 2017 10:59:36 +0300 Subject: [PATCH 1/5] support getting human readable rules --- src/plugins/readable-json/plugin.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/plugins/readable-json/plugin.js diff --git a/src/plugins/readable-json/plugin.js b/src/plugins/readable-json/plugin.js new file mode 100644 index 00000000..9a563d06 --- /dev/null +++ b/src/plugins/readable-json/plugin.js @@ -0,0 +1,12 @@ +/** + * Created by bnaya on 4/3/17. + */ +QueryBuilder.define('readable-json', function(options) { + var qb = this; + console.debug('readable-json plugin loaded'); + this.on('ruleToJson.queryBuilder.filter', function (e, rule) { + e.value.readableValue = rule.textValue || e.value.value; + e.value.readableField = rule.filter.label || e.value.field; + e.value.readableOperator = qb.lang.operators[e.value.operator] || e.value.operator; + }); +}); From 0be73699442f6524954e10bee1d50f67e514f806 Mon Sep 17 00:00:00 2001 From: bnayal Date: Tue, 4 Apr 2017 11:00:44 +0300 Subject: [PATCH 2/5] datepicker plugin which ignores the "not in last days" operator --- examples/bower.json | 5 +-- examples/index.html | 17 +++++++-- src/defaults.js | 16 +++++---- src/i18n/en.json | 6 ++-- src/model.js | 8 +++++ src/plugins/rm-datepicker/plugin.js | 54 +++++++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 src/plugins/rm-datepicker/plugin.js diff --git a/examples/bower.json b/examples/bower.json index 85233d7e..d754ed2b 100644 --- a/examples/bower.json +++ b/examples/bower.json @@ -1,8 +1,9 @@ { "name": "jQuery-QueryBuilder-example", - "dependencies" : { + "dependencies": { "seiyria-bootstrap-slider": "latest", "bootswatch-dist": "#slate", - "selectize": "latest" + "selectize": "latest", + "eonasdan-bootstrap-datetimepicker": "^4.17.47" } } diff --git a/examples/index.html b/examples/index.html index 6704b236..1e83b9a6 100644 --- a/examples/index.html +++ b/examples/index.html @@ -11,6 +11,7 @@ + @@ -123,6 +124,8 @@

Output

+ + @@ -143,6 +146,8 @@

Output

+ + @@ -175,7 +180,8 @@

Output

'unique-filter': null, 'bt-checkbox': { color: 'primary' }, 'invert': null, - 'not-group': null + 'not-group': null, + 'readable-json': null }, // standard operators in custom optgroups @@ -199,10 +205,17 @@

Output

{ type: 'is_empty' }, { type: 'is_not_empty' }, { type: 'is_null' }, - { type: 'is_not_null' } + { type: 'is_not_null' }, + { type: 'in_last_x_days' }, + { type: 'not_in_last_x_days' } ], filters: [ + { + id: 'lastOrderDate', + label: 'Last order date', + type: 'datetime' + }, /* * string with separator */ diff --git a/src/defaults.js b/src/defaults.js index 84a888c3..942697cb 100644 --- a/src/defaults.js +++ b/src/defaults.js @@ -94,8 +94,8 @@ QueryBuilder.regional = {}; QueryBuilder.OPERATORS = { equal: { type: 'equal', nb_inputs: 1, multiple: false, apply_to: ['string', 'number', 'datetime', 'boolean'] }, not_equal: { type: 'not_equal', nb_inputs: 1, multiple: false, apply_to: ['string', 'number', 'datetime', 'boolean'] }, - in: { type: 'in', nb_inputs: 1, multiple: true, apply_to: ['string', 'number', 'datetime'] }, - not_in: { type: 'not_in', nb_inputs: 1, multiple: true, apply_to: ['string', 'number', 'datetime'] }, + in: { type: 'in', nb_inputs: 1, multiple: true, apply_to: ['string'] }, + not_in: { type: 'not_in', nb_inputs: 1, multiple: true, apply_to: ['string'] }, less: { type: 'less', nb_inputs: 1, multiple: false, apply_to: ['number', 'datetime'] }, less_or_equal: { type: 'less_or_equal', nb_inputs: 1, multiple: false, apply_to: ['number', 'datetime'] }, greater: { type: 'greater', nb_inputs: 1, multiple: false, apply_to: ['number', 'datetime'] }, @@ -111,7 +111,9 @@ QueryBuilder.OPERATORS = { is_empty: { type: 'is_empty', nb_inputs: 0, multiple: false, apply_to: ['string'] }, is_not_empty: { type: 'is_not_empty', nb_inputs: 0, multiple: false, apply_to: ['string'] }, is_null: { type: 'is_null', nb_inputs: 0, multiple: false, apply_to: ['string', 'number', 'datetime', 'boolean'] }, - is_not_null: { type: 'is_not_null', nb_inputs: 0, multiple: false, apply_to: ['string', 'number', 'datetime', 'boolean'] } + is_not_null: { type: 'is_not_null', nb_inputs: 0, multiple: false, apply_to: ['string', 'number', 'datetime', 'boolean'] }, + in_last_x_days: {type: 'in_last_x_days', nb_inputs: 1, multiple: false, apply_to: ['datetime'], input_type: 'integer'}, + not_in_last_x_days: {type: 'not_in_last_x_days', nb_inputs: 1, multiple: false, apply_to: ['datetime'], input_type: 'integer'} }; /** @@ -162,8 +164,6 @@ QueryBuilder.DEFAULTS = { operators: [ 'equal', 'not_equal', - 'in', - 'not_in', 'less', 'less_or_equal', 'greater', @@ -178,8 +178,10 @@ QueryBuilder.DEFAULTS = { 'not_ends_with', 'is_empty', 'is_not_empty', - 'is_null', - 'is_not_null' + 'in_last_x_days', + 'not_in_last_x_days', + 'in', + 'not_in' ], icons: { diff --git a/src/i18n/en.json b/src/i18n/en.json index b5828139..aa5679b5 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -32,7 +32,9 @@ "is_empty": "is empty", "is_not_empty": "is not empty", "is_null": "is null", - "is_not_null": "is not null" + "is_not_null": "is not null", + "in_last_x_days": "is within the last x days", + "not_in_last_x_days": "is not within the last x days" }, "errors": { @@ -58,4 +60,4 @@ "boolean_not_valid": "Not a boolean", "operator_not_multiple": "Operator {0} cannot accept multiple values" } -} \ No newline at end of file +} diff --git a/src/model.js b/src/model.js index b5b38e3c..2b8ca8ba 100644 --- a/src/model.js +++ b/src/model.js @@ -556,6 +556,14 @@ var Rule = function(parent, $el) { * @instance */ this.__.value = undefined; + + /** + * @name input_type + * @member {*} + * @memberof Rule + * @instance + */ + this.__.input_type = null; }; Rule.prototype = Object.create(Node.prototype); diff --git a/src/plugins/rm-datepicker/plugin.js b/src/plugins/rm-datepicker/plugin.js new file mode 100644 index 00000000..165d93f4 --- /dev/null +++ b/src/plugins/rm-datepicker/plugin.js @@ -0,0 +1,54 @@ +/** + * @module BtSelectpickerPlugin + * @descriptioon Applies Bootstrap Select on filters and operators combo-boxes. + */ + +/** + * @function init + * @memberof module:BtSelectpickerPlugin + * @param {object} [options] + * @throws MissingLibraryError + */ +QueryBuilder.define('rm-datepicker', function(options) { + var Selectors = QueryBuilder.selectors; + if (!$.fn.datetimepicker || !$.fn.datetimepicker.constructor) { + Utils.error('MissingLibrary', 'datetimepicker is required to use "rm-datepicker" plugin.'); + } + + function onChange(rule, date){ + rule.value = date.format('YYYY-MM-DD'); + } + + this.on('afterCreateRuleInput', function(e, rule) { + if(rule.filter.type == "date" || rule.filter.type == "datetime") { + if(!rule.operator.input_type || rule.operator.input_type !== "integer"){ + var $inputs = rule.$el.find(Selectors.value_container).find('input'); + $inputs.datetimepicker(options); + $inputs.on('dp.change', function(e){ + onChange(rule, e.date); + }); + } + } + }); + + this.on('afterUpdateRuleOperator', function(e, rule) { + if(rule.filter.type == "date" || rule.filter.type == "datetime") { + var $inputs = rule.$el.find(Selectors.value_container).find('input'); + if(!rule.operator.input_type || rule.operator.input_type !== "integer"){ + $inputs.datetimepicker(options); + $inputs.off('dp.change').on('dp.change', function(e){ + onChange(rule, e.date); + }); + } else { + + $inputs.each(function(){ + if($(this).data("DateTimePicker") && typeof $(this).data("DateTimePicker").destroy == "function"){ + $(this).data("DateTimePicker").destroy(); + $(this).val(''); + }; + }); + } + } + }); + +}, {format: "YYYY-MM-DD"}); From b4334c15f6ea63c01ec88b0da4bd62dacfea6b35 Mon Sep 17 00:00:00 2001 From: bnayal Date: Sun, 9 Apr 2017 17:50:31 +0300 Subject: [PATCH 3/5] fix datetimepicker change event --- src/plugins/rm-datepicker/plugin.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/plugins/rm-datepicker/plugin.js b/src/plugins/rm-datepicker/plugin.js index 165d93f4..97b0a0c1 100644 --- a/src/plugins/rm-datepicker/plugin.js +++ b/src/plugins/rm-datepicker/plugin.js @@ -15,34 +15,32 @@ QueryBuilder.define('rm-datepicker', function(options) { Utils.error('MissingLibrary', 'datetimepicker is required to use "rm-datepicker" plugin.'); } - function onChange(rule, date){ - rule.value = date.format('YYYY-MM-DD'); - } - this.on('afterCreateRuleInput', function(e, rule) { - if(rule.filter.type == "date" || rule.filter.type == "datetime") { + if(rule.filter.type === "date" || rule.filter.type === "datetime") { if(!rule.operator.input_type || rule.operator.input_type !== "integer"){ var $inputs = rule.$el.find(Selectors.value_container).find('input'); $inputs.datetimepicker(options); $inputs.on('dp.change', function(e){ - onChange(rule, e.date); + $(this).trigger('change'); }); } } }); this.on('afterUpdateRuleOperator', function(e, rule) { - if(rule.filter.type == "date" || rule.filter.type == "datetime") { + if(rule.filter.type === "date" || rule.filter.type === "datetime") { var $inputs = rule.$el.find(Selectors.value_container).find('input'); + console.log($inputs); + $inputs.val(''); if(!rule.operator.input_type || rule.operator.input_type !== "integer"){ $inputs.datetimepicker(options); - $inputs.off('dp.change').on('dp.change', function(e){ - onChange(rule, e.date); + $inputs.on('dp.change', function(e){ + $(this).trigger('change'); }); } else { $inputs.each(function(){ - if($(this).data("DateTimePicker") && typeof $(this).data("DateTimePicker").destroy == "function"){ + if($(this).data("DateTimePicker") && typeof $(this).data("DateTimePicker").destroy === "function"){ $(this).data("DateTimePicker").destroy(); $(this).val(''); }; From 594384a4d423ce9a69eadbcc5f710d6a52fe8835 Mon Sep 17 00:00:00 2001 From: bnayal Date: Sun, 9 Apr 2017 17:51:02 +0300 Subject: [PATCH 4/5] new readable names for some operators --- src/i18n/en.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/i18n/en.json b/src/i18n/en.json index aa5679b5..3662f36b 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -13,14 +13,14 @@ }, "operators": { - "equal": "equal", - "not_equal": "not equal", + "equal": "equal to", + "not_equal": "not equal to", "in": "in", "not_in": "not in", - "less": "less", - "less_or_equal": "less or equal", - "greater": "greater", - "greater_or_equal": "greater or equal", + "less": "less than", + "less_or_equal": "less or equal to", + "greater": "greater than", + "greater_or_equal": "greater or equal to", "between": "between", "not_between": "not between", "begins_with": "begins with", @@ -33,8 +33,8 @@ "is_not_empty": "is not empty", "is_null": "is null", "is_not_null": "is not null", - "in_last_x_days": "is within the last x days", - "not_in_last_x_days": "is not within the last x days" + "in_last_x_days": "is within the last ___ days", + "not_in_last_x_days": "is not within the last ___ days" }, "errors": { From 5709e6daf051b0f9466c680894f5aa681efce518 Mon Sep 17 00:00:00 2001 From: bnayal Date: Sun, 9 Apr 2017 17:51:22 +0300 Subject: [PATCH 5/5] added plugin to examples --- examples/index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/index.html b/examples/index.html index 1e83b9a6..5ceb4eed 100644 --- a/examples/index.html +++ b/examples/index.html @@ -181,7 +181,8 @@

Output

'bt-checkbox': { color: 'primary' }, 'invert': null, 'not-group': null, - 'readable-json': null + 'readable-json': null, + 'rm-datepicker': null }, // standard operators in custom optgroups