preserve notify state in local storage across page loads
This commit is contained in:
parent
7cbb9defbb
commit
e26504030f
93
js/chess.js
93
js/chess.js
|
|
@ -503,7 +503,7 @@ function renderBoard(board){
|
||||||
if (board.move && board.move.took === 'k') {
|
if (board.move && board.move.took === 'k') {
|
||||||
msg = (isDark(board.move.side) ? 'Dark' : 'Light') + ' player won!';
|
msg = (isDark(board.move.side) ? 'Dark' : 'Light') + ' player won!';
|
||||||
} else {
|
} else {
|
||||||
msg = (isDark(board.player) ? 'Dark' : 'Light') + " player's move";
|
msg = (isDark(board.player) ? 'Dark' : 'Light') + " player's turn";
|
||||||
}
|
}
|
||||||
$('#cb_message').text(msg);
|
$('#cb_message').text(msg);
|
||||||
|
|
||||||
|
|
@ -540,12 +540,25 @@ function setVisibleBoard(board, live){
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCurrentBoard(board){
|
function setCurrentBoard(board){
|
||||||
$('#cb_board').data('board', board);
|
const cb_board = $('#cb_board').first();
|
||||||
|
cb_board.data('board', board);
|
||||||
|
|
||||||
|
const boardState = JSON.stringify(board);
|
||||||
|
|
||||||
/* navigation should not include the redo stack */
|
/* navigation should not include the redo stack */
|
||||||
const visible = cloneJSON(board);
|
const visible = JSON.parse(boardState);
|
||||||
delete visible.subsequent;
|
delete visible.subsequent;
|
||||||
setVisibleBoard(visible, true);
|
setVisibleBoard(visible, true);
|
||||||
|
|
||||||
|
if ($('#cb_notify')[0].checked && boardState !== cb_board.data('last_state')) {
|
||||||
|
/* ignore partial moves and undo/redo */
|
||||||
|
if (!board.phantom && !board.subsequent) {
|
||||||
|
const gameString = cb_board.data('lightName') + ' vs. ' + cb_board.data('darkName');
|
||||||
|
notify(gameString + '\n' + $('#cb_message').text());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cb_board.data('last_state', boardState);
|
||||||
}
|
}
|
||||||
|
|
||||||
function randomId(){
|
function randomId(){
|
||||||
|
|
@ -564,7 +577,7 @@ function putState(board){
|
||||||
var gameId = boardElem.data('gameId');
|
var gameId = boardElem.data('gameId');
|
||||||
var game = gun.get(PacoSakoUUID).get('games').get(gameId);
|
var game = gun.get(PacoSakoUUID).get('games').get(gameId);
|
||||||
var state = JSON.stringify(board);
|
var state = JSON.stringify(board);
|
||||||
$('#cb_board').data('skip_notify', state);
|
boardElem.data('last_state', state);
|
||||||
game.put({ board: state });
|
game.put({ board: state });
|
||||||
putMeta();
|
putMeta();
|
||||||
}
|
}
|
||||||
|
|
@ -602,36 +615,37 @@ function switchGameId(newId){
|
||||||
boardElem.data('gameId', newId);
|
boardElem.data('gameId', newId);
|
||||||
location.hash = '#/' + newId;
|
location.hash = '#/' + newId;
|
||||||
|
|
||||||
|
const notifyAfter = new Date().getTime() + 2000;
|
||||||
|
$(window).data('notifyAfter', new Date().getTime() + 2000);
|
||||||
|
window.setTimeout(function() {
|
||||||
|
/* Delete the notification block in case the system time is changed backward */
|
||||||
|
if ($(window).data('notifyAfter') === notifyAfter) {
|
||||||
|
$(window).removeData('notifyAfter');
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
closeNotifications();
|
||||||
|
|
||||||
/* this will be the starting state if no data is received from peers */
|
/* this will be the starting state if no data is received from peers */
|
||||||
var newBoard = initialBoard();
|
var newBoard = initialBoard();
|
||||||
setCurrentBoard(newBoard);
|
setCurrentBoard(newBoard);
|
||||||
|
boardElem.data('last_state', JSON.stringify(newBoard));
|
||||||
|
|
||||||
$('#cb_notify')[0].checked = false;
|
boardElem.data('lightName', 'Light');
|
||||||
|
boardElem.data('darkName', 'Dark');
|
||||||
$('#cb_light_name').val('');
|
$('#cb_light_name').val('');
|
||||||
$('#cb_dark_name').val('');
|
$('#cb_dark_name').val('');
|
||||||
$('#cb_board').removeData('skip_notify');
|
|
||||||
|
|
||||||
gun.get(PacoSakoUUID).get('games').get(newId).on(function(d){
|
gun.get(PacoSakoUUID).get('games').get(newId).on(function(d){
|
||||||
if (d && d.board && $('#cb_board').data('gameId') === newId) {
|
if (d && d.board && $('#cb_board').data('gameId') === newId) {
|
||||||
const board = JSON.parse(d.board);
|
const board = JSON.parse(d.board);
|
||||||
const cb_board = $('#cb_board').first();
|
|
||||||
if ($('#cb_notify')[0].checked && cb_board.data('skip_notify') !== d.board) {
|
|
||||||
/* ignore partial moves and undo/redo */
|
|
||||||
if (!board.phantom && !board.subsequent) {
|
|
||||||
if (board.move && board.move.took === 'k') {
|
|
||||||
notify((isDark(board.move.side) ? 'Dark' : 'Light') + ' player won!');
|
|
||||||
} else {
|
|
||||||
notify((isDark(board.player) ? 'Dark' : 'Light') + ' player\'s turn');
|
|
||||||
}
|
|
||||||
$('#cb_board').data('skip_notify', d.board);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setCurrentBoard(board);
|
setCurrentBoard(board);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
gun.get(PacoSakoUUID).get('meta').get(newId).on(function(d){
|
gun.get(PacoSakoUUID).get('meta').get(newId).on(function(d){
|
||||||
if (d && $('#cb_board').data('gameId') === newId) {
|
if (d && $('#cb_board').data('gameId') === newId) {
|
||||||
|
$('#cb_board').data('lightName', d.lightName || 'Light');
|
||||||
|
$('#cb_board').data('darkName', d.darkName || 'Dark');
|
||||||
$('#cb_light_name').val(d.lightName || '');
|
$('#cb_light_name').val(d.lightName || '');
|
||||||
$('#cb_dark_name').val(d.darkName || '');
|
$('#cb_dark_name').val(d.darkName || '');
|
||||||
}
|
}
|
||||||
|
|
@ -663,13 +677,14 @@ function requestNotify(){
|
||||||
}
|
}
|
||||||
|
|
||||||
function notify(body) {
|
function notify(body) {
|
||||||
|
const now = new Date().getTime();
|
||||||
|
const then = $(window).data('notifyAfter');
|
||||||
|
if (!then || now >= then) {
|
||||||
try {
|
try {
|
||||||
Notification.requestPermission(function(permission){
|
Notification.requestPermission(function(permission){
|
||||||
if (permission === 'granted') {
|
if (permission === 'granted') {
|
||||||
navigator.serviceWorker.ready.then(function(registration){
|
navigator.serviceWorker.ready.then(function(registration){
|
||||||
registration.showNotification('Paco Ŝako', {
|
registration.showNotification('Paco Ŝako', {
|
||||||
badge: 'svg/Chess_klt45.svg',
|
|
||||||
icon: 'svg/Chess_klt45.svg',
|
|
||||||
body: body,
|
body: body,
|
||||||
tag: 'notice',
|
tag: 'notice',
|
||||||
});
|
});
|
||||||
|
|
@ -682,6 +697,19 @@ function notify(body) {
|
||||||
disableNotify();
|
disableNotify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeNotifications(){
|
||||||
|
try {
|
||||||
|
navigator.serviceWorker.ready.then(function(registration){
|
||||||
|
registration.getNotifications({tag: 'notice'}).then(function(notifications){
|
||||||
|
for (const notification of notifications) {
|
||||||
|
notification.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (err) {}
|
||||||
|
}
|
||||||
|
|
||||||
$(function (){
|
$(function (){
|
||||||
try {
|
try {
|
||||||
|
|
@ -694,6 +722,27 @@ $(function (){
|
||||||
disableNotify();
|
disableNotify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ('localStorage' in window) {
|
||||||
|
function updateNotify(newValue){
|
||||||
|
const doNotify = newValue === 'on';
|
||||||
|
const cb_notify = $('#cb_notify')[0];
|
||||||
|
if (doNotify) {
|
||||||
|
if (!cb_notify.checked) {
|
||||||
|
cb_notify.checked = true;
|
||||||
|
requestNotify();
|
||||||
|
}
|
||||||
|
} else if (cb_notify.checked) {
|
||||||
|
cb_notify.checked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$(window).on('storage', function(event){
|
||||||
|
if (event.originalEvent.key === '/pacosako/notify') {
|
||||||
|
updateNotify(event.originalEvent.newValue);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
updateNotify(window.localStorage.getItem('/pacosako/notify'));
|
||||||
|
}
|
||||||
|
|
||||||
$('.cb-square').droppable({
|
$('.cb-square').droppable({
|
||||||
accept: '.cb-piece',
|
accept: '.cb-piece',
|
||||||
disabled: true,
|
disabled: true,
|
||||||
|
|
@ -708,6 +757,7 @@ $(function (){
|
||||||
dragged.appendTo('#cb_hidden');
|
dragged.appendTo('#cb_hidden');
|
||||||
|
|
||||||
var newBoard = movePiece($('#cb_board').data('board'), type[1], from, to);
|
var newBoard = movePiece($('#cb_board').data('board'), type[1], from, to);
|
||||||
|
renderBoard(newBoard);
|
||||||
putState(newBoard);
|
putState(newBoard);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -823,6 +873,9 @@ $(function (){
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#cb_notify').on('change', function(){
|
$('#cb_notify').on('change', function(){
|
||||||
|
if ('localStorage' in window) {
|
||||||
|
window.localStorage.setItem('/pacosako/notify', this.checked ? 'on' : 'off');
|
||||||
|
}
|
||||||
if (this.checked) {
|
if (this.checked) {
|
||||||
requestNotify();
|
requestNotify();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue