Skip to content

Commit

Permalink
Implement favico.js (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy Reynaud committed Oct 9, 2019
1 parent 1e689d7 commit 9f4e2da
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/Controller/PullRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function __invoke(): Response
'github/pull-request/list.html.twig',
[
'openPullRequests' => $service->getOpen(),
'openPullRequestsCount' => $service->getOpenCount(),
'unreadNotifications' => $this->notificationService->getNotifications(),
'unreadNotificationsCount' => $this->notificationService->getNotificationsCount(),
'labels' => Label::toArray(),
Expand Down
1 change: 0 additions & 1 deletion src/Controller/ReloadPullRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function __invoke(Request $request): Response
'github/pull-request/filtersPullRequests.html.twig',
[
'openPullRequests' => $this->pullRequestFilterService->getOpen(),
'openPullRequestsCount' => $this->pullRequestFilterService->getOpenCount(),
]
)
);
Expand Down
33 changes: 30 additions & 3 deletions templates/github/pull-request/filters.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
{% block javascripts %}
{{ parent() }}

<script src="https://cdnjs.cloudflare.com/ajax/libs/favico.js/0.3.10/favico.min.js"
integrity="sha256-i265HmNQ6FYdanwd4Tvt6Phby2Ht/9KOGQ3cromZq5Y=" crossorigin="anonymous"></script>

<script type="text/javascript">
jQuery(function ($) {
const LOCALSTORAGE_KEY = 'collapses';
let reloadOnFocus = {{ reloadOnFocus ? 'true' : 'false' }};
let reloadEvery = {{ reloadEvery }};
let pullRequestsCount = {{ openPullRequestsCount|json_encode|raw }};
let unreadNotificationsCount = {{ unreadNotificationsCount|json_encode|raw }};
$(document).ready(function () {
let collapses = getCollapses();
Expand All @@ -48,6 +49,7 @@
});
saveCollapses(collapses);
updateFavicon();
});
$(document).on('click', '.accordion .card-header', function (event) {
Expand Down Expand Up @@ -85,6 +87,7 @@
success: function(response, textStatus, jqXHR) {
$accordionPullRequests.html(response);
hideCollapses();
updateFavicon();
},
error: function(response, textStatus, errorThrown) {
alert("Unable to load pull requests.");
Expand All @@ -96,6 +99,7 @@
success: function (response, textStatus, jqXHR) {
$accordionNotifs.html(response);
hideCollapses();
updateFavicon();
},
error: function (response, textStatus, errorThrown) {
alert("Unable to load notifications.");
Expand Down Expand Up @@ -128,6 +132,20 @@
});
};
window.updateFavicon = function () {
let count = 0;
$(document).find('.accordion .collapse').each(function (index, elem) {
count = count + parseInt($(elem).attr('data-count'), 10);
});
if (0 < count) {
new Favico({
animation:'none'
}).badge(count);
}
};
if (reloadOnFocus) {
$(window).on('focus', reloadData);
} else if(reloadEvery > 0) {
Expand All @@ -146,7 +164,8 @@
url: $that.attr('data-url'),
type: 'POST',
success: function(response, textStatus, jqXHR) {
let $strong = $($that.parents('.collapse').get(0)).prev().find('strong');
let $collapse = $($that.parents('.collapse').get(0));
let $strong = $collapse.prev().find('strong');
let number = $strong.text()
.replace('(', '')
.replace(')', '')
Expand All @@ -169,8 +188,16 @@
$listItem.slideUp(250, function () {
--number;
$strong.text('('+number+')');
$collapse.attr('data-count', number);
if (0 === number) {
$collapse.slideUp(250, function () {
$(this).removeClass('show');
});
}
$(this).remove();
updateFavicon();
});
},
error: function(response, textStatus, errorThrown) {
Expand Down
3 changes: 2 additions & 1 deletion templates/github/pull-request/filtersNotifications.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
</button>
</div>

<div id="collapse-notifs-{{ repository|slugify }}" class="collapse {{ unreadNotificationsCount[repository] > 0 ? 'show' : '' }}"
<div id="collapse-notifs-{{ repository|slugify }}" data-count="{{ unreadNotificationsCount[repository] }}"
class="collapse {{ unreadNotificationsCount[repository] > 0 ? 'show' : '' }}"
aria-labelledby="heading-notifs-{{ repository|slugify }}" data-parent="#accordion-notifs">
<div class="card-body">
<div class="list-group">
Expand Down
3 changes: 2 additions & 1 deletion templates/github/pull-request/filtersPullRequests.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
</button>
</div>

<div id="collapse-pr-{{ repository|slugify }}" class="collapse {{ sortedPullRequests.count() > 0 ? 'show' : '' }}"
<div id="collapse-pr-{{ repository|slugify }}" data-count="{{ sortedPullRequests.count() }}"
class="collapse {{ sortedPullRequests.count() > 0 ? 'show' : '' }}"
aria-labelledby="heading-pr-{{ repository|slugify }}" data-parent="#accordion-pull-requests">
<div class="card-body">
<div class="list-group">
Expand Down

0 comments on commit 9f4e2da

Please sign in to comment.