src/pyams_content/skin/resources/js/pyams_content.js
changeset 35 7cdbe0f6e5c2
parent 26 7f025cebf19d
child 67 aaf228e67189
--- a/src/pyams_content/skin/resources/js/pyams_content.js	Thu Jun 02 15:30:56 2016 +0200
+++ b/src/pyams_content/skin/resources/js/pyams_content.js	Thu Jun 02 15:31:37 2016 +0200
@@ -333,6 +333,82 @@
 									   });
 				}
 			}
+		},
+
+
+		/**
+		 * Review comments management
+		 */
+		review: {
+
+			timer: null,
+			timer_duration: {
+				general: 30000,
+				chat: 5000
+			},
+
+			initComments: function(element) {
+				var chat = $('.chat-body', element);
+				chat.animate({scrollTop: chat[0].scrollHeight}, 1000);
+				clearInterval(PyAMS_content.review.timer);
+				PyAMS_content.review.timer = setInterval(PyAMS_content.review.updateComments,
+														 PyAMS_content.review.timer_duration.chat);
+				MyAMS.skin.registerCleanCallback(PyAMS_content.review.cleanCommentsCallback);
+			},
+
+			cleanCommentsCallback: function() {
+				clearInterval(PyAMS_content.review.timer);
+				PyAMS_content.review.timer = setInterval(PyAMS_content.review.updateComments,
+														 PyAMS_content.review.timer_duration.general);
+			},
+
+			updateComments: function() {
+				var badge = $('.badge', 'nav a[href="#review-comments.html"]'),
+					count;
+				var chat = $('.chat-body', '.widget-body');
+				if (chat.exists()) {
+					count = $('.message', chat).length;
+				} else {
+					count = parseInt(badge.text());
+				}
+				MyAMS.ajax.post('get-last-review-comments.json', {count: count}, function(result) {
+					if (chat.exists()) {
+						badge.removeClass('bg-color-danger')
+							 .addClass('bg-color-info');
+					}
+					if (count !== result.count) {
+						badge.text(result.count).removeClass('hidden');
+						if (chat.exists()) {
+							$('.messages', chat).append(result.content);
+							chat.animate({scrollTop: chat[0].scrollHeight}, 1000);
+						}
+						if (!chat.exists()) {
+							badge.removeClass('bg-color-info')
+								 .addClass('bg-color-danger')
+								 .animate({'padding': '3px 12px 2px',
+										   'margin-right': '9px'}, 'slow', function() {
+									$(this).animate({'padding': '3px 6px 2px',
+													 'margin-right': '15px'}, 'slow');
+								 });
+						}
+					}
+				});
+			},
+
+			initCommentData: function(veto) {
+				var chat = $('.chat-body', '.widget-body');
+				return {count: $('.message', chat).length};
+			},
+
+			addCommentCallback: function(options) {
+				var form = $(this);
+				var widget = form.parents('.widget-body');
+				$('.messages', widget).append(options.content);
+				$('textarea[name="comment"]', form).val('');
+				var chat = $('.chat-body', widget);
+				chat.animate({scrollTop: chat[0].scrollHeight}, 1000);
+				$('.badge', 'nav a[href="#review-comments.html"]').text(options.count).removeClass('hidden');
+			}
 		}
 	};
 
@@ -352,7 +428,16 @@
 			case 'galleries_container':
 				PyAMS_content.galleries.refreshContainer(settings);
 				break;
+			case 'review_comments':
+				PyAMS_content.review.updateComments();
+				break;
 		}
 	});
 
+	var badge = $('.badge', 'nav a[href="#review-comments.html"]');
+	if (badge.exists()) {
+		PyAMS_content.review.timer = setInterval(PyAMS_content.review.updateComments,
+												 PyAMS_content.review.timer_duration.general);
+	}
+
 })(jQuery, this);