From ae6f0fdc51be17b3c90bbacfd9d4979bc9b3d56a Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 23 Aug 2010 13:58:33 -0700 Subject: [PATCH] new photo form now loads through ajax --- app/controllers/photos_controller.rb | 2 ++ app/models/user.rb | 7 +++++-- app/views/albums/show.html.haml | 7 ++++++- app/views/photos/_new_photo.haml | 21 +++++++-------------- public/javascripts/view.js | 15 ++++++++++++++- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index eb165ca2d..46332992d 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -2,6 +2,7 @@ class PhotosController < ApplicationController before_filter :authenticate_user! def create + render :nothing => true begin @photo = current_user.post(:photo, params) @@ -21,6 +22,7 @@ class PhotosController < ApplicationController def new @photo = Photo.new + @album = current_user.album_by_id(params[:album_id]) render :partial => "new_photo" end diff --git a/app/models/user.rb b/app/models/user.rb index fe4cb1ea9..38dde280a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,8 +47,6 @@ class User model_class = class_name.to_s.camelize.constantize - pp options - post = model_class.instantiate(options) post.creator_signature = post.sign_with_key(encryption_key) post.save @@ -327,6 +325,11 @@ class User groups.detect{|x| x.id == id } end + def album_by_id( id ) + id = ensure_bson id + albums.detect{|x| x.id == id } + end + def groups_with_person person id = ensure_bson person.id groups.select {|group| group.person_ids.include? id} diff --git a/app/views/albums/show.html.haml b/app/views/albums/show.html.haml index 04b117645..c0cf97427 100644 --- a/app/views/albums/show.html.haml +++ b/app/views/albums/show.html.haml @@ -1,3 +1,9 @@ +:javascript + $(document).ready(function(){ + reset_photo_fancybox(); + }); + +.album_id{:id => @album.id, :style => "display:hidden;"} .back= link_to '⇧ albums', albums_path %h1.big_text @@ -11,7 +17,6 @@ .yo{:style => "display:none;"} #new_photo_pane - = render "photos/new_photo", :photo => @photo, :album => @album .sub_header ="updated #{how_long_ago(@album)}" diff --git a/app/views/photos/_new_photo.haml b/app/views/photos/_new_photo.haml index 0704c0244..5bc6d6882 100644 --- a/app/views/photos/_new_photo.haml +++ b/app/views/photos/_new_photo.haml @@ -2,7 +2,7 @@ $(function() { $("#photo_image").html5_upload({ // WE INSERT ALBUM_ID PARAM HERE - url: "/photos?album_id=#{album.id}", + url: "/photos?album_id=#{@album.id}", sendBoundary: window.FormData || $.browser.mozilla, setName: function(text) { $("#progress_report_name").text(text); @@ -12,8 +12,9 @@ $("#add_photo_loader").fadeOut(400); $("#photo_title_status").text("Done!"); - $("#progress_report").html(""); - resetPhotoButton + $("#progress_report").html("Good job me!"); + + $("#add_photo_button").addClass("uploading_complete"); }, onStart: function(event, total){ $("#add_photo_button").html( "Uploading Photos" ); @@ -27,21 +28,13 @@ }); }); - function resetPhotoButton{ - $("#add_photo_button").click(function (evt){ - $("#new_photo_pane").html($.get("photos/ajax_new")); - $("#add_photo_button").unbind(); - $("#add_photo_button").fancybox(); - } - } - %h1 %span{:id=>"photo_title_status"} Add photos to - %i= album.name -= form_for photo, :html => {:multipart => true} do |f| + %i= @album.name += form_for @photo, :html => {:multipart => true} do |f| = f.error_messages - = f.hidden_field :album_id, :value => album.id + = f.hidden_field :album_id, :value => @album.id = f.file_field :image, :multiple => 'multiple' #progress_report{ :style => "display:none;text-align:center;" } diff --git a/public/javascripts/view.js b/public/javascripts/view.js index b1baae31e..720709b8e 100644 --- a/public/javascripts/view.js +++ b/public/javascripts/view.js @@ -71,7 +71,14 @@ $(document).ready(function(){ $("#add_album_button").fancybox(); $("#add_group_button").fancybox(); $("#add_request_button").fancybox({ 'titleShow': false }); - $("#add_photo_button").fancybox(); + $("#add_photo_button").fancybox({ + 'onClosed' : function(){ + if($("#add_photo_button").hasClass("uploading_complete")){ + $("#add_photo_button").removeClass("uploading_complete"); + reset_photo_fancybox(); + } + } + }); //pane_toggler_button("photo"); @@ -91,6 +98,12 @@ $(document).ready(function(){ });//end document ready +function reset_photo_fancybox(){ + album_id = $(".album_id")[0].id; + ajax = $.get("/photos/new?album_id=" + album_id, function(){ + $("#new_photo_pane").html(ajax.responseText) + }); +} function pane_toggler_button( name ) {