Guest User

Untitled

a guest
Jan 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. jQuery(function($) {
  2.  
  3. var page_id = $("#posts-container").data("page-id");
  4.  
  5. $('.facebook-message').live('mouseover',function() { $(this).find(".message-tool-box-extras").show(); } );
  6. $('.facebook-message').live('mouseout',function() { $(this).find(".message-tool-box-extras").hide(); } );
  7.  
  8. $('#search-container').find(':submit').click(function() {
  9. $("#posts-results").empty();
  10. $.read('/posts/search',
  11. { search: { q: $('#search_q').val() , page_id: page_id } },
  12. post_result);
  13. });
  14.  
  15. function post_result(response) {
  16. $('.feed-more img').hide();
  17. _.each(response, function(post) {
  18. //Jquery templates do not render the new line characters
  19. //so we replace them with br tags
  20. if(post.post.message != null) {
  21. post.post.message = post.post.message.replace(/\n/,'</br>');
  22. }
  23. //Let's get all the available actions for this post
  24. if(post.post.properties.actions != null && post.post.properties.actions.length > 0) {
  25. _.each(post.post.properties.actions, function(item) {
  26. post.post.properties[item.name.toLowerCase() + '_link'] = encodeURIComponent(item.link);
  27. });
  28. }
  29. //Facebook does not tell us when a link is a event, so we have to parse it
  30. //to figure it out
  31. var event_regex = /event.php/;
  32. if(post.post.properties.link != null && post.post.properties.link.match(event_regex)) {
  33. post.post.properties['is_event'] = true;
  34. }
  35. });
  36.  
  37. var tmpl = $("#post_template").tmpl(response).appendTo("#posts-results");
  38. $(tmpl).find(".post-time").localTimeFromUTC('MM/dd/yyyy hh:mm a');
  39. $(tmpl).find(".post-time").each(function() {
  40. var time = $(this).html();
  41. $(this).html($.timeago(time));
  42. });
  43.  
  44. $(".user-avatar").qtip({
  45. content: "Loading...",
  46. hide: { fixed:true, delay:500, event:'mouseleave' },
  47. events: {
  48. show: function(event,api) {
  49. var fb_user_id = $(event.originalEvent.currentTarget).data('fb-user-id');
  50. $.read('/fb_users/{user_id}/user-details',
  51. { page_id: page_id, user_id: fb_user_id },
  52. function(response) {
  53. var user_actions_view = $("#user_actions_template").tmpl(response);
  54. api.set('content.text',user_actions_view);
  55. var user_actions_view = $(api.elements.content);
  56. user_actions_view.find('.submit-keyword').bind('click', function(e) {
  57. console.log('submitting key:' + user_actions_view.find("select").val());
  58. $.create('/keywords',
  59. { keyword: {
  60. label: 'todo',
  61. monitorable_id: page_id,
  62. monitorable_type: 'Page',
  63. data: fb_user_id,
  64. entity_type: 'person',
  65. action: [user_actions_view.find("select").val()]
  66. }
  67. },
  68. function(response) {
  69. alert('tag added');
  70. });
  71. });
  72. user_actions_view.find('.tag-user').bind('click',function(event) {
  73. user_actions_view.find(":hidden").show();
  74. });
  75. });
  76. }
  77. }
  78. });
  79. };
  80.  
  81. $.read('/pages/{page_id}/posts', { page_id: page_id }, post_result);
  82.  
  83. $('.commentCount').live('click', function(event) {
  84.  
  85. if( $(this).parent().parent().find('.comments-container').length )
  86. {
  87. $( this ).parent().parent().find('.comments-container').remove();
  88. return false;
  89. }
  90.  
  91. var post_id = $(this).data("post-id");
  92. var is_post_deleted = $(this).data("post-deleted");
  93.  
  94. //Read the comment list and add any event listeners
  95. $.read(
  96. '/posts/{post_id}/comments',
  97. { post_id: post_id },
  98. function(response) {
  99. $("#comment_list_template").tmpl({comments: response, post_id: post_id, is_post_deleted: is_post_deleted }).appendTo("#post-" + post_id);
  100. $(".flag-post-comment").live("click", function(event)
  101. {
  102. var comment_id = $(this).data("post-comment-id");
  103. var mood = $(this).data("mood");
  104. $.update(
  105. "/posts/{post_id}/comments/{comment_id}",
  106. { post_id: post_id, comment: {mood: mood}, comment_id: comment_id },
  107. function(response) {
  108. $("#comment-" + comment_id + "-flag").children().replaceWith($("#toggle_flag_template").tmpl({data:response.comment,type:'post-comment'}));
  109. }
  110. );
  111. return false;
  112. });
  113.  
  114. /* *Listener for creating a comment */
  115. $(".submit-comment").bind("click", function(event) {
  116. var post_id = $(this).data("post-id");
  117. var post_as = $(this).parent().parent().find(":checked").val()
  118. $.create(
  119. "/posts/{post_id}/comments",
  120. { post_id: post_id,
  121. comment: {
  122. post_id: post_id,
  123. message: $(this).parent().parent().find("textarea").val(),
  124. },
  125. post_as: post_as
  126. },
  127. function(response) {
  128. $("#comment_template").tmpl({ comment: response.comment, is_post_deleted: false, post_id: post_id }).appendTo("#post-" + post_id + "-comments-list");
  129. $("#comment-entry-" + post_id).val("");
  130. var comment_link = $("#post-" + post_id).find(".commentCount");
  131. var comments_count = parseInt(comment_link.data('comment-count')) + 1;
  132. comment_link.attr("data-comment-count" , comments_count);
  133. $("#post-" + post_id).find(".commentCount").html(comments_count + " Comments")
  134. });
  135. });
  136.  
  137. $(".delete-post-comment").live("click", function(event) {
  138. if( confirm('Are you sure?') )
  139. {
  140. var comment_id = $(this).data("comment-id");
  141. $.destroy(
  142. "/comments/{comment_id}",
  143. { comment_id: comment_id },
  144. function(response) {
  145. $("#comment-" + comment_id).replaceWith($("#comment_template").tmpl(response));
  146. },function(error) { alert("error"); });
  147. }
  148. return false;
  149. });
  150. }
  151. );
  152. return false;
  153. });
  154.  
  155. //Delete a post listener
  156. $(".delete-post").live('click', function(event) {
  157. if ( confirm('Are you sure?') ) {
  158. var post_id = $(this).data('postid');
  159. var post_url = '/posts/' + post_id;
  160. $.ajax({
  161. type: 'POST',
  162. url: post_url,
  163. dataType: 'json',
  164. data: { _method: 'delete', id: post_id },
  165. error: function(data) {
  166. alert('error');
  167. },
  168. success: function(event,xhr,options) {
  169. $("#post-" + post_id).replaceWith($("#post_template").tmpl(event));
  170. $("#post-" + post_id).find(".post-time").localTimeFromUTC('MM/dd/yyyy hh:mm a');
  171. }
  172. });
  173. }
  174. return false;
  175. });
  176.  
  177. $("#keywords-link").bind('click', function(event) {
  178. $.read("/pages/{page_id}/keywords",
  179. { page_id: page_id },
  180. function(response) {
  181. var dialog = $("#keywords_dialog_template").tmpl({ keywords: response, page: { id: page_id }}).appendTo("body");
  182. dialog.dialog({modal:true});
  183. var target = $('.keyword-link-container').find('a');
  184. dialog.dialog("option",{position: {
  185. my: 'left top',
  186. at: 'right top',
  187. of: target
  188. },resizable: false});
  189. $("#page-" + page_id + "-keywords").find("form").bind("ajax:success",function(xhr,data,status) {
  190. $("#keyword_errors").html("");
  191. $("#keyword_errors").hide();
  192. $("#keywords_template").tmpl($.parseJSON(data)).appendTo(dialog.find(".keyword-list"));
  193. dialog.find(".keywords-entry").find(":text").val("")
  194. dialog.dialog("close");
  195. });
  196. $(".ui-widget-overlay").live("click", function() { dialog.dialog("close"); } );
  197. });
  198. });
  199.  
  200. $(".flag-post").live('click', function(event) {
  201. var post_id = $(this).data('post-id');
  202. var mood = $(this).data("mood");
  203. $.update('/posts/{post_id}',
  204. { post_id: post_id, post: { mood: mood } },
  205. function(response) {
  206. console.log(response);
  207. $("#flag_" + post_id).replaceWith($("#toggle_flag_template").tmpl({data:response.post,type:'post'}));
  208. });
  209. });
  210.  
  211. $('.feed-more a').click(function() {
  212. $('.feed-more img').show();
  213. var page = $(this).data('current-page') + 1;
  214. $(this).data('current-page', page);
  215. $.read('/pages/{page_id}/posts', { page_id: page_id, page: page }, post_result);
  216. return false;
  217. });
  218. });
Add Comment
Please, Sign In to add comment