From d0cdf28af393b67897c4ac3410988cefb761cc94 Mon Sep 17 00:00:00 2001 From: cruzmiraback Date: Thu, 12 Feb 2015 12:18:28 -0700 Subject: [PATCH] Added support for 'boolean' type. --- src/plugins/mongodb-support/plugin.js | 15 ++++++++++++--- src/plugins/sql-support/plugin.js | 17 +++++++++++++---- src/query-builder.js | 27 ++++++++++++++++++++------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/plugins/mongodb-support/plugin.js b/src/plugins/mongodb-support/plugin.js index 07cfdd36..f7f621d7 100644 --- a/src/plugins/mongodb-support/plugin.js +++ b/src/plugins/mongodb-support/plugin.js @@ -79,7 +79,7 @@ } rule.value.forEach(function(v, i) { - values.push(changeType(v, rule.type)); + values.push(changeType(v, rule.type, 'mongo')); }); } @@ -102,15 +102,24 @@ // UTILITIES // =============================== /** - * Change type of a value to int or float + * Change type of a value to int, float or boolean * @param value {mixed} * @param type {string} * @return {mixed} */ - function changeType(value, type) { + function changeType(value, type, db) { switch (type) { case 'integer': return parseInt(value); case 'double': return parseFloat(value); + case 'boolean': + var bool = value.trim().toLowerCase() === "true" || value.trim() === '1' || value === 1; + if (db === 'sql') { + return bool ? 1 : 0; + } + else if (db === 'mongo') { + return bool; + } + break; default: return value; } } diff --git a/src/plugins/sql-support/plugin.js b/src/plugins/sql-support/plugin.js index d0d70c6d..736aa461 100644 --- a/src/plugins/sql-support/plugin.js +++ b/src/plugins/sql-support/plugin.js @@ -92,8 +92,8 @@ value+= sql.sep; } - if (rule.type=='integer' || rule.type=='double') { - v = changeType(v, rule.type); + if (rule.type=='integer' || rule.type=='double' || rule.type=='boolean') { + v = changeType(v, rule.type, 'sql'); } else if (!stmt) { v = escapeString(v); @@ -174,15 +174,24 @@ // UTILITIES // =============================== /** - * Change type of a value to int or float + * Change type of a value to int, float or boolean * @param value {mixed} * @param type {string} * @return {mixed} */ - function changeType(value, type) { + function changeType(value, type, db) { switch (type) { case 'integer': return parseInt(value); case 'double': return parseFloat(value); + case 'boolean': + var bool = value.trim().toLowerCase() === "true" || value.trim() === '1' || value === 1; + if (db === 'sql') { + return bool ? 1 : 0; + } + else if (db === 'mongo') { + return bool; + } + break; default: return value; } } diff --git a/src/query-builder.js b/src/query-builder.js index acb6088e..262e851c 100644 --- a/src/query-builder.js +++ b/src/query-builder.js @@ -18,12 +18,14 @@ 'double', 'date', 'time', - 'datetime' + 'datetime', + 'boolean' ], internalTypes = [ 'string', 'number', - 'datetime' + 'datetime', + 'boolean' ], inputs = [ 'text', @@ -124,13 +126,14 @@ "number_wrong_step": "Must be a multiple of {0}", "datetime_invalid": "Invalid date format ({0})", "datetime_exceed_min": "Must be after {0}", - "datetime_exceed_max": "Must be before {0}" + "datetime_exceed_max": "Must be before {0}", + "boolean_not_valid": "Not a boolean" } }, operators: [ - {type: 'equal', accept_values: 1, apply_to: ['string', 'number', 'datetime']}, - {type: 'not_equal', accept_values: 1, apply_to: ['string', 'number', 'datetime']}, + {type: 'equal', accept_values: 1, apply_to: ['string', 'number', 'datetime', 'boolean']}, + {type: 'not_equal', accept_values: 1, apply_to: ['string', 'number', 'datetime', 'boolean']}, {type: 'in', accept_values: 1, apply_to: ['string', 'number', 'datetime']}, {type: 'not_in', accept_values: 1, apply_to: ['string', 'number', 'datetime']}, {type: 'less', accept_values: 1, apply_to: ['number', 'datetime']}, @@ -146,8 +149,8 @@ {type: 'not_ends_with', accept_values: 1, apply_to: ['string']}, {type: 'is_empty', accept_values: 0, apply_to: ['string']}, {type: 'is_not_empty', accept_values: 0, apply_to: ['string']}, - {type: 'is_null', accept_values: 0, apply_to: ['string', 'number', 'datetime']}, - {type: 'is_not_null', accept_values: 0, apply_to: ['string', 'number', 'datetime']} + {type: 'is_null', accept_values: 0, apply_to: ['string', 'number', 'datetime', 'boolean']}, + {type: 'is_not_null', accept_values: 0, apply_to: ['string', 'number', 'datetime', 'boolean']} ], icons: { @@ -532,6 +535,9 @@ case 'date': case 'time': case 'datetime': filter.internalType = 'datetime'; break; + case 'boolean': + filter.internalType = 'boolean'; + break; } switch (filter.input) { @@ -1052,6 +1058,13 @@ } } break; + + case 'boolean': + if (value[i].trim().toLowerCase() !== 'true' && value[i].trim().toLowerCase() !== 'false' && + value[i].trim() !== '1' && value[i].trim() !== '0' && value[i] !== 1 && value[i] !== 0) { + result = ['boolean_not_valid']; + break; + } } }