Fix filemanager js

This commit is contained in:
manson 2014-06-03 11:36:43 +08:00
parent 5c4ba2b0d5
commit 0f58f39eb5
7 changed files with 106 additions and 85 deletions

View File

@ -1,3 +1,5 @@
var insertBtn, deleteBtn, language, funcNum, url = null;
!function ($) {
$.fn.checkListLength = function (param){
_defaultSettings = {
@ -30,41 +32,27 @@ $(function () {
});
});
$(document).ready(function(){
var insertBtn = $("#insert_btn"),
deleteBtn = $("#delete_btn"),
url = null;
insertBtn = $("#insert_btn");
deleteBtn = $("#delete_btn");
var showPreview = function(elem){
var type = $("#ext_" + elem.val()).val(),
imgexp = new RegExp(/(\.|\/)(gif|jpe?g|png)$/i),
otherexp = new RegExp(/(\.|\/)(pdf|doc|docx|ppt|pptx|xls|xlsx)$/i)
img = $("<img />"),
preview = $( ".preview-area" );
preview.text("Preview is loading...");
if( imgexp.test(type) ){
var url = $("#url_" + elem.val()).val();
img.attr("src",url).load(function(){
var ratio = this.width / this.height,
targetWidth = targetHeight = 200;
if( ratio < 1 ){
targetWidth = targetHeight * ratio;
}else{
targetHeight = targetWidth / ratio;
}
img.height(targetHeight).width(targetWidth);
preview.html(img);
})
}else if( otherexp.test(type) ) {
var t = type.replace(".",""),
url = "/assets/ft-icons/" + t + "/" + t + "-128_32.png";
img.attr('src', url).load(function(){
preview.html(img);
});
}else{
preview.html("Preview not available.");
}
}
initAssets();
funcNum = getUrlParam( 'CKEditorFuncNum' ),
t = getUrlParam('CKEditor').split("_"),
language = t[t.length-1];
language = (language == "tw" ? "zh_tw" : language);
})
function getUrlParam( paramName ) {
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' );
var match = window.location.search.match(reParam);
return ( match && match.length > 1 ) ? match[ 1 ] : null;
}
var initAssets = function(){
$("#asset_sort_list").on(clickEvent,"input[type=checkbox]",function(){
if($("#asset_sort_list input[type=checkbox]:checked").length == 1){
url = window.location.protocol + "//" + window.location.host + $("#url_" + $("input[type=checkbox]:checked").val()).val();
@ -86,7 +74,7 @@ $(document).ready(function(){
if(url){
window.opener.CKEDITOR.tools.callFunction( funcNum, url, function() {
var element,
dialog = this.getDialog();
dialog = this.getDialog();
if ( dialog.getName() == 'image' ) {
element = dialog.getContentElement( 'info', 'txtAlt' );
if ( element )
@ -98,7 +86,10 @@ $(document).ready(function(){
})
deleteBtn.bind( clickEvent,function(){
var items_to_delete = [],
type = getUrlParam("type");
type = getUrlParam("type");
$( ".preview-area" ).empty();
$("input[type=checkbox]:checked").each(function(){
items_to_delete.push($(this).val());
})
@ -109,7 +100,8 @@ $(document).ready(function(){
var currentEdit = null;
$(document).on("ajax:success","form[data-remote=true]",function(evt, data, xhr){
if(currentEdit){
currentEdit.replaceWith($(data));
$('#assets_content').html($(data));
initAssets();
$("#editform").modal("hide");
}
})
@ -125,17 +117,35 @@ $(document).ready(function(){
$("#editform").on("hidden",function(){
currentEdit = null;
})
}
function getUrlParam( paramName ) {
var reParam = new RegExp( '(?:[\?&]|&)' + paramName + '=([^&]+)', 'i' ) ;
var match = window.location.search.match(reParam) ;
return ( match && match.length > 1 ) ? match[ 1 ] : null ;
var showPreview = function(elem){
var type = $("#ext_" + elem.val()).val(),
imgexp = new RegExp(/(\.|\/)(gif|jpe?g|png)$/i),
otherexp = new RegExp(/(\.|\/)(pdf|doc|docx|ppt|pptx|xls|xlsx)$/i)
img = $("<img />"),
preview = $( ".preview-area" );
preview.text("Preview is loading...");
if( imgexp.test(type) ){
var url = $("#url_" + elem.val()).val();
img.attr("src",url).load(function(){
var ratio = this.width / this.height,
targetWidth = targetHeight = 200;
if( ratio < 1 ){
targetWidth = targetHeight * ratio;
}else{
targetHeight = targetWidth / ratio;
}
img.height(targetHeight).width(targetWidth);
preview.html(img);
})
}else if( otherexp.test(type) ) {
var t = type.replace(".",""),
url = "/assets/ft-icons/" + t + "/" + t + "-128_32.png";
img.attr('src', url).load(function(){
preview.html(img);
});
}else{
preview.html("Preview not available.");
}
var funcNum = getUrlParam( 'CKEditorFuncNum' ),
t = getUrlParam('CKEditor').split("_"),
language = t[t.length-1];
language = (language == "tw" ? "zh_tw" : language);
})
}

View File

@ -2,7 +2,7 @@ class Admin::AssetsController < OrbitAdminController
def index
@assets = current_user.assets
@assets = @assets.page(params[:page]).per(10)
@assets = @assets.order_by(:created_at=>'desc').page(params[:page]).per(10)
render :layout => "assets"
end
@ -22,6 +22,10 @@ class Admin::AssetsController < OrbitAdminController
def update
@asset = Asset.find(params[:id])
@asset.update_attributes(asset_params)
@assets = current_user.assets
@assets = @assets.order_by(:created_at=>'desc').page(params[:page]).per(10)
if asset_params['data'].blank?
render layout: false
else
@ -39,7 +43,8 @@ class Admin::AssetsController < OrbitAdminController
def delete_files
Asset.where(:id.in=>params[:files]).destroy
@assets = current_user.assets.page(params[:page]).per(10)
@assets = current_user.assets
@assets = @assets.order_by(:created_at=>'desc').page(params[:page]).per(10)
end
def file_upload

View File

@ -1,17 +1,35 @@
<tr id="asset_<%= asset.id %>" class="with_action">
<td>
<%= check_box_tag 'files[]', asset.id, false, :class => "checkbox_in_list" %>
<input type="hidden" value="<%= asset.data.url %>" id="url_<%= asset.id %>" />
<% @site_in_use_locales.each_with_index do |locale, i| %>
<input type="hidden" value="<%= asset.description_translations[locale] rescue nil %>" id="<%= locale %>_desc_<%= asset.id %>" />
<input type="hidden" value="<%= asset.title_translations[locale] rescue nil %>" id="<%= locale %>_title_<%= asset.id %>" />
<table id="asset_sort_list" class="table main-list">
<thead>
<tr>
<th></th>
<th><%= t(:filename) %></th>
<th><%= t(:size) %></th>
<th><%= t(:description) %></th>
<th></th>
</tr>
</thead>
<tbody id="tbody_assets" class="sort-holder">
<% @assets.each do |asset| %>
<tr id="asset_<%= asset.id %>" class="with_action">
<td>
<%= check_box_tag 'files[]', asset.id, false, :class => "checkbox_in_list" %>
<input type="hidden" value="<%= asset.data.url %>" id="url_<%= asset.id %>" />
<% @site_in_use_locales.each_with_index do |locale, i| %>
<input type="hidden" value="<%= asset.description_translations[locale] rescue nil %>" id="<%= locale %>_desc_<%= asset.id %>" />
<input type="hidden" value="<%= asset.title_translations[locale] rescue nil %>" id="<%= locale %>_title_<%= asset.id %>" />
<% end %>
<input type="hidden" value="<%= File.extname(asset.data.url) %>" id="ext_<%= asset.id %>" />
</td>
<td><%= asset.title_translations[I18n.locale.to_s] rescue nil %></td>
<td><%= number_to_human_size(asset.data.file.size) rescue nil %></td>
<td><%= asset.description rescue nil %></td>
<td>
<a href="<%= edit_admin_asset_path(asset) %>" class="editform"><%= t(:edit) %></a>
</td>
</tr>
<% end %>
<input type="hidden" value="<%= File.extname(asset.data.url) %>" id="ext_<%= asset.id %>" />
</td>
<td><%= asset.title_translations[I18n.locale.to_s] rescue nil %></td>
<td><%= number_to_human_size(asset.data.file.size) rescue nil %></td>
<td><%= asset.description rescue nil %></td>
<td>
<a href="<%= edit_admin_asset_path(asset) %>" class="editform"><%= t(:edit) %></a>
</td>
</tr>
</tbody>
</table>
<div class="pagination pagination-centered">
<%= paginate @assets, :params => {:controller => 'assets', :action => 'index', :direction => params[:direction], :sort => params[:sort], :type => params[:type]}, :remote => true %>
</div>

View File

@ -1,2 +1,2 @@
$("#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} %>");
$("#assets_content").html("<%= j render :partial => 'asset' %>");
initAssets();

View File

@ -231,22 +231,8 @@
</div>
</div>
</form>
<table id="asset_sort_list" class="table main-list">
<thead>
<tr>
<th></th>
<th><%= t(:filename) %></th>
<th><%= t(:size) %></th>
<th><%= t(:description) %></th>
<th></th>
</tr>
</thead>
<tbody id="tbody_assets" class="sort-holder">
<%= render :partial => 'asset', :collection => @assets %>
</tbody>
</table>
<div class="pagination pagination-centered">
<%= paginate @assets, :params => {:direction => params[:direction], :sort => params[:sort], :type => params[:type]}, :remote => true %>
<div id="assets_content">
<%= render :partial => 'asset' %>
</div>
</div>
<div id="panel_r" class="pull-left">

View File

@ -0,0 +1,2 @@
$('#assets_content').html("<%= j render partial: 'asset' %>");
initAssets();

View File

@ -1 +1 @@
<%= render :partial => 'asset', :object => @asset %>
<%= render partial: 'asset' %>