PluginProbe
Edit Flow / 0.11.0
Edit Flow v0.11.0
0.11.1 0.11.0 0.7.2 0.7.3 0.7.4 0.7.5 0.7.6 0.8 0.8.1 0.8.2 0.9 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 trunk 0.1.5 0.10.0 0.10.1 0.10.2 All 44 releases
edit-flow / modules / editorial-comments / lib / editorial-comments.js

editorial-comments.js in Edit Flow 0.11.0, at modules/editorial-comments/lib/editorial-comments.js

254 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery( document ).ready( function () {
2 editorialCommentReply.init();
3
4 // Check if certain hash flag set and take action
5 if ( location.hash == '#editorialcomments/add' ) {
6 editorialCommentReply.open();
7 } else if ( location.hash.search( /#editorialcomments\/reply/ ) > -1 ) {
8 const reply_id = parseInt(
9 location.hash.substring( location.hash.lastIndexOf( '/' ) + 1 ),
10 10
11 );
12 if ( reply_id > 0 ) {
13 editorialCommentReply.open( reply_id );
14 }
15 }
16 } );
17
18 /**
19 * Blatantly stolen and modified from /wp-admin/js/edit-comment.dev.js -- yay!
20 */
21 editorialCommentReply = {
22 init() {
23 const row = jQuery( '#ef-replyrow' );
24
25 // Bind click events to cancel and submit buttons
26 jQuery( 'a.ef-replycancel', row ).on( 'click', function () {
27 return editorialCommentReply.revert();
28 } );
29 jQuery( 'a.ef-replysave', row ).on( 'click', function () {
30 return editorialCommentReply.send();
31 } );
32
33 // Watch for changes to the subscribed users.
34 jQuery( '#ef-post_following_box' ).on( 'following_list_updated', function () {
35 editorialCommentReply.notifiedMessage();
36 } );
37 },
38
39 revert() {
40 // Fade out slowly, slowly, slowly...
41 jQuery( '#ef-replyrow' ).fadeOut( 'fast', function () {
42 editorialCommentReply.close();
43 } );
44 return false;
45 },
46
47 close() {
48 jQuery( '#ef-comment_respond' ).show();
49
50 // Move reply form back after the main "Respond" form
51 jQuery( '#ef-post_comment' ).after( jQuery( '#ef-replyrow' ) );
52
53 // Empty out all the form values
54 jQuery( '#ef-replycontent' ).val( '' );
55 jQuery( '#ef-comment_parent' ).val( '' );
56
57 // Hide error and waiting
58 jQuery( '#ef-replysubmit .error' ).html( '' ).hide();
59 jQuery( '#ef-comment_loading' ).hide();
60 },
61
62 /**
63 * @id = comment id
64 */
65 open( id ) {
66 let parent;
67
68 // Close any open reply boxes
69 this.close();
70
71 // Check if reply or new comment
72 if ( id ) {
73 jQuery( 'input#ef-comment_parent' ).val( id );
74 parent = '#comment-' + id;
75 } else {
76 parent = '#ef-comments_wrapper';
77 }
78
79 jQuery( '#ef-comment_respond' ).hide();
80
81 // Display who will be notified for this comment
82 this.notifiedMessage();
83
84 // Show reply textbox
85 jQuery( '#ef-replyrow' ).show().appendTo( jQuery( parent ) );
86
87 jQuery( '#ef-replycontent' ).focus();
88
89 return false;
90 },
91
92 /**
93 * Sends the ajax response to save the commment
94 * @param bool reply - indicates whether the comment is a reply or not
95 */
96 send( reply ) {
97 const post = {};
98 const containter_id = '#ef-replyrow';
99
100 jQuery( '#ef-replysubmit .error' ).html( '' ).hide();
101
102 // Validation: check to see if comment entered
103 post.content = jQuery.trim( jQuery( '#ef-replycontent' ).val() );
104 if ( ! post.content ) {
105 jQuery( '#ef-replyrow .error' ).text( 'Please enter a comment' ).show();
106 return;
107 }
108
109 jQuery( '#ef-comment_loading' ).show();
110
111 // Prepare data
112 post.action = 'editflow_ajax_insert_comment';
113 post.parent =
114 jQuery( '#ef-comment_parent' ).val() == '' ? 0 : jQuery( '#ef-comment_parent' ).val();
115 post._nonce = jQuery( '#ef_comment_nonce' ).val();
116 post.post_id = jQuery( '#ef-post_id' ).val();
117 post.notification = jQuery( '#ef-reply-notifier' ).val();
118
119 // Send the request
120 jQuery.ajax( {
121 type: 'POST',
122 url: ajaxurl ? ajaxurl : wpListL10n.url,
123 data: post,
124 success( x ) {
125 editorialCommentReply.show( x );
126 },
127 error( r ) {
128 editorialCommentReply.error( r );
129 },
130 } );
131
132 return false;
133 },
134
135 /**
136 * Display who will be notified of the new comment.
137 */
138 notifiedMessage() {
139 const message_wrapper = jQuery( '#ef-reply-notifier' );
140
141 if ( ! message_wrapper[ 0 ] ) {
142 return;
143 }
144
145 const subscribed_users = jQuery( '.ef-post_following_list li input:checkbox:checked' );
146
147 const usernames = [];
148 subscribed_users.each( function () {
149 // Add usernames of checked users to the list if they don't have a blocking class
150 if (
151 ! jQuery( this )
152 .siblings()
153 .is( '.post_following_list-no_email,.post_following_list-no_access' ) &&
154 ! jQuery( this ).hasClass( 'post_following_list-current_user' )
155 ) {
156 usernames.push(
157 jQuery( this ).parent().siblings( '.ef-user_displayname, .ef-usergroup_name' ).text()
158 );
159 }
160 } );
161
162 // No users will be notified, so return early with a default message.
163 if ( 0 === usernames.length ) {
164 message_wrapper.addClass( 'ef-none-selected' );
165 message_wrapper.val( __ef_localize_post_comment.none_notified );
166 return;
167 }
168
169 // Convert array of usernames into a sentence.
170 let message = usernames.pop();
171 if ( usernames.length > 0 ) {
172 message = usernames.join( ', ' ) + ' ' + __ef_localize_post_comment.and + ' ' + message + '.';
173 }
174
175 message_wrapper.removeClass( 'ef-none-selected' );
176 message_wrapper.val( message );
177 },
178
179 show( xml ) {
180 let response;
181 let comment;
182 let supplemental;
183 let id;
184 let bg;
185
186 // Didn't pass validation, so let's throw an error
187 if ( typeof xml === 'string' ) {
188 this.error( { responseText: xml } );
189 return false;
190 }
191
192 // Parse the response
193 response = wpAjax.parseAjaxResponse( xml );
194 if ( response.errors ) {
195 // Uh oh, errors found
196 this.error( { responseText: wpAjax.broken } );
197 return false;
198 }
199
200 response = response.responses[ 0 ];
201 comment = response.data;
202 supplemental = response.supplemental;
203
204 jQuery( comment ).hide();
205
206 if ( response.action.indexOf( 'reply' ) == -1 || ! ef_thread_comments ) {
207 // Not a reply, so add it to the bottom
208 jQuery( '#ef-comments' ).append( comment );
209 } else {
210 // This is a reply, so add it after the comment replied to
211
212 if ( jQuery( '#ef-replyrow' ).parent().next().is( 'ul' ) ) {
213 // Already been replied to, so just add to the list
214 jQuery( '#ef-replyrow' ).parent().next().append( comment );
215 } else {
216 // This is a first reply, so create an unordered list to house the comment
217 const newUL = jQuery( '<ul></ul>' ).addClass( 'children' ).append( comment );
218 jQuery( '#ef-replyrow' ).parent().after( newUL );
219 }
220 }
221
222 // Get the comment contaner's id
223 this.o = id = '#comment-' + response.id;
224 // Close the reply box
225 this.revert();
226
227 // Show the new comment
228 jQuery( id )
229 .animate( { backgroundColor: '#CCEEBB' }, 600 )
230 .animate( { backgroundColor: '#fff' }, 600 );
231 },
232
233 error( r ) {
234 // Oh noes! We haz an error!
235 jQuery( '#ef-comment_loading' ).hide();
236
237 if ( r.responseText ) {
238 er = r.responseText.replace( /<.[^<>]*?>/g, '' );
239 }
240
241 let message = '';
242 try {
243 er = JSON.parse( er );
244 message = er.message ?? er;
245 } catch ( parseErr ) {
246 message = er;
247 }
248
249 if ( er ) {
250 jQuery( '#ef-replysubmit .error' ).text( message ).show();
251 }
252 },
253 };
254