课程资源库附件单独JS实现

tmp
huang 10 years ago
parent ffc42e22f2
commit 515dad66e2

@ -27,7 +27,7 @@
:id => '_file', :id => '_file',
:class => ie8? ? '':'file_selector', :class => ie8? ? '':'file_selector',
:multiple => true, :multiple => true,
:onchange => 'addInputFiles(this,"'+ checkBox.to_s+'");', :onchange => 'addInputFilesCourseSource(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,

@ -88,7 +88,7 @@ function addFile_board(inputEl, file, eagerUpload, id) {
return null; return null;
} }
function addFile(inputEl, file, eagerUpload,checkBox) { function addFile(inputEl, file, eagerUpload) {
var attachments_frame = '#attachments_fields'; var attachments_frame = '#attachments_fields';
if ($(attachments_frame).children().length < 30) { if ($(attachments_frame).children().length < 30) {
@ -99,28 +99,7 @@ function addFile(inputEl, file, eagerUpload,checkBox) {
'id': 'attachments_' + attachmentId, 'id': 'attachments_' + attachmentId,
'class': 'attachment' 'class': 'attachment'
}); });
//alert(checkBox);
if(checkBox){
fileSpan.append(
$('<input>', {
'type': 'text',
'class': 'filename readonly',
'name': 'attachments[' + attachmentId + '][filename]',
'readonly': 'readonly'
}).val(file.name),
$('<input>', {
'type': 'text',
'class': 'description',
'name': 'attachments[' + attachmentId + '][description]',
'maxlength': 254,
'placeholder': $(inputEl).data('descriptionPlaceholder')
}).toggle(!eagerUpload),
$('<div>', {
'class': 'div_attachments',
'name': 'div_' + 'attachments_' + attachmentId
})
).appendTo('#attachments_fields');
}else {
fileSpan.append( fileSpan.append(
$('<input>', { $('<input>', {
'type': 'text', 'type': 'text',
@ -164,7 +143,6 @@ function addFile(inputEl, file, eagerUpload,checkBox) {
'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);
@ -335,11 +313,10 @@ 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,checkBox); uploadAndAttachFiles(inputEl.files, inputEl);
// $(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
@ -386,7 +363,7 @@ function addInputFiles_board(inputEl, id) {
//clearedFileInput.insertAfter('#attachments_fields'); //clearedFileInput.insertAfter('#attachments_fields');
} }
function uploadAndAttachFiles(files, inputEl,checkBox) { function uploadAndAttachFiles(files, inputEl) {
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');
@ -401,7 +378,7 @@ function uploadAndAttachFiles(files, inputEl,checkBox) {
window.alert(maxFileSizeExceeded); window.alert(maxFileSizeExceeded);
} else { } else {
$.each(files, function() { $.each(files, function() {
addFile(inputEl, this, true,checkBox); addFile(inputEl, this, true);
}); });
} }
} }
@ -490,3 +467,139 @@ $(function() {
} }
}); });
}); });
//课程课件
function addInputFilesCourseSource(inputEl) {
checkBox = arguments[1] == 'public' ? false : true;
// var clearedFileInput = $(inputEl).clone().val('');
if (inputEl.files) {
// upload files using ajax
uploadAndAttachFilesCourseSource(inputEl.files, inputEl,checkBox);
// $(inputEl).remove();
} else {
// browser not supporting the file API, upload on form submission
var attachmentId;
var aFilename = inputEl.value.split(/\/|\\/);
var count = $('#attachments_fields>span').length;
attachmentId = addFile(inputEl, {
name: aFilename[aFilename.length - 1]
}, false);
if (attachmentId) {
$(inputEl).attr({
name: 'attachments[' + attachmentId + '][file]'
}).hide();
if (count <= 0) count = 1;
$('#upload_file_count').html("<span id=\"count\">" + count + "</span>" + $(inputEl).data('fileCount'));
}
}
//clearedFileInput.insertAfter('#attachments_fields');
}
function uploadAndAttachFilesCourseSource(files, inputEl,checkBox) {
var maxFileSize = $(inputEl).data('max-file-size');
var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
var sizeExceeded = false;
$.each(files, function() {
if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) {
sizeExceeded = true;
}
});
if (sizeExceeded) {
window.alert(maxFileSizeExceeded);
} else {
$.each(files, function() {
addFileCourseSource(inputEl, this, true,checkBox);
});
}
}
function addFileCourseSource(inputEl, file, eagerUpload,checkBox) {
var attachments_frame = '#attachments_fields';
if ($(attachments_frame).children().length < 30) {
deleteallfiles = $(inputEl).data('deleteAllFiles');
var attachmentId = addFile.nextAttachmentId++;
var fileSpan = $('<span>', {
'id': 'attachments_' + attachmentId,
'class': 'attachment'
});
//alert(checkBox);
if(checkBox){
fileSpan.append(
$('<input>', {
'type': 'text',
'class': 'filename readonly',
'name': 'attachments[' + attachmentId + '][filename]',
'readonly': 'readonly'
}).val(file.name),
$('<input>', {
'type': 'text',
'class': 'description',
'name': 'attachments[' + attachmentId + '][description]',
'maxlength': 254,
'placeholder': $(inputEl).data('descriptionPlaceholder')
}).toggle(!eagerUpload),
$('<div>', {
'class': 'div_attachments',
'name': 'div_' + 'attachments_' + attachmentId
})
).appendTo('#attachments_fields');
}else {
fileSpan.append(
$('<input>', {
'type': 'text',
'class': 'filename readonly',
'name': 'attachments[' + attachmentId + '][filename]',
'readonly': 'readonly'
}).val(file.name),
$('<input>', {
'type': 'text',
'class': 'description',
'name': 'attachments[' + attachmentId + '][description]',
'maxlength': 254,
'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),
$('<div>', {
'class': 'div_attachments',
'name': 'div_' + 'attachments_' + attachmentId
})
).appendTo('#attachments_fields');
}
if (eagerUpload) {
ajaxUpload(file, attachmentId, fileSpan, inputEl);
}
return attachmentId;
}
return null;
}
addFileCourseSource.nextAttachmentId = 1;
Loading…
Cancel
Save