diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index b189cad86d682c87c2f74af360cde0e082d26cee..287aef9a6473ccf2c27f24a7f3817119465f570f 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -9,7 +9,7 @@ app.Router = Backbone.Router.extend({ "commented(/)": "stream", "community_spotlight(/)": "spotlight", "contacts(/)": "contacts", - "conversations(/)(:id)(/)": "conversations", + "conversations(/)(:id)(?conversation_id=:conversation_id)(/)": "conversations", "followed_tags(/)": "followed_tags", "getting_started(/)": "gettingStarted", "help(/)": "help", @@ -93,8 +93,8 @@ app.Router = Backbone.Router.extend({ app.page = new app.pages.Contacts({stream: stream}); }, - conversations: function(id) { - app.conversations = app.conversations || new app.views.ConversationsInbox(); + conversations: function(id, conversationId) { + app.conversations = app.conversations || new app.views.ConversationsInbox(conversationId); if (parseInt("" + id, 10)) { app.conversations.renderConversation(id); } diff --git a/app/assets/javascripts/app/views/comment_stream_view.js b/app/assets/javascripts/app/views/comment_stream_view.js index 161d4c935a0ef704e6d0b3e1eadd81ff5057aab3..d0652e5da06ccc525e97d4ed6c5a4b657c9f429b 100644 --- a/app/assets/javascripts/app/views/comment_stream_view.js +++ b/app/assets/javascripts/app/views/comment_stream_view.js @@ -31,9 +31,7 @@ app.views.CommentStream = app.views.Base.extend({ new app.views.CommentMention({el: this.$el, postId: this.model.get("id")}); this.mdEditor = new Diaspora.MarkdownEditor(this.$(".comment_box"), { - onPreview: function($mdInstance) { - return "<div class='preview-content'>" + app.helpers.textFormatter($mdInstance.getContent()) + "</div>"; - }, + onPreview: Diaspora.MarkdownEditor.simplePreview, onFocus: this.openForm.bind(this) }); }, @@ -156,8 +154,15 @@ app.views.CommentStream = app.views.Base.extend({ autosize.update(this.commentBox); }, + isCloseAllowed: function() { + if (this.mdEditor === undefined) { + return true; + } + return !this.mdEditor.isPreviewMode() && this.mdEditor.userInputEmpty(); + }, + onFormBlur: function(evt) { - if (this.mdEditor !== undefined && this.mdEditor.isPreviewOrTexareaNotEmpty()) { + if (!this.isCloseAllowed()) { return; } diff --git a/app/assets/javascripts/app/views/conversations_form_view.js b/app/assets/javascripts/app/views/conversations_form_view.js index 54cb86435f90f8a0160c953010ec73012e649f1d..df156b0a74da018614487a8faf9537b6042fa38c 100644 --- a/app/assets/javascripts/app/views/conversations_form_view.js +++ b/app/assets/javascripts/app/views/conversations_form_view.js @@ -26,12 +26,6 @@ app.views.ConversationsForm = app.views.Base.extend({ this.newConversationMdEditor = this.renderMarkdownEditor("#new-message-text"); - // Creates another markdown editor in case of displaying conversation - var responseTextarea = $("#conversation-show .conversation-message-text"); - if (responseTextarea.length === 1) { - this.renderMarkdownEditor(responseTextarea); - } - this.bindTypeaheadEvents(); this.tagListElement.empty(); @@ -45,9 +39,7 @@ app.views.ConversationsForm = app.views.Base.extend({ renderMarkdownEditor: function(element) { return new Diaspora.MarkdownEditor($(element), { - onPreview: function($mdInstance) { - return "<div class='preview-content'>" + app.helpers.textFormatter($mdInstance.getContent()) + "</div>"; - } + onPreview: Diaspora.MarkdownEditor.simplePreview }); }, diff --git a/app/assets/javascripts/app/views/conversations_inbox_view.js b/app/assets/javascripts/app/views/conversations_inbox_view.js index 32fe2d2a713c1ff2ed99abea33748812902e6078..9dc5563439eec2f27dfa02f846eb0bb9f89512ad 100644 --- a/app/assets/javascripts/app/views/conversations_inbox_view.js +++ b/app/assets/javascripts/app/views/conversations_inbox_view.js @@ -8,11 +8,21 @@ app.views.ConversationsInbox = app.views.Base.extend({ "click .new-conversation-btn": "displayNewConversation" }, - initialize: function() { + initialize: function(conversationId) { this.conversationForm = new app.views.ConversationsForm(); + + // Creates markdown editor in case of displaying preloaded conversation + if (conversationId != null) { + this.renderMarkdownEditor(); + } + this.setupConversation(); }, + renderMarkdownEditor: function() { + this.conversationForm.renderMarkdownEditor("#conversation-show .conversation-message-text"); + }, + renderConversation: function(conversationId) { var self = this; $.ajax({ @@ -23,7 +33,7 @@ app.views.ConversationsInbox = app.views.Base.extend({ self.$el.find("#conversation-show").removeClass("hidden").html(data); self.selectConversation(conversationId); self.setupConversation(); - self.conversationForm.renderMarkdownEditor("#conversation-show .conversation-message-text"); + self.renderMarkdownEditor(); autosize(self.$("#conversation-show textarea")); } }); diff --git a/app/assets/javascripts/helpers/markdown_editor.js b/app/assets/javascripts/helpers/markdown_editor.js index 5de028fecbba1294ddfacaec24f3ca08e810e44a..6286434375f4fafc0a9887539594d4c1ff31a9f9 100644 --- a/app/assets/javascripts/helpers/markdown_editor.js +++ b/app/assets/javascripts/helpers/markdown_editor.js @@ -130,11 +130,12 @@ Diaspora.MarkdownEditor.prototype = { } }, - isPreviewOrTexareaNotEmpty: function() { - if (this.instance === undefined) { - return false; - } - return (this.instance.$editor.find(".md-preview").length > 0) || (this.instance.getContent().length > 0); + isPreviewMode: function() { + return this.instance !== undefined && this.instance.$editor.find(".md-preview").length > 0; + }, + + userInputEmpty: function() { + return this.instance === undefined || this.instance.getContent().length === 0; }, localize: function() { @@ -167,3 +168,7 @@ Diaspora.MarkdownEditor.prototype = { return locale; } }; + +Diaspora.MarkdownEditor.simplePreview = function($mdInstance) { + return "<div class='preview-content'>" + app.helpers.textFormatter($mdInstance.getContent()) + "</div>"; +}; diff --git a/spec/javascripts/app/router_spec.js b/spec/javascripts/app/router_spec.js index c6ac8653df96cea04af838c6bb58ca74a084050b..14e6dd2df9be68532dd0369190afe35e6811b49e 100644 --- a/spec/javascripts/app/router_spec.js +++ b/spec/javascripts/app/router_spec.js @@ -112,6 +112,13 @@ describe('app.Router', function () { this.router.conversations("12"); expect(app.views.ConversationsInbox.prototype.renderConversation).toHaveBeenCalledWith("12"); }); + + it("passes conversation_id parameter to ConversationsInbox initializer when passed in URL", function() { + app.conversations = undefined; + spyOn(app.views.ConversationsInbox.prototype, "initialize"); + this.router.navigate("/conversations?conversation_id=45", {trigger: true}); + expect(app.views.ConversationsInbox.prototype.initialize).toHaveBeenCalledWith("45"); + }); }); describe("stream", function() { diff --git a/spec/javascripts/app/views/comment_stream_view_spec.js b/spec/javascripts/app/views/comment_stream_view_spec.js index 4bd1835522c3990918d97b04db4a1757d5ae919a..38b3a95875acf1981b01ad5f080abb2a45b15ad6 100644 --- a/spec/javascripts/app/views/comment_stream_view_spec.js +++ b/spec/javascripts/app/views/comment_stream_view_spec.js @@ -368,16 +368,26 @@ describe("app.views.CommentStream", function(){ spec.content().html("<div class='new-comment'/><div class='focus_comment_textarea'/>"); }); - it("does not call closeForm if markdown editor contains text or is in preview mode", function() { + it("does not call closeForm if markdown editor is in preview mode", function() { spyOn(this.view, "closeForm"); - spyOn(this.view.mdEditor, "isPreviewOrTexareaNotEmpty").and.returnValue(true); + spyOn(this.view.mdEditor, "isPreviewMode").and.returnValue(true); + spyOn(this.view.mdEditor, "userInputEmpty").and.returnValue(true); + this.view.onFormBlur(); + expect(this.view.closeForm).not.toHaveBeenCalled(); + }); + + it("does not call closeForm if markdown editor contains text", function() { + spyOn(this.view, "closeForm"); + spyOn(this.view.mdEditor, "isPreviewMode").and.returnValue(false); + spyOn(this.view.mdEditor, "userInputEmpty").and.returnValue(false); this.view.onFormBlur(); expect(this.view.closeForm).not.toHaveBeenCalled(); }); it("does not call closeForm when the form is clicked", function() { spyOn(this.view, "closeForm"); - spyOn(this.view.mdEditor, "isPreviewOrTexareaNotEmpty").and.returnValue(false); + spyOn(this.view.mdEditor, "isPreviewMode").and.returnValue(false); + spyOn(this.view.mdEditor, "userInputEmpty").and.returnValue(true); this.view.onFormBlur($.Event("click", {target: $(".new-comment")})); expect(this.view.closeForm).not.toHaveBeenCalled(); this.view.onFormBlur($.Event("click", {target: $(".focus_comment_textarea")})); @@ -386,7 +396,8 @@ describe("app.views.CommentStream", function(){ it("calls closeForm when the user clicks outside of the form", function() { spyOn(this.view, "closeForm"); - spyOn(this.view.mdEditor, "isPreviewOrTexareaNotEmpty").and.returnValue(false); + spyOn(this.view.mdEditor, "isPreviewMode").and.returnValue(false); + spyOn(this.view.mdEditor, "userInputEmpty").and.returnValue(true); this.view.onFormBlur($.Event("click", {target: $("body")})); expect(this.view.closeForm).toHaveBeenCalled(); }); diff --git a/spec/javascripts/app/views/conversations_form_view_spec.js b/spec/javascripts/app/views/conversations_form_view_spec.js index 50252d4cad9f48ad9a2bbe59838d37bfb242c914..959955e6d002b705963c20e5ec496922faefa6b4 100644 --- a/spec/javascripts/app/views/conversations_form_view_spec.js +++ b/spec/javascripts/app/views/conversations_form_view_spec.js @@ -41,13 +41,6 @@ describe("app.views.ConversationsForm", function() { this.target.initialize(); expect(this.target.renderMarkdownEditor).toHaveBeenCalledWith("#new-message-text"); }); - - it("creates markdown editor for an existing conversation", function() { - spyOn(this.target, "renderMarkdownEditor"); - this.target.initialize(); - expect(this.target.renderMarkdownEditor).toHaveBeenCalledWith( - $("#conversation-show .conversation-message-text")); - }); }); describe("renderMarkdownEditor", function() { diff --git a/spec/javascripts/app/views/conversations_inbox_view_spec.js b/spec/javascripts/app/views/conversations_inbox_view_spec.js index 756b690dbe1f209608b9abb233fd82f53c8ecd0b..f1c539a24374735e0e341a555a79f2ae162a2e54 100644 --- a/spec/javascripts/app/views/conversations_inbox_view_spec.js +++ b/spec/javascripts/app/views/conversations_inbox_view_spec.js @@ -17,6 +17,14 @@ describe("app.views.ConversationsInbox", function() { new app.views.ConversationsInbox(); expect(app.views.ConversationsInbox.prototype.setupConversation).toHaveBeenCalled(); }); + + it("creates markdown editor for an existing conversation", function() { + spyOn(app.views.ConversationsForm.prototype, "renderMarkdownEditor"); + new app.views.ConversationsInbox(1); + expect(app.views.ConversationsForm.prototype.renderMarkdownEditor).toHaveBeenCalledWith( + "#conversation-show .conversation-message-text" + ); + }); }); describe("renderConversation", function() { diff --git a/spec/javascripts/helpers/markdown_editor_spec.js b/spec/javascripts/helpers/markdown_editor_spec.js index d834571b3bf42d4ee8e6fbbb0836b61449f3a52e..73c0047badd9c9b535489ccf437e5edab5732490 100644 --- a/spec/javascripts/helpers/markdown_editor_spec.js +++ b/spec/javascripts/helpers/markdown_editor_spec.js @@ -220,24 +220,45 @@ describe("Diaspora.MarkdownEditor", function() { }); }); - describe("isPreviewOrTexareaNotEmpty", function() { + describe("isPreviewMode", function() { beforeEach(function() { this.target = new Diaspora.MarkdownEditor(this.$el, {onPreview: $.noop, onPostPreview: $.noop()}); }); it("return false if editor is not visible yet", function() { this.target.instance = undefined; - expect(this.target.isPreviewOrTexareaNotEmpty()).toBe(false); + expect(this.target.isPreviewMode()).toBe(false); + }); + + it("returns false if the editor is in write (default) mode", function() { + expect(this.target.instance).toBeDefined(); + expect(this.target.isPreviewMode()).toBe(false); }); it("returns true if editor is in preview mode", function() { this.target.showPreview(); - expect(this.target.isPreviewOrTexareaNotEmpty()).toBe(true); + expect(this.target.isPreviewMode()).toBe(true); + }); + }); + + describe("userInputEmpty", function() { + beforeEach(function() { + this.target = new Diaspora.MarkdownEditor(this.$el, {onPreview: $.noop, onPostPreview: $.noop()}); + }); + + it("return true if editor is not visible yet", function() { + this.target.instance = undefined; + expect(this.target.userInputEmpty()).toBe(true); }); - it("returns true if editor has content", function() { + it("returns true if editor has no content", function() { + $("textarea").text(""); + expect(this.target.userInputEmpty()).toBe(true); + }); + + it("returns false if editor has content", function() { $("textarea").text("Yolo"); - expect(this.target.isPreviewOrTexareaNotEmpty()).toBe(true); + expect(this.target.userInputEmpty()).toBe(false); }); }); @@ -255,4 +276,18 @@ describe("Diaspora.MarkdownEditor", function() { expect($.fn.markdown.messages[Diaspora.I18n.language]).toBeDefined(); }); }); + + describe("simplePreview", function() { + beforeEach(function() { + this.target = new Diaspora.MarkdownEditor(this.$el, {}); + }); + + it("generates HTML for preview", function() { + spyOn(app.helpers, "textFormatter").and.callThrough(); + this.$el[0].value = "<p>hello</p>"; + var res = Diaspora.MarkdownEditor.simplePreview(this.target.instance); + expect(app.helpers.textFormatter).toHaveBeenCalledWith("<p>hello</p>"); + expect(res).toBe("<div class='preview-content'><p>hello</p></div>"); + }); + }); });