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..5ceb4eed 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,9 @@

Output

'unique-filter': null, 'bt-checkbox': { color: 'primary' }, 'invert': null, - 'not-group': null + 'not-group': null, + 'readable-json': null, + 'rm-datepicker': null }, // standard operators in custom optgroups @@ -199,10 +206,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..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", @@ -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 ___ days", + "not_in_last_x_days": "is not within the last ___ 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/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; + }); +}); diff --git a/src/plugins/rm-datepicker/plugin.js b/src/plugins/rm-datepicker/plugin.js new file mode 100644 index 00000000..97b0a0c1 --- /dev/null +++ b/src/plugins/rm-datepicker/plugin.js @@ -0,0 +1,52 @@ +/** + * @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.'); + } + + 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){ + $(this).trigger('change'); + }); + } + } + }); + + 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'); + console.log($inputs); + $inputs.val(''); + if(!rule.operator.input_type || rule.operator.input_type !== "integer"){ + $inputs.datetimepicker(options); + $inputs.on('dp.change', function(e){ + $(this).trigger('change'); + }); + } else { + + $inputs.each(function(){ + if($(this).data("DateTimePicker") && typeof $(this).data("DateTimePicker").destroy === "function"){ + $(this).data("DateTimePicker").destroy(); + $(this).val(''); + }; + }); + } + } + }); + +}, {format: "YYYY-MM-DD"});