Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Register service worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anduin2017 committed Jan 24, 2019
1 parent 563f02b commit 1a1775d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 132 deletions.
1 change: 1 addition & 0 deletions src/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="theme-color" content="#3097D1">
@this.UseDisableZoom()
@this.UseDnsPrefetch()
@if (!string.IsNullOrWhiteSpace(Model.Description))
Expand Down
278 changes: 146 additions & 132 deletions src/wwwroot/js/application.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,124 @@
var $input = $('<div class="modal-body"><input type="text" class="form-control" placeholder="Message"></div>')
var $input = $('<div class="modal-body"><input type="text" class="form-control" placeholder="Message"></div>');

$(document).on('click', '.js-msgGroup', function () {
$('.js-msgGroup, .js-newMsg').addClass('d-none')
$('.js-conversation').removeClass('d-none')
$('.modal-title').html('<a href="#" class="js-gotoMsgs">Back</a>')
$input.insertBefore('.js-modalBody')
})
$('.js-msgGroup, .js-newMsg').addClass('d-none');
$('.js-conversation').removeClass('d-none');
$('.modal-title').html('<a href="#" class="js-gotoMsgs">Back</a>');
$input.insertBefore('.js-modalBody');
});

$(function () {
function getRight() {
if (!$('[data-toggle="popover"]').length) return 0
return ($(window).width() - ($('[data-toggle="popover"]').offset().left + $('[data-toggle="popover"]').outerWidth()))
}

$(window).on('resize', function () {
var instance = $('[data-toggle="popover"]').data('bs.popover')
if (instance) {
instance.config.viewport.padding = getRight()
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/sw.js').then(function (registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
})

$('[data-toggle="popover"]').popover({
template: '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-body popover-content px-0"></div></div>',
title: '',
html: true,
trigger: 'manual',
placement:'bottom',
viewport: {
selector: 'body',
padding: getRight()
},
content: function () {
var $nav = $('#js-popoverContent').clone()
return '<ul class="nav nav-pills nav-stacked flex-column" style="width: 120px">' + $nav.html() + '</ul>'
});

$(function () {
function getRight() {
if (!$('[data-toggle="popover"]').length) return 0
return ($(window).width() - ($('[data-toggle="popover"]').offset().left + $('[data-toggle="popover"]').outerWidth()))
}
})

$('[data-toggle="popover"]').on('click', function (e) {
e.stopPropagation()
$(window).on('resize', function () {
var instance = $('[data-toggle="popover"]').data('bs.popover')
if (instance) {
instance.config.viewport.padding = getRight()
}
})

if ($($('[data-toggle="popover"]').data('bs.popover').getTipElement()).hasClass('in')) {
$('[data-toggle="popover"]').popover('hide')
$(document).off('click.app.popover')
$('[data-toggle="popover"]').popover({
template: '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-body popover-content px-0"></div></div>',
title: '',
html: true,
trigger: 'manual',
placement: 'bottom',
viewport: {
selector: 'body',
padding: getRight()
},
content: function () {
var $nav = $('#js-popoverContent').clone()
return '<ul class="nav nav-pills nav-stacked flex-column" style="width: 120px">' + $nav.html() + '</ul>'
}
})

} else {
$('[data-toggle="popover"]').popover('show')
$('[data-toggle="popover"]').on('click', function (e) {
e.stopPropagation()

setTimeout(function () {
$(document).one('click.app.popover', function () {
$('[data-toggle="popover"]').popover('hide')
})
}, 1)
}
})
if ($($('[data-toggle="popover"]').data('bs.popover').getTipElement()).hasClass('in')) {
$('[data-toggle="popover"]').popover('hide')
$(document).off('click.app.popover')

} else {
$('[data-toggle="popover"]').popover('show')

setTimeout(function () {
$(document).one('click.app.popover', function () {
$('[data-toggle="popover"]').popover('hide')
})
}, 1)
}
})

})

$(document).on('click', '.js-gotoMsgs', function () {
$input.remove()
$('.js-conversation').addClass('d-none')
$('.js-msgGroup, .js-newMsg').removeClass('d-none')
$('.modal-title').html('Messages')
$input.remove()
$('.js-conversation').addClass('d-none')
$('.js-msgGroup, .js-newMsg').removeClass('d-none')
$('.modal-title').html('Messages')
})

$(document).on('click', '[data-action=growl]', function (e) {
e.preventDefault()

$('#app-growl').append(
'<div class="alert alert-dark alert-dismissible fade show" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close">'+
'<span aria-hidden="true">×</span>'+
'</button>'+
'Click the x on the upper right to dismiss this little thing. Or click growl again to show more growls'+
'</div>'
)
e.preventDefault()

$('#app-growl').append(
'<div class="alert alert-dark alert-dismissible fade show" role="alert">' +
'<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
'<span aria-hidden="true">×</span>' +
'</button>' +
'Click the x on the upper right to dismiss this little thing. Or click growl again to show more growls' +
'</div>'
)
})

$(document).on('focus', '[data-action="grow"]', function () {
if ($(window).width() > 1000) {
$(this).animate({
width: 300
})
}
if ($(window).width() > 1000) {
$(this).animate({
width: 300
})
}
})

$(document).on('blur', '[data-action="grow"]', function () {
if ($(window).width() > 1000) {
var $this = $(this).animate({
width: 180
})
}
if ($(window).width() > 1000) {
var $this = $(this).animate({
width: 180
})
}
})

// back to top button - docs
$(function () {
if ($('.docs-top').length) {
_backToTopButton()
$(window).on('scroll', _backToTopButton)
function _backToTopButton () {
if ($(window).scrollTop() > $(window).height()) {
$('.docs-top').fadeIn()
} else {
$('.docs-top').fadeOut()
}
if ($('.docs-top').length) {
_backToTopButton()
$(window).on('scroll', _backToTopButton)
function _backToTopButton() {
if ($(window).scrollTop() > $(window).height()) {
$('.docs-top').fadeIn()
} else {
$('.docs-top').fadeOut()
}
}
}
}
})

$(function () {
Expand All @@ -117,70 +131,70 @@ $(function () {

if ($toc[0]) {

maybeActivateDocNavigation()
$window.on('resize', maybeActivateDocNavigation)
maybeActivateDocNavigation()
$window.on('resize', maybeActivateDocNavigation)

function maybeActivateDocNavigation () {
if ($window.width() > 768) {
activateDocNavigation()
} else {
deactivateDocNavigation()
function maybeActivateDocNavigation() {
if ($window.width() > 768) {
activateDocNavigation()
} else {
deactivateDocNavigation()
}
}

function deactivateDocNavigation() {
$window.off('resize.theme.nav')
$window.off('scroll.theme.nav')
$toc.css({
position: '',
left: '',
top: ''
})
}
}

function deactivateDocNavigation() {
$window.off('resize.theme.nav')
$window.off('scroll.theme.nav')
$toc.css({
position: '',
left: '',
top: ''
})
}

function activateDocNavigation() {
function activateDocNavigation() {

var cache = {}
var cache = {}

function updateCache() {
cache.containerTop = $('.docs-content').offset().top - 40
cache.containerRight = $('.docs-content').offset().left + $('.docs-content').width() + 45
measure()
}
function updateCache() {
cache.containerTop = $('.docs-content').offset().top - 40
cache.containerRight = $('.docs-content').offset().left + $('.docs-content').width() + 45
measure()
}

function measure() {
var scrollTop = $window.scrollTop()
var distance = Math.max(scrollTop - cache.containerTop, 0)
function measure() {
var scrollTop = $window.scrollTop()
var distance = Math.max(scrollTop - cache.containerTop, 0)

if (!distance) {
$($toc.find('li a')[1]).addClass('active')
return $toc.css({
position: '',
left: '',
top: ''
})
}
if (!distance) {
$($toc.find('li a')[1]).addClass('active')
return $toc.css({
position: '',
left: '',
top: ''
})
}

$toc.css({
position: 'fixed',
left: cache.containerRight,
top: 40
})
}
$toc.css({
position: 'fixed',
left: cache.containerRight,
top: 40
})
}

updateCache()
updateCache()

$(window)
.on('resize.theme.nav', updateCache)
.on('scroll.theme.nav', measure)
$(window)
.on('resize.theme.nav', updateCache)
.on('scroll.theme.nav', measure)

$('body').scrollspy({
target: '#markdown-toc'
})
$('body').scrollspy({
target: '#markdown-toc'
})

setTimeout(function () {
$('body').scrollspy('refresh')
}, 1000)
}
setTimeout(function () {
$('body').scrollspy('refresh')
}, 1000)
}
}
})
28 changes: 28 additions & 0 deletions src/wwwroot/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [

];
self.addEventListener('install', function (event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});

self.addEventListener('fetch', function (event) {
event.respondWith(
caches.match(event.request)
.then(function (response) {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
}
)
);
});

0 comments on commit 1a1775d

Please sign in to comment.