Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit fce3e37

Browse files
Aliaksandr Astashenkaunaomiblack
authored andcommitted
Stop page flickering on API search page.
Show/hide sections and items instead of filtering and reapplying section/items array, so instead of hiding everything and rerendeing filtered (causes flickering), show/hide items that match/doesn't match. Closes #965
1 parent 639ca37 commit fce3e37

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

public/resources/js/directives/api-list.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ angularIO.directive('apiList', function () {
1010
' <dd ng-class="{ active: !$ctrl.apiType }" ng-click="$ctrl.apiType = null">All</dd>' +
1111
' <dd ng-repeat="apiType in $ctrl.apiTypes" ng-class="{ active: $ctrl.apiType === apiType }" ng-click="$ctrl.setType(apiType)" class="{{apiType.cssClass}}">{{apiType.title}}</dd>' +
1212
' </dl>' +
13-
' <input placeholder="Filter" ng-model="$ctrl.apiFilter" ng-model-options="{updateOn: \'default blur\', debounce: {\'default\': 400, \'blur\': 0}}" class="api-filter">' +
13+
' <input placeholder="Filter" ng-model="$ctrl.apiFilter" ng-model-options="{updateOn: \'default blur\', debounce: {\'default\': 350, \'blur\': 0}}" class="api-filter">' +
1414
'</div>' +
1515
'<article class="l-content-small grid-fluid docs-content">' +
16-
' <div ng-repeat="section in $ctrl.filteredSections" ng-cloak="ng-cloak">' +
16+
' <div ng-repeat="section in $ctrl.groupedSections" ng-if="$ctrl.isFiltered(section)" ng-cloak="ng-cloak">' +
1717
' <h3>{{ section.title }}</h3>' +
1818
' <ul class="api-list">' +
19-
' <li ng-repeat="item in section.items" class="api-item">' +
19+
' <li ng-repeat="item in section.items" ng-show="item.show" class="api-item">' +
2020
' <a ng-href="{{ item.path }}"><span class="symbol {{ item.docType }}"></span>{{ item.title }}</a>' +
2121
' </li>' +
2222
' </ul>' +
@@ -38,15 +38,36 @@ angularIO.directive('apiList', function () {
3838

3939
$ctrl.apiFilter = getApiFilterFromLocation();
4040
$ctrl.apiType = getApiTypeFromLocation();
41-
$ctrl.filteredSections = [];
41+
$ctrl.groupedSections = [];
4242

4343
$ctrl.setType = function (type) {
4444
if (type === $ctrl.apiType) $ctrl.apiType = null;
4545
else $ctrl.apiType = type;
4646
};
4747

48+
$ctrl.isFiltered = function(section) {
49+
var apiFilter = ($ctrl.apiFilter || '').toLowerCase();
50+
var matchesModule = $ctrl.apiFilter === '' || $ctrl.apiFilter === null || section.title.toLowerCase().indexOf($ctrl.apiFilter.toLowerCase()) !== -1;
51+
var isVisible = false;
52+
53+
section.items.forEach(function(item) {
54+
var matchesDocType = !$ctrl.apiType || $ctrl.apiType.matches.indexOf(item.docType) !== -1;
55+
var matchesTitle = !apiFilter || item.title.toLowerCase().indexOf(apiFilter) !== -1;
56+
item.show = matchesDocType && (matchesTitle || matchesModule);
57+
58+
if (item.show) {
59+
isVisible = true;
60+
}
61+
});
62+
63+
return isVisible;
64+
};
65+
4866
$http.get($attrs.src).then(function(response) {
4967
$ctrl.sections = response.data;
68+
$ctrl.groupedSections = Object.keys($ctrl.sections).map(function(title) {
69+
return { title: title, items: $ctrl.sections[title] };
70+
});
5071
});
5172

5273
$scope.$watchGroup(
@@ -56,19 +77,6 @@ angularIO.directive('apiList', function () {
5677

5778
$location.search(API_FILTER_KEY, apiFilter || null);
5879
$location.search(API_TYPE_KEY, $ctrl.apiType && $ctrl.apiType.title || null);
59-
60-
$ctrl.filteredSections.length = 0;
61-
angular.forEach($ctrl.sections, function(section, title) {
62-
var matchesModule = $ctrl.apiFilter === '' || $ctrl.apiFilter === null || title.toLowerCase().indexOf($ctrl.apiFilter.toLowerCase()) !== -1;
63-
var filteredItems = section.filter(function(item) {
64-
var matchesDocType = !$ctrl.apiType || $ctrl.apiType.matches.indexOf(item.docType) !== -1;
65-
var matchesTitle = !apiFilter || item.title.toLowerCase().indexOf(apiFilter) !== -1;
66-
return matchesDocType && (matchesTitle || matchesModule);
67-
});
68-
if (filteredItems.length) {
69-
$ctrl.filteredSections.push({ title: title, items: filteredItems });
70-
}
71-
});
7280
}
7381
);
7482

@@ -81,7 +89,7 @@ angularIO.directive('apiList', function () {
8189
if (!apiFilter) {
8290
return null;
8391
} else if (!$ctrl.apiFilter || $ctrl.apiFilter.title != apiFilter) {
84-
for(var i = 0, ii = $ctrl.apiTypes.length; i < ii; i++) {
92+
for (var i = 0, ii = $ctrl.apiTypes.length; i < ii; i++) {
8593
if ($ctrl.apiTypes[i].title == apiFilter) {
8694
return $ctrl.apiTypes[i];
8795
}

0 commit comments

Comments
 (0)