Commit 6a27b875a4703ca0ae36481e6d5cacf24bd928ca
1 parent
f746553b
Exists in
master
and in
29 other branches
File upload working
Showing
5 changed files
with
95 additions
and
18 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
... | ... | @@ -338,7 +338,18 @@ class CmsController < MyProfileController |
338 | 338 | def search |
339 | 339 | query = params[:q] |
340 | 340 | results = query.blank? ? [] : profile.articles.published.find_by_contents(query) |
341 | - render :text => results.map { |item| { 'title' => item.title, 'url' => url_for(item.url), :icon => icon_for_article(item), :content_type => item.mime_type }}.to_json, :content_type => 'application/json' | |
341 | + render :text => article_list_to_json(results), :content_type => 'application/json' | |
342 | + end | |
343 | + def media_upload | |
344 | + files_uploaded = [] | |
345 | + parent = check_parent(params[:parent_id]) | |
346 | + files = [:file1,:file2, :file3].map { |f| params[f] } | |
347 | + if request.post? | |
348 | + files.each do |file| | |
349 | + files_uploaded << UploadedFile.create(:uploaded_data => file, :profile => profile, :parent => parent) unless file == '' | |
350 | + end | |
351 | + end | |
352 | + render :text => article_list_to_json(files_uploaded), :content_type => 'application/json' | |
342 | 353 | end |
343 | 354 | |
344 | 355 | protected |
... | ... | @@ -386,5 +397,16 @@ class CmsController < MyProfileController |
386 | 397 | @selected_locale = @article.language || FastGettext.locale |
387 | 398 | end |
388 | 399 | |
400 | + def article_list_to_json(list) | |
401 | + list.map do |item| | |
402 | + { | |
403 | + 'title' => item.title, | |
404 | + 'url' => url_for(item.url), | |
405 | + :icon => icon_for_article(item), | |
406 | + :content_type => item.mime_type | |
407 | + } | |
408 | + end.to_json | |
409 | + end | |
410 | + | |
389 | 411 | end |
390 | 412 | ... | ... |
app/views/cms/_textile_media.rhtml
... | ... | @@ -18,31 +18,43 @@ |
18 | 18 | <p><%= _('See also a more complete <a href="%s">Textile Reference</a>') % 'http://redcloth.org/hobix.com/textile/' %></p> |
19 | 19 | </div> |
20 | 20 | </div> |
21 | - <div class='textile-editor-sidebar-box'> | |
21 | + <div class='textile-editor-sidebar-box' id='media-upload-box'> | |
22 | 22 | <p><strong>Media upload</strong></p> |
23 | - <% @folders = profile.folders %> | |
24 | - <% form_tag({ :action => 'media_upload' }, :multipart => true) do |f| %> | |
25 | - <%= render :partial => 'upload_file_form', :locals => { :size => 30 } %> | |
26 | - <% end %> | |
27 | - <div id='media-upload-results' style='display: none'> | |
23 | + <div id='media-upload-form'> | |
24 | + <% form_tag({ :action => 'media_upload' }, :multipart => true) do |f| %> | |
25 | + <div class='formfield'> | |
26 | + <%# TODO duplicated from partial upload_file_form %> | |
27 | + <%= labelled_form_field(_('Choose folder to upload files:'), select_tag('parent_id', options_for_select([[profile.identifier, '']] + profile.folders.collect {|f| [ profile.identifier + '/' + f.full_name, f.id ] }))) %> | |
28 | + </div> | |
29 | + <p><%= file_field_tag('file1') %></p> | |
30 | + <p><%= file_field_tag('file2') %></p> | |
31 | + <p><%= file_field_tag('file3') %></p> | |
32 | + <% button_bar do %> | |
33 | + <%= submit_button(:save, _('Upload')) %> | |
34 | + <% end %> | |
35 | + <% end %> | |
36 | + </div> | |
37 | + <div id='media-upload-results' class='media-list-results' style='display: none'> | |
28 | 38 | <ul> |
29 | 39 | </ul> |
30 | 40 | <p> |
31 | 41 | <em><%= _('Drag item names to the text to add their URL') %></em> |
32 | 42 | </p> |
33 | - <p><%= link_to('#', _('Upload more files'), :id => 'media-upload-more-files') %></p> | |
43 | + <p><%= link_to(_('Upload more files ...'), '#', :id => 'media-upload-more-files')%></p> | |
34 | 44 | </div> |
35 | 45 | </div> |
36 | 46 | <div class='textile-editor-sidebar-box'> |
37 | 47 | <p><strong>Media search</strong></p> |
38 | 48 | <% form_tag({ :action => 'search' }) do %> |
39 | - <span class='formfield'> | |
40 | - <input name='q' type='text' id='media-search-query' style='width: 250px;'/> | |
41 | - </span> | |
42 | - <%= submit_button :search, _('Search'), :id => 'media-search-button' %> | |
49 | + <% button_bar do %> | |
50 | + <span class='formfield'> | |
51 | + <input name='q' type='text' id='media-search-query' style='width: 250px;'/> | |
52 | + </span> | |
53 | + <%= submit_button :search, _('Search'), :id => 'media-search-button' %> | |
54 | + <% end %> | |
43 | 55 | <% end %> |
44 | 56 | </form> |
45 | - <div id='media-search-results' style='display: none'> | |
57 | + <div id='media-search-results' class='media-list-results' style='display: none'> | |
46 | 58 | <ul> |
47 | 59 | </ul> |
48 | 60 | <p> | ... | ... |
public/javascripts/article.js
... | ... | @@ -39,13 +39,34 @@ jQuery(function($) { |
39 | 39 | var $button = $(this); |
40 | 40 | $button.toggleClass('icon-loading'); |
41 | 41 | $.get($(this).parent().attr('action'), { 'q': query }, function(data) { |
42 | - $button.toggleClass('icon-loading'); | |
43 | 42 | insert_items(data, '#media-search-results ul'); |
44 | 43 | if (data.length && data.length > 0) { |
45 | 44 | $('#media-search-results').slideDown(); |
46 | 45 | } |
46 | + $button.toggleClass('icon-loading'); | |
47 | 47 | }); |
48 | 48 | return false; |
49 | 49 | }); |
50 | + $('#media-upload-form form').ajaxForm({ | |
51 | + dataType: 'json', | |
52 | + resetForm: true, | |
53 | + beforeSubmit: | |
54 | + function() { | |
55 | + $('#media-upload-form').slideUp(); | |
56 | + $('#media-upload-box').toggleClass('icon-loading'); | |
57 | + }, | |
58 | + success: | |
59 | + function(data) { | |
60 | + insert_items(data, '#media-upload-results ul'); | |
61 | + if (data.length && data.length > 0) { | |
62 | + $('#media-upload-results').slideDown(); | |
63 | + } | |
64 | + $('#media-upload-box').toggleClass('icon-loading'); | |
65 | + } | |
66 | + }); | |
67 | + $('#media-upload-more-files').click(function() { | |
68 | + $('#media-upload-results').hide(); | |
69 | + $('#media-upload-form').show(); | |
70 | + }); | |
50 | 71 | |
51 | 72 | }); | ... | ... |
public/stylesheets/application.css
... | ... | @@ -3396,11 +3396,11 @@ div.with_media_panel .formfield input { |
3396 | 3396 | .textile-editor-sidebar .icon-loading { |
3397 | 3397 | background-image: url(../images/loading-small.gif); |
3398 | 3398 | } |
3399 | -.textile-editor-sidebar #media-search-results ul { | |
3399 | +.textile-editor-sidebar .media-list-results ul { | |
3400 | 3400 | padding: 0px; |
3401 | 3401 | list-style: none; |
3402 | 3402 | } |
3403 | -.textile-editor-sidebar #media-search-results li { | |
3403 | +.textile-editor-sidebar .media-list-results li { | |
3404 | 3404 | background-repeat: no-repeat; |
3405 | 3405 | background-position: 0px 0px; |
3406 | 3406 | padding-left: 20px; |
... | ... | @@ -3409,11 +3409,14 @@ div.with_media_panel .formfield input { |
3409 | 3409 | border: none; |
3410 | 3410 | margin-bottom: 2px; |
3411 | 3411 | } |
3412 | -.textile-editor-sidebar #media-search-results li:hover { | |
3412 | +.textile-editor-sidebar .media-list-results li:hover { | |
3413 | 3413 | background-color: transparent; |
3414 | 3414 | border: none; |
3415 | 3415 | } |
3416 | - | |
3416 | +.textile-editor-sidebar #media-upload-box { | |
3417 | + background-repeat: no-repeat; | |
3418 | + background-position: top right ; | |
3419 | +} | |
3417 | 3420 | |
3418 | 3421 | /* ==> public/stylesheets/controller_contact.css <== */ |
3419 | 3422 | /*** SELECT CITY ***/ | ... | ... |
test/functional/cms_controller_test.rb
... | ... | @@ -1631,4 +1631,23 @@ class CmsControllerTest < Test::Unit::TestCase |
1631 | 1631 | assert_match /text/, data.first['content_type'] |
1632 | 1632 | end |
1633 | 1633 | |
1634 | + should 'upload media by AJAX' do | |
1635 | + post :media_upload, :profile => profile.identifier, :uploaded_files => [fixture_file_upload('/files/test.txt', 'text/plain'), fixture_file_upload('/files/rails.png', 'image/png')] | |
1636 | + assert_match 'test.txt', @response.body | |
1637 | + assert_equal 'application/json', @response.content_type | |
1638 | + | |
1639 | + data = eval(@response.body.gsub('":', '"=>')) | |
1640 | + | |
1641 | + assert_equal 'test.txt', data[0]['title'] | |
1642 | + assert_match /\/testinguser\/test.txt$/, data[0]['url'] | |
1643 | + assert_match /text/, data[0]['icon'] | |
1644 | + assert_match /text/, data[0]['content_type'] | |
1645 | + | |
1646 | + assert_equal 'rails.png', data[1]['title'] | |
1647 | + assert_match /\/testinguser\/rails.png$/, data[1]['url'] | |
1648 | + assert_match /png$/, data[1]['icon'] | |
1649 | + assert_match /image/, data[1]['content_type'] | |
1650 | + | |
1651 | + end | |
1652 | + | |
1634 | 1653 | end | ... | ... |