1+ const { subscribe, select } = wp . data ;
2+
3+ const BADGES_STATUS = {
4+ error : 'error' ,
5+ warning : 'warning' ,
6+ success : 'success'
7+ }
8+
9+ const BADGES = {
10+ NO_ACCESS : {
11+ id : 'no_access' ,
12+ name : ef_notifications_localization . no_access ,
13+ status : BADGES_STATUS [ 'error' ]
14+ } ,
15+ NO_EMAIL : {
16+ id : 'no_email' ,
17+ name : ef_notifications_localization . no_email ,
18+ status : BADGES_STATUS [ 'error' ]
19+ } ,
20+ POST_AUTHOR : {
21+ id : 'post_author' ,
22+ name : ef_notifications_localization . post_author ,
23+ class : 'ef-badge-neutral'
24+ } ,
25+ AUTO_SUBSCRIBE : {
26+ id : 'auto_subscribed' ,
27+ name : ef_notifications_localization . auto_subscribed ,
28+ class : 'ef-badge-neutral'
29+ } ,
30+ }
31+
32+ const addBadgeToEl = ( $el , badge ) => {
33+ if ( getBadge ( $el , badge ) ) {
34+ return ;
35+ }
36+
37+ $el . append ( badgeTemplate ( badge ) )
38+ }
39+
40+ const removeBadgeFromEl = ( $el , badge ) => {
41+ const existingBadge = getBadge ( $el , badge ) ;
42+
43+ if ( ! existingBadge ) {
44+ return ;
45+ }
46+
47+ existingBadge . remove ( ) ;
48+ }
49+
50+ const getBadge = ( $el , badge ) => {
51+ const exists = $el . find ( `[data-badge-id='${ badge . id } ']` ) ;
52+
53+ if ( exists . length ) {
54+ return jQuery ( exists [ 0 ] ) ;
55+ } else {
56+ return null ;
57+ }
58+ }
59+
60+ const badgeTemplate = ( badge ) => {
61+ let classes = 'ef-user-badge' ;
62+
63+ if ( BADGES_STATUS [ 'error' ] === badge . status ) {
64+ classes += ' ef-user-badge-error' ;
65+ }
66+
67+ return `<div class="${ classes } " data-badge-id="${ badge . id } ">${ badge . name } </div>`
68+ }
69+
170jQuery ( document ) . ready ( function ( $ ) {
2- $ ( '#ef-post_following_users_box ul' ) . listFilterizer ( ) ;
71+ jQuery ( '#ef-post_following_users_box ul' ) . listFilterizer ( ) ;
372
473 var params = {
574 action : 'save_notifications' ,
6- post_id : $ ( '#post_ID' ) . val ( ) ,
75+ post_id : jQuery ( '#post_ID' ) . val ( ) ,
776 } ;
77+
878
979 var toggle_warning_badges = function ( container , response ) {
10- // Remove any existing badges
11- if ( $ ( container ) . siblings ( 'span' ) . length ) {
12- $ ( container ) . siblings ( 'span' ) . remove ( ) ;
13- }
14-
80+ const $el = jQuery ( container ) . parent ( ) ;
81+ const $badgesContainer = $el . closest ( 'li' ) . find ( '.ef-user-list-badges' ) ;
1582 // "No Access" If this user was flagged as not having access
16- var user_has_no_access = response . data . subscribers_with_no_access . includes ( parseInt ( $ ( container ) . val ( ) ) ) ;
83+ var user_has_no_access = response . data . subscribers_with_no_access . includes ( parseInt ( jQuery ( container ) . val ( ) ) ) ;
1784 if ( user_has_no_access ) {
18- var span = $ ( '<span />' ) . addClass ( 'post_following_list-no_access' ) ;
19- span . text ( ef_notifications_localization . no_access ) ;
20- $ ( container ) . parent ( ) . prepend ( span ) ;
21- warning_background = true ;
85+ addBadgeToEl ( $badgesContainer , BADGES [ 'NO_ACCESS' ] ) ;
86+ } else {
87+ removeBadgeFromEl ( $badgesContainer , BADGES [ 'NO_ACCESS' ] ) ;
2288 }
89+
2390 // "No Email" If this user was flagged as not having an email
24- var user_has_no_email = response . data . subscribers_with_no_email . includes ( parseInt ( $ ( container ) . val ( ) ) ) ;
91+ var user_has_no_email = response . data . subscribers_with_no_email . includes ( parseInt ( jQuery ( container ) . val ( ) ) ) ;
2592 if ( user_has_no_email ) {
26- var span = $ ( '<span />' ) . addClass ( 'post_following_list-no_email' ) ;
27- span . text ( ef_notifications_localization . no_email ) ;
28- $ ( container ) . parent ( ) . prepend ( span ) ;
29- warning_background = true ;
93+ addBadgeToEl ( $badgesContainer , BADGES [ 'NO_EMAIL' ] ) ;
94+ } else {
95+ removeBadgeFromEl ( $badgesContainer , BADGES [ 'NO_EMAIL' ] ) ;
3096 }
3197 }
3298
33- $ ( document ) . on ( 'click' , '.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox' , function ( ) {
99+ const show_post_author_badge = ( ) => {
100+ const $userListItemActions = jQuery ( "label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges" ) ;
101+ addBadgeToEl ( $userListItemActions , BADGES [ 'POST_AUTHOR' ] ) ;
102+ }
103+
104+ show_post_author_badge ( ) ;
105+
106+
107+ const show_autosubscribed_badge = ( ) => {
108+ const $userListItemActions = jQuery ( "label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges" ) ;
109+ addBadgeToEl ( $userListItemActions , BADGES [ 'AUTO_SUBSCRIBE' ] ) ;
110+ }
111+
112+ const disable_autosubscribe_checkbox = ( ) => {
113+ jQuery ( '#ef-selected-users-' + ef_post_author_id ) . prop ( 'disabled' , true ) ;
114+ }
115+
116+ if ( typeof ef_post_author_auto_subscribe !== 'undefined' ) {
117+ show_autosubscribed_badge ( ) ;
118+ disable_autosubscribe_checkbox ( ) ;
119+ }
120+
121+
122+
123+ jQuery ( document ) . on ( 'click' , '.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox' , function ( ) {
34124 var user_group_ids = [ ] ;
35- var parent_this = $ ( this ) ;
36- params . ef_notifications_name = $ ( this ) . attr ( 'name' ) ;
37- params . _nonce = $ ( "#ef_notifications_nonce" ) . val ( ) ;
125+ var parent_this = jQuery ( this ) ;
126+ params . ef_notifications_name = jQuery ( this ) . attr ( 'name' ) ;
127+ params . _nonce = jQuery ( "#ef_notifications_nonce" ) . val ( ) ;
38128
39- $ ( this )
129+ jQuery ( this )
40130 . parents ( '.ef-post_following_list' )
41131 . find ( 'input:checked' )
42132 . map ( function ( ) {
43- user_group_ids . push ( $ ( this ) . val ( ) ) ;
133+ user_group_ids . push ( jQuery ( this ) . val ( ) ) ;
44134 } )
45135
46136 params . user_group_ids = user_group_ids ;
@@ -56,25 +146,25 @@ jQuery(document).ready(function($) {
56146
57147 // Toggle the warning badges ("No Access" and "No Email") to signal the user won't receive notifications
58148 if ( undefined !== response . data ) {
59- toggle_warning_badges ( $ ( parent_this ) , response ) ;
149+ toggle_warning_badges ( jQuery ( parent_this ) , response ) ;
60150 }
61151 // Green 40% by default
62152 var backgroundHighlightColor = "#90d296" ;
63153 if ( warning_background ) {
64154 // Red 40% if there's a warning
65155 var backgroundHighlightColor = "#ea8484" ;
66156 }
67- var backgroundColor = parent_this . css ( 'background-color' ) ;
68- $ ( parent_this . parents ( 'li ' ) )
157+ var backgroundColor = 'transparent' ;
158+ jQuery ( parent_this . parents ( 'label ' ) )
69159 . animate ( { 'backgroundColor' : backgroundHighlightColor } , 200 )
70- . animate ( { 'backgroundColor' :backgroundColor } , 200 ) ;
160+ . animate ( { 'backgroundColor' : backgroundColor } , 200 ) ;
71161
72162 // This event is used to show an updated list of who will be notified of editorial comments and status updates.
73- $ ( '#ef-post_following_box' ) . trigger ( 'following_list_updated' ) ;
163+ jQuery ( '#ef-post_following_box' ) . trigger ( 'following_list_updated' ) ;
74164 } ,
75165 error : function ( r ) {
76- $ ( '#ef-post_following_users_box' ) . prev ( ) . append ( ' <p class="error">There was an error. Please reload the page.</p>' ) ;
166+ jQuery ( '#ef-post_following_users_box' ) . prev ( ) . append ( ' <p class="error">There was an error. Please reload the page.</p>' ) ;
77167 }
78168 } ) ;
79169 } ) ;
80- } ) ;
170+ } ) ;
0 commit comments