From fb8e217c81ab53251fb5772dd37c9d21bfdd55ee Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Wed, 28 May 2014 13:17:06 +0530 Subject: [PATCH] Feature: Filtering and sorting in Products and Accounts list pages. --- brewman/static/partial/account-list.html | 14 ++++- brewman/static/partial/product-list.html | 2 +- brewman/static/scripts/overlord.js | 2 +- brewman/static/scripts/product.js | 67 +++--------------------- 4 files changed, 21 insertions(+), 64 deletions(-) diff --git a/brewman/static/partial/account-list.html b/brewman/static/partial/account-list.html index 13d99c11..0bdaee07 100644 --- a/brewman/static/partial/account-list.html +++ b/brewman/static/partial/account-list.html @@ -1,4 +1,14 @@ -

Accounts Add

+

Accounts +
+
+ +
+
+ Add +
+
+

@@ -10,7 +20,7 @@ - + diff --git a/brewman/static/partial/product-list.html b/brewman/static/partial/product-list.html index 58568dbe..004fdbe6 100644 --- a/brewman/static/partial/product-list.html +++ b/brewman/static/partial/product-list.html @@ -1,7 +1,7 @@

Products
- +
Add -1; - }}, - 'a': {'Col': 'IsActive', 'Comparator': function (obj, text) { - return obj === $scope.isTrue(text); - }}, - 'u': {'Col': 'Units', 'Comparator': function (obj, text) { - return obj.toLowerCase().indexOf(text) > -1; - }}, - 't': {'Col': 'ProductGroup', 'Comparator': function (obj, text) { - return obj.toLowerCase().indexOf(text) > -1; - }} + 'n': {'Col': 'Name', 'Comparator': 'text'}, + 'a': {'Col': 'IsActive', 'Comparator': 'boolean'}, + 'u': {'Col': 'Units', 'Comparator': 'text'}, + 't': {'Col': 'ProductGroup', 'Comparator': 'text'} }; $scope.sorter = { 'c': 'Code', @@ -38,34 +24,6 @@ var ProductListCtrl = ['$scope', '$location', '$routeParams', '$filter', 'produc 't': 'ProductGroup', 'a': 'IsActive' }; - $scope.parseFilterString = function (q, defaultKey, comparator, sorter) { - var re = /((([^ ]+):\s*('[^':]+'|"[^":]+"|[^ ]+))|[^ ]+[^: '"]*)/g, - matches = [], - sorting = []; - - defaultKey = comparator[defaultKey || comparator.def]; - var m = q.match(re); - _.forEach(m, function (item) { - item = item.trim(); - if (item.indexOf(':') === -1) { - if ((item.length > 1) && (item.charAt(0) === '+' || item.charAt(0) === '-') && (item.substr(1) in sorter)) { - sorting.push(item.charAt(0) + sorter[item.substr(1)]); - } else if (item.length > 2) { - matches.push({'Col': defaultKey.Col, 'Comparator': defaultKey.Comparator, 'Value': item}); - } - } else { - var key = item.substr(0, item.indexOf(':')).toLowerCase(); - var value = item.substr(item.indexOf(':') + 1, item.length).trim().toLowerCase(); - if (value.indexOf("'") !== -1 || value.indexOf('"') !== -1) { - value = value.substring(1, value.length - 1).trim(); - } - if (key !== '' && value !== '' && key in comparator) { - matches.push({'Col': comparator.Col, 'Comparator': comparator[key], 'Value': item}); - } - } - }); - return {'q': matches, 'o': sorting}; - }; $scope.filterProducts = _.debounce(function (q) { if (q !== $scope._search) { @@ -76,27 +34,16 @@ var ProductListCtrl = ['$scope', '$location', '$routeParams', '$filter', 'produc $location.path('/Products').search({'q': q}).replace(); } $scope.$apply(function () { - var matches = $scope.parseFilterString(q, 'n', $scope.comparator, $scope.sorter); + var matches = Tokenizer.parseFilterString(q, $scope.comparator, $scope.sorter); $scope.show_extended = _.any(matches.q, function (item) { return ((item.key === 'e' || item.key === 'ext') && $scope.isTrue(item.value)); }); - $scope.products = $scope.doFilter(q, $scope.info, matches.q, matches.o); + $scope.products = Tokenizer.doFilter('/Products ' + q, $scope.info, matches); }); } }, 350); - $scope.doFilter = _.memoize(function (q, array, filterExpressions, sortPredicates) { - var filterCount = filterExpressions.length; - var arrayCopy = filterCount === 0 ? array : _.filter($scope.info, function (item) { - return _.every(filterExpressions, function (expression) { - return expression.Comparator(item[expression.Col], expression.Value); - }); - }); - arrayCopy = sortPredicates.length === 0 ? arrayCopy : $filter('orderBy')(arrayCopy, sortPredicates); - return arrayCopy; - - }); }]; ProductListCtrl.resolve = {

{{item.Name}} {{item.Type}} {{item.IsActive}}