Skip to content
This repository has been archived by the owner on Jan 30, 2018. It is now read-only.

Commit

Permalink
asynchronous file uploads in Pages
Browse files Browse the repository at this point in the history
RJS + iframe magic
  • Loading branch information
mislav committed Jun 16, 2010
1 parent 82e2e7f commit 93e8069
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
12 changes: 11 additions & 1 deletion app/controllers/uploads_controller.rb
Expand Up @@ -62,7 +62,13 @@ def create
flash[:error] = "There was an error uploading the file"
redirect_to :back
elsif @upload.page
redirect_to [@current_project, @upload.page]
if iframe?
template = self.view_paths.find_template(default_template_name(action_name), :js)
@code = render_to_string :template => template
render :template => 'shared/iframe_rjs', :layout => false
else
redirect_to [@current_project, @upload.page]
end
else
redirect_to [@current_project, :uploads]
end
Expand Down Expand Up @@ -108,5 +114,9 @@ def find_upload
@upload = @current_project.uploads.find_by_asset_file_name(params[:id])
end
end

def iframe?
params[:iframe] == 'true'
end

end
7 changes: 7 additions & 0 deletions app/javascripts/pages.js
Expand Up @@ -361,3 +361,10 @@ document.on('ajax:complete', '.page_slot .note form, .page_slot .divider form',
document.on('ajax:create', 'form.edit_note', function(e, element) {
element.down('img.loading').show()
});

document.on('submit', 'body.show_pages form#new_upload', function(e, form) {
var iframe = new Element('iframe', { id: 'file_upload_iframe' }).hide()
$(document.body).insert(iframe)
form.target = iframe.id
form.insert(new Element('input', { type: 'hidden', name: 'iframe', value: 'true' }))
})
9 changes: 9 additions & 0 deletions app/views/shared/iframe_rjs.html.erb
@@ -0,0 +1,9 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content='text/html;charset=utf-8' http-equiv='content-type' />
</head>
<body>
<%= javascript_tag("window.top.eval('#{escape_javascript(@code)}')") %>
</body>
</html>
15 changes: 15 additions & 0 deletions app/views/uploads/create.js.rjs
@@ -0,0 +1,15 @@
if @insert_element.nil?
position = :bottom
location = 'slots'
else
position = @insert_before ? :before : :after
location = @insert_element
end

page.insert_widget dom_id(@upload), position, location,
:partial => 'pages/slot',
:object => @slot,
:locals => { :page => @page, :project => @current_project }

page.call "InsertionBar.clearWidgetForm"
page.reload_page_sort
19 changes: 0 additions & 19 deletions app/views/uploads/create_page.erb

This file was deleted.

1 comment on commit 93e8069

@mislav
Copy link
Contributor Author

@mislav mislav commented on 93e8069 Aug 9, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the iframe upload demonstrated here was broken in Firefox and fixed in f8279ff

Please sign in to comment.