This commit is contained in:
cxt 2015-10-19 18:32:51 +08:00
commit bf5360164c
3 changed files with 76 additions and 52 deletions

View File

@ -11,4 +11,5 @@ fileSpan.find('a.remove-upload')
}) })
.off('click'); .off('click');
$('<input>', { type: 'hidden', name: 'attachments[<%= j params[:attachment_id] %>][token]' } ).val('<%= j @attachment.token %>').appendTo(fileSpan); $('<input>', { type: 'hidden', name: 'attachments[<%= j params[:attachment_id] %>][token]' } ).val('<%= j @attachment.token %>').appendTo(fileSpan);
console.log(fileSpan.html())
<% end %> <% end %>

View File

@ -21,13 +21,13 @@
<% end %> <% end %>
</span> </span>
</div> </div>
<% checkBox = (@course.present? && @course.is_public?) ? 'public' : 'private'%>
<button name="button" class="sub_btn" onclick="_file.click()" onmouseover="this.focus()" style="<%= ie8? ? 'display:none' : ''%>" type="button" ><%= l(:label_browse) %></button> <button name="button" class="sub_btn" onclick="_file.click()" onmouseover="this.focus()" style="<%= ie8? ? 'display:none' : ''%>" type="button" ><%= l(:label_browse) %></button>
<%= file_field_tag 'attachments[dummy][file]', <%= file_field_tag 'attachments[dummy][file]',
:id => '_file', :id => '_file',
:class => ie8? ? '':'file_selector', :class => ie8? ? '':'file_selector',
:multiple => true, :multiple => true,
:onchange => 'addInputFiles(this);', :onchange => 'addInputFiles(this,"'+ checkBox.to_s+'");',
:style => ie8? ? '': 'display:none', :style => ie8? ? '': 'display:none',
:data => { :data => {
:max_file_size => Setting.attachment_max_size.to_i.kilobytes, :max_file_size => Setting.attachment_max_size.to_i.kilobytes,

View File

@ -88,7 +88,7 @@ function addFile_board(inputEl, file, eagerUpload, id) {
return null; return null;
} }
function addFile(inputEl, file, eagerUpload) { function addFile(inputEl, file, eagerUpload,checkBox) {
var attachments_frame = '#attachments_fields'; var attachments_frame = '#attachments_fields';
if ($(attachments_frame).children().length < 30) { if ($(attachments_frame).children().length < 30) {
@ -99,50 +99,72 @@ function addFile(inputEl, file, eagerUpload) {
'id': 'attachments_' + attachmentId, 'id': 'attachments_' + attachmentId,
'class': 'attachment' 'class': 'attachment'
}); });
//alert(checkBox);
fileSpan.append( if(checkBox){
$('<input>', { fileSpan.append(
'type': 'text', $('<input>', {
'class': 'filename readonly', 'type': 'text',
'name': 'attachments[' + attachmentId + '][filename]', 'class': 'filename readonly',
'readonly': 'readonly' 'name': 'attachments[' + attachmentId + '][filename]',
}).val(file.name), 'readonly': 'readonly'
$('<input>', { }).val(file.name),
'type': 'text', $('<input>', {
'class': 'description', 'type': 'text',
'name': 'attachments[' + attachmentId + '][description]', 'class': 'description',
'maxlength': 254, 'name': 'attachments[' + attachmentId + '][description]',
'placeholder': $(inputEl).data('descriptionPlaceholder') 'maxlength': 254,
}).toggle(!eagerUpload), 'placeholder': $(inputEl).data('descriptionPlaceholder')
$('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({ }).toggle(!eagerUpload),
'class': 'ispublic-label' $('<div>', {
}), 'class': 'div_attachments',
$('<input>', { 'name': 'div_' + 'attachments_' + attachmentId
'type': 'checkbox', })
'class': 'is_public_checkbox', ).appendTo('#attachments_fields');
'value': 1, }else {
'name': 'attachments[' + attachmentId + '][is_public_checkbox]', fileSpan.append(
checked: 'checked' $('<input>', {
}).toggle(!eagerUpload), 'type': 'text',
$('<a>&nbsp</a>').attr({ 'class': 'filename readonly',
'href': "#", 'name': 'attachments[' + attachmentId + '][filename]',
'class': 'remove-upload' 'readonly': 'readonly'
}).click(function() { }).val(file.name),
if (confirm($(inputEl).data('areYouSure'))) { $('<input>', {
removeFile(); 'type': 'text',
if (!eagerUpload) { 'class': 'description',
(function(e) { 'name': 'attachments[' + attachmentId + '][description]',
reload(e); 'maxlength': 254,
})(fileSpan); 'placeholder': $(inputEl).data('descriptionPlaceholder')
}).toggle(!eagerUpload),
$('<span >' + $(inputEl).data('fieldIsPublic') + ':</span>').attr({
'class': 'ispublic-label'
}),
$('<input>', {
'type': 'checkbox',
'class': 'is_public_checkbox',
'value': 1,
'name': 'attachments[' + attachmentId + '][is_public_checkbox]',
checked: 'checked'
}).toggle(!eagerUpload),
$('<a>&nbsp</a>').attr({
'href': "#",
'class': 'remove-upload'
}).click(function () {
if (confirm($(inputEl).data('areYouSure'))) {
removeFile();
if (!eagerUpload) {
(function (e) {
reload(e);
})(fileSpan);
}
} }
}
}).toggle(!eagerUpload), }).toggle(!eagerUpload),
$('<div>', { $('<div>', {
'class': 'div_attachments', 'class': 'div_attachments',
'name': 'div_' + 'attachments_' + attachmentId 'name': 'div_' + 'attachments_' + attachmentId
}) })
).appendTo('#attachments_fields'); ).appendTo('#attachments_fields');
}
if (eagerUpload) { if (eagerUpload) {
ajaxUpload(file, attachmentId, fileSpan, inputEl); ajaxUpload(file, attachmentId, fileSpan, inputEl);
@ -198,7 +220,7 @@ function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
//gcm files count and add delete_all link //gcm files count and add delete_all link
//modify by yutao 2015-5-14 当1个页面存在多个上传控件时此块代码存在bug 故改之 start //modify by yutao 2015-5-14 <EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ start
var containerid = $(inputEl).data('containerid'); var containerid = $(inputEl).data('containerid');
if (containerid == undefined) { if (containerid == undefined) {
var count = $('#attachments_fields>span').length; var count = $('#attachments_fields>span').length;
@ -233,7 +255,7 @@ function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
})); }));
} }
} }
//modify by yutao 2015-5-14 当1个页面存在多个上传控件时此块代码存在bug 故改之 end //modify by yutao 2015-5-14 <EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ end
} }
//gcm //gcm
@ -260,7 +282,7 @@ function removeFile() {
} }
//gcm delete all file //gcm delete all file
//modify by yutao 2015-5-14 当1个页面存在多个上传控件时此块代码存在bug 故改之 start //modify by yutao 2015-5-14 <EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ start
function removeAll(containerid) { function removeAll(containerid) {
if (confirm(deleteallfiles)) { if (confirm(deleteallfiles)) {
if (containerid == undefined) { if (containerid == undefined) {
@ -276,7 +298,7 @@ function removeAll(containerid) {
} }
// return false; // return false;
} }
//modify by yutao 2015-5-14 当1个页面存在多个上传控件时此块代码存在bug 故改之 end //modify by yutao 2015-5-14 <EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ end
//gcm //gcm
function uploadBlob(blob, uploadUrl, attachmentId, options) { function uploadBlob(blob, uploadUrl, attachmentId, options) {
@ -313,10 +335,11 @@ function uploadBlob(blob, uploadUrl, attachmentId, options) {
} }
function addInputFiles(inputEl) { function addInputFiles(inputEl) {
checkBox = arguments[1] == 'public' ? false : true;
// var clearedFileInput = $(inputEl).clone().val(''); // var clearedFileInput = $(inputEl).clone().val('');
if (inputEl.files) { if (inputEl.files) {
// upload files using ajax // upload files using ajax
uploadAndAttachFiles(inputEl.files, inputEl); uploadAndAttachFiles(inputEl.files, inputEl,checkBox);
// $(inputEl).remove(); // $(inputEl).remove();
} else { } else {
// browser not supporting the file API, upload on form submission // browser not supporting the file API, upload on form submission
@ -363,7 +386,7 @@ function addInputFiles_board(inputEl, id) {
//clearedFileInput.insertAfter('#attachments_fields'); //clearedFileInput.insertAfter('#attachments_fields');
} }
function uploadAndAttachFiles(files, inputEl) { function uploadAndAttachFiles(files, inputEl,checkBox) {
var maxFileSize = $(inputEl).data('max-file-size'); var maxFileSize = $(inputEl).data('max-file-size');
var maxFileSizeExceeded = $(inputEl).data('max-file-size-message'); var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
@ -378,7 +401,7 @@ function uploadAndAttachFiles(files, inputEl) {
window.alert(maxFileSizeExceeded); window.alert(maxFileSizeExceeded);
} else { } else {
$.each(files, function() { $.each(files, function() {
addFile(inputEl, this, true); addFile(inputEl, this, true,checkBox);
}); });
} }
} }