From 9672c272329d10a85a6cb56229379f73148b380d Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Sun, 2 Oct 2011 12:26:56 -0700 Subject: [PATCH] using two less delegates in mobilejs; made some partials for mobile stream elements for sanity's sake --- app/controllers/likes_controller.rb | 2 +- app/views/reshares/_reshare.mobile.haml | 27 +----- app/views/shared/_photo_area.mobile.haml | 15 ++++ app/views/shared/_post_info.mobile.haml | 23 +++++ app/views/shared/_stream_element.mobile.haml | 32 +------ public/javascripts/mobile.js | 93 ++++++++++---------- public/stylesheets/sass/mobile.scss | 1 - 7 files changed, 90 insertions(+), 103 deletions(-) create mode 100644 app/views/shared/_photo_area.mobile.haml create mode 100644 app/views/shared/_post_info.mobile.haml diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 233e1dc2c..12ad5562a 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -21,7 +21,7 @@ class LikesController < ApplicationController format.js { render 'likes/update', :status => 201 } format.html { render :nothing => true, :status => 201 } format.mobile { redirect_to post_path(@like.post_id) } - format.json { render :nothing => true, :status => 201 } + format.json { render :json => {"id" => @like.id}, :status => 201 } end else render :nothing => true, :status => 422 diff --git a/app/views/reshares/_reshare.mobile.haml b/app/views/reshares/_reshare.mobile.haml index 32a9b1aa4..6d8a11549 100644 --- a/app/views/reshares/_reshare.mobile.haml +++ b/app/views/reshares/_reshare.mobile.haml @@ -4,33 +4,10 @@ .reshare - if post - .photo_area - - if post.is_a?(StatusMessage) - -if post.photos.size > 0 - .photo_attachments - - if post.photos.size > 1 - .additional_photo_count - = "+ #{post.photos.size-1}" - = image_tag post.photos.first.url(:thumb_large), :class => "stream-photo big-stream-photo" - - elsif post.activity_streams? - = image_tag post.image_url + = render 'shared/photo_area', :post => post .content - .from - = person_image_link(post.author, :size => :thumb_small) - = person_link(post.author) - .info - %span.time{:integer => post.created_at.to_i} - = t('ago', :time => time_ago_in_words(post.created_at)) - %span.via - - if post.activity_streams? - = t('shared.stream_element.via', :link => link_to("#{post.provider_display_name}", post.actor_url)).html_safe - – - %span.scope_scope - - if post.public? - = t('public') - - else - = t('limited') + = render 'shared/post_info', :post => post - if !post.activity_streams? = render 'status_messages/status_message', :post => post, :photos => post.photos diff --git a/app/views/shared/_photo_area.mobile.haml b/app/views/shared/_photo_area.mobile.haml new file mode 100644 index 000000000..0ba6126d5 --- /dev/null +++ b/app/views/shared/_photo_area.mobile.haml @@ -0,0 +1,15 @@ +-# Copyright (c) 2010-2011, Diaspora Inc. This file is +-# licensed under the Affero General Public License version 3 or later. See +-# the COPYRIGHT file. + +.photo_area + - if post.is_a?(StatusMessage) + -if post.photos.size > 0 + .photo_attachments + - if post.photos.size > 1 + .additional_photo_count + = "+ #{post.photos.size-1}" + = image_tag post.photos.first.url(:thumb_large), :class => "stream-photo big-stream-photo" + - elsif post.activity_streams? + = image_tag post.image_url + diff --git a/app/views/shared/_post_info.mobile.haml b/app/views/shared/_post_info.mobile.haml new file mode 100644 index 000000000..91f9b424c --- /dev/null +++ b/app/views/shared/_post_info.mobile.haml @@ -0,0 +1,23 @@ +-# Copyright (c) 2010-2011, Diaspora Inc. This file is +-# licensed under the Affero General Public License version 3 or later. See +-# the COPYRIGHT file. + +.from + = person_image_link(post.author, :size => :thumb_small) + = person_link(post.author) + .info + %span.time{:integer => post.created_at.to_i} + = t('ago', :time => time_ago_in_words(post.created_at)) + %span.via + - if post.activity_streams? + = t('shared.stream_element.via', :link => link_to("#{post.provider_display_name}", post.actor_url)).html_safe + - elsif post.provider_display_name == 'mobile' + = t('shared.stream_element.via', :link => nil) + mobile + – + %span.scope_scope + - if post.public? + = t('public') + - else + = t('limited') + diff --git a/app/views/shared/_stream_element.mobile.haml b/app/views/shared/_stream_element.mobile.haml index bb5b88bf6..e7066cb79 100644 --- a/app/views/shared/_stream_element.mobile.haml +++ b/app/views/shared/_stream_element.mobile.haml @@ -7,38 +7,10 @@ - if post.is_a?(Reshare) = render 'reshares/reshare', :reshare => post, :post => post.root - .photo_area - - if post.is_a?(StatusMessage) - -if post.photos.size > 0 - .photo_attachments - - if post.photos.size > 1 - .additional_photo_count - = "+ #{post.photos.size-1}" - = image_tag post.photos.first.url(:thumb_large), :class => "stream-photo big-stream-photo" - - elsif post.activity_streams? - = image_tag post.image_url + = render 'shared/photo_area', :post => post .content - .from - = person_image_link(post.author, :size => :thumb_small) - = person_link(post.author) - .info - %span.time{:integer => post.created_at.to_i} - = t('ago', :time => time_ago_in_words(post.created_at)) - %span.via - - if post.activity_streams? - = t('.via', :link => link_to("#{post.provider_display_name}", post.actor_url)).html_safe - - elsif post.provider_display_name == 'mobile' - = t('.via', :link => nil) - mobile - - – - %span.scope_scope - - if post.public? - = t('public') - - else - = t('limited') - + = render 'shared/post_info', :post => post - if post.is_a?(StatusMessage) = render 'status_messages/status_message', :post => post, :photos => post.photos diff --git a/public/javascripts/mobile.js b/public/javascripts/mobile.js index f6b563c25..c338c5e31 100644 --- a/public/javascripts/mobile.js +++ b/public/javascripts/mobile.js @@ -1,58 +1,59 @@ $(document).ready(function(){ - $(".stream").delegate(".like_action.inactive", "tap click", function(evt){ + + /* Heart toggle */ + $(".like_action", ".stream").bind("tap click", function(evt){ evt.preventDefault(); var link = $(this), likeCounter = $(this).closest(".stream_element").find("like_count"), - postId = link.closest(".stream_element").data("post-guid"); + href = link.attr("href"); - $.ajax({ - url: link.attr("href"), - dataType: 'json', - type: 'POST', - beforeSend: function(){ - link.removeClass('inactive') - .addClass('loading'); - }, - complete: function(data){ - link.removeClass('loading') - .removeClass('inactive') - .addClass('active') - .data('post-id', postId); + var showLoader = function(link){ + link.addClass('loading'); + }; - if(likeCounter){ - likeCounter.text(parseInt(likeCounter.text) + 1); + var removeLoader = function(link){ + link.removeClass('loading') + .toggleClass('active') + .toggleClass('inactive'); + }; + + if(!link.hasClass("loading") && link.hasClass('inactive')) { + + $.ajax({ + url: href, + dataType: 'json', + type: 'POST', + beforeSend: showLoader(link), + success: function(data){ + removeLoader(link); + link.attr("href", href + "/" + data["id"]); + + if(likeCounter){ + likeCounter.text(parseInt(likeCounter.text) + 1); + } } - } - }); + }); + } + else if(!link.hasClass("loading") && link.hasClass("active")){ + $.ajax({ + url: link.attr("href"), + dataType: 'json', + type: 'DELETE', + beforeSend: showLoader(link), + complete: function(data){ + removeLoader(link); + link.attr("href", href.replace(/\/\d+$/, '')); + + if(likeCounter){ + likeCounter.text(parseInt(likeCounter.text) - 1); + } + } + }); + } }); - $(".stream").delegate(".like_action.active", "tap click", function(evt){ - evt.preventDefault(); - var link = $(this); - likeCounter = $(this).closest(".stream_element").find("like_count"); - - $.ajax({ - url: link.attr("href"), - dataType: 'json', - type: 'DELETE', - beforeSend: function(){ - link.removeClass('active') - .addClass('loading'); - }, - complete: function(data){ - link.removeClass('loading') - .removeClass('active') - .addClass('inactive') - .data('like-id', ''); - - if(likeCounter){ - likeCounter.text(parseInt(likeCounter.text) - 1); - } - } - }); - }); - - $(".stream").delegate(".show_comments", "tap click", function(evt){ + /* Show comments */ + $(".show_comments", ".stream").bind("tap click", function(evt){ evt.preventDefault(); var link = $(this), parent = link.closest(".bottom_bar").first(), diff --git a/public/stylesheets/sass/mobile.scss b/public/stylesheets/sass/mobile.scss index 29ce44b59..37f99c911 100644 --- a/public/stylesheets/sass/mobile.scss +++ b/public/stylesheets/sass/mobile.scss @@ -630,7 +630,6 @@ footer { .comment_action.image_link { background-image: url("/images/icons/pencil_mobile_grey_active.png"); - &.inactive { background-image: url("/images/icons/pencil_mobile_grey.png"); }