1- jQuery ( document ) . ready ( function ( $ ) {
2- $ ( '#ef-post_following_users_box ul' ) . listFilterizer ( ) ;
1+ /* global wp, jQuery, ajaxurl, ef_notifications_localization, document, wpListL10n, ef_post_author_id */
32
4- var params = {
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 getBadge = ( $el , badge ) => {
33+ const exists = $el . find ( `[data-badge-id='${ badge . id } ']` ) ;
34+
35+ if ( exists . length ) {
36+ return jQuery ( exists [ 0 ] ) ;
37+ }
38+ return null ;
39+ } ;
40+
41+ const badgeTemplate = badge => {
42+ let classes = 'ef-user-badge' ;
43+
44+ if ( BADGES_STATUS . error === badge . status ) {
45+ classes += ' ef-user-badge-error' ;
46+ }
47+
48+ return `<div class="${ classes } " data-badge-id="${ badge . id } ">${ badge . name } </div>` ;
49+ } ;
50+
51+ const addBadgeToEl = ( $el , badge ) => {
52+ if ( getBadge ( $el , badge ) ) {
53+ return ;
54+ }
55+
56+ $el . append ( badgeTemplate ( badge ) ) ;
57+ } ;
58+
59+ const removeBadgeFromEl = ( $el , badge ) => {
60+ const existingBadge = getBadge ( $el , badge ) ;
61+
62+ if ( ! existingBadge ) {
63+ return ;
64+ }
65+
66+ existingBadge . remove ( ) ;
67+ } ;
68+
69+ jQuery ( document ) . ready ( function ( $ ) {
70+ jQuery ( '#ef-post_following_users_box ul' ) . listFilterizer ( ) ;
71+
72+ const params = {
573 action : 'save_notifications' ,
6- post_id : $ ( '#post_ID' ) . val ( ) ,
74+ post_id : jQuery ( '#post_ID' ) . val ( ) ,
775 } ;
8-
9- 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-
76+
77+ const toggleWarningBadges = function ( container , { userHasNoAccess, userHasNoEmail } ) {
78+ const $el = jQuery ( container ) . parent ( ) ;
79+ const $badgesContainer = $el . closest ( 'li' ) . find ( '.ef-user-list-badges' ) ;
80+
1581 // "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 ( ) ) ) ;
17- 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 ;
82+ if ( userHasNoAccess ) {
83+ addBadgeToEl ( $badgesContainer , BADGES . NO_ACCESS ) ;
84+ } else {
85+ removeBadgeFromEl ( $badgesContainer , BADGES . NO_ACCESS ) ;
2286 }
87+
2388 // "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 ( ) ) ) ;
25- 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 ;
89+ if ( userHasNoEmail ) {
90+ addBadgeToEl ( $badgesContainer , BADGES . NO_EMAIL ) ;
91+ } else {
92+ removeBadgeFromEl ( $badgesContainer , BADGES . NO_EMAIL ) ;
3093 }
94+ } ;
95+
96+ const show_post_author_badge = ( ) => {
97+ const $userListItemActions = jQuery ( "label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges" ) ;
98+ addBadgeToEl ( $userListItemActions , BADGES . POST_AUTHOR ) ;
99+ } ;
100+
101+ /**
102+ * Until assets are correctly loaded on their respective pages, `ef_post_author_id` should
103+ * only have a value on a post page, so only execute `show_post_author_badge` if it has a value
104+ */
105+ if ( 'undefined' !== typeof ef_post_author_id ) {
106+ show_post_author_badge ( ) ;
31107 }
32108
33- $ ( document ) . on ( 'click' , '.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox' , function ( ) {
34- var user_group_ids = [ ] ;
35- var parent_this = $ ( this ) ;
36- params . ef_notifications_name = $ ( this ) . attr ( 'name' ) ;
37- params . _nonce = $ ( "#ef_notifications_nonce" ) . val ( ) ;
38-
39- $ ( this )
40- . parents ( '.ef-post_following_list' )
41- . find ( 'input:checked' )
42- . map ( function ( ) {
43- user_group_ids . push ( $ ( this ) . val ( ) ) ;
44- } )
45-
46- params . user_group_ids = user_group_ids ;
47-
48- $ . ajax ( {
49- type : 'POST' ,
50- url : ( ajaxurl ) ? ajaxurl : wpListL10n . url ,
51- data : params ,
52-
53- success : function ( response ) {
54- // Reset background color (set during toggle_warning_badges if there's a warning)
55- warning_background = false ;
56-
109+ const showAutosubscribedBadge = ( ) => {
110+ const $userListItemActions = jQuery ( "label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges" ) ;
111+ addBadgeToEl ( $userListItemActions , BADGES . AUTO_SUBSCRIBE ) ;
112+ } ;
113+
114+ const disableAutosubscribeCheckbox = ( ) => {
115+ jQuery ( '#ef-selected-users-' + ef_post_author_id ) . prop ( 'disabled' , true ) ;
116+ } ;
117+
118+ if ( typeof ef_post_author_auto_subscribe !== 'undefined' ) {
119+ showAutosubscribedBadge ( ) ;
120+ disableAutosubscribeCheckbox ( ) ;
121+ }
122+
123+ jQuery ( document ) . on ( 'click' , '.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox' , function ( ) {
124+ const userGroupIds = [ ] ;
125+ const checkbox = jQuery ( this ) ;
126+ params . ef_notifications_name = jQuery ( this ) . attr ( 'name' ) ;
127+ params . _nonce = jQuery ( '#ef_notifications_nonce' ) . val ( ) ;
128+
129+ jQuery ( this )
130+ . parents ( '.ef-post_following_list' )
131+ . find ( 'input:checked' )
132+ . map ( function ( ) {
133+ userGroupIds . push ( jQuery ( this ) . val ( ) ) ;
134+ } ) ;
135+
136+ params . user_group_ids = userGroupIds ;
137+
138+ $ . ajax ( {
139+ type : 'POST' ,
140+ url : ( ajaxurl ) ? ajaxurl : wpListL10n . url ,
141+ data : params ,
142+
143+ success : function ( response ) {
57144 // Toggle the warning badges ("No Access" and "No Email") to signal the user won't receive notifications
58- if ( undefined !== response . data ) {
59- toggle_warning_badges ( $ ( parent_this ) , response ) ;
60- }
145+ const userHasNoAccess = response . data . subscribers_with_no_access . includes ( parseInt ( jQuery ( checkbox ) . val ( ) , 10 ) ) ;
146+ const userHasNoEmail = response . data . subscribers_with_no_email . includes ( parseInt ( jQuery ( checkbox ) . val ( ) , 10 ) ) ;
147+
148+ toggleWarningBadges ( jQuery ( checkbox ) , { userHasNoAccess, userHasNoEmail } ) ;
149+
61150 // Green 40% by default
62- var backgroundHighlightColor = "#90d296" ;
63- if ( warning_background ) {
151+ let backgroundHighlightColor = '#90d296' ;
152+
153+ if ( userHasNoAccess || userHasNoEmail ) {
64154 // Red 40% if there's a warning
65- var backgroundHighlightColor = " #ea8484" ;
155+ backgroundHighlightColor = ' #ea8484' ;
66156 }
67- var backgroundColor = parent_this . css ( 'background-color' ) ;
68- $ ( parent_this . parents ( 'li' ) )
69- . animate ( { 'backgroundColor' : backgroundHighlightColor } , 200 )
70- . animate ( { 'backgroundColor' :backgroundColor } , 200 ) ;
71-
157+
158+ const backgroundColor = 'transparent' ;
159+ jQuery ( checkbox . parents ( 'label' ) )
160+ . animate ( { backgroundColor : backgroundHighlightColor } , 200 )
161+ . animate ( { backgroundColor : backgroundColor } , 200 ) ;
162+
72163 // 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' ) ;
164+ jQuery ( '#ef-post_following_box' ) . trigger ( 'following_list_updated' ) ;
165+ } ,
166+ error : function ( ) {
167+ jQuery ( '#ef-post_following_users_box' ) . prev ( ) . append ( ' <p class="error">There was an error. Please reload the page.</p>' ) ;
74168 } ,
75- error : function ( r ) {
76- $ ( '#ef-post_following_users_box' ) . prev ( ) . append ( ' <p class="error">There was an error. Please reload the page.</p>' ) ;
77- }
78- } ) ;
79- } ) ;
80- } ) ;
169+ } ) ;
170+ } ) ;
171+ } ) ;
0 commit comments