From 80f797a581d078773067ae9ef2b59b4250291b15 Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Thu, 8 Dec 2011 17:17:13 -0600 Subject: [PATCH] introduce comments --- app/views/comments/create.js.erb | 4 ++ app/views/layouts/application.html.haml | 6 +-- app/views/shared/_comment.html.underscore | 31 +++++++++++++++ .../shared/_stream_element.html.underscore | 38 +------------------ public/javascripts/app.js | 2 - public/javascripts/collections/comments.js | 3 ++ public/javascripts/collections/stream.js | 2 +- public/javascripts/models/comment.js | 2 + public/javascripts/models/post.js | 7 +++- public/javascripts/views/comment-stream.js | 17 +++++++++ public/javascripts/views/comment.js | 17 +++++++++ public/javascripts/views/post.js | 22 +++++++++++ public/javascripts/views/stream.js | 21 +++------- spec/javascripts/collections/stream-spec.js | 18 +++++++++ spec/javascripts/models/post-spec.js | 12 ++++++ spec/javascripts/support/jasmine.yml | 9 ++++- 16 files changed, 151 insertions(+), 60 deletions(-) create mode 100644 app/views/shared/_comment.html.underscore create mode 100644 public/javascripts/collections/comments.js create mode 100644 public/javascripts/models/comment.js create mode 100644 public/javascripts/views/comment-stream.js create mode 100644 public/javascripts/views/comment.js create mode 100644 public/javascripts/views/post.js create mode 100644 spec/javascripts/collections/stream-spec.js create mode 100644 spec/javascripts/models/post-spec.js diff --git a/app/views/comments/create.js.erb b/app/views/comments/create.js.erb index e9840a2f2..aaee57e03 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 cac081f60..ceac7bc1b 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 000000000..54062b689 --- /dev/null +++ b/app/views/shared/_comment.html.underscore @@ -0,0 +1,31 @@ +
  • +
    + + <% if(author.id === current_user.id) { %> + + + Deletelabel + + <% } %> +
    + + + + + +
    + + + <%= author.name %> + + + +

    + <%= text %> +

    + +
    +
    +
    +
  • diff --git a/app/views/shared/_stream_element.html.underscore b/app/views/shared/_stream_element.html.underscore index 4941e0040..ff9c3e63c 100644 --- a/app/views/shared/_stream_element.html.underscore +++ b/app/views/shared/_stream_element.html.underscore @@ -78,43 +78,7 @@ - +
    diff --git a/public/javascripts/app.js b/public/javascripts/app.js index 2d01dbef0..83f9efb06 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 000000000..954e8f160 --- /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 6f8e22be1..55146137f 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 000000000..df76c6f72 --- /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 89449e5ab..eb204b86b 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 000000000..e0e79f24f --- /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 000000000..e3e4024d2 --- /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 000000000..6dac59635 --- /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 ee4f6201b..9dc8d768e 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($("", { 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 000000000..53e1c5734 --- /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 000000000..f409f1459 --- /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 0c9bdcd20..3062f72a9 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