Update desktop notifications logic without yesno field

This commit is contained in:
Hypolite Petovan 2020-02-05 21:38:22 -05:00
parent 40db6baa6b
commit f2732a4ad7
2 changed files with 32 additions and 56 deletions

View file

@ -133,47 +133,34 @@
{{include file="field_checkbox.tpl" field=$desktop_notifications}} {{include file="field_checkbox.tpl" field=$desktop_notifications}}
<script> <script>
(function(){ (function(){
var elm = $("#id_{{$desktop_notifications.0}}_onoff"); let $notificationField = $("#div_id_{{$desktop_notifications.0}}");
var ckbox = $("#id_{{$desktop_notifications.0}}"); let $notificationCheckbox = $("#id_{{$desktop_notifications.0}}");
if (getNotificationPermission() === 'granted') { if (getNotificationPermission() === 'granted') {
ckbox.val(1); $notificationCheckbox.prop('checked', true);
elm.find(".off").addClass("hidden"); }
elm.find(".on").removeClass("hidden");
}
if (getNotificationPermission() === null) { if (getNotificationPermission() === null) {
elm.parent(".field.yesno").hide(); $notificationField.hide();
} }
$("#id_{{$desktop_notifications.0}}_onoff").on("click", function(e){ $notificationCheckbox.on('change', function(e) {
if (Notification.permission === 'granted') {
localStorage.setItem('notification-permissions', $notificationCheckbox.prop('checked') ? 'granted' : 'denied');
} else if (Notification.permission === 'denied') {
localStorage.setItem('notification-permissions', 'denied');
if (Notification.permission === 'granted') { $notificationCheckbox.prop('checked', false);
localStorage.setItem('notification-permissions', ckbox.val()==1 ? 'granted' : 'denied'); } else if (Notification.permission === 'default') {
} else if (Notification.permission === 'denied') { Notification.requestPermission(function(choice) {
localStorage.setItem('notification-permissions', 'denied'); if (choice === 'granted') {
localStorage.setItem('notification-permissions', $notificationCheckbox.prop('checked') ? 'granted' : 'denied');
ckbox.val(0); } else {
elm.find(".on").addClass("hidden"); localStorage.setItem('notification-permissions', 'denied');
elm.find(".off").removeClass("hidden"); $notificationCheckbox.prop('checked', false);
}
} else if (Notification.permission === 'default') { });
Notification.requestPermission(function(choice) { }
if (choice === 'granted') { })
localStorage.setItem('notification-permissions', ckbox.val()==1 ? 'granted' : 'denied');
} else {
localStorage.setItem('notification-permissions', 'denied');
ckbox.val(0);
elm.find(".on").addClass("hidden");
elm.find(".off").removeClass("hidden");
}
});
}
//console.log(getNotificationPermission());
})
})(); })();
</script> </script>

View file

@ -195,44 +195,33 @@
{{include file="field_checkbox.tpl" field=$desktop_notifications}} {{include file="field_checkbox.tpl" field=$desktop_notifications}}
<script type="text/javascript"> <script type="text/javascript">
(function(){ (function(){
var elm = $("#id_{{$desktop_notifications.0}}_onoff"); let $notificationField = $("#div_id_{{$desktop_notifications.0}}");
var ckbox = $("#id_{{$desktop_notifications.0}}"); let $notificationCheckbox = $("#id_{{$desktop_notifications.0}}");
if (getNotificationPermission() === 'granted') { if (getNotificationPermission() === 'granted') {
ckbox.val(1); $notificationCheckbox.prop('checked', true);
elm.find(".off").addClass("hidden");
elm.find(".on").removeClass("hidden");
} }
if (getNotificationPermission() === null) { if (getNotificationPermission() === null) {
elm.parent(".field.yesno").hide(); $notificationField.hide();
} }
$("#id_{{$desktop_notifications.0}}_onoff").on("click", function(e){ $notificationCheckbox.on('change', function(e){
if (Notification.permission === 'granted') { if (Notification.permission === 'granted') {
localStorage.setItem('notification-permissions', ckbox.val()==1 ? 'granted' : 'denied'); localStorage.setItem('notification-permissions', $notificationCheckbox.prop('checked') ? 'granted' : 'denied');
} else if (Notification.permission === 'denied') { } else if (Notification.permission === 'denied') {
localStorage.setItem('notification-permissions', 'denied'); localStorage.setItem('notification-permissions', 'denied');
ckbox.val(0); $notificationCheckbox.prop('checked', false);
elm.find(".on").addClass("hidden");
elm.find(".off").removeClass("hidden");
} else if (Notification.permission === 'default') { } else if (Notification.permission === 'default') {
Notification.requestPermission(function(choice) { Notification.requestPermission(function(choice) {
if (choice === 'granted') { if (choice === 'granted') {
localStorage.setItem('notification-permissions', ckbox.val()==1 ? 'granted' : 'denied'); localStorage.setItem('notification-permissions', $notificationCheckbox.prop('checked') ? 'granted' : 'denied');
} else { } else {
localStorage.setItem('notification-permissions', 'denied'); localStorage.setItem('notification-permissions', 'denied');
ckbox.val(0); $notificationCheckbox.prop('checked', false);
elm.find(".on").addClass("hidden");
elm.find(".off").removeClass("hidden");
} }
}); });
} }
//console.log(getNotificationPermission());
}) })
})(); })();
</script> </script>