Matteo Scandolo | 686547a | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 19 | (function() { |
| 20 | 'use strict'; |
| 21 | |
| 22 | /** |
| 23 | * @ngdoc service |
| 24 | * @name xos.uiComponents.Comparator |
| 25 | * @description |
| 26 | * This factory define a function that replace the native angular.filter comparator. |
| 27 | * |
| 28 | * It is done to allow the comparation between (0|1) values with booleans. |
| 29 | * >Note that this factory return a single function, not an object. |
| 30 | * |
| 31 | * The tipical usage of this factory is inside an `ng-repeat` |
| 32 | * @example |
| 33 | * <example module="comparator"> |
| 34 | * <file name="index.html"> |
| 35 | * <div ng-controller="sample as vm"> |
| 36 | * <div class="row"> |
| 37 | * <div class="col-xs-6"> |
| 38 | * <label>Filter by name:</label> |
| 39 | * <input class="form-control" type="text" ng-model="vm.query.name"/> |
| 40 | * </div> |
| 41 | * <div class="col-xs-6"> |
| 42 | * <label>Filter by status:</label> |
| 43 | * <select |
| 44 | * ng-model="vm.query.status" |
| 45 | * ng-options="i for i in [true, false]"> |
| 46 | * </select> |
| 47 | * </div> |
| 48 | * </div> |
| 49 | * <div ng-repeat="item in vm.data | filter:vm.query:vm.comparator"> |
| 50 | * <div class="row"> |
| 51 | * <div class="col-xs-6">{{item.name}}</div> |
| 52 | * <div class="col-xs-6">{{item.status}}</div> |
| 53 | * </div> |
| 54 | * </div> |
| 55 | * </div> |
| 56 | * </file> |
| 57 | * <file name="script.js"> |
| 58 | * angular.module('comparator', ['xos.uiComponents']) |
| 59 | * .controller('sample', function(Comparator){ |
| 60 | * this.comparator = Comparator; |
| 61 | * this.data = [ |
| 62 | * {name: 'Jhon', status: 1}, |
| 63 | * {name: 'Jack', status: 0}, |
| 64 | * {name: 'Mike', status: 1}, |
| 65 | * {name: 'Scott', status: 0} |
| 66 | * ]; |
| 67 | * }); |
| 68 | * </file> |
| 69 | * </example> |
| 70 | **/ |
| 71 | |
| 72 | angular |
| 73 | .module('xos.uiComponents') |
| 74 | .factory('Comparator', comparator); |
| 75 | |
| 76 | function comparator(_) { |
| 77 | |
| 78 | return function(actual, expected){ |
| 79 | |
| 80 | if (angular.isUndefined(actual)) { |
| 81 | // No substring matching against `undefined` |
| 82 | return false; |
| 83 | } |
| 84 | if ((actual === null) || (expected === null)) { |
| 85 | // No substring matching against `null`; only match against `null` |
| 86 | return actual === expected; |
| 87 | } |
| 88 | if (angular.isObject(expected) || (angular.isObject(actual))){ |
| 89 | return angular.equals(expected, actual); |
| 90 | } |
| 91 | |
| 92 | if(_.isBoolean(actual) || _.isBoolean(expected)){ |
| 93 | if(actual === 0 || actual === 1){ |
| 94 | actual = !!actual; |
| 95 | } |
| 96 | return angular.equals(expected, actual); |
| 97 | } |
| 98 | |
| 99 | if(!angular.isString(actual) || !angular.isString(expected)){ |
| 100 | if(angular.isDefined(actual.toString) && angular.isDefined(expected.toString)){ |
| 101 | actual = actual.toString(); |
| 102 | expected = expected.toString(); |
| 103 | } |
| 104 | else { |
| 105 | return actual === expected; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | actual = actual.toLowerCase() + ''; |
| 110 | expected = expected.toLowerCase() + ''; |
| 111 | return actual.indexOf(expected) !== -1; |
| 112 | }; |
| 113 | } |
| 114 | })(); |