notifications.js
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* global _SEARCHWP */
( function($) {
'use strict';
const app = {
/**
* Init.
*
* @since 4.2.8
*/
init: () => {
$( app.ready );
},
/**
* Document ready
*
* @since 4.2.8
*/
ready: () => {
app.events();
},
/**
* Extension page events.
*
* @since 4.2.8
*/
events: () => {
const $notificationsPanel = $( '.swp-notifications-panel--wrapper' );
const showNotificationsPanelOnHash = () => {
if ( window.location.hash === '#notifications' ) {
$notificationsPanel.show();
}
};
showNotificationsPanelOnHash();
$( window ).on( 'hashchange', showNotificationsPanelOnHash );
$( '.searchwp-admin-menu-notification-indicator' ).parent()
.on( 'click', showNotificationsPanelOnHash );
$( '#swp-notifications-page-header-button' )
.on( 'click', () => $notificationsPanel.show() );
$( '.swp-notifications-panel--close' )
.on( 'click', () => $notificationsPanel.hide() );
$( '.swp-notifications-backdrop' )
.on( 'click', () => $notificationsPanel.hide() );
$( '.swp-notification--dismiss' ).on( 'click', app.dismiss );
},
/**
* Dismiss notification.
*
* @since 4.2.8
*/
dismiss: ( event ) => {
const $el = $( event.target );
// AJAX call - update option.
const data = {
_ajax_nonce: _SEARCHWP.nonce,
action: _SEARCHWP.prefix + 'notification_dismiss',
id: $el.data( 'id' ),
};
const $notification = $el.closest( '.swp-notifications--notification' );
const handleResponse = ( res ) => {
if ( res.success ) {
$notification.fadeOut(
100,
() => {
$notification.remove();
app.updateNotificationCount();
}
);
}
};
$.post( ajaxurl, data, handleResponse );
},
/**
* Update notification count in various places or reload the page if no notifications left.
*
* @since 4.2.8
*/
updateNotificationCount: () => {
const notificationsCount = $( '.swp-notifications-panel--notifications' ).children().length;
$( '.searchwp-branding-bar__actions-button-count' ).text( notificationsCount );
$( '#wp-admin-bar-searchwp .searchwp-menu-notification-counter' ).text( notificationsCount );
$( '.swp-notifications-panel--header span span' ).text( notificationsCount );
if ( notificationsCount !== 0 ) {
return;
}
if ( window.location.hash ) {
window.location.hash = '';
}
location.reload();
}
};
app.init();
window.searchwp = window.searchwp || {};
window.searchwp.AdminNotifications = app;
}( jQuery ) );