angularIO.directive('apiList', function () { var API_FILTER_KEY = 'apiFilter'; var API_TYPE_KEY = 'apiType'; return { restrict: 'E', template: '' + '
' + '
' + '

{{ section.title }}

' + ' ' + '
' + '
', controllerAs: '$ctrl', controller: function($scope, $attrs, $http, $location) { var $ctrl = this; $ctrl.apiTypes = [ { cssClass: 'directive', title: 'Directive', matches: ['directive'] }, { cssClass: 'decorator', title: 'Decorator', matches: ['decorator'] }, { cssClass: 'class', title: 'Class', matches: ['class'] }, { cssClass: 'interface', title: 'Interface', matches: ['interface'] }, { cssClass: 'function', title: 'Function', matches: ['function'] }, { cssClass: 'const', title: 'Const or Enum', matches: ['const', 'enum'] }, { cssClass: 'var', title: 'Variable', matches: ['var', 'let'] } ]; $ctrl.apiFilter = getApiFilterFromLocation(); $ctrl.apiType = getApiTypeFromLocation(); $ctrl.groupedSections = []; $ctrl.setType = function (type) { if (type === $ctrl.apiType) $ctrl.apiType = null; else $ctrl.apiType = type; }; $ctrl.isFiltered = function(section) { var apiFilter = ($ctrl.apiFilter || '').toLowerCase(); var matchesModule = $ctrl.apiFilter === '' || $ctrl.apiFilter === null || section.title.toLowerCase().indexOf($ctrl.apiFilter.toLowerCase()) !== -1; var isVisible = false; section.items.forEach(function(item) { var matchesDocType = !$ctrl.apiType || $ctrl.apiType.matches.indexOf(item.docType) !== -1; var matchesTitle = !apiFilter || item.title.toLowerCase().indexOf(apiFilter) !== -1; item.show = matchesDocType && (matchesTitle || matchesModule); if (item.show) { isVisible = true; } }); return isVisible; }; $http.get($attrs.src).then(function(response) { $ctrl.sections = response.data; $ctrl.groupedSections = Object.keys($ctrl.sections).map(function(title) { return { title: title, items: $ctrl.sections[title] }; }); }); $scope.$watchGroup( [function() { return $ctrl.apiFilter}, function() { return $ctrl.apiType; }, function() { return $ctrl.sections; }], function() { var apiFilter = ($ctrl.apiFilter || '').toLowerCase(); $location.search(API_FILTER_KEY, apiFilter || null); $location.search(API_TYPE_KEY, $ctrl.apiType && $ctrl.apiType.title || null); } ); function getApiFilterFromLocation() { return $location.search()[API_FILTER_KEY] || null; } function getApiTypeFromLocation() { var apiFilter = $location.search()[API_TYPE_KEY]; if (!apiFilter) { return null; } else if (!$ctrl.apiFilter || $ctrl.apiFilter.title != apiFilter) { for (var i = 0, ii = $ctrl.apiTypes.length; i < ii; i++) { if ($ctrl.apiTypes[i].title == apiFilter) { return $ctrl.apiTypes[i]; } } } // If we get here then the apiType query didn't match any apiTypes $location.search(API_TYPE_KEY, null); } } }; });