diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index e9840a2f2fbfaaa1c75db9854f7d25141dcd48d1..aaee57e030a22c141752f471f80b5ebd1a338186 100644 --- a/app/views/comments/create.js.erb +++ b/app/views/comments/create.js.erb @@ -1,3 +1,7 @@ +var post = App.stream.collection.get(<%= @comment.post.id %>); + +console.log(post); + ContentUpdater.addCommentToPost("<%= @comment.post.guid %>", "<%= @comment.guid%>", "<%= escape_javascript(render(:partial => 'comments/comment', :locals => { :comment => @comment, :person => current_user.person, :post => @comment.post}))%>"); diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index cac081f60345b661aa76bacac878c493e1ad3b59..ceac7bc1be58a07b8eabb903b84cac18e458c20b 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -45,9 +45,6 @@ current_user: #{current_user.person.as_api_response(:backbone).to_json} }); - Diaspora.I18n.loadLocale(#{get_javascript_strings_for(I18n.locale).to_json}, "#{I18n.locale}"); - Diaspora.Page = "#{params[:controller].camelcase}#{params[:action].camelcase}"; - = yield(:head) -unless Rails.env == "production" @@ -80,6 +77,9 @@ %script{:id => "stream-element-template", :type => 'text/template'} != File.read("#{Rails.root}/app/views/shared/_stream_element.html.underscore") + %script{:id => "comment-template", :type => 'text/template'} + != File.read("#{Rails.root}/app/views/shared/_comment.html.underscore") + .container{:style=> "#{yield(:break_the_mold)}"} - if @aspsect == :getting_started || @page == :logged_out = yield diff --git a/app/views/shared/_comment.html.underscore b/app/views/shared/_comment.html.underscore new file mode 100644 index 0000000000000000000000000000000000000000..54062b689a1f18b0b0b3054f5845ef878798cd44 --- /dev/null +++ b/app/views/shared/_comment.html.underscore @@ -0,0 +1,31 @@ +<li id="<%= id %>" class="comment"> + <div class="right controls"> + <!-- need access to post --> + <% if(author.id === current_user.id) { %> + <!-- LINK BROKEN FOR NOW --> + <a href="/posts/<%= id %>/comments/<%= id %>" class="delete comment_delete" data-original-title="Delete"> + <img alt="Deletelabel" src="/images/deletelabel.png"> + </a> + <% } %> + </div> + + <a href="/people/<%= author.id %>"> + <img src="<%= author.avatar.small %>" class="avatar" data-person-id="<%= author.id %>"/> + </a> + + <div class="content"> + <span class="from"> + <a href="/people/<%= author.id %>"> + <%= author.name %> + </a> + </span> + + <p> + <%= text %> + </p> + + <div class="comment_info"> + <time class="timeago" datetime="<%= created_at %>"/> + </div> + </div> +</li> diff --git a/app/views/shared/_stream_element.html.underscore b/app/views/shared/_stream_element.html.underscore index 4941e0040eff293a35a9e343d14441948a17e95a..ff9c3e63c75b00ead678a183308bc5171b1e3b89 100644 --- a/app/views/shared/_stream_element.html.underscore +++ b/app/views/shared/_stream_element.html.underscore @@ -78,43 +78,7 @@ </li> </ul> - <ul class="comments"> - - <% _.each(last_three_comments, function(comment) { %> - <li id="<%= comment.guid %>" class="comment"> - - <% if(comment.author.id == current_user.id || author.id == current_user.id) { %> - <div class="right controls"> - <a href="/posts/<%= id %>/comments/<%= comment.id %>" class="delete comment_delete" data-original-title="Delete"> - <img alt="Deletelabel" src="/images/deletelabel.png"> - </a> - </div> - <% } %> - - <a href="/people/<%= author.id %>"> - <img src="<%= comment.author.avatar.small %>" class="avatar" data-person-id="<%= comment.author.id %>"/> - </a> - - <div class="content"> - <span class="from"> - <a href="/people/<%= comment.author.id %>"> - <%= comment.author.name %> - </a> - </span> - - <p> - <%= comment.text %> - </p> - - <div class="comment_info"> - <time class="timeago" datetime="<%= comment.created_at %>"/> - </div> - - </div> - </li> - <% }) %> - - </ul> + <ul class="comments"> </ul> <div class="new_comment_form_wrapper <%= comments_count > 0 ? '' : 'hidden' %>"> <form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" data-remote="true" id="new_comment_on_<%= id %>" method="post"> diff --git a/public/javascripts/app.js b/public/javascripts/app.js index 2d01dbef03dafd260dcdc88ffa84a2e95a23b983..83f9efb06132917e8d7a4ee77e682fd7aafadc1c 100644 --- a/public/javascripts/app.js +++ b/public/javascripts/app.js @@ -18,8 +18,6 @@ var App = { Backbone.history.start({pushState: true}); } - - }; $(function() { App.initialize(); }); diff --git a/public/javascripts/collections/comments.js b/public/javascripts/collections/comments.js new file mode 100644 index 0000000000000000000000000000000000000000..954e8f16095014005e69c89d72647b65586cc241 --- /dev/null +++ b/public/javascripts/collections/comments.js @@ -0,0 +1,3 @@ +App.Collections.Comments = Backbone.Collection.extend({ + model: App.Models.Comment +}); diff --git a/public/javascripts/collections/stream.js b/public/javascripts/collections/stream.js index 6f8e22be1bab0a33277b4e2365e7a2d5d4115a04..55146137f6d83e83a2321a6906f2b011a929d387 100644 --- a/public/javascripts/collections/stream.js +++ b/public/javascripts/collections/stream.js @@ -2,7 +2,7 @@ App.Collections.Stream = Backbone.Collection.extend({ url: function() { var path = document.location.pathname + ".json"; - if(this.models.length) { path += "?max_time=" + _.last(this.models).intTime(); } + if(this.models.length) { path += "?max_time=" + _.last(this.models).createdAt(); } return path; }, diff --git a/public/javascripts/models/comment.js b/public/javascripts/models/comment.js new file mode 100644 index 0000000000000000000000000000000000000000..df76c6f72056edea61c997aa2a09dbae23ddcfd3 --- /dev/null +++ b/public/javascripts/models/comment.js @@ -0,0 +1,2 @@ +App.Models.Comment = Backbone.Model.extend({ +}); diff --git a/public/javascripts/models/post.js b/public/javascripts/models/post.js index 89449e5ab1fbc8e59db5ccfdede9b4695c35103f..eb204b86b6b521b305d3d24bd4455ed318273752 100644 --- a/public/javascripts/models/post.js +++ b/public/javascripts/models/post.js @@ -1,6 +1,11 @@ App.Models.Post = Backbone.Model.extend({ url: "/posts/:id", - intTime: function(){ + + initialize: function() { + this.comments = new App.Collections.Comments(this.get("last_three_comments")); + }, + + createdAt: function(){ return +new Date(this.get("created_at")) / 1000; } }); diff --git a/public/javascripts/views/comment-stream.js b/public/javascripts/views/comment-stream.js new file mode 100644 index 0000000000000000000000000000000000000000..e0e79f24f87035ee0f7ee2b63607ba24a7b84d19 --- /dev/null +++ b/public/javascripts/views/comment-stream.js @@ -0,0 +1,17 @@ +App.Views.CommentStream = Backbone.View.extend({ + initialize: function(options) { + this.collection = options.collection; + }, + + render: function() { + var self = this; + + this.collection.each(function(comment) { + $(self.el).append(new App.Views.Comment({ + model: comment + }).render()); + }); + + return this.el; + } +}); diff --git a/public/javascripts/views/comment.js b/public/javascripts/views/comment.js new file mode 100644 index 0000000000000000000000000000000000000000..e3e4024d23427957b9f67c700fa3b2399e979d02 --- /dev/null +++ b/public/javascripts/views/comment.js @@ -0,0 +1,17 @@ +App.Views.Comment = Backbone.View.extend({ + initialize: function(options) { + this.model = options.model; + this.template = _.template($("#comment-template").html()); + }, + + render: function() { + this.el = $(this.template($.extend( + this.model.toJSON(), + App.user() + ))); + + console.log(this.el); + + return this.el; + } +}); diff --git a/public/javascripts/views/post.js b/public/javascripts/views/post.js new file mode 100644 index 0000000000000000000000000000000000000000..6dac596351f5e34913b61996de1d4bdd993d4100 --- /dev/null +++ b/public/javascripts/views/post.js @@ -0,0 +1,22 @@ +App.Views.Post = Backbone.View.extend({ + initialize: function(options) { + this.model = options.model; + this.template = _.template($("#stream-element-template").html()); + }, + + render: function() { + this.el = $(this.template($.extend( + this.model.toJSON(), + App.user() + ))); + + this.$("ul.comments").html(new App.Views.CommentStream({ + collection: this.model.comments + }).render()); + + this.$(".details time").timeago(); + this.$("label").inFieldLabels(); + + return this.el; + } +}); diff --git a/public/javascripts/views/stream.js b/public/javascripts/views/stream.js index ee4f6201baf8f9d3513b953fc74c10e21422e291..9dc8d768e8970f11f3de51fd2cc5820c883196fc 100644 --- a/public/javascripts/views/stream.js +++ b/public/javascripts/views/stream.js @@ -5,28 +5,21 @@ App.Views.Stream = Backbone.View.extend({ initialize: function(){ this.el = $("#main_stream"); - this.template = _.template($("#stream-element-template").html()); - _.bindAll(this, "appendPost", "collectionFetched"); this.collection = new App.Collections.Stream; this.collection.bind("add", this.appendPost); + this.loadMore(); }, - appendPost: function(model) { - var post = $(this.template($.extend( - model.toJSON(), - App.user() - ))); - $(this.el).append(post); - Diaspora.BaseWidget.instantiate("StreamElement", post); + appendPost: function(post) { + $(this.el).append(new App.Views.Post({ + model: post + }).render()); }, collectionFetched: function() { - this.$(".details time").timeago(); - this.$("label").inFieldLabels(); - this.$("#paginate").remove(); $(this.el).append($("<a>", { href: this.collection.url(), @@ -35,9 +28,7 @@ App.Views.Stream = Backbone.View.extend({ }, loadMore: function(evt) { - if(evt) { - evt.preventDefault(); - } + if(evt) { evt.preventDefault(); } this.collection.fetch({ add: true, diff --git a/spec/javascripts/collections/stream-spec.js b/spec/javascripts/collections/stream-spec.js new file mode 100644 index 0000000000000000000000000000000000000000..53e1c5734d6c6a39e62bf71370a05d7aae2899b4 --- /dev/null +++ b/spec/javascripts/collections/stream-spec.js @@ -0,0 +1,18 @@ +describe("Stream", function() { + describe("url", function() { + var stream = new BackboneStream(), + expectedPath = document.location.pathname + ".json"; + it("returns the json path", function() { + expect(stream.url()).toEqual(expectedPath); + }); + + it("returns the json path with max_time if the collection has models", function() { + var post = new Post(); + spyOn(post, "createdAt").andReturn(1234); + + stream.add(post); + + expect(stream.url()).toEqual(expectedPath + "?max_time=1234"); + }); + }); +}); diff --git a/spec/javascripts/models/post-spec.js b/spec/javascripts/models/post-spec.js new file mode 100644 index 0000000000000000000000000000000000000000..f409f14597d2cc6089deaae112278a847a632e89 --- /dev/null +++ b/spec/javascripts/models/post-spec.js @@ -0,0 +1,12 @@ +describe("App.Models.Post", function() { + describe("createdAt", function() { + var post = new App.Models.Post(); + it("returns the post's created_at as an integer", function() { + var date = new Date; + post.set({ created_at: +date * 1000 }); + + expect(typeof post.createdAt()).toEqual("number"); + expect(post.createdAt()).toEqual(+date); + }); + }); +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 0c9bdcd20dd804718ca88df6a82f18a49e7d7e24..3062f72a98a67f19b1ce6e5a2afa091620b51df1 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -25,6 +25,8 @@ src_files: - public/javascripts/vendor/timeago.js - public/javascripts/vendor/facebox.js - public/javascripts/jquery.infieldlabel-custom.js + - public/javascripts/vendor/underscore.js + - public/javascripts/vendor/backbone.js - public/javascripts/fileuploader-custom.js - public/javascripts/jquery.autocomplete-custom.js - public/javascripts/diaspora.js @@ -33,7 +35,12 @@ src_files: - public/javascripts/pages/* - public/javascripts/helpers/* - public/javascripts/widgets/* - + + - public/javascripts/app.js + - public/javascripts/models/* + - public/javascripts/collections/* + - public/javascripts/views/* + - public/javascripts/mobile.js - public/javascripts/contact-list.js - public/javascripts/web-socket-receiver.js