|
| 1 | +/* global wp, jQuery, ajaxurl, ef_notifications_localization, document, wpListL10n, ef_post_author_id */ |
| 2 | + |
1 | 3 | const { subscribe, select } = wp.data; |
2 | 4 |
|
3 | 5 | const BADGES_STATUS = { |
4 | 6 | error: 'error', |
5 | 7 | warning: 'warning', |
6 | | - success: 'success' |
7 | | -} |
| 8 | + success: 'success', |
| 9 | +}; |
8 | 10 |
|
9 | 11 | const BADGES = { |
10 | 12 | NO_ACCESS: { |
11 | 13 | id: 'no_access', |
12 | 14 | name: ef_notifications_localization.no_access, |
13 | | - status: BADGES_STATUS['error'] |
| 15 | + status: BADGES_STATUS.error, |
14 | 16 | }, |
15 | 17 | NO_EMAIL: { |
16 | 18 | id: 'no_email', |
17 | 19 | name: ef_notifications_localization.no_email, |
18 | | - status: BADGES_STATUS['error'] |
| 20 | + status: BADGES_STATUS.error, |
19 | 21 | }, |
20 | 22 | POST_AUTHOR: { |
21 | 23 | id: 'post_author', |
22 | 24 | name: ef_notifications_localization.post_author, |
23 | | - class: 'ef-badge-neutral' |
| 25 | + class: 'ef-badge-neutral', |
24 | 26 | }, |
25 | 27 | AUTO_SUBSCRIBE: { |
26 | 28 | id: 'auto_subscribed', |
27 | 29 | name: ef_notifications_localization.auto_subscribed, |
28 | | - class: 'ef-badge-neutral' |
| 30 | + class: 'ef-badge-neutral', |
29 | 31 | }, |
30 | | -} |
| 32 | +}; |
| 33 | + |
| 34 | +const getBadge = ( $el, badge ) => { |
| 35 | + const exists = $el.find( `[data-badge-id='${ badge.id }']` ); |
| 36 | + |
| 37 | + if ( exists.length ) { |
| 38 | + return jQuery( exists[ 0 ] ); |
| 39 | + } |
| 40 | + return null; |
| 41 | +}; |
| 42 | + |
| 43 | +const badgeTemplate = badge => { |
| 44 | + let classes = 'ef-user-badge'; |
| 45 | + |
| 46 | + if ( BADGES_STATUS.error === badge.status ) { |
| 47 | + classes += ' ef-user-badge-error'; |
| 48 | + } |
| 49 | + |
| 50 | + return `<div class="${ classes }" data-badge-id="${ badge.id }">${ badge.name }</div>`; |
| 51 | +}; |
31 | 52 |
|
32 | 53 | const addBadgeToEl = ( $el, badge ) => { |
33 | 54 | if ( getBadge( $el, badge ) ) { |
34 | 55 | return; |
35 | 56 | } |
36 | 57 |
|
37 | | - $el.append( badgeTemplate(badge ) ) |
38 | | -} |
| 58 | + $el.append( badgeTemplate( badge ) ); |
| 59 | +}; |
39 | 60 |
|
40 | 61 | const removeBadgeFromEl = ( $el, badge ) => { |
41 | 62 | const existingBadge = getBadge( $el, badge ); |
42 | 63 |
|
43 | | - if ( !existingBadge ) { |
| 64 | + if ( ! existingBadge ) { |
44 | 65 | return; |
45 | 66 | } |
46 | 67 |
|
47 | 68 | 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 | | -} |
| 69 | +}; |
59 | 70 |
|
60 | | -const badgeTemplate = ( badge ) => { |
61 | | - let classes = 'ef-user-badge'; |
| 71 | +jQuery( document ).ready( function( $ ) { |
| 72 | + jQuery( '#ef-post_following_users_box ul' ).listFilterizer(); |
62 | 73 |
|
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 | | - |
70 | | -jQuery(document).ready(function($) { |
71 | | - jQuery('#ef-post_following_users_box ul').listFilterizer(); |
72 | | - |
73 | | - var params = { |
| 74 | + const params = { |
74 | 75 | action: 'save_notifications', |
75 | | - post_id: jQuery('#post_ID').val(), |
| 76 | + post_id: jQuery( '#post_ID' ).val(), |
76 | 77 | }; |
77 | 78 |
|
78 | | - |
79 | | - var toggle_warning_badges = function( container, response ) { |
| 79 | + const toggleWarningBadges = function( container, response ) { |
80 | 80 | const $el = jQuery( container ).parent(); |
81 | 81 | const $badgesContainer = $el.closest( 'li' ).find( '.ef-user-list-badges' ); |
82 | 82 | // "No Access" If this user was flagged as not having access |
83 | | - var user_has_no_access = response.data.subscribers_with_no_access.includes( parseInt( jQuery( container ).val() ) ); |
84 | | - if ( user_has_no_access ) { |
85 | | - addBadgeToEl( $badgesContainer, BADGES[ 'NO_ACCESS' ] ); |
| 83 | + const userHasNoAccess = response.data.subscribers_with_no_access.includes( parseInt( jQuery( container ).val(), 10 ) ); |
| 84 | + if ( userHasNoAccess ) { |
| 85 | + addBadgeToEl( $badgesContainer, BADGES.NO_ACCESS ); |
86 | 86 | } else { |
87 | | - removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_ACCESS' ] ); |
| 87 | + removeBadgeFromEl( $badgesContainer, BADGES.NO_ACCESS ); |
88 | 88 | } |
89 | | - |
| 89 | + |
90 | 90 | // "No Email" If this user was flagged as not having an email |
91 | | - var user_has_no_email = response.data.subscribers_with_no_email.includes( parseInt( jQuery( container ).val() ) ); |
92 | | - if ( user_has_no_email ) { |
93 | | - addBadgeToEl( $badgesContainer, BADGES[ 'NO_EMAIL' ] ); |
| 91 | + const userHasNoEmail = response.data.subscribers_with_no_email.includes( parseInt( jQuery( container ).val(), 10 ) ); |
| 92 | + if ( userHasNoEmail ) { |
| 93 | + addBadgeToEl( $badgesContainer, BADGES.NO_EMAIL ); |
94 | 94 | } else { |
95 | | - removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_EMAIL' ] ); |
| 95 | + removeBadgeFromEl( $badgesContainer, BADGES.NO_EMAIL ); |
96 | 96 | } |
97 | | - } |
| 97 | + }; |
98 | 98 |
|
99 | 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 | | - } |
| 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 | 103 |
|
104 | 104 | show_post_author_badge(); |
105 | 105 |
|
| 106 | + const showAutosubscribedBadge = () => { |
| 107 | + const $userListItemActions = jQuery( "label[for='ef-selected-users-" + ef_post_author_id + "'] .ef-user-list-badges" ); |
| 108 | + addBadgeToEl( $userListItemActions, BADGES.AUTO_SUBSCRIBE ); |
| 109 | + }; |
106 | 110 |
|
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 | + const disableAutosubscribeCheckbox = () => { |
| 112 | + jQuery( '#ef-selected-users-' + ef_post_author_id ).prop( 'disabled', true ); |
| 113 | + }; |
111 | 114 |
|
112 | | - const disable_autosubscribe_checkbox = () => { |
113 | | - jQuery('#ef-selected-users-' + ef_post_author_id ).prop( 'disabled', true ); |
114 | | - } |
115 | | - |
116 | 115 | if ( typeof ef_post_author_auto_subscribe !== 'undefined' ) { |
117 | | - show_autosubscribed_badge(); |
118 | | - disable_autosubscribe_checkbox(); |
| 116 | + showAutosubscribedBadge(); |
| 117 | + disableAutosubscribeCheckbox(); |
119 | 118 | } |
120 | 119 |
|
| 120 | + jQuery( document ).on( 'click', '.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox', function() { |
| 121 | + const userGroupIds = []; |
| 122 | + const parentThis = jQuery( this ); |
| 123 | + params.ef_notifications_name = jQuery( this ).attr( 'name' ); |
| 124 | + params._nonce = jQuery( '#ef_notifications_nonce' ).val(); |
121 | 125 |
|
| 126 | + jQuery( this ) |
| 127 | + .parents( '.ef-post_following_list' ) |
| 128 | + .find( 'input:checked' ) |
| 129 | + .map( function() { |
| 130 | + userGroupIds.push( jQuery( this ).val() ); |
| 131 | + } ); |
122 | 132 |
|
123 | | - jQuery(document).on('click','.ef-post_following_list li input:checkbox, .ef-following_usergroups li input:checkbox', function() { |
124 | | - var user_group_ids = []; |
125 | | - var parent_this = 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 | | - user_group_ids.push(jQuery(this).val()); |
134 | | - }) |
| 133 | + params.user_group_ids = userGroupIds; |
135 | 134 |
|
136 | | - params.user_group_ids = user_group_ids; |
| 135 | + $.ajax( { |
| 136 | + type: 'POST', |
| 137 | + url: ( ajaxurl ) ? ajaxurl : wpListL10n.url, |
| 138 | + data: params, |
137 | 139 |
|
138 | | - $.ajax({ |
139 | | - type : 'POST', |
140 | | - url : (ajaxurl) ? ajaxurl : wpListL10n.url, |
141 | | - data : params, |
142 | | - |
143 | | - success : function( response ) { |
144 | | - // Reset background color (set during toggle_warning_badges if there's a warning) |
145 | | - warning_background = false; |
146 | | - |
| 140 | + success: function( response ) { |
147 | 141 | // Toggle the warning badges ("No Access" and "No Email") to signal the user won't receive notifications |
148 | 142 | if ( undefined !== response.data ) { |
149 | | - toggle_warning_badges( jQuery( parent_this ), response ); |
| 143 | + toggleWarningBadges( jQuery( parentThis ), response ); |
150 | 144 | } |
| 145 | + |
151 | 146 | // Green 40% by default |
152 | | - var backgroundHighlightColor = "#90d296"; |
153 | | - if ( warning_background ) { |
154 | | - // Red 40% if there's a warning |
155 | | - var backgroundHighlightColor = "#ea8484"; |
156 | | - } |
157 | | - var backgroundColor = 'transparent'; |
158 | | - jQuery(parent_this.parents('label')) |
159 | | - .animate( { 'backgroundColor': backgroundHighlightColor }, 200 ) |
160 | | - .animate( { 'backgroundColor': backgroundColor }, 200 ); |
161 | | - |
| 147 | + const backgroundHighlightColor = '#90d296'; |
| 148 | + |
| 149 | + const backgroundColor = 'transparent'; |
| 150 | + jQuery( parentThis.parents( 'label' ) ) |
| 151 | + .animate( { backgroundColor: backgroundHighlightColor }, 200 ) |
| 152 | + .animate( { backgroundColor: backgroundColor }, 200 ); |
| 153 | + |
162 | 154 | // This event is used to show an updated list of who will be notified of editorial comments and status updates. |
163 | 155 | jQuery( '#ef-post_following_box' ).trigger( 'following_list_updated' ); |
164 | 156 | }, |
165 | | - error : function(r) { |
166 | | - jQuery('#ef-post_following_users_box').prev().append(' <p class="error">There was an error. Please reload the page.</p>'); |
167 | | - } |
168 | | - }); |
169 | | - }); |
170 | | -}); |
| 157 | + error: function() { |
| 158 | + jQuery( '#ef-post_following_users_box' ).prev().append( ' <p class="error">There was an error. Please reload the page.</p>' ); |
| 159 | + }, |
| 160 | + } ); |
| 161 | + } ); |
| 162 | +} ); |
0 commit comments