diaspora/public/javascripts/stream.js
2010-12-18 20:31:02 -08:00

147 lines
4.5 KiB
JavaScript

/* Copyright (c) 2010, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file.
*/
var Stream = {
initialize: function() {
var $stream = $(".stream");
var $publisher = $("#publisher");
$stream.not(".show").delegate("a.show_post_comments", "click", Stream.toggleComments);
// publisher textarea reset
$publisher.find("textarea").bind("blur", function(){
$(this).css('height','42px');
})
// comment submit action
$stream.delegate("textarea.comment_box", "keydown", function(e){
if (e.keyCode === 13) {
if(!e.shiftKey) {
$(this).closest("form").submit();
}
}
});
$stream.delegate("textarea.comment_box", "focus", function(evt) {
var commentBox = $(this);
commentBox
.closest("li").find(".submit_instructions").removeClass('hidden');
});
$stream.delegate("textarea.comment_box", "blur", function(evt) {
var commentBox = $(this);
if (!commentBox.val()) {
commentBox
.attr('rows',2)
.css('height','2.4em')
.closest("li").find(".submit_instructions").addClass('hidden');
}
});
// reshare button action
$stream.delegate(".reshare_button", "click", function(evt) {
evt.preventDefault();
button = $(this)
box = button.siblings(".reshare_box");
if (box.length > 0) {
button.toggleClass("active");
box.toggle();
}
});
$stream.delegate("a.video-link", "click", function(evt) {
evt.preventDefault();
var $this = $(this),
container = document.createElement("div"),
$container = $(container).attr("class", "video-container"),
$videoContainer = $this.parent().siblings("div.video-container");
if ($videoContainer.length > 0) {
$videoContainer.slideUp('fast', function () {
$videoContainer.detach();
});
return;
}
if ($("div.video-container").length > 0) {
$("div.video-container").slideUp("fast", function() {
$(this).detach();
});
}
if ($this.data("host") === "youtube.com") {
$container.html(
'<a href="//www.youtube.com/watch?v=' + $this.data("video-id") + '" target="_blank">Watch this video on Youtube</a><br />' +
'<iframe class="youtube-player" type="text/html" src="http://www.youtube.com/embed/' + $this.data("video-id") + '"></iframe>'
);
} else {
$container.html('Invalid videotype <i>' + $this.data("host") + '</i> (ID: ' + $this.data("video-id") + ')');
}
$container.hide()
.insertAfter($this.parent())
.slideDown('fast');
$this.click(function() {
$container.slideUp('fast', function() {
$(this).detach();
});
});
});
$(".new_status_message").bind('ajax:success', function(data, json, xhr) {
json = $.parseJSON(json);
WebSocketReceiver.addPostToStream(json['post_id'], json['html']);
//collapse publisher
$("#publisher").addClass("closed");
$("#photodropzone").find('li').remove();
});
$(".new_status_message").bind('ajax:failure', function(data, html, xhr) {
alert('failed to post message!');
});
$(".new_comment").live('ajax:success', function(data, json, xhr) {
json = $.parseJSON(json);
WebSocketReceiver.processComment(json['post_id'], json['comment_id'], json['html'], false);
});
$(".new_comment").live('ajax:failure', function(data, html, xhr) {
alert('failed to post message!');
});
$(".stream").find(".delete").live('ajax:success', function(data, html, xhr) {
$(this).parents(".message").fadeOut(150);
});
},
toggleComments: function(evt) {
evt.preventDefault();
var $this = $(this),
text = $this.html(),
commentBlock = $this.closest("li").find("ul.comments", ".content"),
commentBlockMore = $this.closest("li").find(".older_comments", ".content"),
show = (text.indexOf("show") != -1);
if( commentBlockMore.hasClass("inactive") ) {
commentBlockMore.fadeIn(150, function(){
commentBlockMore.removeClass("inactive");
commentBlockMore.removeClass("hidden");
});
} else {
if(commentBlock.hasClass("hidden")) {
commentBlock.fadeIn(150);
}else{
commentBlock.hide();
}
commentBlock.toggleClass("hidden");
}
$this.html(text.replace((show) ? "show" : "hide", (show) ? "hide" : "show"));
}
};
$(document).ready(Stream.initialize);