课程资源库附件单独JS实现
This commit is contained in:
parent
ffc42e22f2
commit
515dad66e2
|
@ -27,7 +27,7 @@
|
|||
:id => '_file',
|
||||
:class => ie8? ? '':'file_selector',
|
||||
:multiple => true,
|
||||
:onchange => 'addInputFiles(this,"'+ checkBox.to_s+'");',
|
||||
:onchange => 'addInputFilesCourseSource(this,"'+ checkBox.to_s+'");',
|
||||
:style => ie8? ? '': 'display:none',
|
||||
:data => {
|
||||
:max_file_size => Setting.attachment_max_size.to_i.kilobytes,
|
||||
|
|
|
@ -88,7 +88,7 @@ function addFile_board(inputEl, file, eagerUpload, id) {
|
|||
return null;
|
||||
}
|
||||
|
||||
function addFile(inputEl, file, eagerUpload,checkBox) {
|
||||
function addFile(inputEl, file, eagerUpload) {
|
||||
|
||||
var attachments_frame = '#attachments_fields';
|
||||
if ($(attachments_frame).children().length < 30) {
|
||||
|
@ -99,72 +99,50 @@ function addFile(inputEl, file, eagerUpload,checkBox) {
|
|||
'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> </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');
|
||||
}
|
||||
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> </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);
|
||||
|
@ -195,9 +173,9 @@ function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
|
|||
ajaxUpload.uploading++;
|
||||
|
||||
uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, {
|
||||
loadstartEventHandler: onLoadstart.bind(progressSpan),
|
||||
progressEventHandler: onProgress.bind(progressSpan)
|
||||
})
|
||||
loadstartEventHandler: onLoadstart.bind(progressSpan),
|
||||
progressEventHandler: onProgress.bind(progressSpan)
|
||||
})
|
||||
.done(function(result) {
|
||||
progressSpan.progressbar('value', 100).remove();
|
||||
fileSpan.find('input.description, a').css('display', 'inline-block');
|
||||
|
@ -284,22 +262,22 @@ function removeFile() {
|
|||
//gcm delete all file
|
||||
//modify by yutao 2015-5-14 <20><>1<EFBFBD><31>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>ϴ<EFBFBD><CFB4>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ start
|
||||
function removeAll(containerid) {
|
||||
if (confirm(deleteallfiles)) {
|
||||
if (containerid == undefined) {
|
||||
$(".remove-upload").removeAttr("data-confirm");
|
||||
$(".remove-upload").click();
|
||||
} else {
|
||||
var arr = $(".remove-upload").filter(function() {
|
||||
return $(this).data('containerid') == containerid;
|
||||
});
|
||||
arr.removeAttr("data-confirm");
|
||||
arr.click();
|
||||
}
|
||||
if (confirm(deleteallfiles)) {
|
||||
if (containerid == undefined) {
|
||||
$(".remove-upload").removeAttr("data-confirm");
|
||||
$(".remove-upload").click();
|
||||
} else {
|
||||
var arr = $(".remove-upload").filter(function() {
|
||||
return $(this).data('containerid') == containerid;
|
||||
});
|
||||
arr.removeAttr("data-confirm");
|
||||
arr.click();
|
||||
}
|
||||
// return false;
|
||||
}
|
||||
//modify by yutao 2015-5-14 <20><>1<EFBFBD><31>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>ϴ<EFBFBD><CFB4>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ end
|
||||
//gcm
|
||||
// return false;
|
||||
}
|
||||
//modify by yutao 2015-5-14 <20><>1<EFBFBD><31>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>ϴ<EFBFBD><CFB4>ؼ<EFBFBD>ʱ<EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bug <20>ʸ<EFBFBD>֮ end
|
||||
//gcm
|
||||
|
||||
function uploadBlob(blob, uploadUrl, attachmentId, options) {
|
||||
|
||||
|
@ -335,11 +313,10 @@ function uploadBlob(blob, uploadUrl, attachmentId, options) {
|
|||
}
|
||||
|
||||
function addInputFiles(inputEl) {
|
||||
checkBox = arguments[1] == 'public' ? false : true;
|
||||
// var clearedFileInput = $(inputEl).clone().val('');
|
||||
if (inputEl.files) {
|
||||
// upload files using ajax
|
||||
uploadAndAttachFiles(inputEl.files, inputEl,checkBox);
|
||||
uploadAndAttachFiles(inputEl.files, inputEl);
|
||||
// $(inputEl).remove();
|
||||
} else {
|
||||
// browser not supporting the file API, upload on form submission
|
||||
|
@ -386,7 +363,7 @@ function addInputFiles_board(inputEl, id) {
|
|||
//clearedFileInput.insertAfter('#attachments_fields');
|
||||
}
|
||||
|
||||
function uploadAndAttachFiles(files, inputEl,checkBox) {
|
||||
function uploadAndAttachFiles(files, inputEl) {
|
||||
|
||||
var maxFileSize = $(inputEl).data('max-file-size');
|
||||
var maxFileSizeExceeded = $(inputEl).data('max-file-size-message');
|
||||
|
@ -401,7 +378,7 @@ function uploadAndAttachFiles(files, inputEl,checkBox) {
|
|||
window.alert(maxFileSizeExceeded);
|
||||
} else {
|
||||
$.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> </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…
Reference in New Issue