|
1 (function($) { |
|
2 /* |
|
3 * Append social-js class to html element. This allows us to prevent JS FOUC |
|
4 * in an accessible way. |
|
5 * Run it ASAP. Doesn't need to run on domReady because the html element is |
|
6 * the first thing available. |
|
7 */ |
|
8 var c = document.getElementsByTagName('html')[0]; |
|
9 c.className += ' social-js'; |
|
10 |
|
11 $(function() { |
|
12 $('.social-login').click(function(e) { |
|
13 e.preventDefault(); |
|
14 |
|
15 var $window = $(this), |
|
16 $auth_window = window.open($(this).attr('href'), "ServiceAssociate", 'width=700,height=400'), |
|
17 auth_poll = null; |
|
18 |
|
19 auth_poll = setInterval(function() { |
|
20 if ($auth_window.closed) { |
|
21 clearInterval(auth_poll); |
|
22 |
|
23 if (!$window.hasClass('comments')) { |
|
24 window.location.reload(); |
|
25 } |
|
26 else { |
|
27 var $parent = $('.social-post'); |
|
28 $.post($parent.find('#reload_url').val(), {}, function(response) { |
|
29 if (response.result == 'success') { |
|
30 // Add logged-in body class since we're not going to be refreshing the page. |
|
31 $('body').addClass('logged-in'); |
|
32 |
|
33 var $cancel = $('#cancel-comment-reply-link'), |
|
34 $parent = $cancel.closest('li'), |
|
35 $clone = $cancel.clone(true); |
|
36 $cancel.click(); |
|
37 $('#respond').replaceWith(response.html); |
|
38 // replace the cancel button with the one we cloned, to retain actions |
|
39 $('#respond').find('#cancel-comment-reply-link').remove().end() |
|
40 .find('#reply-title small:first').append($clone); |
|
41 $parent.find('.comment-reply-link:first').click(); |
|
42 |
|
43 $('#primary').find('#social_login').parent().html(response.disconnect_url); |
|
44 } |
|
45 }, 'json'); |
|
46 } |
|
47 } |
|
48 }, 100); |
|
49 }); |
|
50 |
|
51 // comments.php |
|
52 if ($('#social').length) { |
|
53 // MCC Tabs |
|
54 var $prevLink = null, |
|
55 prevLink = null, |
|
56 $nextLink = null, |
|
57 nextLink = null; |
|
58 if ($('#comments .nav-previous a').length) { |
|
59 $prevLink = $('#comments .nav-previous a'); |
|
60 prevLink = $prevLink.attr('href'); |
|
61 } |
|
62 |
|
63 if ($('#comments .nav-next a').length) { |
|
64 $nextLink = $('#comments .nav-next a'); |
|
65 nextLink = $nextLink.attr('href'); |
|
66 } |
|
67 |
|
68 $('.social-nav a').click(function(e) { |
|
69 e.preventDefault(); |
|
70 $('#cancel-comment-reply-link').trigger('click'); |
|
71 |
|
72 $('.social-current-tab').removeClass('social-current-tab'); |
|
73 $(this).parent().addClass('social-current-tab'); |
|
74 $('.social-items').removeClass('social-comment-collapse'); |
|
75 |
|
76 var className = $(this).attr('rel'); |
|
77 if (className == 'social-all') { |
|
78 if (nextLink !== null) { |
|
79 $nextLink.attr('href', nextLink); |
|
80 } |
|
81 |
|
82 if (prevLink !== null) { |
|
83 $prevLink.attr('href', prevLink); |
|
84 } |
|
85 |
|
86 $('.social-commentlist li').removeClass('social-comment-collapse'); |
|
87 } else { |
|
88 $('.social-items:not(.' + className + ')').addClass('social-comment-collapse'); |
|
89 $('.social-commentlist li').each(function() { |
|
90 if (!$(this).hasClass(className)) { |
|
91 $(this).addClass('social-comment-collapse'); |
|
92 } |
|
93 else { |
|
94 $(this).removeClass('social-comment-collapse'); |
|
95 } |
|
96 }); |
|
97 |
|
98 if (prevLink !== null) { |
|
99 var _prevLink = prevLink.split('#comments'); |
|
100 if (_prevLink.indexOf('?') == -1) { |
|
101 _prevLink[0] = _prevLink[0] + '?'; |
|
102 } |
|
103 else { |
|
104 _prevLink[0] = _prevLink[0] + '&'; |
|
105 } |
|
106 $prevLink.attr('href', _prevLink[0] + 'social_tab=' + className + '#comments'); |
|
107 } |
|
108 |
|
109 if (nextLink !== null) { |
|
110 var _nextLink = nextLink.split('#comments'); |
|
111 if (_nextLink.indexOf('?') == -1) { |
|
112 _nextLink[0] = _nextLink[0] + '?'; |
|
113 } |
|
114 else { |
|
115 _nextLink[0] = _nextLink[0] + '&'; |
|
116 } |
|
117 $nextLink.attr('href', _nextLink[0] + 'social_tab=' + className + '#comments'); |
|
118 } |
|
119 } |
|
120 }); |
|
121 |
|
122 $('.social-current-tab a').trigger('click'); |
|
123 |
|
124 /** |
|
125 * Inserts the Twitter username for the reply to content. |
|
126 * |
|
127 * @param $author |
|
128 * @param $textarea |
|
129 * @param extraContent |
|
130 */ |
|
131 function insertTwitterUsername($author, $textarea, extraContent) { |
|
132 var username = $author.html() + ' '; |
|
133 if (username.substr(0, 1) != '@') { |
|
134 username = '@' + username; |
|
135 } |
|
136 if (extraContent !== undefined) { |
|
137 username += extraContent; |
|
138 } |
|
139 var val = $textarea.val(); |
|
140 if (val.substr(0, username.length) != username) { |
|
141 var content = ''; |
|
142 var content = (val.length > 0 ? username + val : username); |
|
143 var pos = content.length; |
|
144 $textarea.val(content); |
|
145 if ($textarea.get(0).setSelectionRange) { |
|
146 $textarea.focus(); |
|
147 $textarea.get(0).setSelectionRange(pos, pos); |
|
148 } |
|
149 else if ($textarea.createTextRange) { |
|
150 var range = $textarea.get(0).createTextRange(); |
|
151 range.collapse(true); |
|
152 range.moveEnd('character', pos); |
|
153 range.moveStart('character', pos); |
|
154 range.select(); |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 /** |
|
160 * Removes the inserted Twitter username from the content. |
|
161 * |
|
162 * @param $author |
|
163 * @param $textarea |
|
164 */ |
|
165 function removeTwitterUsername($author, $textarea) { |
|
166 var username = $author.html() + ' '; |
|
167 if (username.substr(0, 1) != '@') { |
|
168 username = '@' + username; |
|
169 } |
|
170 var val = $textarea.val(); |
|
171 if (val.substr(0, username.length) == username) { |
|
172 var content = val.substr(username.length); |
|
173 var pos = content.length; |
|
174 $textarea.val(content); |
|
175 if ($textarea.get(0).setSelectionRange) { |
|
176 $textarea.focus(); |
|
177 $textarea.get(0).setSelectionRange(pos, pos); |
|
178 } |
|
179 else if ($textarea.createTextRange) { |
|
180 var range = $textarea.get(0).createTextRange(); |
|
181 range.collapse(true); |
|
182 range.moveEnd('character', pos); |
|
183 range.moveStart('character', pos); |
|
184 range.select(); |
|
185 } |
|
186 } |
|
187 } |
|
188 |
|
189 $('.comment-reply-link').click(function() { |
|
190 $('.comment-reply-link').show(); |
|
191 $(this).hide(); |
|
192 var $title = $('#reply-title'), |
|
193 $cancel = null, |
|
194 $parent = $(this).closest('li'), |
|
195 $textarea = $parent.find('textarea'), |
|
196 $form = $('#commentform'); |
|
197 // set data attr for current title |
|
198 // set title to Sociali18n.commentReplyTitle |
|
199 $cancel = $title.find('small').hide().appendTo($title); |
|
200 $title.data('orig-title', $title.find('span').text()) |
|
201 .find('span').html(Sociali18n.commentReplyTitle + ' ') |
|
202 .append($cancel.show()); |
|
203 if ($parent.hasClass('social-twitter') || $parent.hasClass('social-facebook')) { |
|
204 var commentService = ($parent.hasClass('social-twitter') ? 'twitter' : 'facebook'); |
|
205 // check to see if the current user has a profile for the service |
|
206 if ($('#post_accounts option[data-type="' + commentService + '"]').size() == 0 && |
|
207 $form.find('.social-identity .social-' + commentService + '-icon').size() == 0) { |
|
208 return; |
|
209 } |
|
210 var username = '', |
|
211 matched = false, |
|
212 $option = false, |
|
213 $options = $('#post_accounts option[data-type="' + commentService + '"]'); |
|
214 switch (commentService) { |
|
215 case 'twitter': |
|
216 // look for a username at the beginning of the comment |
|
217 // if we have that account available, use it |
|
218 // if not, select the first account for that service |
|
219 var content = $.trim($parent.find('.social-comment-body:first').text()); |
|
220 if (content.substr(0, 1) == '@') { |
|
221 username = content.split(' ')[0].substr(1); |
|
222 } |
|
223 $options.each(function() { |
|
224 if ($(this).html() == username) { |
|
225 matched = true; |
|
226 $option = $(this); |
|
227 return false; |
|
228 } |
|
229 }); |
|
230 var $author = $parent.find('.social-comment-author a'), |
|
231 author_rel = $author.attr('rel').split(' '); |
|
232 $('#in_reply_to_status_id').val(author_rel[0]); |
|
233 // if click invoked programatically, do nothing. |
|
234 if ($textarea.size()) { |
|
235 insertTwitterUsername($author, $textarea); |
|
236 } |
|
237 break; |
|
238 case 'facebook': |
|
239 break; |
|
240 } |
|
241 if (!matched) { |
|
242 $options.each(function() { |
|
243 $option = $(this); |
|
244 return false; |
|
245 }); |
|
246 } |
|
247 if ($option) { |
|
248 $('#post_accounts').val($option.attr('value')).change(); |
|
249 } |
|
250 $('#post_to_service').prop('checked', true); |
|
251 } |
|
252 }); |
|
253 |
|
254 // some actions need the "live" treatment |
|
255 $(document).on('click', '#cancel-comment-reply-link', function() { |
|
256 $('.comment-reply-link').show(); |
|
257 $('#post_to_service').prop('checked', false); |
|
258 $('#in_reply_to_status_id').val(''); |
|
259 }); |
|
260 // due to the way WP's comment JS works, we can't run this on the "live" action above |
|
261 // by the time this runs, the node is moved and $parent is an empty set |
|
262 $('#cancel-comment-reply-link').click(function() { |
|
263 var $title = $('#reply-title'), |
|
264 $cancel = null, |
|
265 $parent = $(this).closest('li'), |
|
266 $textarea = $parent.find('textarea'), |
|
267 $author = $parent.find('.social-comment-author a'); |
|
268 // if click invoked programatically, do nothing. |
|
269 if ($textarea.size()) { |
|
270 // restore title |
|
271 $cancel = $title.find('small').hide().appendTo($title); |
|
272 $title.find('span').html($title.data('orig-title') + ' ').append($cancel); |
|
273 removeTwitterUsername($author, $textarea); |
|
274 } |
|
275 }); |
|
276 |
|
277 var $avatar = $('#commentform .avatar'); |
|
278 var original_avatar = $avatar.attr('src'); |
|
279 $(document).on('change', '#post_accounts', function() { |
|
280 $(this).find('option:selected').each(function() { |
|
281 var $parent = $(this).closest('li'), |
|
282 $textarea = $parent.find('textarea'), |
|
283 $author = $parent.find('.social-comment-author a'); |
|
284 if ($textarea.size()) { |
|
285 if ($(this).data('type') == 'twitter') { |
|
286 insertTwitterUsername($author, $textarea); |
|
287 } |
|
288 else { |
|
289 removeTwitterUsername($author, $textarea); |
|
290 } |
|
291 } |
|
292 var avatar = $(this).attr('rel'); |
|
293 if (avatar !== undefined) { |
|
294 $avatar.attr('src', avatar); |
|
295 } |
|
296 else { |
|
297 $avatar.attr('src', original_avatar); |
|
298 } |
|
299 var label = $(this).parent().attr('label'); |
|
300 if (label !== undefined) { |
|
301 $('#post_to').show().find('span').html(label); |
|
302 } |
|
303 else { |
|
304 $('#post_to').hide(); |
|
305 } |
|
306 }); |
|
307 }); |
|
308 $('#post_accounts').trigger('change'); |
|
309 } |
|
310 |
|
311 /** |
|
312 * Manual Aggregation |
|
313 */ |
|
314 var $social_comments_adminbar_item = $('#wp-admin-bar-social-find-comments'); |
|
315 if ($social_comments_adminbar_item.size()) { |
|
316 var $social_spinner = $social_comments_adminbar_item.find('.social-aggregation-spinner'); |
|
317 var $social_aggregation = $('#social_aggregation'); |
|
318 var $comment_adminbar_item = $('#wp-admin-bar-comments'); |
|
319 $social_aggregation.click(function(e) { |
|
320 if ($(this).attr('href') == '#') { |
|
321 e.preventDefault(); |
|
322 } |
|
323 }).removeClass('running-aggregation'); |
|
324 $comment_adminbar_item.removeClass('running-aggregation'); |
|
325 |
|
326 $social_comments_adminbar_item.find('a').click(function(e) { |
|
327 e.preventDefault(); |
|
328 if (!$comment_adminbar_item.hasClass('running-aggregation')) { |
|
329 $comment_adminbar_item.addClass('running-aggregation'); |
|
330 |
|
331 // remove old results (slide left) |
|
332 $('#wp-adminbar-comments-social').animate({ width: '0' }, function() { |
|
333 $(this).remove(); |
|
334 }); |
|
335 |
|
336 // show spinner |
|
337 $comment_adminbar_item.find('#ab-awaiting-mod').hide().end() |
|
338 .find('a:first').append($social_spinner); |
|
339 $social_spinner.show(); |
|
340 SocialLoadingInterval = setInterval(function() { |
|
341 var next = false; |
|
342 var $dots = jQuery('.social-aggregation-spinner').find('.social-dot'); |
|
343 $dots.each(function() { |
|
344 var active = jQuery(this).hasClass('dot-active'); |
|
345 if (next) { |
|
346 jQuery(this).addClass('dot-active'); |
|
347 } |
|
348 if (active) { |
|
349 jQuery(this).removeClass('dot-active'); |
|
350 next = true; |
|
351 } |
|
352 else { |
|
353 next = false; |
|
354 } |
|
355 }); |
|
356 if ($dots.filter('.dot-active').size() == 0) { |
|
357 $dots.filter(':first').addClass('dot-active'); |
|
358 } |
|
359 }, 425); |
|
360 |
|
361 // make AJAX call |
|
362 $.get( |
|
363 $(this).attr('href'), |
|
364 { render: 'false' }, |
|
365 function(response) { |
|
366 // hide spinner |
|
367 $social_spinner.hide(); |
|
368 $social_comments_adminbar_item.append($social_spinner); |
|
369 clearInterval(SocialLoadingInterval); |
|
370 |
|
371 // update count, show count |
|
372 $comment_adminbar_item.find('#ab-awaiting-mod') |
|
373 .html(response.total).show(); |
|
374 |
|
375 // show results (slide right) |
|
376 $comment_adminbar_item.addClass('social-comments-found').after(response.html); |
|
377 var $social_comments_found = $('#wp-adminbar-comments-social'); |
|
378 var found_width = $social_comments_found.width(); |
|
379 $social_comments_found.css({ |
|
380 position: 'relative', |
|
381 visibility: 'visible', |
|
382 width: 0 |
|
383 }).animate({ width: found_width + 'px' }); |
|
384 |
|
385 // set params for next call |
|
386 $social_aggregation.attr('href', response.link); |
|
387 |
|
388 $comment_adminbar_item.removeClass('running-aggregation'); |
|
389 }, |
|
390 'json' |
|
391 ); |
|
392 } |
|
393 }); |
|
394 } |
|
395 $('#wp-admin-bar-social-add-tweet-by-url a').each(function() { |
|
396 $form = $(this).find('form'); |
|
397 $form.data('running-import', false).keydown(function(e) { |
|
398 e.stopPropagation(); |
|
399 }).submit(function() { |
|
400 if (!$(this).data('running-import')) { |
|
401 var $this = $(this), |
|
402 $inputs = $this.find('input'); |
|
403 $this.data('running-import', true); |
|
404 $inputs.attr('disabled', 'disabled'); |
|
405 |
|
406 var $loading = $('<div class="loading"></div>') |
|
407 .height($this.height()) |
|
408 .width($this.width()); |
|
409 $this.hide().closest('li') |
|
410 .find('.msg').remove().end() |
|
411 .end() |
|
412 .after($loading); |
|
413 |
|
414 $.get($this.attr('action'), { |
|
415 url: $('input[name=url]').val() |
|
416 }, function(response) { |
|
417 var msg = msgClass = ''; |
|
418 switch (response) { |
|
419 case 'protected': |
|
420 case 'invalid': |
|
421 msg = socialAdminBarMsgs[response]; |
|
422 msgClass = ' error'; |
|
423 break; |
|
424 default: |
|
425 msg = socialAdminBarMsgs['success']; |
|
426 } |
|
427 $loading.remove(); |
|
428 $this.data('running-import', false).show() |
|
429 .after('<p class="msg' + msgClass + '">' + msg + '</p>'); |
|
430 $inputs.removeAttr('disabled').filter(':text').val('').focus(); |
|
431 }); |
|
432 } |
|
433 return false; |
|
434 }).appendTo($(this).closest('li')); |
|
435 }).click(function(e) { |
|
436 $(this).hide().closest('li').find('form').show().find(':text').focus().select(); |
|
437 return false; |
|
438 }); |
|
439 |
|
440 /** |
|
441 * Social items |
|
442 */ |
|
443 if ($('.social-items-and-more').length) { |
|
444 $('.social-items-and-more').click(function(e) { |
|
445 e.preventDefault(); |
|
446 |
|
447 $(this).parent().find('a').show(); |
|
448 $(this).hide(); |
|
449 }); |
|
450 } |
|
451 }); |
|
452 })(jQuery); |