Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 1 | (function() { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** |
| 5 | * @ngdoc service |
| 6 | * @name xos.uiComponents.Comparator |
| 7 | * @description |
| 8 | * This factory define a function that replace the native angular.filter comparator. |
| 9 | * |
| 10 | * It is done to allow the comparation between (0|1) values with booleans. |
| 11 | * >Note that this factory return a single function, not an object. |
| 12 | * |
| 13 | * The tipical usage of this factory is inside an `ng-repeat` |
| 14 | * @example |
| 15 | * <example module="comparator"> |
| 16 | * <file name="index.html"> |
| 17 | * <div ng-controller="sample as vm"> |
| 18 | * <div class="row"> |
| 19 | * <div class="col-xs-6"> |
| 20 | * <label>Filter by name:</label> |
| 21 | * <input class="form-control" type="text" ng-model="vm.query.name"/> |
| 22 | * </div> |
| 23 | * <div class="col-xs-6"> |
| 24 | * <label>Filter by status:</label> |
| 25 | * <select |
| 26 | * ng-model="vm.query.status" |
| 27 | * ng-options="i for i in [true, false]"> |
| 28 | * </select> |
| 29 | * </div> |
| 30 | * </div> |
| 31 | * <div ng-repeat="item in vm.data | filter:vm.query:vm.comparator"> |
| 32 | * <div class="row"> |
| 33 | * <div class="col-xs-6">{{item.name}}</div> |
| 34 | * <div class="col-xs-6">{{item.status}}</div> |
| 35 | * </div> |
| 36 | * </div> |
| 37 | * </div> |
| 38 | * </file> |
| 39 | * <file name="script.js"> |
| 40 | * angular.module('comparator', ['xos.uiComponents']) |
| 41 | * .controller('sample', function(Comparator){ |
| 42 | * this.comparator = Comparator; |
| 43 | * this.data = [ |
| 44 | * {name: 'Jhon', status: 1}, |
| 45 | * {name: 'Jack', status: 0}, |
| 46 | * {name: 'Mike', status: 1}, |
| 47 | * {name: 'Scott', status: 0} |
| 48 | * ]; |
| 49 | * }); |
| 50 | * </file> |
| 51 | * </example> |
| 52 | **/ |
| 53 | |
| 54 | angular |
| 55 | .module('xos.uiComponents') |
| 56 | .factory('Comparator', comparator); |
| 57 | |
| 58 | function comparator(_) { |
| 59 | |
| 60 | return function(actual, expected){ |
| 61 | |
| 62 | if (angular.isUndefined(actual)) { |
| 63 | // No substring matching against `undefined` |
| 64 | return false; |
| 65 | } |
| 66 | if ((actual === null) || (expected === null)) { |
| 67 | // No substring matching against `null`; only match against `null` |
| 68 | return actual === expected; |
| 69 | } |
| 70 | if (angular.isObject(expected) || (angular.isObject(actual))){ |
| 71 | return angular.equals(expected, actual); |
| 72 | } |
| 73 | |
| 74 | if(_.isBoolean(actual) || _.isBoolean(expected)){ |
| 75 | if(actual === 0 || actual === 1){ |
| 76 | actual = !!actual; |
| 77 | } |
| 78 | return angular.equals(expected, actual); |
| 79 | } |
| 80 | |
| 81 | if(!angular.isString(actual) || !angular.isString(expected)){ |
| 82 | if(angular.isDefined(actual.toString) && angular.isDefined(expected.toString)){ |
| 83 | actual = actual.toString(); |
| 84 | expected = expected.toString(); |
| 85 | } |
| 86 | else { |
| 87 | return actual === expected; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | actual = actual.toLowerCase() + ''; |
| 92 | expected = expected.toLowerCase() + ''; |
| 93 | return actual.indexOf(expected) !== -1; |
| 94 | }; |
| 95 | } |
| 96 | })(); |