var app = angular.module('app'); app.filter('positive', function () { return function (input) { if (!input) { return 0; } return Math.abs(input); }; }); //filter items by associated tags app.filter('tagfilter', function () { return function (items, tagText) { if (!items || items === undefined) return null; if (!tagText) return items; var filtered = []; if (tagText === 'absolutely no tags whatsoever' && items !== undefined) { for (var i = 0; i < items.length; i++) { var item = items[i]; if (item.tags) { if (item.tags.length === 0) filtered.push(item); } } } else { for (var i = 0; i < items.length; i++) { var item = items[i]; if (item.tags) { for (var j = 0; j < item.tags.length; j++) { var text = item.tags[j].text; if (text === tagText) filtered.push(item); } } } } return filtered; }; }); //filter items by associated tags app.filter('issuerfilter', function () { return function (issuers, text) { if (!issuers || issuers === undefined) return null; if (!text) return issuers; var filtered = []; for (var i = 0; i < issuers.length; i++) { if (issuers[i].issuer) { var issuer = issuers[i].issuer; if (issuer.name == text) { filtered.push(issuers[i]); } } } return filtered; }; }); //filter items by associated roles app.filter('roleFilter', function () { return function (users, role, org) { if (!users || users === undefined) return null; if (!role) return users; var filtered = []; for (var i = 0; i < users.length; i++) { var returnUser = false; if (users[i].roleAllocations) { for (var j = 0; j < users[i].roleAllocations.length; j++) { if (org) { if (users[i].roleAllocations[j].roleDisplayName == role && users[i].roleAllocations[j].organisationId == org) { returnUser = true; } } else { if (users[i].roleAllocations[j].roleDisplayName == role) { returnUser = true; } } } } if (returnUser) filtered.push(users[i]); } return filtered; }; }); app.filter('unique', function () { return function (items, filterOn) { if (filterOn === false) { return items; } if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) { var hashCheck = {}, newItems = []; var extractValueToCompare = function (item) { if (angular.isObject(item) && angular.isString(filterOn)) { return item[filterOn]; } else { return item; } }; angular.forEach(items, function (item) { var valueToCheck, isDuplicate = false; for (var i = 0; i < newItems.length; i++) { if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) { isDuplicate = true; break; } } if (!isDuplicate) { newItems.push(item); } }); items = newItems; } return items; }; }); //filter items by searching the item for the 'search' text app.filter('itemtextfilter', function (search) { return function (items, searchText) { if (!items) return null; if (!searchText) return items; var filtered = []; //we are using lunr full text searching to implement this filter var results = search.searchItem(searchText); var matchedIds = $.map(results, function(obj) { return obj.ref; }); for (var i = 0; i < items.length; i++) { if(matchedIds.indexOf(items[i].itemId)>-1) filtered.push(items[i]); } return filtered; }; }); //filter items by associated competency app.filter('competencyfilter', function () { return function (items, competencyId) { if (!items) return null; if (!competencyId) return items; var filtered = []; for (var i = 0; i < items.length; i++) { var item = items[i]; if (item.competencies) { for (var j = 0; j < item.competencies.length; j++) { if (competencyId === item.competencies[j]) filtered.push(item); } } } return filtered; }; }); app.filter('numberFixedLength', function () { return function (n, len) { var num = parseInt(n, 10); len = parseInt(len, 10); if (isNaN(num) || isNaN(len)) { return n; } num = '' + num; while (num.length < len) { num = '0' + num; } return num; }; }); app.filter('trimStart', function () { return function (n, len) { var num = parseInt(n, 10); len = parseInt(len, 10); if (isNaN(num) || isNaN(len)) { return n; } num = '' + num; num = num.slice(len); return num; }; }); app.filter('capitalizeFirst', function () { return function (s) { return (angular.isString(s) && s.length > 0) ? s[0].toUpperCase() + s.substr(1).toLowerCase() : s; } }); app.filter('UTCDate', function () { return function (utcDateString, format) { // return if input date is null or undefined if (!utcDateString) { return; } // append 'Z' to the date string to indicate UTC time if the timezone isn't already specified if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) { utcDateString += 'Z'; } // convert and format date using the built in angularjs date filter return utcDateString; }; }); app.filter('usersSelectedGroupFilter', function () { return function (users, selectedOrg) { if (!selectedOrg || !selectedOrg.length) return users; var filterOrgIds = [selectedOrg]; return users.filter(function (user) { var match = false; if (user.groups) { user.groups .map(function (org) { return org.id }) .forEach(function (id) { if (filterOrgIds.indexOf(id) > -1) match = true; }); return match; } }); } }); app.filter('filesize', function () { return function (size) { if (isNaN(size)) size = 0; if (size < 1024) return size + ' Bytes'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' Kb'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' Mb'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' Gb'; size /= 1024; return size.toFixed(2) + ' Tb'; }; }); app.filter('dateRange', function () { return function (items, startDate, endDate, name) { var retArray = []; if (!startDate && !endDate) { return items; } angular.forEach(items, function (obj) { var receivedDate = name ? obj[name] : obj.date; if (moment(receivedDate).isAfter(startDate) && moment(receivedDate).isBefore(endDate)) { retArray.push(obj); } }); return retArray; }; }) app.filter('encodeURIComponent', function () { return window.encodeURIComponent; }); app.filter('dotsFilter', [ '$filter', function ($filter) { function dotsFilter(input, limit) { var newContent = $filter('limitTo')(input, limit); if (newContent && newContent.length < input.length) { newContent += '...'; } return newContent; } return dotsFilter; } ]);