Asset upload

This commit is contained in:
Christophe Vilayphiou 2012-05-17 06:28:29 +08:00
parent 9129835f23
commit 26292f0dcc
15 changed files with 191 additions and 43 deletions

View File

@ -46,7 +46,7 @@ function load_tinymce() {
function myFileBrowser(field_name, url, type, win) {
var cmsURL = window.location.toString();
cmsURL = cmsURL.split("/");
cmsURL = "<%= Rails.application.routes.url_helpers.new_admin_asset_path %>";
cmsURL = "<%= Rails.application.routes.url_helpers.file_upload_admin_assets_path %>";
console.log(cmsURL);
// script URL - use an absolute path!
if (cmsURL.indexOf("?") < 0) {

View File

@ -11,6 +11,7 @@ class Admin::AssetsController < OrbitBackendController
def new
@asset = Asset.new
@asset_categories = AssetCategory.all
@tags = AssetTag.all
respond_to do |format|
format.html {}
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} }
@ -20,6 +21,7 @@ class Admin::AssetsController < OrbitBackendController
def edit
@asset = Asset.find(params[:id])
@asset_categories = AssetCategory.all
@tags = AssetTag.all
respond_to do |format|
format.html {}
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/edit'} }
@ -28,15 +30,29 @@ class Admin::AssetsController < OrbitBackendController
def create
@asset = Asset.new(params[:asset])
if @asset.save
@asset.filename = @asset.i18n_variable[I18n.locale] rescue nil
if @asset.filename && @asset.save
respond_to do |format|
format.html { render :text => @asset.data.url }
format.js {}
format.js {
if params[:uploader]
render 'create'
else
render 'js/remove_pop_up_and_reload_content', :locals => {:function => 'replaceWith', :id => "asset_#{@asset.id}", :value => @asset, :values => nil, :partial => 'admin/assets/asset', :locals => nil}
end
}
end
else
flash[:error] = t(:create_fail)
@asset_categories = AssetCategory.all
@tags = AssetTag.all
respond_to do |format|
format.html { render :action => :new }
format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/new', :locals => {:is_html => false}} }
format.js {
if params[:uploader]
render 'file_upload'
else
render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/new', :locals => {:is_html => false}}
end
}
end
end
end
@ -49,6 +65,9 @@ class Admin::AssetsController < OrbitBackendController
format.js { render 'js/remove_pop_up_and_reload_content', :locals => {:function => 'replaceWith', :id => "asset_#{@asset.id}", :value => @asset, :values => nil, :partial => 'admin/assets/asset', :locals => nil} }
end
else
flash[:error] = t(:update_fail)
@asset_categories = AssetCategory.all
@tags = AssetTag.all
respond_to do |format|
format.html { render :action => :edit }
format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/edit', :locals => {:is_html => false}} }
@ -72,4 +91,16 @@ class Admin::AssetsController < OrbitBackendController
redirect_to assets_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
end
def file_upload
@asset = Asset.new
@asset_categories = AssetCategory.all
@tags = AssetTag.all
render :layout => false
end
def file_select
@assets = Asset.all
render :layout => false
end
end

View File

@ -8,9 +8,16 @@ class Asset
field :filename
field :description
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
validates_presence_of :filename, :data
belongs_to :asset_category
belongs_to :assetable, polymorphic: true
has_and_belongs_to_many :tags, :class_name => "AssetTag"
def sorted_tags
tags.order_by(I18n.locale, :asc)
end
end

View File

@ -1,7 +1,8 @@
<tr id="asset_<%= asset.id %>" class="with_action">
<td><%= check_box_tag 'to_delete[]', asset.id, false, :class => "checkbox_in_list" %></td>
<td><%= asset.i18n_variable[I18n.locale] rescue nil %></td>
<td>
<div class="assets_pic"><%= image_tag(asset.data.url) %></div>
<i class="icons-picture img-peview" rel="popover" data-content="<img src='<%= asset.data.url %>' />" data-original-title="<%= asset.data.filename %>"></i>
<div class="quick-edit">
<ul class="nav nav-pills hide">
<li><%= link_to t(:edit), edit_admin_asset_path(asset), :remote => true, :class => 'edit' %></li>
@ -9,8 +10,22 @@
</ul>
</div>
</td>
<td><%= asset.description %></td>
<td><%= asset.data.file.content_type %></td>
<td><%= asset.data_identifier %></td>
<td><%= number_to_human_size(asset.data.file.file_length) %></td>
<td><%= asset.category.i18n_variable[I18n.locale] rescue nil %></td>
<td>
<div class="label-group">
<div class="label-td">
<% asset.sorted_tags.each do |tag| %>
<span class="label label-tags"><%= tag[I18n.locale] %></span>
<% end %>
</div>
</div>
</td>
</tr>
<script type="text/javascript">
$('.img-peview').popover({
delay: { show: 100, hide: 300 }
});
</script>

View File

@ -1,6 +1,8 @@
<div id='pop_up_content' class="main2">
<h1><%= t('editing_asset') %></h1>
<%= flash_messages %>
<%= form_for @asset, :url => admin_asset_path(@asset), :html => {:id => (is_html ? nil : 'ajaxForm'), :multipart => true } do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
@ -9,7 +11,7 @@
<%= link_back %>
<%= f.submit t(:edit) %>
<% else %>
<a id='ajax_form_submit'><%= t(:update) %></a>
<a id='ajax_form_submit' class="btn btn-primary"><%= t(:update) %></a>
<% end %>
</div>
<% end %>

View File

@ -1,19 +1,38 @@
<p>
<%= f.label :filename, t('admin.file_name') %>
<%= f.text_field :filename, :class => 'text' %>
</p>
<p>
<%= f.label :description, t('admin.description') %>
<%= f.text_field :description, :class => 'text' %>
</p>
<p>
<%= f.label :category %>
<%= f.select :asset_category_id, @asset_categories.collect{|t| [ t.i18n_variable[I18n.locale], t.id ]}, {}, :class => "input-medium" %>
</p>
<p>
<%= f.label :data, t('admin.data'), :class => 'file' %>
<%= f.file_field :data %>
</p>
<div class="control-group">
<label for="title" class="control-label"><%= t 'admin.title' %></label>
<div class="controls">
<%= f.fields_for :i18n_variable, (@asset.new_record? ? @asset.build_i18n_variable : @asset.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %>
<%= content_tag :label do -%>
<div>
<%= I18nVariable.from_locale(locale) %>
<%= f.text_field locale, :class => "input-large" %>
</div>
<% end %>
<% end %>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= f.label :category, t('admin.category') %></label>
<div class="controls">
<%= f.select :asset_category_id, @asset_categories.collect{|t| [ t.i18n_variable[I18n.locale], t.id ]}, {}, :class => "input-large" %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= t 'admin.tags' %></label>
<div class="controls">
<% @tags.each do |tag| %>
<%= content_tag :label, :class => "checkbox inline" do -%>
<%= check_box_tag 'asset[tag_ids][]', tag.id, @asset.tag_ids.include?(tag.id) %>
<%= tag[I18n.locale] %>
<% end %>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= f.label :data, t('admin.data') %></label>
<div class="controls">
<%= f.file_field :data, :class => 'upload' %>
</div>
</div>

View File

@ -1,11 +1,13 @@
<div id='pop_up_content' class="main2">
<h1><%= t('admin.new_asset') %></h1>
<%= flash_messages %>
<%= form_for :asset, :url => admin_assets_path, :html => {:id => 'ajaxForm', :multipart => true } do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
<div class="button_bar">
<a id='ajax_form_submit' class="new"><%= t(:create) %></a>
<a id='ajax_form_submit' class="btn btn-primary"><%= t(:create) %></a>
</div>
<% end %>
</div>

View File

@ -1,5 +1,6 @@
<%= render_sort_bar(true, ['title', 'title','span1-2', 'admin.title'],
['description', 'description', 'span1-2', 'admin.description'],
['intro', 'intro', 'span1-2', 'admin.intro'],
['intro', 'intro', 'span1-2', 'admin.intro'],
['intro', 'intro', 'span1-2', 'admin.file_length']).html_safe %>
<%= render_sort_bar(true, ['title', 'title','span4', 'admin.title'],
['description', 'description', 'span1-2', 'admin.data'],
['intro', 'intro', 'span1-2', 'admin.file_type'],
['intro', 'intro', 'span1-2', 'admin.file_length'],
['intro', 'intro', 'span1-2', 'admin.category'],
['intro', 'intro', 'span1-2', 'admin.tags']).html_safe %>

View File

@ -0,0 +1,56 @@
<%= javascript_include_tag "new_admin" %>
<%= stylesheet_link_tag "new_admin" %>
<%= flash_messages %>
<%= form_for :asset, :url => admin_assets_path(:uploader => true), :html => {:id => 'ajaxForm', :multipart => true } do |f| %>
<div class="modal-header">
<h3><%= t 'admin.file_upload' %></h3>
</div>
<div class="modal-body form-horizontal">
<div class="control-group">
<label for="title" class="control-label"><%= t 'admin.title' %></label>
<div class="controls">
<%= f.fields_for :i18n_variable, (@asset.new_record? ? @asset.build_i18n_variable : @asset.i18n_variable) do |f| %>
<% @site_valid_locales.each do |locale| %>
<%= content_tag :label do -%>
<div>
<%= I18nVariable.from_locale(locale) %>
<%= f.text_field locale, :class => "input-large" %>
</div>
<% end %>
<% end %>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= f.label :category, t('admin.category') %></label>
<div class="controls">
<%= f.select :asset_category_id, @asset_categories.collect{|t| [ t.i18n_variable[I18n.locale], t.id ]}, {}, :class => "input-large" %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= t 'admin.tags' %></label>
<div class="controls">
<% @tags.each do |tag| %>
<%= content_tag :label, :class => "checkbox inline" do -%>
<%= check_box_tag 'asset[tag_ids][]', tag.id, @asset.tag_ids.include?(tag.id) %>
<%= tag[I18n.locale] %>
<% end %>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label"><%= f.label :data, t('admin.data') %></label>
<div class="controls">
<%= f.file_field :data, :class => 'upload' %>
</div>
</div>
</div>
<div style="position: absolute; width: 100%; bottom: 0;">
<div class="modal-footer">
<a id='ajax_form_submit' class="btn btn-primary"><%= t(:create) %></a>
</div>
</div>
<% end %>

View File

@ -3,12 +3,13 @@
<table id="asset_sort_list" class="table main-list">
<thead>
<tr>
<th class="span1"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1"></th>
<th class="span4"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
<th class="span1-2"></th>
</tr>
</thead>
<tbody id="tbody_assets" class="sort-holder">
@ -20,7 +21,7 @@
<div class="form-actions form-fixed pagination-right">
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t('admin.add'), new_admin_asset_path, :remote => true, :class => 'btn btn-primary pull-right' %>
<div id="asset_pagination" class="paginationFixed">
<%= paginate @assets, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]} %>
<%= paginate @assets, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]}, :remote => true %>
</div>
</div>

View File

@ -0,0 +1,4 @@
$("#delete_all").attr("action", "<%= delete_admin_assets_path(:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]) %>");
$("#sort_headers").html("<%= j render 'sort_headers' %>");
$("#tbody_assets").html("<%= j render :partial => 'asset', :collection => @assets %>");
$("#asset_pagination").html("<%= j paginate @assets, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %>");

View File

@ -72,7 +72,6 @@
<%= link_to content_tag(:i, nil, :class => 'icons-link') + t('admin.asset'), admin_assets_path %>
<%= content_tag :ul, :class => ("nav nav-list " + visible_for_controllers('assets', '/admin/asset_tags', 'asset_categories')) do -%>
<%= content_tag :li, link_to(t('admin.all_assets'), admin_assets_path), :class => active_for_action('assets', 'index') %>
<%= content_tag :li, link_to(t('admin.add_new'), new_admin_asset_path), :class => active_for_action('assets', 'new') %>
<%= content_tag :li, link_to(t('admin.categories'), admin_asset_categories_path), :class => active_for_action('asset_categories', 'index') %>
<%= content_tag :li, link_to(t('admin.tags'), admin_asset_tags_path), :class => active_for_action('/admin/asset_tags', 'index') %>
<% end -%>

View File

@ -11,6 +11,7 @@ en:
back: Back
browse: Browse
cancel: Cancel
create_fail: Creation failed
create: Create
delete: Delete
desktop: Desktop
@ -41,6 +42,7 @@ en:
submit: Submit
sure?: Are you sure?
update: Update
update_failed: Update failed
view: View
view_count: View count
yes_: "Yes"
@ -117,6 +119,7 @@ en:
calendar: Calendar
cant_delete_self: You can not delete yourself.
cant_revoke_self_admin: You can not revoke your admin role yourself.
category: Category
choose_file: Choose a file...
class: Class
content: Content
@ -146,6 +149,7 @@ en:
enabled_for: Enabled for
file_name: Filename
file_size: File size
file_upload: File upload
format: Format
home: Home
id: ID
@ -231,6 +235,7 @@ en:
site_title: Site title
super_pages: Super pages
structure: Structure
tags: Tags
title: Title
translation: Translation
type: Type

View File

@ -9,6 +9,7 @@ zh_tw:
browse: 選擇檔案
cancel: 取消
create: 新增
create_fail: 創建失敗
delete: 刪除
desktop: 桌面
disable: 禁用
@ -35,6 +36,7 @@ zh_tw:
submit: 送出
sure?: 您肯定嗎?
update: 更新
update: 更新失敗
view: 檢視
view_count: 查看次數
yes_: "Yes"
@ -110,6 +112,7 @@ zh_tw:
calendar: 行事曆
cant_delete_self: 您不可以刪除自己。
cant_revoke_self_admin: 您不可以撤銷自己的管理身份。
category: 類別
choose_file: 請選擇一個文件...
class: 階級
content: 內容
@ -143,6 +146,7 @@ zh_tw:
enabled_for: 啟用
file_name: 檔名
file_size: 檔案大小
file_upload: 文件上載
format: 格式
home: 首頁
id: ID
@ -230,6 +234,7 @@ zh_tw:
site_setting: 網站設定
super_pages: 可編頁面
structure: 網站結構
tags: 標籤
title: 標題
translation: 翻譯
type: 類型

View File

@ -15,6 +15,7 @@ Orbit::Application.routes.draw do
mount Resque::Server.new, :at => "/resque"
resources :assets do
collection do
get 'file_upload'
post 'delete'
end
end