/applications/dashboard/js/activity.js

https://github.com/Emaratilicious/Garden · JavaScript · 162 lines · 123 code · 15 blank · 24 comment · 17 complexity · 49381b9de6589970e9b990f60dff4b57 MD5 · raw file

  1. jQuery(document).ready(function($) {
  2. // Set the max chars in the activity comment boxes
  3. $('form.Activity textarea').setMaxChars(1000);
  4. // Hide activity deletes and hijack their clicks to confirm
  5. $('ul.Activities a.Delete, ul.Activities a.DeleteComment').popup({
  6. confirm: true,
  7. followConfirm: false,
  8. afterConfirm: function(json, sender) {
  9. var row = $(sender).parents('li:first');
  10. $(row).slideUp('fast', function() {
  11. $(row).remove();
  12. });
  13. }
  14. });
  15. // Reveal activity deletes on hover
  16. $('ul.Activities li').livequery(function() {
  17. $(this).find('a.Delete').hide();
  18. $(this).hover(function() {
  19. $(this).find('a.Delete').show();
  20. }, function() {
  21. $(this).find('a.Delete').hide();
  22. });
  23. });
  24. /* Comments */
  25. // Hide/reveal the comments when the comment link is clicked
  26. $('a.CommentOption').live('click', function() {
  27. var comments = $(this).parents('li.Activity').find('ul.ActivityComments');
  28. comments.toggle();
  29. comments.find('a.CommentLink').click();
  30. return false;
  31. });
  32. // Hijack commentlink clicks
  33. $('a.CommentLink').live('click', function() {
  34. // Hide the anchor
  35. var anchor = this;
  36. $(anchor).hide();
  37. var row = $(anchor).parents('li.CommentForm');
  38. // Reveal the form
  39. var frm = $(row).find('form');
  40. frm.show();
  41. // Focus on the textbox
  42. var textbox = frm.find('textarea');
  43. textbox.focus().blur(function() {
  44. // Hide the form onblur if empty
  45. if (this.value == '') {
  46. var comments = $(anchor).parents('.Comments');
  47. var children = $(comments).children();
  48. var rowCount = children.length - 1; // Subtract the commentform row
  49. if (rowCount > 0) {
  50. // Hide/clear the form and reveal the anchor
  51. $(comments).find('.Errors').remove();
  52. $(frm).hide();
  53. $(anchor).show();
  54. } else {
  55. // Hide all elements in .Comments
  56. $(comments).hide();
  57. }
  58. }
  59. });
  60. return false;
  61. });
  62. // Hijack comment form button clicks
  63. $('ul.ActivityComments form input.Button').live('click', function() {
  64. var button = this;
  65. var frm = $(button).parents('form');
  66. var row = $(frm).parents('li.CommentForm');
  67. var textbox = $(row).find('textarea');
  68. // Post the form, place the results above the input, and erase the textbox
  69. var postValues = frm.serialize() + '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW
  70. var activityId = frm.find('[name$=ActivityID]').val();
  71. var action = frm.attr('action');
  72. $.ajax({
  73. type: "POST",
  74. url: action,
  75. data: postValues,
  76. dataType: 'json',
  77. error: function(XMLHttpRequest, textStatus, errorThrown) {
  78. $.popup({}, XMLHttpRequest.responseText);
  79. },
  80. success: function(json) {
  81. json = $.postParseJson(json);
  82. // Remove any old errors from the form
  83. $('div.Errors').remove();
  84. if (json.FormSaved == false) {
  85. if (json.ErrorMessages)
  86. $(row).prepend(json.ErrorMessages);
  87. } else {
  88. $(row).before(json.Data);
  89. textbox.val('').blur();
  90. // Make sure that hidden items appear
  91. $('ul.ActivityComments li.Hidden').slideDown('fast');
  92. }
  93. }
  94. });
  95. return false;
  96. });
  97. // Hijack activity comment form submits
  98. $('form.Activity :submit').live('click', function() {
  99. var but = this;
  100. var frm = $(this).parents('form');
  101. var inp = $(frm).find('textarea');
  102. // Only submit the form if the textarea isn't empty
  103. if ($(inp).val() != '') {
  104. $('span.Progress').remove();
  105. $(but).after('<span class="Progress">&#160;</span>');
  106. var postValues = $(frm).serialize();
  107. postValues += '&DeliveryType=VIEW&DeliveryMethod=JSON';
  108. $.ajax({
  109. type: "POST",
  110. url: $(frm).attr('action'),
  111. data: postValues,
  112. dataType: 'json',
  113. error: function(XMLHttpRequest, textStatus, errorThrown) {
  114. $.popup({}, XMLHttpRequest.responseText);
  115. },
  116. success: function(json) {
  117. json = $.postParseJson(json);
  118. $('span.Progress').remove();
  119. if (json['FormSaved'] == true) {
  120. $(inp).val('');
  121. // If there were no activities
  122. if ($('ul.Activities').length == 0) {
  123. // Make sure that empty rows are removed
  124. $('div.EmptyInfo').slideUp('fast');
  125. // And add the activity list
  126. $(frm).after('<ul class="Activities"></ul>');
  127. }
  128. $('ul.Activities').prepend(json.Data);
  129. // Make sure that hidden items appear
  130. $('ul.Activities li.Hidden').slideDown('fast');
  131. // If the user's status was updated, show it.
  132. if (typeof(json['UserData']) != 'undefined') {
  133. $('div.User').remove();
  134. $('div.Profile').prepend(json['UserData']);
  135. }
  136. }
  137. }
  138. });
  139. }
  140. return false;
  141. });
  142. // Set up paging
  143. if ($.morepager)
  144. $('.MorePager').morepager({
  145. pageContainerSelector: 'ul.DataList:first'
  146. });
  147. });