Skip to content

Commit 728e000

Browse files
committed
Update JS code style
1 parent d62781e commit 728e000

File tree

1 file changed

+91
-99
lines changed

1 file changed

+91
-99
lines changed
Lines changed: 91 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,162 @@
1+
/* global wp, jQuery, ajaxurl, ef_notifications_localization, document, wpListL10n, ef_post_author_id */
2+
13
const { subscribe, select } = wp.data;
24

35
const BADGES_STATUS = {
46
error: 'error',
57
warning: 'warning',
6-
success: 'success'
7-
}
8+
success: 'success',
9+
};
810

911
const BADGES = {
1012
NO_ACCESS: {
1113
id: 'no_access',
1214
name: ef_notifications_localization.no_access,
13-
status: BADGES_STATUS['error']
15+
status: BADGES_STATUS.error,
1416
},
1517
NO_EMAIL: {
1618
id: 'no_email',
1719
name: ef_notifications_localization.no_email,
18-
status: BADGES_STATUS['error']
20+
status: BADGES_STATUS.error,
1921
},
2022
POST_AUTHOR: {
2123
id: 'post_author',
2224
name: ef_notifications_localization.post_author,
23-
class: 'ef-badge-neutral'
25+
class: 'ef-badge-neutral',
2426
},
2527
AUTO_SUBSCRIBE: {
2628
id: 'auto_subscribed',
2729
name: ef_notifications_localization.auto_subscribed,
28-
class: 'ef-badge-neutral'
30+
class: 'ef-badge-neutral',
2931
},
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+
};
3152

3253
const addBadgeToEl = ( $el, badge ) => {
3354
if ( getBadge( $el, badge ) ) {
3455
return;
3556
}
3657

37-
$el.append( badgeTemplate(badge ) )
38-
}
58+
$el.append( badgeTemplate( badge ) );
59+
};
3960

4061
const removeBadgeFromEl = ( $el, badge ) => {
4162
const existingBadge = getBadge( $el, badge );
4263

43-
if ( !existingBadge ) {
64+
if ( ! existingBadge ) {
4465
return;
4566
}
4667

4768
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+
};
5970

60-
const badgeTemplate = ( badge ) => {
61-
let classes = 'ef-user-badge';
71+
jQuery( document ).ready( function( $ ) {
72+
jQuery( '#ef-post_following_users_box ul' ).listFilterizer();
6273

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 = {
7475
action: 'save_notifications',
75-
post_id: jQuery('#post_ID').val(),
76+
post_id: jQuery( '#post_ID' ).val(),
7677
};
7778

78-
79-
var toggle_warning_badges = function( container, response ) {
79+
const toggleWarningBadges = function( container, response ) {
8080
const $el = jQuery( container ).parent();
8181
const $badgesContainer = $el.closest( 'li' ).find( '.ef-user-list-badges' );
8282
// "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 );
8686
} else {
87-
removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_ACCESS' ] );
87+
removeBadgeFromEl( $badgesContainer, BADGES.NO_ACCESS );
8888
}
89-
89+
9090
// "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 );
9494
} else {
95-
removeBadgeFromEl( $badgesContainer, BADGES[ 'NO_EMAIL' ] );
95+
removeBadgeFromEl( $badgesContainer, BADGES.NO_EMAIL );
9696
}
97-
}
97+
};
9898

9999
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+
};
103103

104104
show_post_author_badge();
105105

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+
};
106110

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+
};
111114

112-
const disable_autosubscribe_checkbox = () => {
113-
jQuery('#ef-selected-users-' + ef_post_author_id ).prop( 'disabled', true );
114-
}
115-
116115
if ( typeof ef_post_author_auto_subscribe !== 'undefined' ) {
117-
show_autosubscribed_badge();
118-
disable_autosubscribe_checkbox();
116+
showAutosubscribedBadge();
117+
disableAutosubscribeCheckbox();
119118
}
120119

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();
121125

126+
jQuery( this )
127+
.parents( '.ef-post_following_list' )
128+
.find( 'input:checked' )
129+
.map( function() {
130+
userGroupIds.push( jQuery( this ).val() );
131+
} );
122132

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;
135134

136-
params.user_group_ids = user_group_ids;
135+
$.ajax( {
136+
type: 'POST',
137+
url: ( ajaxurl ) ? ajaxurl : wpListL10n.url,
138+
data: params,
137139

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 ) {
147141
// Toggle the warning badges ("No Access" and "No Email") to signal the user won't receive notifications
148142
if ( undefined !== response.data ) {
149-
toggle_warning_badges( jQuery( parent_this ), response );
143+
toggleWarningBadges( jQuery( parentThis ), response );
150144
}
145+
151146
// 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+
162154
// This event is used to show an updated list of who will be notified of editorial comments and status updates.
163155
jQuery( '#ef-post_following_box' ).trigger( 'following_list_updated' );
164156
},
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

Comments
 (0)