Merge pull request #6363 from svbergerem/mobile-view-multiple-reaction-boxes

Mobile view multiple reaction boxes
This commit is contained in:
Jonne Haß 2015-08-29 17:02:10 +02:00
commit 05e87b3170
6 changed files with 295 additions and 145 deletions

View file

@ -57,6 +57,7 @@ With the port to Bootstrap 3, app/views/terms/default.haml has a new structure.
* Improve accessibility of a couple pages [#6227](https://github.com/diaspora/diaspora/pull/6227) * Improve accessibility of a couple pages [#6227](https://github.com/diaspora/diaspora/pull/6227)
* Capitalize "Powered by diaspora" [#6254](https://github.com/diaspora/diaspora/pull/6254) * Capitalize "Powered by diaspora" [#6254](https://github.com/diaspora/diaspora/pull/6254)
* Display username and avatar for NSFW posts in mobile view [#6245](https://github.com/diaspora/diaspora/6245) * Display username and avatar for NSFW posts in mobile view [#6245](https://github.com/diaspora/diaspora/6245)
* Prevent multiple comment boxes on mobile [#6363](https://github.com/diaspora/diaspora/pull/6363)
## Features ## Features
* Support color themes [#6033](https://github.com/diaspora/diaspora/pull/6033) * Support color themes [#6033](https://github.com/diaspora/diaspora/pull/6033)

View file

@ -77,9 +77,12 @@ Diaspora.I18n = {
reset: function() { reset: function() {
this.locale.data = {}; this.locale.data = {};
this.locale.fallback.data = {};
if( arguments.length > 0 && !(_.isEmpty(arguments[0])) ) if(arguments.length > 0 && !(_.isEmpty(arguments[0]))) {
this.locale.data = arguments[0]; this.locale.data = arguments[0];
this.locale.fallback.data = arguments[0];
}
} }
}; };
// @license-end // @license-end

View file

@ -4,162 +4,180 @@
* the COPYRIGHT file. * the COPYRIGHT file.
*/ */
$(document).ready(function() { (function() {
Diaspora.Mobile = {};
Diaspora.Mobile.Comments = {
initialize: function() {
var self = this;
$(".stream").on("tap click", "a.back_to_stream_element_top", function() {
var bottomBar = $(this).closest(".bottom_bar").first();
var streamElement = bottomBar.parent();
$("html, body").animate({
scrollTop: streamElement.offset().top - 54
}, 1000);
});
$(".stream").on("tap click", "a.back_to_stream_element_top", function() { $(".stream").on("tap click", "a.show_comments", function(evt){
var bottomBar = $(this).closest(".bottom_bar").first(); evt.preventDefault();
var streamElement = bottomBar.parent(); self.toggleComments($(this));
$("html, body").animate({ });
scrollTop: streamElement.offset().top - 54
}, 1000);
});
$(".stream").on("tap click", "a.show_comments", function(evt){ $(".stream").on("tap click", "a.comment-action", function(evt) {
evt.preventDefault(); evt.preventDefault();
toggleComments($(this)); self.showCommentBox($(this));
}); var bottomBar = $(this).closest(".bottom_bar").first();
var commentContainer = bottomBar.find(".comment_container").first();
self.scrollToOffset(commentContainer);
});
function toggleComments(toggleReactionsLink) { $(".stream").on("submit", ".new_comment", function(evt) {
if (toggleReactionsLink.hasClass("active")) { evt.preventDefault();
hideComments(toggleReactionsLink); var form = $(this);
} else { $.post(form.attr("action")+"?format=mobile", form.serialize(), function(data) {
showComments(toggleReactionsLink); self.updateStream(form, data);
} }, "html");
} });
},
function hideComments(toggleReactionsLink) { toggleComments: function(toggleReactionsLink) {
var bottomBar = toggleReactionsLink.closest(".bottom_bar").first(), if(toggleReactionsLink.hasClass("loading")) { return; }
commentsContainer = commentsContainerLazy(bottomBar), if (toggleReactionsLink.hasClass("active")) {
existingCommentsContainer = commentsContainer(); this.hideComments(toggleReactionsLink);
existingCommentsContainer.hide(); } else {
toggleReactionsLink.removeClass("active"); this.showComments(toggleReactionsLink);
}
function showComments(toggleReactionsLink) {
var bottomBar = toggleReactionsLink.closest(".bottom_bar").first(),
commentsContainer = commentsContainerLazy(bottomBar),
existingCommentsContainer = commentsContainer(),
commentActionLink = bottomBar.find("a.comment-action");
if (existingCommentsContainer.length > 0) {
showLoadedComments(toggleReactionsLink, existingCommentsContainer, commentActionLink);
} else {
showUnloadedComments(toggleReactionsLink, bottomBar, commentActionLink);
}
}
function showLoadedComments(toggleReactionsLink, existingCommentsContainer, commentActionLink) {
existingCommentsContainer.show();
showCommentBox(commentActionLink);
toggleReactionsLink.addClass("active");
existingCommentsContainer.find("time.timeago").timeago();
}
function showUnloadedComments(toggleReactionsLink, bottomBar, commentActionLink) {
var commentsContainer = commentsContainerLazy(bottomBar);
$.ajax({
url: toggleReactionsLink.attr("href"),
success: function (data) {
$(data).insertAfter(bottomBar.children(".show_comments").first());
showCommentBox(commentActionLink);
toggleReactionsLink.addClass("active");
commentsContainer().find("time.timeago").timeago();
} }
}); },
}
function commentsContainerLazy(bottomBar) { hideComments: function(toggleReactionsLink) {
return function() { var bottomBar = toggleReactionsLink.closest(".bottom_bar").first(),
return bottomBar.find(".comment_container").first(); commentsContainer = this.commentsContainerLazy(bottomBar),
}; existingCommentsContainer = commentsContainer();
} existingCommentsContainer.hide();
toggleReactionsLink.removeClass("active");
},
$(".stream").on("tap click", "a.comment-action", function(evt) { showComments: function(toggleReactionsLink) {
evt.preventDefault(); var bottomBar = toggleReactionsLink.closest(".bottom_bar").first(),
showCommentBox(this); commentsContainer = this.commentsContainerLazy(bottomBar),
var bottomBar = $(this).closest(".bottom_bar").first(); existingCommentsContainer = commentsContainer(),
var commentContainer = bottomBar.find(".comment_container").first(); commentActionLink = bottomBar.find("a.comment-action");
scrollToOffset(commentContainer); if (existingCommentsContainer.length > 0) {
}); this.showLoadedComments(toggleReactionsLink, existingCommentsContainer, commentActionLink);
var scrollToOffset = function(commentsContainer){ } else {
var commentCount = commentsContainer.find("li.comment").length; this.showUnloadedComments(toggleReactionsLink, bottomBar, commentActionLink);
if ( commentCount > 3 ) { }
var lastComment = commentsContainer.find("li:nth-child("+(commentCount-3)+")"); },
$("html,body").animate({
scrollTop: lastComment.offset().top
}, 1000);
}
};
function showCommentBox(link){ showLoadedComments: function(toggleReactionsLink, existingCommentsContainer, commentActionLink) {
var commentActionLink = $(link); toggleReactionsLink.addClass("active");
if(commentActionLink.hasClass("inactive")) { existingCommentsContainer.show();
this.showCommentBox(commentActionLink);
existingCommentsContainer.find("time.timeago").timeago();
},
showUnloadedComments: function(toggleReactionsLink, bottomBar, commentActionLink) {
toggleReactionsLink.addClass("loading");
var commentsContainer = this.commentsContainerLazy(bottomBar);
var self = this;
$.ajax({ $.ajax({
url: commentActionLink.attr("href"), url: toggleReactionsLink.attr("href"),
beforeSend: function(){ success: function (data) {
commentActionLink.addClass("loading"); toggleReactionsLink.addClass("active").removeClass("loading");
$(data).insertAfter(bottomBar.children(".show_comments").first());
self.showCommentBox(commentActionLink);
commentsContainer().find("time.timeago").timeago();
}, },
context: commentActionLink, error: function() {
success: function(data){ toggleReactionsLink.removeClass("loading");
appendCommentBox.call(this, commentActionLink, data);
} }
}); });
},
commentsContainerLazy: function(bottomBar) {
return function() {
return bottomBar.find(".comment_container").first();
};
},
scrollToOffset: function(commentsContainer){
var commentCount = commentsContainer.find("li.comment").length;
if ( commentCount > 3 ) {
var lastComment = commentsContainer.find("li:nth-child("+(commentCount-3)+")");
$("html,body").animate({
scrollTop: lastComment.offset().top
}, 1000);
}
},
showCommentBox: function(link){
if(!link.hasClass("inactive") || link.hasClass("loading")) { return; }
var self = this;
$.ajax({
url: link.attr("href"),
beforeSend: function(){
link.addClass("loading");
},
context: link,
success: function(data) {
self.appendCommentBox.call(this, link, data);
},
error: function() {
link.removeClass("loading");
}
});
},
appendCommentBox: function(link, data) {
link.removeClass("loading");
link.removeClass("inactive");
var bottomBar = link.closest(".bottom_bar").first();
bottomBar.append(data);
var textArea = bottomBar.find("textarea.comment_box").first()[0];
autosize(textArea);
},
updateStream: function(form, data) {
var bottomBar = form.closest(".bottom_bar").first();
this.addNewComments(bottomBar, data);
this.updateCommentCount(bottomBar);
this.updateReactionCount(bottomBar);
this.handleCommentShowing(form, bottomBar);
bottomBar.find("time.timeago").timeago();
},
addNewComments: function(bottomBar, data) {
var commentsContainer = bottomBar.find(".comment_container").first();
var comments = commentsContainer.find(".comments").first();
comments.append(data);
},
// Fix for no comments
updateCommentCount: function(bottomBar) {
var commentCount = bottomBar.find(".comment_count");
commentCount.text(commentCount.text().replace(/(\d+)/, function (match) {
return parseInt(match) + 1;
}));
},
// Fix for no reactions
updateReactionCount: function(bottomBar) {
var toggleReactionsLink = bottomBar.find(".show_comments").first();
toggleReactionsLink.text(toggleReactionsLink.text().replace(/(\d+)/, function (match) {
return parseInt(match) + 1;
}));
},
handleCommentShowing: function(form, bottomBar) {
var formContainer = form.parent();
formContainer.remove();
var commentActionLink = bottomBar.find("a.comment-action").first();
commentActionLink.addClass("inactive");
var toggleReactionsLink = bottomBar.find(".show_comments").first();
this.showComments(toggleReactionsLink);
} }
} };
})();
function appendCommentBox(link, data) { $(document).ready(function() {
link.removeClass("loading"); Diaspora.Mobile.Comments.initialize();
link.removeClass("inactive");
var bottomBar = link.closest(".bottom_bar").first();
bottomBar.append(data);
var textArea = bottomBar.find("textarea.comment_box").first()[0];
autosize(textArea);
}
$(".stream").on("submit", ".new_comment", function(evt) {
evt.preventDefault();
var form = $(this);
$.post(form.attr("action")+"?format=mobile", form.serialize(), function(data) {
updateStream(form, data);
}, "html");
});
function updateStream(form, data) {
var bottomBar = form.closest(".bottom_bar").first();
addNewComments(bottomBar, data);
updateCommentCount(bottomBar);
updateReactionCount(bottomBar);
handleCommentShowing(form, bottomBar);
bottomBar.find("time.timeago").timeago();
}
function addNewComments(bottomBar, data) {
var commentsContainer = bottomBar.find(".comment_container").first();
var comments = commentsContainer.find(".comments").first();
comments.append(data);
}
// Fix for no comments
function updateCommentCount(bottomBar) {
var commentCount = bottomBar.find(".comment_count");
commentCount.text(commentCount.text().replace(/(\d+)/, function (match) {
return parseInt(match) + 1;
}));
}
// Fix for no reactions
function updateReactionCount(bottomBar) {
var toggleReactionsLink = bottomBar.find(".show_comments").first();
toggleReactionsLink.text(toggleReactionsLink.text().replace(/(\d+)/, function (match) {
return parseInt(match) + 1;
}));
}
function handleCommentShowing(form, bottomBar) {
var formContainer = form.parent();
formContainer.remove();
var commentActionLink = bottomBar.find("a.comment-action").first();
commentActionLink.addClass("inactive");
var toggleReactionsLink = bottomBar.find(".show_comments").first();
showComments(toggleReactionsLink);
}
}); });

View file

@ -58,6 +58,13 @@ describe StreamsController, :type => :controller do
save_fixture(html_for("body"), "aspects_index_post_with_comments") save_fixture(html_for("body"), "aspects_index_post_with_comments")
end end
it "generates a mobile jasmine fixture with a post with comments", fixture: true do
message = bob.post(:status_message, text: "HALO WHIRLED", to: @bob.aspects.where(name: "generic").first.id)
5.times { bob.comment!(message, "what") }
get :aspects, format: :mobile
save_fixture(html_for("body"), "aspects_index_mobile_post_with_comments")
end
it 'generates a jasmine fixture with a followed tag', :fixture => true do it 'generates a jasmine fixture with a followed tag', :fixture => true do
@tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent") @tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent")
TagFollowing.create!(:tag => @tag, :user => alice) TagFollowing.create!(:tag => @tag, :user => alice)

View file

@ -0,0 +1,119 @@
describe("Diaspora.Mobile.Comments", function(){
describe("toggleComments", function() {
beforeEach(function() {
spec.loadFixture("aspects_index_mobile_post_with_comments");
this.link = $(".stream .show_comments").first();
spyOn(Diaspora.Mobile.Comments, "showComments");
spyOn(Diaspora.Mobile.Comments, "hideComments");
});
it("calls showComments", function() {
Diaspora.Mobile.Comments.toggleComments(this.link);
expect(Diaspora.Mobile.Comments.showComments).toHaveBeenCalled();
expect(Diaspora.Mobile.Comments.hideComments).not.toHaveBeenCalled();
});
it("calls hideComments if the link class is 'active'", function() {
this.link.addClass("active");
Diaspora.Mobile.Comments.toggleComments(this.link);
expect(Diaspora.Mobile.Comments.showComments).not.toHaveBeenCalled();
expect(Diaspora.Mobile.Comments.hideComments).toHaveBeenCalled();
});
it("doesn't call any function if the link class is 'loading'", function() {
this.link.addClass("loading");
Diaspora.Mobile.Comments.toggleComments(this.link);
expect(Diaspora.Mobile.Comments.showComments).not.toHaveBeenCalled();
expect(Diaspora.Mobile.Comments.hideComments).not.toHaveBeenCalled();
});
});
describe("showUnloadedComments", function() {
beforeEach(function() {
spec.loadFixture("aspects_index_mobile_post_with_comments");
this.link = $(".stream .show_comments").first();
this.bottomBar = this.link.closest(".bottom_bar").first();
this.commentActionLink = this.bottomBar.find("a.comment-action");
});
it("adds the 'loading' class to the link", function() {
Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
expect($(".show_comments").first()).toHaveClass("loading");
});
it("removes the 'loading' class if the request failed", function() {
Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
jasmine.Ajax.requests.mostRecent().respondWith({status: 400});
expect($(".show_comments").first()).not.toHaveClass("loading");
});
it("adds the 'active' class if the request succeeded", function() {
Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
expect($(".show_comments").first()).toHaveClass("active");
expect($(".show_comments").first()).not.toHaveClass("loading");
});
it("calls showCommentBox", function() {
spyOn(Diaspora.Mobile.Comments, "showCommentBox");
Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
expect(Diaspora.Mobile.Comments.showCommentBox).toHaveBeenCalledWith(this.commentActionLink);
});
it("adds the response text to the comments list", function() {
Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: "text/plain",
responseText: "<div class=\"commentContainerForTest\">new comments</div>"
});
expect($(".stream .stream_element").first()).toContainElement(".commentContainerForTest");
});
});
describe("showCommentBox", function() {
beforeEach(function() {
spec.loadFixture("aspects_index_mobile_post_with_comments");
this.link = $(".stream .comment-action").first();
});
it("adds the 'loading' class to the link", function() {
Diaspora.Mobile.Comments.showCommentBox(this.link);
expect($(".comment-action").first()).toHaveClass("loading");
});
it("removes the 'loading' class if the request failed", function() {
Diaspora.Mobile.Comments.showCommentBox(this.link);
jasmine.Ajax.requests.mostRecent().respondWith({status: 400});
expect($(".comment-action").first()).not.toHaveClass("loading");
});
it("fires an AJAX call", function() {
spyOn(jQuery, "ajax");
Diaspora.Mobile.Comments.showCommentBox(this.link);
expect(jQuery.ajax).toHaveBeenCalled();
});
it("calls appendCommentBox", function() {
spyOn(Diaspora.Mobile.Comments, "appendCommentBox");
Diaspora.Mobile.Comments.showCommentBox(this.link);
jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
expect(Diaspora.Mobile.Comments.appendCommentBox).toHaveBeenCalledWith(this.link, "test");
});
it("doesn't do anything if the link class is 'loading'", function() {
spyOn(jQuery, "ajax");
this.link.addClass("loading");
Diaspora.Mobile.Comments.showCommentBox(this.link);
expect(jQuery.ajax).not.toHaveBeenCalled();
});
it("doesn't do anything if the link class is not 'inactive'", function() {
spyOn(jQuery, "ajax");
this.link.removeClass("inactive");
Diaspora.Mobile.Comments.showCommentBox(this.link);
expect(jQuery.ajax).not.toHaveBeenCalled();
});
});
});

View file

@ -90,6 +90,7 @@ describe("Diaspora.I18n", function() {
Diaspora.I18n.load(locale, "en", locale); Diaspora.I18n.load(locale, "en", locale);
Diaspora.I18n.reset(); Diaspora.I18n.reset();
expect(Diaspora.I18n.locale.data).toEqual({}); expect(Diaspora.I18n.locale.data).toEqual({});
expect(Diaspora.I18n.locale.fallback.data).toEqual({});
}); });
it("sets the locale to only a specific value", function() { it("sets the locale to only a specific value", function() {
@ -97,6 +98,7 @@ describe("Diaspora.I18n", function() {
Diaspora.I18n.load(locale, "en", locale); Diaspora.I18n.load(locale, "en", locale);
Diaspora.I18n.reset(data); Diaspora.I18n.reset(data);
expect(Diaspora.I18n.locale.data).toEqual(data); expect(Diaspora.I18n.locale.data).toEqual(data);
expect(Diaspora.I18n.locale.fallback.data).toEqual(data);
}); });
}); });
}); });