PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/files/wp-social/2.6/assets/social.js

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